问题描述
在python中是否可以生成具有给定期望值的截断正态分布?我知道scipy.stats.truncnorm可以给出以原始正态分布的均值为参数的截断正态分布,但是我想创建一个截断正态分布,以使截短的分布是一个特定值。
Is it possible in python to generate a truncated normal distribution with a given expected value? I know that scipy.stats.truncnorm can give a truncated normal distribution that takes the mean of the original normal distribution as a parameter, but I want to create a truncated normal distribution such that the expected value of the truncated distribution is a particular value. Is this possible?
推荐答案
您可以在 mu
和均值之间进行转换,有关详细信息,请参见,
对于平均值,有一个简单的表达式,要得到 mu
,您必须求解非线性方程
You could convert between mu
and mean, see https://en.wikipedia.org/wiki/Truncated_normal_distribution for details,for mean there is simple expression, to get mu
you have to solve nonlinear equation
import scipy
from scipy.stats import norm
def get_mean(mu, sigma, a, b):
alpha = (a - mu)/sigma
beta = (b - mu)/sigma
Z = norm.cdf(beta) - norm.cdf(alpha)
return mu + (norm.pdf(alpha) - norm.pdf(beta)) / Z
def f(x, mean, sigma, a, b):
return mean - get_mean(x, sigma, a, b)
def get_mu(mean, sigma, a, b):
mu = scipy.optimize.brentq(f, a, b, args=(mean, sigma, a, b))
return mu
a = -2.0
b = 3.0
sigma = 1.0
mu = 0.0
mean = get_mean(mu, sigma, a, b)
print(mean)
mu = get_mu(mean, sigma, a, b)
print(mu)
从期望的均值得到 mu
后,可以将其放入采样例程
After getting mu
from desired mean, you could put it into sampling routine
这篇关于以给定的平均值截断法线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!