在给定累积概率的情况下获取随机变量的值Python

这里是快速的背景信息。我正在尝试使用蒙特卡洛方法为两个对数正态随机变量的线性组合获取组合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中实现

相关文章

本文适合有 Python 基础的小伙伴进阶学习 作者:pwwang 一、...
前言 目前有个python应用需要在容器镜像内拉取git私有仓库的...
前言 当网络不稳定或应用页面加载有问题,可以设置等待,避免...
前言 map()、reduce()、filter()是python的三个高阶函数。所...
入门使用 # 示例代码 warframe = [&quot;saryn&quot...
前言 功能描述:批量重命名指定目录下的文件,文件名加前缀,...