TrailDreaming
TrailDreaming

Reputation: 567

How do you set the 'tail probabilities' in a scipy genextreme distribution?

Does anyone know how to set the 'q' parameter(which controls the lower or upper tail probability) in scipy's 'genextreme' distribution?

#/usr/bin/env python

import numpy as np
import pylab as plt
from scipy.stats import genextreme

mu, sigma = 10, 4
# QUESTION: How do you set q, the lower or upper tail probability, in genextreme?
rv = genextreme(c=[0], loc=mu, scale=sigma)
x = np.linspace(0, 40, 50) # Generate 50 values from 0-40

y_pdf = rv.pdf(x)
plt.plot( x, y_pdf, 'ro-', label='PDF' )

y_cdf = rv.cdf(x)
plt.plot( x, y_cdf, 'b*-', label='CDF' )

plt.legend()
plt.show()

@Warren: Thanks for the information. I am new to scipy stats. How can I move the distribution so the pdf is more heavily weighted at the upper end? I tried changing the 'c' param to [0.9], but the output was very choppy, with a hocky-stick rise and then a dramatic almost straight-line fall to zero where it stayed.

Upvotes: 0

Views: 1927

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114781

q is not a parameter of scipy's genextreme distribution. It is the argument to the methods ppf (inverse of the cdf) and isf (inverse of the survival function). If you look at the documentation for other distributions--e.g. norm, gamma--you'll see that all the class-level docstrings list q in their "parameters", but that documentation is an overview of the arguments to all the methods. genextreme has the standard location and scale parameters, and one shape parameter, c.

Example:

>>> genextreme.cdf(genextreme.ppf(0.95, c=0.5), c=0.5)
0.94999999999999996

You can find more about the generalized extreme distribution on wikipedia, but note that the shape parameter used in scipy has the opposite sign as the shape parameter in the wikipedia article. For example, the following generates the wikipedia plot:

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

# Create the wikipedia plot.
plt.figure(1)
x = np.linspace(-4, 4, 201)
plt.plot(x, genextreme.pdf(x, 0.5), color='#00FF00', label='c = 0.5')
plt.plot(x, genextreme.pdf(x, 0.0), 'r', label='c = 0')
plt.plot(x, genextreme.pdf(x, -0.5), 'b', label='c = -0.5')
plt.ylim(-0.01, 0.51)
plt.legend(loc='upper left')
plt.xlabel('x')
plt.ylabel('Density')
plt.title('Generalized extreme value densities')
plt.show()

Result:

genextreme densities

When c < 0, the left end of the support of the distribution is loc + scale / c. The following creates a plot of the distribution with c = -0.25, scale = 5, and loc = -scale / c (so the left end of the support is at 0):

c = -0.25
scale = 5
loc = -scale / c
x = np.linspace(0, 80, 401)
pdf = genextreme.pdf(x, c, loc=loc, scale=scale)
plt.plot(x, pdf)
plt.xlabel('x')
plt.ylabel('Density')

Plot:

genextreme example

The stats tutorial on the scipy documentation site has more information about the distribution classes, including a section on shifting and scaling a distribution.

Upvotes: 2

Related Questions