Issue with plotting graph using recessionplot

2 views (last 30 days)
I have an issue when trying to plot a graph and overaly recession bar on it using Matlab's inbuilt function recessionplot.
Recessionplot requires dates to be formatted as datenum.
Below is the code I am using currently (its a long code so I am provideing the important bits only!):
% Clean working environment
close all; clear; clc
% parameters that will be used by NLS nonlinear optimization routine:
global y X X2 k ndeltas yr ;
% load s&p500 price index file
load Data30.txt;
PI = Data30;
yr = PI(:, 1);
mo = PI(:, 2);
dy = PI(:, 3);
m30 = PI(:, 4); % price index 30-minutes before event time
p30 = PI(:, 5); % price index 30-minutes after event time
returns_30mins = ((p30-m30)./m30) ; % returns for event window (-30,30)
date = datenum(yr,mo,dy) ; % for graphing
nrows = length(yr);
%%%%%%%%%%%%%%%REMOVED PART OF THE CODE TO KEEP ONLY IMPORTANT MATERIAL %%%%%%%%%%%%
% Use estimated beta to run rolling regressions of delta
i = find(yr==2004 & mo==1); startrolldate = i(1); % rolling regression plot start date
i = find(yr==2016 & mo==12); endrolldate = i(end); % rolling regression plot end date
% define new regressor
surp = [const, X *beta] ;
% rolling regression parameters
rollby = 1 ; % number of days to roll by - Daily
% ES trades from 18:01 to 17:00 Friday to Sunday; non-trading hours are from
% 16:00 to 16:15 & 17:01 to 18:00 ==> 1,365 data points per day
% Size of rolling regression window (centered), 1 yr = 254 business days
window = 254 ;
rollvec = 1:rollby:nrows ;
for i = 1:length(rollvec) ;
[deltatemp, omegatemp] = ols(y(max(1,rollvec(i)-window/2+1):min(rollvec(i)+window/2,nrows)), ...
surp(max(1,rollvec(i)-window/2+1):min(rollvec(i)+window/2,nrows),:), 0) ;
timevardelta(i) = deltatemp(2) ;
timevarsd(i) = sqrt(omegatemp(2,2)) ;
end ;
% adjust timevarsd to account for uncertainty about beta
for i = startyr:endyr ;
j = find(yr==i & mo==6) ;
j2 = find(rollvec > j(end)) ;
yrmidpts(i-startyr+1) = rollvec(j2(1)) ;
end ;
% yrmidpts(2016-startyr+1) = rollvec(end) ; %uncomment this if sample doesn't end in Dec
for i = 2:length(yrmidpts)-1 ;
j = find(rollvec>=yrmidpts(i) & rollvec<yrmidpts(i+1)) ;
adjtimevarsd(j) = (stderrs(k+i-1) + [0:length(j)-1]*(stderrs(k+i)-stderrs(k+i-1))/length(j)) .* ...
timevarsd(j)./ (timevarsd(j(1)) + [0:length(j)-1]*(timevarsd(j(end))-timevarsd(j(1)))/length(j)) ;
end ;
j = find(rollvec<yrmidpts(2)) ;
adjtimevarsd(j) = stderrs(k+1) * timevarsd(j)/timevarsd(j(end)) ;
j = find(rollvec>=yrmidpts(end)) ;
adjtimevarsd(j) = stderrs(k+ndeltas-1) * timevarsd(j)/timevarsd(j(1)) ;
% Plot rolling regression results in Figure 1:
figure(1); clf ;
plot(date,timevardelta','LineWidth',1.5) ;
hold on ;
plot(date,timevardelta'+1.96*adjtimevarsd',':','Color',[.4 .4 .4],'LineWidth',1.5) ;
plot(date,timevardelta'-1.96*adjtimevarsd',':','Color',[.4 .4 .4],'LineWidth',1.5) ;
plot(date,ones(size(date)),'k', date, zeros(size(date)),'k') ;
ax = gca;
datetick('x','yyyy')
xlabel('Year');
ylabel('Delta');
axis tight;
recessionplot;
The code runs ok when the rollby value is 1, i.e. I am performing daily rolling regressions.
However, when I change rollby to, say, 10. The code for graphing the coefficient does not work.
I get the following error:
Error using plot
Vectors must be the same length.
Error in Mutivariate_30Mins (line 173)
plot(dn,timevardelta','LineWidth',1.5)
It appears that I am missing a line of code that adjusts the x-axis values for date when rolling window changes; so, my issue is kind of understandable because Matlab cannot figure out that the date vector has changed to the fact that the rolling window has changed.
Any tip around how to get past this issue?
Thank you in advance for your help.
PS:
The dates have gaps.
I cannot keep the whole dataset as I have 4.8 million plus observations and it would be difficult to run an nls regression if I keep the full length of my dataset.

Answers (1)

Cris LaPierre
Cris LaPierre on 17 Dec 2018
Edited: Cris LaPierre on 17 Dec 2018
The error means that, in your plot(X,Y) command, your variables X and Y do not have the same number of data points.
As I see it, your XData (date) is set at the beginning of your code and never changes, while YData (timevardelta) is set inside your for loop, where the number of data points created will depend on the value of rollby.
To work, you will need to adjust date so it is the same length as timedelta AND ensure that the values in date correspond to the values in timedelta.
If everything works when rollby==1, what about something like this
date = date(1:rollby:nrows);

Categories

Find more on Multivariate Models in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!