Code covered by the BSD License

Rolling Window Regression (For Beginners)

Karan Puri (view profile)

A beginners tool for analysing time varying coefficients within regression analysis.

Rolling_Window_Regression.m
```%Author: Karan Puri (PhD Researcher in Finance University of Bath) [Email:kp212@bath.ac.uk]
%--------------------------------------------------------------------------
%A simple exampe of rolling window regression
%The purpose of this file is to show the user a starting point in understading time varying coefficients
%The example used here is based on on the function 'regress' which performs OLS on the given parameters. However, the loop structure can be applied to other methods
%I am currently working on making the file more efficient. Comments are highly welcome
%--------------------------------------------------------------------------
clc;
clear;
Y=rand(211,10); %replace with dependant variable values, NOTE: Y cannot be a matrix; I have worked around it by creating a higher loop which goes through each col of Y and repeats the analysis
X=rand(211,1); %replace with independant variable values,X can be a vector or matrix, if latter then update regress as well!
steps=size(Y,1); % this function gives you the size of the colume vector
d=24;%this is discrete window length you wish to regress over - Change as per requirements
p=1; %number of regressors - Change as per requirements
j=1; %increment level so in this case we are running a rolling regression with a fixed window length of 24 data points that updates with 1 point increment. The loop will work for j>1 as well.
for k=1; % this is for the dependant variable so 1=col 1 and so on..increase k as per requirements
for i=1:j:steps-d;
[b bint r rint stats]=regress(Y(i:23+i,k),[ones(24,1),X(i:23+i,1)]);
Alpha(i,k)=b(1);
Beta(i,k)=b(2);
R_Squared(i,k)=stats(1); %stats provides other information as well such as F-stat;p-value etc that can be added to this easily
%Adj_R_Squared(i)=1-(1-R_Squared(i))*adjustment; %Use this when the number of regressors is greater than 1
error(:,i,k)=r(:,1); %if k>1 then the residulas will be 3D but are easily understood based on the matrix structure
CI_Alpha(i,:,k)=bint(1,:);
CI_Beta(i,:,k)=bint(2,:);
if CI_Alpha(i,1,k)*CI_Alpha(i,2,k)<0;
Significance_Alpha(i,k)=0;
else
Significance_Alpha(i,k)=1;
end
if CI_Beta(i,1,k)*CI_Beta(i,2,k)<0;
Significance_Beta(i,k)=0;
else
Significance_Beta(i,k)=1;
end

end
end
%Graphs
subplot(3,2,1),plot(Alpha); legend('Rolling_Alpha')
subplot(3,2,2),plot(Beta); legend('Rolling_Beta')
%you can add as many here by changing the structure of the subplot command. Alternativly use the plot command for individual figures.```