pystable: Stable distributions in Python

pystable is a Python module for calculating various quantities related to stable distributions. It implements several parameterisations and can convert between them as required. The generation of random variables is by an algorithm of Chambers, Mallows and Stuck [CMS], and the calculation of densities and distribution functions is via the integral formula given in Nolan [N].

The module should be considered alpha quality. There are several known issues.

Here it is:

Health warning! Although I work with stable distributions every day, I am not an expert in numerical methods, and I can't guarantee any level of accuracy for this code. I'd welcome any advice about implementation.

Installation and use

I don't have a proper Python package for this yet. However, if you are on a Unix-based platform, you should be able to simply put the file somewhere and run python in that directory; you should then be able to import the module, as below.

In addition to the standard library, the module requires the pygraph and scipy packages.

Here are a few simple use cases.

from pystable import *
import pylab

st = StableVar(pn_ST,alpha=1.5,beta=0.5)

# Draw a histogram:
x = rstable(10000,st)
pylab.hist(x,bins=100,range=(-10,10)) # heavy tails make the cutoff essential

# My First Stable Process Simulation
N = 100000
dt = 1/float(N)
t = pylab.linspace(dt,1,N)
x = map(lambda y: pow(dt, 1/st.params['alpha'])*y, rstable(len(t),st))
for i in range(len(x)-1):
    x[i+1] = x[i]+x[i+1]


These produced the following pictures.

Histogram of a stable distribution Crude representation of
    a stable sample path

You might also have fun with this simple animation of a stable process:


View online here: Documentation. This is an (accurate) draft.

Other implementations

I'm aware of some implementations of stable processes in various languages. For example,

Use of this module

If you would like to, you can cite as:
A. R. Watson, `pystable' Python module. URL


Chambers, J. M.; Mallows, C. L.; Stuck, B. W.
A method for simulating stable random variables.
J. Amer. Statist. Assoc. 71 (1976), no. 354, 340–344. [JSTOR] [MR]
Nolan, John P.
Numerical calculation of stable densities and distribution functions.
Comm. Statist. Stochastic Models 13 (1997), no. 4, 759–774. [doi] [MR]
Samorodnitsky, Gennady; Taqqu, Murad S.
Stable non-Gaussian random processes.
Chapman & Hall, 1994. ISBN: 0-412-05171-0. [MR]
Zolotarev, V. M.
One-dimensional stable distributions.
Translated from the Russian by H. H. McFaden. Translation edited by Ben Silver. Translations of Mathematical Monographs, 65.
American Mathematical Society, Providence, RI, 1986. x+284 pp. ISBN: 0-8218-4519-5 [MR]