- Python Data Analysis Cookbook
- Ivan Idris
- 245字
- 2021-07-14 11:05:43
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:
- 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
- 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)
- 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
- The relevant SciPy documentation at http://docs.scipy.org/doc/scipy-dev/reference/generated/scipy.stats.gamma.html#scipy.stats.gamma (retrieved August 2015)
- The Wikipedia page for the gamma distribution at https://en.wikipedia.org/wiki/Gamma_distribution (retrieved August 2015)
- FuelPHP Application Development Blueprints
- Spring 5.0 By Example
- Learning SAP Analytics Cloud
- Neo4j Essentials
- The Computer Vision Workshop
- Mastering RStudio:Develop,Communicate,and Collaborate with R
- AppInventor實(shí)踐教程:Android智能應(yīng)用開發(fā)前傳
- Python語言實(shí)用教程
- Learning Material Design
- Python從入門到精通(第3版)
- Appcelerator Titanium:Patterns and Best Practices
- The Statistics and Calculus with Python Workshop
- Python程序設(shè)計(jì)教程
- Wearable:Tech Projects with the Raspberry Pi Zero
- C語言程序設(shè)計(jì)