简体   繁体   English

如何更有效地根据先前列 y 中的值计算列 y+1(蒙特卡洛模拟)

[英]How to calculate column y+1 based on value in prior column y more efficienty (Monte Carlo SImulation)

my current code is following:我当前的代码如下:

stock_values = np.zeros([path, steps+1])
stock_values[:, 0] = s
for y in range(0, steps):
    stock_values[:, y+1] = stock_values[:, y] * (
        np.exp(change[:,y]))

with:和:

change = (r_d - 0.5*(sigma_d ** 2)) * deltat + sigma_d * np.sqrt(deltat) * np.random.normal(0, 1, size=(path, steps)) + np.random.poisson(lambda_j*deltat,size=(path, steps))* np.random.normal(r_j,sigma_j, size=(path, steps))

Stock_values and change are both an array with 1 000 000 x 1015 elements So, I run a Monte Carlo Simulation with GBM and Jump Diffusion, 1 000 000 paths and 1045 steps. Stock_values 和 change 都是具有 1 000 000 x 1015 个元素的数组因此,我使用 GBM 和 Jump Diffusion、1 000 000 条路径和 1045 步运行蒙特卡罗模拟。 Like this, the computing time is pretty slow, esp.像这样,计算时间很慢,尤其是。 since I would rather like to use 100 000 000 paths for this.因为我宁愿为此使用 100 000 000 条路径。 Unfortunately python only uses one kernel for the loop and lets the 7 others unused.不幸的是,python 只使用一个 kernel 作为循环,并让其他 7 个未使用。 For the "Change" matrix, it is able to use all kernels... (Sorry, do not have good technical/hardware skills and knowledge...)对于“更改”矩阵,它能够使用所有内核......(对不起,没有良好的技术/硬件技能和知识......)

I am looking for a function to exchange the "for" loop which calculates column y+1 based on value in col y, y+2 based on y+1 etc until y+1044.我正在寻找一个 function 来交换“for”循环,该循环根据 col y 中的值计算列 y+1,根据 y+1 等计算列 y+2,直到 y+1044。

Any ideas?有任何想法吗? Many thanks!非常感谢!

One first easy improvement if you have enough memory is to move np.exp out of the loop:如果您有足够的 memory,第一个简单的改进是将np.exp移出循环:

stock_values = np.zeros([path, steps+1])
stock_values[:, 0] = s
e = np.exp(changes)
for y in range(0, steps):
    stock_values[:, y+1] = stock_values[:, y] * e[:,y]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM