0

Is there any way to get the parameters of a distribution? I know almost every distribution has "loc" and "scale" but theres differences between them, for example alpha has "a", beta has "a" ,"b".

What i want to do is programatically print(after fiting a distribution) key value pairs of parameter,value.

But i dont want to write a print routine for every possible distribution.

Zub
  • 808
  • 3
  • 12
  • 23
Luis Leal
  • 3,388
  • 5
  • 26
  • 49

2 Answers2

2

inspecting the _pdf method appears to work:

import inspect

# keys
[p for p in inspect.signature(stats.beta._pdf).parameters if not p=='x']
# ['a', 'b']

# keys and values
dist = stats.alpha(a=1)
inspect.signature(stats.alpha._pdf).bind('x', *dist.args, **dist.kwds).arguments
# OrderedDict([('x', 'x'), ('a', 1)])
# 'x' probably doesn't count as a parameter
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • Thanks, the problem with this option is that in order to use : inspect.signature(stats.alpha._pdf).bind('x', *dist.args, **dist.kwds).arguments You first need to create the "dist" variable in the previous line , and in order to create it, you need to specify the parameters so we get recursive: I need to specify the parameters in order to run the line of code to get the parameters – Luis Leal Nov 24 '17 at 05:28
  • @LuisLeal This is just for the sake of example. For `dist` you would put the result of your fit procedure. (I was assuming this produces a `frozen_rv` or whatever it's called, no?) – Paul Panzer Nov 24 '17 at 05:35
  • Nope, that would be helpful, but fit just returns a tuple with the fitted numeric values of the parameters, what im trying to to do is associate every value returned by fit , to the parameter name. – Luis Leal Nov 24 '17 at 05:40
  • @LuisLeal But in that case can't you just use the first line? The one below `# keys`? It returns the parameter names in the right order. – Paul Panzer Nov 24 '17 at 05:43
  • I think it could be, but not completly sure yet, because for example for beta, fit returns 4 values , and your sample returns 2, im almost sure the extra 2 values are loc, and shape, but how do i know in which order they are returned? By trial and error i will try to figure out the order of fit outputs ,and how to match it with your example output. What makes me doubt its this line in the documentation: For most random variables, shape statistics will be returned, but there are exceptions (e.g. norm). – Luis Leal Nov 24 '17 at 05:49
  • @LuisLeal From the example in the [docs](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_continuous.fit.html) `loc` and `scale` appear to go last. Exceptions like `norm` you'll have to treat separately. At least they appear to be easy to detect. – Paul Panzer Nov 24 '17 at 06:42
  • Yes , i think it is like that, i tried 10 different distributions and they seem to work like that, so what i did was: parameter_names = [p for p in inspect.signature(distribution._pdf).parameters if not p=='x'] + ["loc","scale"] parameters = distribution.fit(pd_series) distribution_parameters_dictionary = dict(zip(parameter_names,parameters)) Your first example helped a lot. – Luis Leal Nov 24 '17 at 06:52
0

In the end what i did was:

parameter_names = [p for p in inspect.signature(distribution._pdf).parameters if not p=='x'] + ["loc","scale"]
parameters = distribution.fit(pd_series)
distribution_parameters_dictionary =dict(zip(parameter_names,parameters))

Where pd_series is a pandas series of the data being fitted.

Luis Leal
  • 3,388
  • 5
  • 26
  • 49