Why is this monte carlo simulation not faster with numba jit? Removing @jit makes it run a bit faster. However, I thought these loops where what numba was good at...
import numpy as np
from numba import jit
T = 1000
ALPHA = 0.11
BETA = 0.22
GAMMA = 0.33
@jit(nopython=True, fastmath=True)
def sim(n):
mu = np.array([0.0, 0.1]).reshape(2,1)
rho = 0.1
sigma = np.array([[1, rho*4],[rho*4, 4^2]])
A = np.linalg.cholesky(sigma)
out = np.empty((n, 2))
for i in range(n):
# (a)
u = np.random.randn(T)
# X = np.random.multivariate_normal(mu, sigma, T)
X = mu + A @ np.random.randn(2,T)
X = np.concatenate((np.ones((T, 1)), X.T), axis=1)
y = X @ np.array([ALPHA, BETA, GAMMA]) + u
# (b)
thetahat = np.linalg.solve(X.T @ X, X.T @ y)
Xf = X[:,:2].copy()
thetatilde = np.linalg.solve(Xf.T @ Xf, Xf.T @ y)
out[i,:] = (thetahat[1], thetatilde[1])
return out
n = 10**5
s = sim(n)
print(s)