这里是快速的背景信息。我正在尝试使用蒙特卡洛方法为两个对数正态随机变量的线性组合获取组合CDF,然后将其反转以进行采样。这是用于执行相同操作的Python代码:
import numpy as np
from scipy import special
# parameters of distribution 1
mu1 = 0.3108
s1=0.3588
# parameters of distribution 2
mu2=1.2271
s2=0.2313
a = 2
b=3
N_sampling = 10000
kk=0
Y=np.zeros(N_sampling)
X1=np.zeros(N_sampling)
X2=np.zeros(N_sampling)
while(kk<N_sampling):
F = np.random.rand(2)
X1[kk]=np.exp(mu1+(2**0.5)*s1*special.erfinv(2*F[0]-1)) # sampling X1 (distribution1) by inverting the CDF
X2[kk]=np.exp(mu2+(2**0.5)*s2*special.erfinv(2*F[1]-1)) # sampling X2 (distribution2) by inverting the CDF
Y[kk]=a*X1[kk]+b*X2[kk] # obtain the random variable as a linear combination of X1 and X2
kk=kk+1
# Obtain the CDF of Y
freq,bin_borders = np.histogram(Y,bins=50)
norm_freq = freq/np.sum(freq)
cdf_Y = np.cumsum(norm_freq)
# obtain the value of Y given the value of cdf_Y
cdf_Y_input=0.5
idx=np.searchsorted(cdf_Y,cdf_Y_input)
Y_out = 0.5*(bin_borders[idx-1]+bin_borders[idx])
问题:
scipy中是否有直接功能来执行此操作?
在代码的最后一行中,我取的是平均值,有没有办法通过内插等方式获得更准确的值?如果是这样,如何在Python中实现