d.b
d.b

Reputation: 32558

fit theoretical distribution and draw random values

In order to fit given data to theoretical distribution and then draw random values from that distribution, does this scheme work with all distributions in scipy.stats?

from scipy.stats import beta, expon, gamma, genpareto, genextreme, lognorm, kappa3, pearson3, weibull_min

data = [529, 7065, 2739, 1838, 817, 1376, 3791, 5070,
        736, 805, 577, 2963, 7017, 3026, 2542, 2160,
        221, 3340, 582, 1080, 1040, 1310, 5500, 4800,
        485, 7110, 4150, 2700, 4610, 1270, 10476, 1975,
        731, 150, 1163, 985, 5476, 5762, 1750, 609, 1009,
        14704, 678, 3837, 1069, 948, 460, 1819, 5958,
        1356, 2025, 1136, 4500, 882, 8230, 3521, 1561,
        695, 3380, 1210, 4311]

d = {"beta": beta,
     "expon": expon,
     "gamma": gamma,
     "genpareto": genpareto,
     "genextreme": genextreme,
     "lognorm": lognorm,
     "kappa3": kappa3,
     "pearson3": pearson3,
     "weibull_min": weibull_min} # there could be more later

ans = {}
for nm, f in d.items():
    params = f.fit(data)
    ans[nm] = f.rvs(*params, size=100)

I am mainly concerned about the ans[nm] = f.rvs(*params, size=100) line. Does *params always insert appropriate arguments into the rvs method? Or is it necessary to tinker with input based on distribution? For example, in case of pearson3, is it necessary to separately calculate skew?

Upvotes: 0

Views: 255

Answers (3)

shomeax
shomeax

Reputation: 865

unpacking tuples work even for empty ones flawlessly.

simple test:

def foo():
   print('foo')

empty=()   # empty tuple
>>> foo(*empty)
foo
def bar(*args):
    print(args)

something = (1, 2, 3,)
>>> bar(*something)
(1,2,3)
>>> bar(*empty)
()
def baz(*args, **kwargs):
    print(args)
    print(kwargs)
>>> baz(*something, key='value')
(1,2,3)
{'key': 'value'}

So, all even norm distribution which does not have any specific shape parameters would work with the code you provide rvs(*fit(data))

Upvotes: 1

Ekansh
Ekansh

Reputation: 21

Yes, as ev-br mentioned *params will insert the appropriate arguments into the rvs method.

From scipy's documentation for scipy.stats.rv_continuous.fit: Returns a tuple of estimates for any shape parameters (if applicable), followed by those for location and scale. Shape parameters, location, and scale can be unpacked from the params in your code

These estimates for shape parameters are then used by scipy.stats.rv_continuous.rvs

Upvotes: 1

ev-br
ev-br

Reputation: 26090

f.rvs(*params, size=100) line. Does *params always insert appropriate arguments into the rvs method?

Yes.

f.fit returns a tuple of shapes, loc, scale, which can be unpacked into all other methods of f.

Upvotes: 3

Related Questions