Skip to content Skip to sidebar Skip to footer

Truncated Normal With A Given Mean

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 ta

Solution 1:

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)

After getting mu from desired mean, you could put it into sampling routine

Post a Comment for "Truncated Normal With A Given Mean"