Hi Matlab,
I'm running a program, calculating the recurvie residuals (see code below). The loop indicates the model is run for some base period (Z) until end of the sample.
for (i = Z:206)
%data
vX = [LOG_GDP(1:i,:,1), gvm_GDP(1:i,:,1), goverment_bond(1:i,:,1), t_bill(1:i,:,1), inflation(1:i,:,1), LOG_private_investment(1:i,:,1), LOG_mon_base(1:i,:,1), LOG_gvm_spending(1:i,:,1), excess_return(1:i,:,1), Unemployment(1:i,:,1), LOG_private_con(1:i,:,1), LOG_house(1:i,:,1)];
%function to calculate residuals from VECM
[res1] = f(vX, K, r, p)
end
However, I do not understand how to store the estimates forthe loop. If I run the program, it only returns the values for last value.
Does someone know how to store the values?
Any help is greatly appreciated
Best regards

 Respuesta aceptada

Rik
Rik el 8 de Mzo. de 2024

0 votos

If you want to store results from all iterations, you need to store them and not overwrite them:
res1 = cell(1,100);index = 0;
for i = Z:206
%data
vX = [LOG_GDP(1:i,:,1), gvm_GDP(1:i,:,1), goverment_bond(1:i,:,1), t_bill(1:i,:,1), inflation(1:i,:,1), LOG_private_investment(1:i,:,1), LOG_mon_base(1:i,:,1), LOG_gvm_spending(1:i,:,1), excess_return(1:i,:,1), Unemployment(1:i,:,1), LOG_private_con(1:i,:,1), LOG_house(1:i,:,1)];
%function to calculate residuals from VECM
index = index+1;
res1{index} = f(vX, K, r, p);
end
res1((index+1):end) = []; % remove unused elements

3 comentarios

Simon Christensen
Simon Christensen el 8 de Mzo. de 2024
Editada: Simon Christensen el 8 de Mzo. de 2024
Thank you very much @Rik, greatly appreciated! Just one small question: in my function, although not shown above, I have 12 outputs: res1, res2,..., res12. I cannot simple use the code above for all of them, since it always returns the value for res1.
E.g.
res2 = cell(1,100);index = 0;
for i = Z:206
%data
vX = [LOG_GDP(1:i,:,1), gvm_GDP(1:i,:,1), goverment_bond(1:i,:,1), t_bill(1:i,:,1), inflation(1:i,:,1), LOG_private_investment(1:i,:,1), LOG_mon_base(1:i,:,1), LOG_gvm_spending(1:i,:,1), excess_return(1:i,:,1), Unemployment(1:i,:,1), LOG_private_con(1:i,:,1), LOG_house(1:i,:,1)];
%function to calculate residuals from VECM
index = index+1;
res2{index} = f(vX, K, r, p);
end
res2((index+1):end) = []; % remove unused elements
The code return the values for res1 given the output of the function is counstructed [res1,...,res12] (just shortend here, in the 'real version', they are defined as full sequenece between res1 and res12)
do you know how to deal with his?
Simon Christensen
Simon Christensen el 8 de Mzo. de 2024
Actually, I think i found the solution to the above by myself: It is just to rearrange the function outout. All good!
You should not use numbered variables. That encodes data in the variable name, which is a fragile method of design that doesn't scale well.
res = cell(100,4);index = 0;
for n=1:3
index = index+1;
[res{index,:}]=example_function;
end
res((index+1):end,:) = []; % remove unused elements
disp(res)
{[1.1713]} {[2.3546]} {[3.6638]} {[4.5237]} {[1.4065]} {[2.1759]} {[3.1997]} {[4.8497]} {[1.1356]} {[2.5412]} {[3.4620]} {[4.7298]}
% example function with 4 outputs
function [a,b,c,d]=example_function
a=1+rand;b=2+rand;c=3+rand;d=4+rand;
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 8 de Mzo. de 2024

Comentada:

Rik
el 8 de Mzo. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by