官术网_书友最值得收藏!

Fitting aggregated data to the gamma distribution

The gamma distribution can be used to model the size of insurance claims, rainfall, and the distribution of inter-spike intervals in brains. The PDF for the gamma distribution is defined by shape k and scale θ as follows:

There is also a definition that uses an inverse scale parameter (used by SciPy). The mean and variance of the gamma distribution are described by (3.3) and (3.4). As you can see, we can estimate the shape parameter from the mean and variance using simple algebra.

How to do it...

Let's fit aggregates for the rain data for January to the gamma distribution:

  1. Start with the following imports:
    from scipy.stats.distributions import gamma
    import matplotlib.pyplot as plt
    import dautil as dl
    import pandas as pd
    from IPython.display import HTML
  2. Load the data and select aggregates for January:
    rain = dl.data.Weather.load()['RAIN'].resample('M').dropna()
    rain = dl.ts.groupby_month(rain)
    rain = rain.get_group(1)
  3. Derive a value for k from the mean and variance of the distribution, and use it to fit the data:
    dist = dl.stats.Distribution(rain, gamma)
    
    a = (dist.mean() ** 2)/dist.var()
    shape, loc, scale = dist.fit(a)

The rest of the code is similar to the code in Fitting data to the exponential distribution. Refer to the following screenshot for the end result (the code is in the fitting_gamma.ipynb file in this book's code bundle):

See also

主站蜘蛛池模板: 高邑县| 宜君县| 女性| 西林县| 仁怀市| 龙井市| 梅州市| 合阳县| 潢川县| 康平县| 祁阳县| 自贡市| 绥滨县| 郓城县| 手机| 兴文县| 南宫市| 霍林郭勒市| 温宿县| 泸溪县| 綦江县| 奉贤区| 札达县| 特克斯县| 牟定县| 富顺县| 浮山县| 巨鹿县| 鱼台县| 怀远县| 江安县| 原阳县| 扬州市| 两当县| 东城区| 木兰县| 宜丰县| 莫力| 黄浦区| 肥东县| 武威市|