1+ # # Test case from Issue #445
2+
3+ function stockcorr ()
4+ # STOCKCORR - The original, unoptimised code that simulates two correlated assets
5+
6+ # # Correlated asset information
7+ CurrentPrice = [78. 102. ]; # Initial Prices of the two stocks
8+ Corr = [1. 0.4 ; 0.4 1. ]; # Correlation Matrix
9+ T = 500 ; # Number of days to simulate = 2years = 500days
10+ n = 100000 ; # Number of simulations
11+ dt = 1. / 250. ; # Time step (1year = 250days)
12+ Div= [0.01 0.01 ]; # Dividend
13+ Vol= [0.2 0.3 ]; # Volatility
14+
15+ # #Market Information
16+ r = 0.03 ; # Risk-free rate
17+
18+ # # Define storages
19+ SimulPriceA= zeros (T,n); # Simulated Price of Asset A
20+ SimulPriceA[1 ,:]= CurrentPrice[1 ];
21+ SimulPriceB= zeros (T,n); # Simulated Price of Asset B
22+ SimulPriceB[1 ,:]= CurrentPrice[2 ];
23+
24+ # # Generating the paths of stock prices by Geometric Brownian Motion
25+ UpperTriangle= chol (Corr); # UpperTriangle Matrix by Cholesky decomposition
26+
27+ for i= 1 : n
28+ Wiener= randn (T- 1 ,2 );
29+ CorrWiener= Wiener* UpperTriangle;
30+ for j= 2 : T
31+ SimulPriceA[j,i]= SimulPriceA[j- 1 ,i]* exp ((r- Div[1 ]- Vol[1 ]^ 2 / 2 )* dt+ Vol[1 ]* sqrt (dt)* CorrWiener[j- 1 ,1 ]);
32+ SimulPriceB[j,i]= SimulPriceB[j- 1 ,i]* exp ((r- Div[2 ]- Vol[2 ]^ 2 / 2 )* dt+ Vol[2 ]* sqrt (dt)* CorrWiener[j- 1 ,2 ]);
33+ end
34+ end
35+
36+ return (SimulPriceA, SimulPriceB)
37+
38+ end
0 commit comments