【发布时间】:2020-12-05 12:10:50
【问题描述】:
这里是一个快速的背景信息。我正在尝试使用蒙特卡罗方法为两个对数正态随机变量的线性组合获得组合 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 中实现它
【问题讨论】:
-
你为什么要模拟对数正态分布?您可以使用 scipy 对两个累积对数正态进行平均。然后使用此函数反转函数:pypi.org/project/pynverse 进行采样。
-
你能解释一下如何用 scipy 平均两个累积对数正态吗?
-
(lognorm.cdf(第一组参数) + lognorm.cdf(第二组参数))/2.平均积分是积分的平均值。
-
@LevB 这是错误的,你没有平均CDF,请检查我的答案
-
@Mechanician 目标只是做采样吗?我的意思是要问是否需要 CDF 用于采样以外的其他目的。感谢您提供任何信息。
标签: python statistics probability distribution probability-distribution