/***************************************************************************************************************** SAS file name: RSLN2_Model.sas File location: _________________________________________________________________________________________________________________ Purpose: To demonstrate how to simulate the RSLN-2 model in SAS using an IML module. Author: Peter Clemmensen Creation Date: 12/05/2016 This program supports the post "Simulate An RSLN-2 Model In SAS" on SASnrd.com Function to simulate RSLN-2 model using algorithm on page 98 in Hardy. *****************************************************************************************************************/ proc iml; Start RSLN2(per,sce) global(pi_1,spmean, spvar, P); t = t(1:per); /* Vector of time per */ S = j(per,sce,1); /* Vector of Prices */ rho = j(per,1,0); /* Space for regimes */ /* 1. Generate initial Uniform Random Variable */ u = rand("Uniform"); /* 2. If generated u is less than calculated pi_1 */ /* then we assume that the process is in regime 1. */ if u < pi_1 then do; rho[1]=1; mu=spmean[1]; sigma=spvar[1]; end; /* Otherwise we assume that the process is */ /* in regime 2. */ else do; rho[1]=2; mu=spmean[2]; sigma=spvar[2]; end; do j = 1 to sce; do i = 1 to per-1; /* 3. Draw z from standard normal */ z = rand("Normal"); /* 4. Calculate LOG return based on */ /* value of rho and calculate Stock Price */ Y = mu + sigma*z; /* Update Stock Price */ S[i+1,j]=S[i,j]*exp(Y); /* 5. Generate new Uniform Random Variable */ u = rand("Uniform"); /* 6. If the newly generated u is less than the */ /* relevant transition probability, then we */ /* assume that the new regime is 1 */ if (u<(P[rho[i],1])) then do; rho[i+1]=1; mu=spmean[1]; sigma=spvar[1]; end; /* Otherwise we assume regime 2 */ else do; rho[i+1]=2; mu=spmean[2]; sigma=spvar[2]; end; /* 7. Repeat steps 3-7 for desired # periods */ end; /* 8. Repeat steps 1-7 for desired # scenarios */ end; /* Return matrix of Equity Prices S */ S = S || rho; return (S); Finish; P = {0.9532 0.0468, 0.3232 0.6768}; pi_1 = P[1,2]/(P[1,2]+P[2,1]); spmean = {0.0127, -0.0162}; spvar = {0.0351, 0.1748}; per=80; sce=1; /* Simulate Stockprices */ M = RSLN2(per,sce); VarNames="S1":"S"+char(sce) || "Region"; /* Create Dataset with StockPrice Data */ create StockPrices from M[c=VarNames]; append from M; close StockPrices; quit; data StockPrices; format Period; set StockPrices; Period=_N_; Regionc=ifc(Region=1, "Region 1", "Region 2"); run; title "RSLN-2 Simulated Stock Price Evolution"; proc sgplot data=StockPrices; styleattrs datacolors=(red green); block x=Period block=Regionc / novalues name="Region"; series x=Period y=S1 / name="Price" lineattrs=(thickness=2 color=Black); xaxis display=(noline noticks) label="Period" labelattrs=(size=12 weight=bold); yaxis display=(noline noticks nolabel); keylegend "Price" "Region"; run;