Matrix size error in quantiles estimation

3 次查看(过去 30 天)
I have the following code that estimates quantiles for a comovement box. The code was not written by me and it works for the original data-set. When i run it on my dataset (same dimensions), i get an error message:
"Index exceeds matrix dimensions.
Error in CAViaROptimisation (line 44) empiricalQuantile = prctile(y(1:300), THETA*100);
Error in QuantileEstimation (line 31) [quantile, dq] = CAViaROptimisation(100*X(:,c), THETA(i), X(:,3));"
I cannot find where the error is. Thanks for the help!
Code:
% ************************************************************************************************************************************** % * % * Codes for the paper "Measuring Comovements by Regression Quantiles" by Lorenzo Cappiello, * % * Bruno Gerard and Simone Manganelli * % * * % * By SIMONE MANGANELLI, European Central Bank, Frankfurt am Main. * % * Created on 26 May, 2004. Last modified 13 April 2005. * % * * % *************************************************************************************************************************************** clear clc %NAME = ['BUL'; 'FRA']; %NAME = ['BRA'; 'ARG']; %NAME = ['CAR'; 'DSE'; 'EDR'; 'EHE'; 'EMN'; 'GMA'; 'MAR'; 'RVA'; 'DSE'; 'DSE']; NAME = ['CAR'; 'AVG'];
N = size(NAME,1);
THETA = [0.05:0.05:.95]; %THETA = [0.25, 0.75];
% Computes the CAViaR quantiles corresponding to THETA for i=1:length(THETA) THETA(i) for n = 1:N-1 for m = n+1:N couple = [NAME(n,:), NAME(m,:)]; %eval(['X=load(''', 'c:\BNBProject\', couple, '.txt''', ');']) eval(['X=load(''' couple, '.txt''', ');']) for c = 1:2 [quantile, dq] = CAViaROptimisation(100*X(:,c), THETA(i), X(:,3)); eval(['quantile_', int2str(n), '_', int2str(m), '_', int2str(c), '_', int2str(3), '(:,i) = quantile;']) eval(['dq_', int2str(n), '_', int2str(m), '_', int2str(c), '_', int2str(3), '(:,i*5-4:i*5) = dq;']) clear quantile dq end end end end save quantileEstimates4
function [quantile, dq] = CAViaROptimisation(y, THETA, C)
% ************************************************************************************************************************************** % * * % * Codes for the paper "Measuring Comovements by Regression Quantiles" by Lorenzo Cappiello, * % * Bruno Gerard and Simone Manganelli * % * * % * By SIMONE MANGANELLI, European Central Bank, Frankfurt am Main. * % * Created on 26 May, 2004. Last modified 13 April 2005. * % * *
% ****************************************************************************************************************************************
%
%
%************ INPUTS *********************
%
% y = (T,1) time series of returns
% THETA = confidence level of the Value at Risk
% C = Crisis dummy
%
%************ OUTPUT *********************
%
% quantile = (T,1)-vector of estimated quantiles
% dq = vector of quantile derivatives
%
%*****************************************************************************************
% *****************************************************************************************
% Set parameters for optimisation.
% *****************************************************************************************
T = length(y);
%REP = 5; % Number of times the optimization algorithm is repeated.
%nInitialVectors = [1000, 3]; % Number of initial vector fed in the uniform random number generator for AS model.
%nInitialVectors = [1, 5]; % Number of initial vector fed in the uniform random number generator for AS model.
nInitialCond = 5; % Select the number of initial conditions for the optimisation.
MaxFunEvals = 10000; % Parameters for the optimisation algorithm. Increase them in case the algorithm does not converge.
MaxIter = 100;
%options = optimset('LargeScale', 'off', 'HessUpdate', 'dfp', 'LineSearchType', 'quadcubic','MaxFunEvals', MaxFunEvals, ...
% 'display', 'off', 'MaxIter', MaxIter, 'TolFun', 1e-8, 'TolX', 1e-8);
warning off
% Compute the empirical THETA-quantile for y (the in-sample vector of observations).
%empiricalQuantile = ysort(round(300*THETA));
empiricalQuantile = prctile(y(1:300), THETA*100);
eps = 1e-10;
%
%
%**************************** Optimization Routine ******************************************
initialTargetVectors = [unifrnd(-.1, .1, nInitialCond, 3), unifrnd(.7, .99, nInitialCond, 1), unifrnd(-.1, .1, nInitialCond, 1)];
b=[];
for i = 1:nInitialCond
b0 = initialTargetVectors(i,:)'; b0 = b0; b1 = b0+3*eps;
RQ1 = 1e10;
while norm(b1-b0)>eps
b0 = b1; %RQ0 = RQ1;
b1 = fminsearch('CAViaR', b0, [], y, C, THETA, 1);
end
q = CAViaR(b1, y, C, THETA, 0);%q = CAViaR(y, b0, C, .05);
RQ1 = abs(THETA-(y<q))'*abs(y-q);
b = [b;RQ1/100, b1'];
end
b
[aa,a] = min(b(:,1));
b1 = b(a,2:end)';
%************************** Compute variables that enter output *****************************
% Compute VaR and Hit for the estimated parameters of RQ.
quantile = CAViaR(b1, y, C, THETA, 0);
%return
% Compute gradient
dq1 = zeros(T,1); dq2 = dq1; dq3 = dq1; dq4 = dq1; dq5 = dq1; q = dq1;
for i = 3:T
q(i) = b1(1) + b1(2)*C(i) + b1(3)*y(i-1) + b1(4)*q(i-1) - b1(3)*b1(4)*y(i-2) + b1(5)*abs(y(i-1));
dq1(i,1) = 1 + b1(4) * dq1(i-1,1);
dq2(i,1) = C(i) + b1(4) * dq2(i-1,1);
dq3(i,1) = y(i-1) + b1(4) * dq3(i-1,1) - b1(4)*y(i-2);
dq4(i,1) = q(i-1) + b1(4)*dq4(i-1,1) - b1(3)*y(i-2);
dq5(i,1) = abs(y(i-1)) + b1(4) * dq5(i-1,1);
end
dq = [dq1, dq2, dq3, dq4, dq5];

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Frequently-used Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by