Module Levy¶
This is a package for calculation of Levy stable distributions (probability density function and cumulative density function) and for fitting these distributions to data.
It operates by interpolating values from a table, as direct computation of these distributions requires a lengthy numerical integration. This interpolation scheme allows fast fitting of data by Maximum Likelihood.
Notes on the parameters¶
- the parameters of the Levy stable distribution can be given in multiple ways: parametrizations. Here, you can use both parametrizations 0 and 1, in the notation of Nolan (http://fs2.american.edu/jpnolan/www/stable/stable.html) and parametrizations A, B and M from Zolotarev (Chance and Stability).
- Nolan parametrizations are a bit easier to understand. Parametrization 0 is typically preferred for numerical calculations, and has \(E(X)=\delta_0-\beta\gamma\tan(\pi\alpha/2)\) while parametrization 1 is preferred for better intuition, since \(E(X)=\delta_1\).
- parametrizations are dealt automatically by the module, you just need to specify which one you want to use. Also, you can use the function Parameters.convert to transform the parameters from one parametrization to another. The module uses internally parametrization 0.
- pylevy does not support alpha values lower than 0.5.
Classes¶
-
class
levy.
Parameters
(par='0', **kwargs)¶ This class is a wrap for the parameters; it works such that if we fit fixing one or more parameters, the optimization only acts on the other (the key thing here is the setter). The only useful function to be used directly is convert, which allows to transform parameters from one parametrization to another. Available parametrizations are {0, 1, A, B, M}.
-
classmethod
convert
(pars, par_in, par_out)¶ Use to convert a parameter array from one parametrization to another.
- Examples:
>>> a = np.array([1.6, 0.5, 0.3, 1.2]) >>> b = Parameters.convert(a, '1', 'B') >>> b array([1.6 , 0.55457302, 0.2460079 , 1.4243171 ]) >>> c = Parameters.convert(b, 'B', '1') >>> c array([1.6, 0.5, 0.3, 1.2]) >>> np.testing.assert_allclose(a, c)
Parameters: Returns: array of parameters in the desired parametrization
Return type:
-
get
(par_out=None)¶ Same as convert but using from within the Parameter object.
- Examples:
>>> p = Parameters(par='1', alpha=1.5, beta=0.5, mu=0, sigma=1.2) # to convert >>> p.get('B') # returns the parameters in the parametrization B array([1.5 , 0.59033447, 0.03896531, 1.46969385])
-
classmethod
Functions¶
-
levy.
random
(alpha, beta, mu=0.0, sigma=1.0, shape=())¶ Generate random values sampled from an alpha-stable distribution. Notice that this method is “exact”, in the sense that is derived directly from the definition of stable variable. It uses parametrization 0 (to get it from another parametrization, convert).
- Example:
>>> rnd = random(1.5, 0, shape=(100,)) # parametrization 0 is implicit >>> par = np.array([1.5, 0.905, 0.707, 1.414]) >>> rnd = random(*Parameters.convert(par ,'B' ,'0'), shape=(100,)) # example with convert
Parameters: Returns: generated random values
Return type:
-
levy.
levy
(x, alpha, beta, mu=0.0, sigma=1.0, cdf=False)¶ Levy distribution with the tail replaced by the analytical (power law) approximation.
alpha in (0, 2] is the index of stability, or characteristic exponent. beta in [-1, 1] is the skewness. mu in the reals and sigma > 0 are the location and scale of the distribution (corresponding to delta and gamma in Nolan’s notation; note that sigma in levy corresponds to sqrt(2) sigma in the Normal distribution). cdf is a Boolean that specifies if it returns the cdf instead of the pdf.
It uses parametrization 0 (to get it from another parametrization, convert).
- Example:
>>> x = np.array([1, 2, 3]) >>> levy(x, 1.5, 0, cdf=True) array([0.75634202, 0.89496045, 0.94840227])
Parameters: Returns: values of the pdf (or cdf if parameter ‘cdf’ is set to True) at ‘x’
Return type:
-
levy.
fit_levy
(x, par='0', **kwargs)¶ Estimate parameters of Levy stable distribution given data x, using Maximum Likelihood estimation.
By default, searches all possible Levy stable distributions. However you may restrict the search by specifying the values of one or more parameters. Notice that the parameters to be fixed can be chosen in all the available parametrizations {0, 1, A, B, M}.
- Examples:
>>> np.random.seed(0) >>> x = random(1.5, 0, 0, 1, shape=(200,)) >>> fit_levy(x) # -- Fit a stable distribution to x (par=0, alpha=1.52, beta=-0.08, mu=0.05, sigma=0.99, 402.37150603509247)
>>> fit_levy(x, beta=0.0) # -- Fit a symmetric stable distribution to x (par=0, alpha=1.53, beta=0.00, mu=0.03, sigma=0.99, 402.43833088693725)
>>> fit_levy(x, beta=0.0, mu=0.0) # -- Fit a symmetric distribution centered on zero to x (par=0, alpha=1.53, beta=0.00, mu=0.00, sigma=0.99, 402.4736618823546)
>>> fit_levy(x, alpha=1.0, beta=0.0) # -- Fit a Cauchy distribution to x (par=0, alpha=1.00, beta=0.00, mu=0.10, sigma=0.90, 416.54249079255976)
Parameters: Returns: a tuple with a Parameters object and the negative log likelihood of the data.
Return type: