# 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: pystable.py.

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 pystable.py 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
pylab.show()

# My First Stable Process Simulation
pylab.cla()
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]

pylab.plot(t,x)
pylab.show()


These produced the following pictures.  You might also have fun with this simple animation of a stable process: stable_anim.py

## Documentation

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,
• John Nolan has a collection of Windows binaries for simulating stable random variables, densities and so on. He also has a spin-off company which will sell you versions for other platforms and for commercial use.
• The stabledist package for R implements all the usual quantities according to an algorithm of Nolan. GPL.
• The circular package for R also generates stable random variables, using the [CMS] algorithm. GPL.
• PyLevy: An alternative implementation in Python, which also fits stable distributions to data using maximum likelihood estimation. GPL.

## Use of this module

If you would like to, you can cite as:
A. R. Watson, pystable' Python module. URL http://caskly.org/arw/stable`

## References

[CMS]
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]
[N]
Nolan, John P.
Numerical calculation of stable densities and distribution functions.
Comm. Statist. Stochastic Models 13 (1997), no. 4, 759–774. [doi] [MR]
[ST]