gis20
gis20

Reputation: 1094

Different values weibull pdf

I was wondering why the values of weibull pdf with the prebuilt function dweibull.pdf are more or less the half they should be

I did a test. For the same x I created the weibull pdf for A=10 and K=2 twice, one by writing myself the formula and the other one with the prebuilt function of dweibull.

import numpy as np
from scipy.stats import exponweib,dweibull
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
K=2.0
A=10.0
x=np.arange(0.,20.,1)
#own function
def weib(data,a,k):
    return (k / a) * (data / a)**(k - 1) * np.exp(-(data / a)**k)

pdf1=weib(x,A,K) 
print sum(pdf1)

#prebuilt function            
dist=dweibull(K,1,A)
pdf2=dist.pdf(x)
print sum(pdf2)

f=plt.figure()
suba=f.add_subplot(121)
suba.plot(x,pdf1)
suba.set_title('pdf dweibull')
subb=f.add_subplot(122)
subb.plot(x,pdf2)
subb.set_title('pdf own function')
f.show()

It seems with dweibull the pdf values are the half but that this is wrong as the summation should be in total 1 and not aroung 0.5 as it is with dweibull. By writing myself the formula the summation is around 1[enter image description here

Upvotes: 0

Views: 456

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114811

scipy.stats.dweibull implements the double Weibull distribution. Its support is the real line. Your function weib corresponds to the PDF of scipy's weibull_min distribution.

Compare your function weib to weibull_min.pdf:

In [128]: from scipy.stats import weibull_min

In [129]: x = np.arange(0, 20, 1.0)

In [130]: K = 2.0

In [131]: A = 10.0

Your implementation:

In [132]: weib(x, A, K)
Out[132]: 
array([ 0.        ,  0.019801  ,  0.03843158,  0.05483587,  0.0681715 ,
        0.07788008,  0.08372116,  0.0857677 ,  0.08436679,  0.08007445,
        0.07357589,  0.0656034 ,  0.05686266,  0.04797508,  0.03944036,
        0.03161977,  0.02473752,  0.01889591,  0.014099  ,  0.0102797 ])

scipy.stats.weibull_min.pdf:

In [133]: weibull_min.pdf(x, K, scale=A)
Out[133]: 
array([ 0.        ,  0.019801  ,  0.03843158,  0.05483587,  0.0681715 ,
        0.07788008,  0.08372116,  0.0857677 ,  0.08436679,  0.08007445,
        0.07357589,  0.0656034 ,  0.05686266,  0.04797508,  0.03944036,
        0.03161977,  0.02473752,  0.01889591,  0.014099  ,  0.0102797 ])

By the way, there is a mistake in this line of your code:

dist=dweibull(K,1,A)

The order of the parameters is shape, location, scale, so you are setting the location parameter to 1. That's why the values in your second plot are shifted by one. That line should have been

dist = dweibull(K, 0, A)

Upvotes: 1

Related Questions