kilojoules
kilojoules

Reputation: 10073

scipy: How do I use weibull_min.pdf?

I'm finding scipy's weibull_min pdf function unintuitive and hard to use. I want to generate a weibull PDF with scale=30 and shape=2.5. This is what that is supposed to look like:enter image description hereenter image description here

Here's what I've tried so far:

import matplotlib.pyplot as plt
from scipy.stats import weibull_max, uniform
import numpy as np
aoas = np.linspace(0, 8, 1000)
speeds = np.linspace(1, 80, 1000)
plt.fill_between(speeds, speeds * 0, weibull_max.pdf(speeds, 2.5, 30), facecolor='k')
plt.ylabel('Probability Density')
plt.savefig('speedDist.pdf')
plt.clf()

enter image description here

Of course, my code is wrong. The documentation says that weibull_min expects pdf(x, c, loc=0, scale=1), where x are quantiles, c is the shape factor, and scale is the scale factor. But, when I change my code to weibull_max.pdf(speeds, 2.5, 0, 30), the output pdf is composed of only zero values. So, I am confusion. How do I generate the desired Weibull PDF?

Upvotes: 2

Views: 3424

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114781

There are two problems in your code.

  • weibull_min and weibull_max are not the same distribution. You refer to weibull_min in the title and description of the problem, but you use weibull_max in the code. To match the plot that you show, use weibull_min.

  • The parameters of the pdf method are (x, shape, loc, scale). You wrote pdf(speeds, 2.5, 30), which assigned 30 to the loc parameter. Instead, you should use pdf(speeds, 2.5, scale=30)

Here's a variation of your script:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import weibull_min


speeds = np.linspace(0, 80, 1000)    
p = weibull_min.pdf(speeds, 2.5, scale=30)

plt.plot(speeds, p, 'b', linewidth=1)
plt.fill_between(speeds, speeds * 0, p, facecolor='b', alpha=0.1)
plt.ylabel('Probability Density')
plt.show()

It generates this plot:

plot

Upvotes: 3

Related Questions