Error in plot for VaR
    3 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
Hi, i am trying to run Pattons Code for Copula Methods from Forecasting Multivariate Time Series. I cant run the plot part of the VaR.
that is the error (Error using plot
Vectors must be the same length.)
the error is in this plot((1:T)',squeeze(outVAR(:,wstar,qq,1,1)),'r:','LineWidth',2) 
how i can fix it?
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% VALUE-AT-RISK AND EXPECTED SHORTFALL FROM COPULA-BASED MODELS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% getting VaR and ES for a portfolio of these variables
% WARNING: THIS PART IS VERY SLOW
WW = [0;0.01;(0.05:0.05:0.95)';0.99;1];  % getting lots of weights, as this part is cheap once we have the siumulations
QQ = [0.001;0.005;0.01;0.05;0.1];  % quantiles to look at
muhat = rets1 - resids;  % conditional mean
sims = 5000;
outVAR = nan(T,2,length(WW),length(QQ),2,2);
tic;
for tt=1:T;
    U3 = 1-Gumbel_rnd(kappat(tt),sims);
    U4 = tdis_cdf(mvtrnd([[1,rhot(tt)];[rhot(tt),1]],1/theta8tvs(2),sims),1/theta8tvs(2));
    UUU = U3;
    UUU(:,:,2) = U4;
    EEE = nan(sims,2,2);
    YYY = nan(sims,2,2);
    for cc=1:2;
        for mm=1:2;
            EEE(:,mm,cc) = quantile(stdresids(:,mm),UUU(:,mm,cc),1);
            YYY(:,mm,cc) = muhat(tt,mm) + sqrt(hhat_opt(tt,mm))*EEE(:,mm,cc);  % simulated value for return
        end
        for ww=1:length(WW);
            w = WW(ww);
            pf = w*YYY(:,1,cc) + (1-w)*YYY(:,2,cc);
            pf2 = w*EEE(:,1,cc) + (1-w)*EEE(:,2,cc);
            outVAR(tt,cc,ww,:,1,1) = quantile(pf,QQ);                 % Value at Risk
            outVAR(tt,cc,ww,:,1,2) = quantile(pf2,QQ);                 % Value at Risk of the std resids (useful for seeing where the copula matters)
            for qq=1:length(QQ);
                temp124 = (pf<=quantile(pf,QQ(qq)));  % observations below this quantile
                if sum(temp124)>0
                    outVAR(tt,cc,ww,qq,2,1) = mean(pf(temp124));   % Expected Shortfall
                end
                temp124 = (pf2<=quantile(pf2,QQ(qq)));  % observations below this quantile
                if sum(temp124)>0
                    outVAR(tt,cc,ww,qq,2,2) = mean(pf2(temp124));   % Expected Shortfall
                end
            end
        end
    end
    if mod(tt,100)==0
        [tt,toc]
    end
end
toc  % takes about 3.88 hours for sims=5000
wstar = find(WW==0.5);
qq = find(QQ==0.01);
figure(2200+qq),subplot(2,1,1),plot((1:T)',squeeze(outVAR(:,wstar,qq,1,1)),'r:','LineWidth',2);hold on;
plot((1:T)',squeeze(outVAR(:,2,wstar,qq,1,1)),'b');
title(['Value-at-Risk from time-varying copula models, w=[',num2str(WW(wstar)),',',num2str(WW(wstar)),'], q=',num2str(QQ(qq))]);
legend('RotGumbel','Stud t');grid on;hold off;
set(gca,'XTick',jandates(1:2:end));
set(gca,'XTickLabel',datestr(datenum(datesYMD(jandates(1:2:end),1),datesYMD(jandates(1:2:end),2),datesYMD(jandates(1:2:end),3)),12));hold off;
0 Commenti
Risposte (1)
  Himanshu
      
 il 16 Mag 2025
        Hello,
The error arises because the dimensions of the "outVAR" array do not align with the indices used in the "squeeze" function, causing mismatched vector lengths.
"wstar" (weight index) and "qq" (quantile index) are incorrectly applied to the copula model dimension (second dimension of "outVAR"), leading to a dimension mismatch when plotting.
To fix this, adjust the indexing to correctly reference the copula model dimension (1 or 2) and the weight/quantile indices. For example, replace "squeeze(outVAR(:,wstar,qq,1,1))" with "squeeze(outVAR(:,1,wstar,qq,1,1))" for the first copula model (RotGumbel) and "squeeze(outVAR(:,2,wstar,qq,1,1))" for the second (Stud t). Ensure all dimensions align.
Verify the dimensions of "outVAR" using "size(outVAR)" and confirm "wstar" and "qq" are valid indices for the weight and quantile dimensions.
I hope this helps.
0 Commenti
Vedere anche
Categorie
				Scopri di più su Probability Distributions and Hypothesis Tests in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

