Saturday, May 26, 2012

Future Pricing and MR factor model

MATLAB code

[hist_date, hist_high, hist_low, hist_open, hist_close, hist_vol] =get_hist_stock_data('SPX','2006');
Data=hist_close;
[n,nn] = size(Data);

R_dollar=hist_close(2:end)-hist_close(1:end-1);
R_pct=R_dollar./hist_close(1:end-1);

[b_int,nn,nn,nn,stats] = regress(R_pct,[ones(n-1,1),Data(1:(n-1),1)]); 

intercept = b_int(1);
slope = b_int(2);
if (slope > 0)
    error('Cannot use geometric mean reversion: pct chg=k*(mu-s)+sigma*dW requiring k>0)');
end

sigma = sqrt(stats(4));
k = -slope; 
mu = -intercept/slope;


days=20;
dT = 1; 

St=zeros(days,1);
St(1)=hist_close(end);

for i=1:1:days
    St(i+1)=St(i)+(k*(mu-St(i))+sigma*normrnd(0,1))*St(i);
end 

% future price dS/S = dLn(F(t))/dt *dt + signma*dW or
%              Ft-1=Ft*exp(-dS/S+signma*dW)
Ft=zeros(days,1);
Ft(end)=St(end);  
for i=days:-1:2
    chg =(St(i+1)-St(i))/St(i);
    Ft(i-1)=Ft(i)*exp(-chg+sigma*normrnd(0,1));
end 

f1=figure(1);
set(f1,'name','%-Return Mean Reversion Factor Model');
p=plot(0:days,St);
set(p,'color','red');
hold on;
p=plot(1:days,Ft);
hold off;


No comments: