A subroutine in SAS to simulate asset pricing paths

This post was kindly contributed by SAS ANALYSIS - go there to comment and to read the full post.

For matrix computation in SAS, SAS/IML is the choice. This module has its own syntax, functions and even plotting subsystem. Some statisticians used it to realize the algorithms beyond the reach of SAS’s procedures, for example, boosting [Ref. 1]. However, comparing with other popular matrix-based languages, such as R and Matlab, SAS/IML has no edge. SAS’s most valuable products are still its robust data step and statistical procedures. ‘Porting’ source codes from other languages into SAS has to rely on data step.

Asset prices can be estimated by Monte Carlo simulation. To generate a series of price-evolving paths with several most common parameters, Dr. Brandimarte codes a naive Matlab function to apply the standard Wiener process [Ref. 2]. With random seeds for normal distribution, multiple pricing mechanisms can be demonstrated and compared. In SAS 9.2, the workflow of simulation and visualization would be modularized as a data step subroutine. Later, such a pricing subroutine could be easily invoked under given circumstances.

It is said that SAS 9.3 is going to be released 2011Q3. Hope this time, the data step function compiler, Proc Fcmp, could be more dynamic and with more methods.

References:
1. Dmitrienko, Alex, Christy Chuang-Stein, and Ralph D’Agostino. Pharmaceutical Statistics Using SAS: A Practical Guide. SAS Publishing. 2007
2. Paolo Brandimarte. Numerical methods in finance. John Wiley & Sons. 2002.

/*******************READ ME*********************************************
* - A SUBROUTINE IN SAS TO SIMULATE ASSET PRICING PATHS -
*
* VERSION: SAS 9.2(ts2m0), windows 64bit
* DATE: 17apr2011
* AUTHOR: hchao8@gmail.com
*
****************END OF READ ME*****************************************/

****************(1) MODULE-BUILDING STEP******************;
******(1.1) COMPILE FUNCTION-ACCOMPANYING MACRO*************;
option mstored sasmstore = sasuser;
%macro AssetPath_macro() / store source;
data _tmp1;
set _tmp;
day = _n_ - 1;
run;

proc transpose data = _tmp1 out = _tmp2 ;
by day;
var path:;
run;

data _tmp2;
set _tmp2;
label _name_ = 'Simulated paths';
run;

ods html style = money;
proc sgplot data = _tmp2;
series x = day y = col1 / group = _name_;
yaxis grid label = 'Asset price';
xaxis grid label = 'Change by days';
run;
ods html close;
%mend;

******(1.2) COMPILE SUBROUTINE FOR ASSET PRICING*************;
proc fcmp outlib = sasuser.astpth.funcs;
subroutine AssetPath(S0, mu, sigma, T, NSteps, NRepl);
/*************************************************************
* SUBROUTINE: AssetPath()
* PARAMETERS: S0 = the initial price
* mu = the drift
* sigma = the volatility
* T = the horizontal time
* NSteps= the number of time steps
* NRepl = the number of replications
*************************************************************/
array SPaths[1, 1] / nosymbols;
array Path[1, 1] / nosymbols;
call dynamic_array(SPaths, NRepl, NSteps + 1);
call dynamic_array(Path, NSteps + 1, NRepl);
call zeromatrix(SPaths);
do _row = 1 to NRepl;
SPaths[_row, 1] = S0;
end;
dt = T / NSteps;
nudt = (mu - 0.5*sigma**2) * dt;
sidt = sigma * sqrt(dt);
do _row = 1 to NRepl;
do _col = 1 to Nsteps;
SPaths[_row, _col + 1] = SPaths[_row, _col] *
exp(nudt + sidt*rannor(0));
end;
end;
call transpose (SPaths, Path);
rc1 = write_array('_tmp', Path);
rc2 = run_macro('AssetPath_macro');
endsub;
quit;
****************END OF STEP (1)******************;

****************(2) TESTING STEP******************;
option cmplib = (sasuser.astpth) mstored sasmstore = sasuser;
data _null_;
S0 = 50; mu = 0.1; sigma = 0.3; T = 1; NSteps = 365; NRepl = 3;
call AssetPath(S0, mu, sigma, T, NSteps, NRepl);
run;
****************END OF STEP (2)******************;

****************END OF ALL CODING***************************************;

This post was kindly contributed by SAS ANALYSIS - go there to comment and to read the full post.