使用重要性抽样公式积分

Integral using importance sampling formula

我正在尝试通过重要性抽样公式进行积分。算法如下: Code

积分值应为:0.838932960013382

还有我一定要用下 probability distribution to generate 1,000,000 random numbers between 0 and 1. I also use the next weight function。 最后,根据这些数字,我必须计算出这个 formula。 但是我得到的数值是错误的,我不确定1.000.000个随机数的计算结果。

你没有正确计算权重归一化和逆,检查你的数学

下面的代码,Python 3.9,Win 10 x64

import numpy as np
from scipy import integrate

def f(x):
    return 1.0/((np.exp(x)+1.0)*np.sqrt(x))

def w(x):
    return 0.5/np.sqrt(x)

def inv(x):
    return x*x

rng = np.random.default_rng()

N = 100000

x = rng.random(N)

p = inv(x)

q = f(p)/w(p)

print(np.mean(q))

print(integrate.quad(f, 0, 1))

打印

0.8389948486429488
(0.8389329600133858, 2.0727863869751673e-13)

看起来不错?