简体   繁体   中英

truncated normal distribution with scipy in python

I am trying to use a truncated normal distribution with scipy in Python3. I want to do something simple: plot the pdf of a truncated normal centered at 0.5 and ranging from 0 to 1. I have the following code line

from scipy import truncnorm
import matplotlib.pyplot as plt
plt.plot([truncnorm.pdf(p,0,1, loc=0.5) for p in np.arange(0,1.1,0.1)])

However, this does not give the nice bell-shaped probability distribution function I would expect. Rather, it equals 0 before 0.5, and I cannot figure out why. Any advice on this?

Here's the procedure to follow according to the documentation of truncnorm .

# user input
myclip_a = 0
myclip_b = 1
my_mean = 0.5
my_std = 0.3

a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std
x_range = np.linspace(-1,2,1000)
plt.plot(x_range, truncnorm.pdf(x_range, a, b, loc = my_mean, scale = my_std))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM