Help with changing comovement code

7 visualizaciones (últimos 30 días)
Dilyana
Dilyana el 1 de Jul. de 2014
Comentada: Image Analyst el 2 de Jul. de 2014
Hello,
I need help changing some existing working code that estimates co movements. The code is reproduced below. I would like to change the estimation to omit the dummy C. Simply removing it does not work since some matrix dimensions change but I cannot figure out exactly which ones.
% ************************************************************************************************************************************** % * % * 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']; N = size(NAME,1);
THETA = [0.05:0.10:.95]; %THETA = [0.05, 0.25, 0.50, 0.75, 0.95];
% 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:\Simonefiles\', 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 c:\Simonefiles\quantileEstimates exit
---------------------------------------------------------------------------------------------- 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 = 100; % 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];
---------------------------------------------------------------------------------------------- function q = CAViaR(b, y, C, THETA, IND)
% ************************************************************************************************************************************** % * % * 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. * % * * % ***************************************************************************************************************************************
T = length(y); q = zeros(T,1); %ysort = sortrows(y(1:300), 1); %empiricalQuantile = ysort(round(300*THETA)); empiricalQuantile = prctile(y(1:300), THETA*100); q(1) = empiricalQuantile; q(2)=q(1); for t = 3:T %q(t) = b(1) + b(2) * abs(y(t-1)) + b(3) * q(t-1); %q(t) = b(1) + b(2) * C(t) + b(3) * q(t-1) + b(4) * abs(y(t-1)); q(t) = b(1) + b(2) * C(t) + b(3) * y(t-1) + b(4) * q(t-1) - b(3)*b(4) * y(t-2) + b(5) * abs(y(t-1)); end
if IND == 1 RQ = abs(THETA-(y<q))' * abs(y-q); q = RQ; end
  1 comentario
Image Analyst
Image Analyst el 2 de Jul. de 2014
Just attach your m-file with the paper clip icon. Or read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup I tried copying and pasting but after fixing 5 errors and still couldn't even run it, I just said it was too much trouble to help you and gave up. Maybe someone else will take a shot at it if you make it easy for them.

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 2 de Jul. de 2014

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by