计算不当积分Monte Carlo的方法

Calculate the improper integral Monte Carlo method

例如,我正在尝试转换积分:

我需要将其转换为从 0 到 1(或从 a 到 b)的积分,以便应用我实现的 Monte Carlo 算法。我已经有了 Monte Carlo 计算定积分的函数,但我需要将这个不正确的积分转换为定积分,我真的不知道该怎么做。

理想情况下,我想转换这个积分(从 -inf 到 inf):

我也试过做转换,这是我在网上找到的(代换积分:x=-ln(y), dx=-1/y),但它不起作用:

那么这个积分怎么转换呢?

I need to transform it to an integral that goes from 0 to 1 (or from a to b) in order to apply the algorithm of Monte Carlo I implemented.

不,你不知道。

如果你有积分

0 w(x) g(x) dx

您可以从 w(x) 中采样并计算采样点处 g(x) 的平均值。

你积分

0 e-x cos(x) dx

非常适合这种方法 - 您从 e-x 中采样并计算 E[cos(x)]

沿线(Python 3.9,Win10 x64)

import numpy as np

rng = np.random.default_rng()

N = 1000000
U = rng.random(N)

W = -np.log(1.0 - U) # sampling exp(-x)

G = np.cos(W)

ans = np.mean(G)
print(ans)

会打印类似

的内容
0.5002769491719996

关于你的第二个积分,见What is the issue in my array division step?