3

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 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

6 Comments

MichaelTr7, thank you so much m y friend. While this is one way of looking into things, what I wanted is to get ESdynamic as a matrix , where the columns are not p values, but considering each columns as a different variable. Let us consider p=0.05 only. But how can i run a loop to make a matrix of ESdynamic. The ES analysis is conducted on each column. I want them to work on all columns through a loop. So, instead of using r=Data(:,1);...I want the loop to go through r = Data(:, 1:end) with only p=0.05. How to edit the code for that mate?
I've got no clue what you exactly want. A more streamlined example of what you're trying to achieve might help me figure out what you're trying to do.
Hi MichaelTr7, thank you mate. I have updated the question and the code for your consideration. I want ES to be estimated for each columns in a loop as we have a matrix as an input.
Hi MichaelTr7, I have updated this question in Create a matrix by looping the estimation in columns....if you can comment there and help would really be great
thank you so much. This solved my problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.