getting the parameter names of scipy.stats distributions

  • Last Update :
  • Techknowledgy :

Warren Weckesser and I have developed a more robust solution:

import sys
import scipy.stats

def list_parameters(distribution):
   ""
"List parameters for scipy.stats.distribution.
# Arguments
distribution: a string or scipy.stats distribution object.
# Returns
A list of distribution parameter strings.
""
"
if isinstance(distribution, str):
   distribution = getattr(scipy.stats, distribution)
if distribution.shapes:
   parameters = [name.strip() for name in distribution.shapes.split(',')]
else:
   parameters = []
if distribution.name in scipy.stats._discrete_distns._distn_names:
   parameters += ['loc']
elif distribution.name in scipy.stats._continuous_distns._distn_names:
   parameters += ['loc', 'scale']
else:
   sys.exit("Distribution name not found in discrete or continuous lists.")
return parameters

This code demonstrates the information that ev-br gave in his answer in case anyone else lands here.

>>> from scipy
import stats
   >>>
   dists = ['alpha', 'anglit', 'arcsine', 'beta', 'betaprime', 'bradford', 'norm'] >>>
   for d in dists:
   ...dist = getattr(scipy.stats, d)
   ...dist.name, dist.shapes
   ...
   ('alpha', 'a')
   ('anglit', None)
   ('arcsine', None)
   ('beta', 'a, b')
   ('betaprime', 'a, b')
   ('bradford', 'c')
   ('norm', None)

Suggestion : 2

Let’s check the number and name of the shape parameters of the gamma distribution. (We know from the above that this should be 1.),In all three tests, the p-values are very low and we can reject the hypothesis that the our sample has skew and kurtosis of the normal distribution.,The main additional methods of the not frozen distribution are related to the estimation of distribution parameters:,Now, we set the value of the shape variable to 1 to obtain the exponential distribution, so that we compare easily whether we get the results we expect.

>>> from scipy
import stats
>>> from scipy.stats
import norm
>>> print('bounds of distribution lower: %s, upper: %s' % norm.support())
bounds of distribution lower: -inf, upper: inf
>>> rv = norm() >>>
   dir(rv) # reformatted['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
      '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
      '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
      '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
      '__str__', '__subclasshook__', '__weakref__', 'a', 'args', 'b', 'cdf',
      'dist', 'entropy', 'expect', 'interval', 'isf', 'kwds', 'logcdf',
      'logpdf', 'logpmf', 'logsf', 'mean', 'median', 'moment', 'pdf', 'pmf',
      'ppf', 'random_state', 'rvs', 'sf', 'stats', 'std', 'var']
>>> dist_continu = [d
      for d in dir(stats) if
      ...isinstance(getattr(stats, d), stats.rv_continuous)
   ] >>>
   dist_discrete = [d
      for d in dir(stats) if
      ...isinstance(getattr(stats, d), stats.rv_discrete)
   ] >>>
   print('number of continuous distributions: %d' % len(dist_continu))
number of continuous distributions: 106 >>>
   print('number of discrete distributions:   %d' % len(dist_discrete))
number of discrete distributions: 19
>>> norm.cdf(0)
0.5

Suggestion : 3

I am writing a script to find the best-fitting distribution over a dataset using scipy.stats. I first have a list of distribution names, over which I iterate:, 1 week ago Dictionary with parameters names (str) as keys and distributions or lists of parameters to try. Distributions must provide a rvs method for sampling (such as those from scipy.stats.distributions). If a list is given, it is sampled uniformly. If a list of dicts is given, first a dict is sampled uniformly, and then a parameter is sampled using ... ,Now, after this loop, I select the minimum D-Value in order to get the best fitting distribution. Now, each distribution returns a specific set of parameters in ps, each with their names and so on (for instance, for 'alpha' it would be alpha, whereas for 'norm' they would be mean and std)., 5 days ago Dec 05, 2018  · Yes, I'm the one that upvoted the answer at that link 👍 There is only one issue with your solution, which is that discrete distributions do not have a scale parameter. So, one would need to add a check to see whether the distribution name is in scipy.stats._discrete_distns._distn_names or scipy.stats._continuous_distns._distn_names …


dists = ['alpha', 'anglit', 'arcsine', 'beta', 'betaprime', 'bradford', 'norm']
for d in dists: dist = getattr(scipy.stats, d) ps = dist.fit(selected_data) errors.loc[d, ['D-Value', 'P-Value']] = kstest(selected.tolist(), d, args = ps) errors.loc[d, 'Params'] = ps

>>> from scipy
import stats >>> dists = ['alpha', 'anglit', 'arcsine', 'beta', 'betaprime', 'bradford', 'norm'] >>>
   for d in dists: ...dist = getattr(scipy.stats, d)...dist.name, dist.shapes...('alpha', 'a')('anglit', None)('arcsine', None)('beta', 'a, b')('betaprime', 'a, b')('bradford', 'c')('norm', None)
distribution = "gamma" distr = getattr(stats, distribution) print(distr) # <scipy.stats._continuous_distns.gamma_gen object at 0x11688f518>str(distr).split(".")[3].split("_")[0] # 'gamma'
from scipy
import stats print(stats.gamma.name)
>>> from scipy
import stats >>> distribution = "gamma" >>> distr = getattr(stats, distribution) >>> distr.name 'gamma'

Suggestion : 4

SciPy does not have a simple Weibull distribution but instead has a generalization of the Weibull called the exponentiated Weibull. Set the exponential parameter to 1 and you get the ordinary Weibull distribution.,Probability distribution classes are located in scipy.stats.,Note that the argument of the PDF, in this example 5, comes before the distribution parameters. Note also that for discrete distributions, one would call pmf (probability mass function) rather than the pdf (probability density function).,Note that the parameters for the log-normal are the mean and standard deviation of the log of the distribution, not the mean and standard deviation of the distribution itself.

Distributions have a general form and a “frozen” form. The general form is stateless: you supply the distribution parameters as arguments to every call. The frozen form creates an object with the distribution parameters set. For example, you could evaluate the PDF of a normal(3, 4) distribution at the value 5 by

	stats.norm.pdf(5, 3, 4)

or by

	mydist = stats.norm(3, 4)
	mydist.pdf(5)

The PDF or PMF of a distribution is contained in the extradoc string. For example:

	>>> stats.poisson.extradoc
	Poisson distribution
	poisson.pmf(k, mu) = exp(-mu) * mu ** k / k!
	   for k >= 0