Looping Issues in MATLAB- immediate help requested

1 次查看(过去 30 天)
I am trying to estimate a rolling extreme shortfall matrix by looping. In the following code ESdynamic is crated for each column on the simulated matrix.
% Simulate data set
mu = [0 0 0 0 0 0];
A = rand(6);
Sigma= A * A';
% Simulation of variables
rng('default') % For reproducibility
Data = mvnrnd(mu,Sigma,1000);
r=Data(:,1);
Now, instead of representing r as just one column of data matrix, I want to out the entire code in a loop so that, at first Esdynamic is populated with column one, than on column 2... at the end we have a matrix of ESdynamic.
T= length(r);
conditionalvariance=[];
%p = [0.5, 0.1, 0.05];
p=0.5;
VarMdl = garch(1,1);
Mdl = arima('ARLags',1,'Variance',VarMdl);
EstMdl = estimate(Mdl,r);
[res,v,logL] = infer(EstMdl,r);
conditionalvariance=[conditionalvariance,v];
Sigma=conditionalvariance;
ESdynamic=[];
VaRdynamic=[];
for P_Index = 1: +1: length(p)
P_Value = p(P_Index);
for J= 1:T
[Var_Normal, ES_Normal]=hNormalVaRES(Sigma(J),P_Value);
VaR = Var_Normal;
ES = ES_Normal;
disp(J)
disp('');
ESdynamic = [ESdynamic,ES];
I also want a Vardynamic matrix. how to ket the loop update it?
VaRdynamic = []
end
ES_Matrix(:,P_Index) = ESdynamic';
Now is it possible to put another loop so that the ESdynamic and VaRdynamic matrices, are generated for p = [0.5, 0.1, 0.05, 0.025, 0.01, 0.001] in a loop?
The plot compares all 6 columns of ESdynamic, for each ...please help
plot(ES_Matrix(:,P_Index));
hold on
ESdynamic = [];
end
hold off
% Local Functions
function [VaR,ES] = hNormalVaRES(Sigma,p)
% Compute VaR and ES for normal distribution
% See [4] for technical details
VaR = -norminv(p);
ES = -Sigma*quad(@(q)q.*normpdf(q),-6,-VaR)/p;
end
Your kind suggestions are requested.

回答(1 个)

Srivardhan Gadila
Srivardhan Gadila 2020-11-28
If for all values of p the sizes of all the matrices of ESdynamic are same and sizes of all the matrices of VaRdynamic are same respectively then you can initialize them with zeros as follows:
%let size of ESdynamic is mxn (note that it may be different in your case like mxnxox..xq
ESdynamicAllmatrices = zeros([p m n]);
In the for loop based on the variable p assign the value as follows:
ESdynamicAllmatrices(P_Index,:,:) = ESdynamic;
If the sizes are different then consider using cell arrays. Refer to Multidimensional Arrays, zeros and cell.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by