简体   繁体   中英

Plotting truncated normal distribution

I am trying to plot a truncated Gaussian distribution (using scipy) with a mean of 0.5 , and a standard distribution of 1.0 . The distribution is truncated to be only in the interval (0,1) .

x = np.linspace(0,1,100)
dist=truncnorm(a=0,b=1,loc=0.5, scale = 1.0)
plt.plot(x, dist.pdf(x), 'k-', lw=2, label='normalised truncated Gaussian')

However I get this instead:

在此处输入图片说明

Everything after x=0.5 seems normal but below that you get a sudden dip to zero. However the distribution should only be zero outside of (0,1) . What is going on and how do I fix it?

You are telling it to plot that way with loc which shifts the plot.

dist=truncnorm(a=0,b=1,loc=0.5, scale = 1.0) should be dist=truncnorm(a=0,b=1, scale = 1.0) to get the standard plot.

From the source code on truncnorm():

For a uniform distribution MLE, the location is the minimum of the data, and the scale is the maximum minus the minimum. (Line 6570)

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