JuliaLang/julia — Issue #445: Julia's is slow when generating stochastic time-series
Author: dgleich
Created: 2012-02-22 16:35:57+00:00
State: open

Julia looks really interesting. My impression was that it'd be a great language to compare in the studies done here: http://www.walkingrandomly.com/?p=3604.

The idea is to generate about 500MB of synthetic time series data for financial applications.

The unoptimized Matlab code takes about 10 seconds on my machine.

I expected Julia to do much better on that same unoptimized code, which heavily utilizes for loops.

However, when I tried the following julia port:

```
function original_corr()
#ORIGINAL_CORR - The original, unoptimised code that simulates two correlated assets

## Correlated asset information
CurrentPrice = [78. 102.]; #Initial Prices of the two stocks
Corr = [1. 0.4; 0.4 1.]; #Correlation Matrix
T = 500; #Number of days to simulate = 2years = 500days
n = 100000; #Number of simulations
dt = 1./250.; #Time step (1year = 250days)
Div=[0.01 0.01]; #Dividend
Vol=[0.2 0.3]; #Volatility

##Market Information
r = 0.03; #Risk-free rate

## Define storages
SimulPriceA=zeros(T,n); #Simulated Price of Asset A
SimulPriceA[1,:]=CurrentPrice[1];
SimulPriceB=zeros(T,n); #Simulated Price of Asset B
SimulPriceB[1,:]=CurrentPrice[2];

## Generating the paths of stock prices by Geometric Brownian Motion
UpperTriangle=chol(Corr); #UpperTriangle Matrix by Cholesky decomposition

for i=1:n
Wiener=randn(T-1,2);
CorrWiener=Wiener*UpperTriangle;
for j=2:T
SimulPriceA[j,i]=SimulPriceA[j-1,i]*exp((r-Div[1]-Vol[1]^2/2)*dt+Vol[1]*sqrt(dt)*CorrWiener[j-1,1]);
SimulPriceB[j,i]=SimulPriceB[j-1,i]*exp((r-Div[2]-Vol[2]^2/2)*dt+Vol[2]*sqrt(dt)*CorrWiener[j-1,2]);
end
end

return (SimulPriceA, SimulPriceB)

end
```

I was surprised to see that it took 60 seconds... 6 times longer than Matlab.

If I take out the matrix multiplication:
```
Wiener=randn(T-1,2);
#CorrWiener=Wiener*UpperTriangle;
CorrWiener=Wiener;
```

Then the function takes only 6 seconds instead, which is still a bit slower than I would have expected. Vectorized Matlab code takes 3 seconds.

Is there something else in that code that is "slow" in julia?

And does the MKL in Matlab really make the 500x2 -times- 2x2 mat-mults that much faster?