piccolo
piccolo

Reputation: 2227

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:

enter image description here

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?

Upvotes: 1

Views: 480

Answers (1)

Brennan
Brennan

Reputation: 514

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)

Upvotes: 2

Related Questions