Looping issue in MATLAB

Viewed 69

This is a very basic problem I am dealing with.

I have simulated a dataset with 6 variables and I want to measure dynamic extremen shortfall for each of the vectors.

I am trying to loop the following code in a way, that the final variable ES will present a matrix that will have T rows and 6 colums going through all data points in the simulated rectengular matrix.

clc
clear
%% 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);

I want to use r = Data in that the estimation runs on all columns instead of on each column and the final ESmatrix plots all columns in one plot.

 T= length(r);
    conditionalvariance=[];
    p= [0.025];
    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=[];
    bpoe=[];
    for J= 1:T
    [Var_Normal, ES_Normal]=hNormalVaRES(Sigma(J),p);
    VaR=Var_Normal;
    ES=ES_Normal;
    disp(J)
    disp('');
    ESdynamic=[ESdynamic,ES];
    VaRdynamic=[]
    end
    ES=ESdynamic;
    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

When I plot(ES), i want to have a plot of 6 variables.

Please kindly help me out with this looping issue.

1 Answers

Not sure if the plot is expected to look like this but using an additional outer for-loop to index through the values of p and run the function multiple times can be used to achieve this result. To plot the same axes the hold on property was introduced after the first plot() call. Also not sure if p = [0.5, 0.1, 0.05, 0.025, 0.01, 0.001] is the correct set since you mentioned it was for six different p values. In the question it had 0.1 as 0,1.

6 Combined Plots

clc
clear
% 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);
T= length(r);
conditionalvariance=[];
p = [0.5, 0.1, 0.05, 0.025, 0.01, 0.001];


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=[];
bpoe=[];

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];
VaRdynamic = []
end
ES_Matrix(:,P_Index) = ESdynamic';
plot(ES_Matrix(:,P_Index));
hold on

ESdynamic = [];
end 
hold off

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

Ran using MATLAB R2019b

Related