A small note on the statistical method of moments for fitting a probability model to data

by Nasser Abbasi, Nov 16, 2007
Mathematics 502 probability and statistics, CSUF, Fall 2007

The problem to solve : Given some data, we seek to fit a probability law to the data. In other words, we want to determine the best probability distribution function by which the given data could have been generated according to.

We call the given data the population data. The idea of this method is as follows: Assume that the data was generated according to some distribution, say Normal or Gamma or Poisson, etc... For each one of these Choice we need to determine the relevant distribution parameters to be able to fully specify the pdf.

For example, if we want to fit the population data to the normal distribution, then we need to determine the mean and variance of the data "method_of_moments_1.gif" since the normal pdf is fully specified by these 2 parameters "method_of_moments_2.gif"

If we want to fit the population to the Gamma distribution, then we need to determine the parameters {α,λ} since the Gamma distribution is is fully specified by these 2 parameters "method_of_moments_3.gif".

If we have to determine 2 parameters (as in the above 2 cases) then we need 2 equations. But if we wanted to fit the data to Poisson distribution, then we only need one equation since the Poisson pdf is defined in terms on one parameter λ as in f(x)="method_of_moments_4.gif"

Let us assume there are n parameters to be determined (i.e. we want to fit the data to some distribution which is defined using n parameters). We call these "method_of_moments_5.gif", so for the case of fitting to a normal distribution n=2, "method_of_moments_6.gif" and  "method_of_moments_7.gif".

We start by writing down the n probability moments, called "method_of_moments_8.gif" for the selected pdf we want to fit the data to. These are known analytical expressions for the selected pdf and can be looked up or derived from the assumed pdf.

The "method_of_moments_9.gif" moment is defined as "method_of_moments_10.gif". This will give us n equations expressed as functions of the "method_of_moments_11.gif",

Next we calculate the moments from the data itself and set these to be equal to the moments for the pdf and solve for the "method_of_moments_12.gif".

An example will help. Suppose to want to fit the data to a normal distribution, then we know that the first moment is given by "method_of_moments_13.gif" and that the second moment is given by "method_of_moments_14.gif".
So now we have 2 equations in 2 unknowns

"method_of_moments_15.gif"

It is easier to re - write the above as follows

"method_of_moments_16.gif"

Now we determine an estimate for "method_of_moments_17.gif" and "method_of_moments_18.gif" from the data, or the sample, and substitute in the above and solve for μ and "method_of_moments_19.gif"

"method_of_moments_20.gif"

"method_of_moments_21.gif"

Hence the solution from above gives an estimate of the pdf parameters from the data itself. We can now plot this selected pdf using the calculated parameters on top of the histogram of the data and see how good the fit is. If the fit is not good, we can try to fit the data to a different distribution.

This is another example, suppose we have data we want to fit to a Gamma distribution, hence we know that for a Gamma distribution "method_of_moments_22.gif" and that "method_of_moments_23.gif" hence we have

"method_of_moments_24.gif"

It is easier to re - write the above as follows

"method_of_moments_25.gif"

Now using (5) we solve for α and λ using the calculated values for "method_of_moments_26.gif" and "method_of_moments_27.gif" from the data as shown in (3).

Numerical example

In these examples I will first generate random data (the population) from known distributions then take a small random sample from the data (with replacement), then use the method of moments above to estimate the parameters of the population (which is of course known in this case) and fit the found parameters on the population histogram to see how good the fit it.

Example 1, fitting to normal

Using real data

This data is the annual precipitation in Seattle (I think) for the years 1863 to 1999, it was downloaded from http://www.seattlecentral.edu/qelp/sets/049/049.html.
First load the data, and do histogram on it, then try to fit a normal distribution on it and see how good the fit is.

Load the data

In[3]:=

"method_of_moments_28.gif"

In[4]:=

"method_of_moments_29.gif"

Out[4]=

"method_of_moments_30.gif"

Display few lines of data

In[5]:=

"method_of_moments_31.gif"

Out[5]//TableForm=

year annual rain
in inches
1863 46.31
1864 38.42
1865 49.65
1866 41.51
1867 49.94
1868 48.43
1869 45.41
1870 48.62
1871 48.84
1872 43.9

Decide on numbers of bins, and make histogram

In[6]:=

"method_of_moments_32.gif"

Calculate first and second moments of data

In[9]:=

"method_of_moments_33.gif"

Estimate data parameters. Solve the method of moments equations (this solves equations (2) above)

In[12]:=

"method_of_moments_34.gif"

Out[14]=

"method_of_moments_35.gif"

Plot the fitted PDF using the above estimated parameters

In[17]:=

"method_of_moments_36.gif"

Out[18]=

Graphics:Annual rain over Seattle in inches. Fitting Normal distribution on data using method of moments to estimate PDF parameters Estimated μ=41.4796    Estimated σ=6.52451

Using Random data

Make some random data from Normal and plot its histogram (see appendix for function to make histogram)

In[19]:=

"method_of_moments_38.gif"

Take a small sample with replacement and obtain the first and second moments from the sample

In[24]:=

"method_of_moments_39.gif"

Solve the method of moments equations (this solves equations (2) above)

In[28]:=

"method_of_moments_40.gif"

Out[30]=

"method_of_moments_41.gif"

Plot the fitted PDF using the above estimated parameters

In[35]:=

"method_of_moments_42.gif"

Out[36]=

Graphics:None

Example 2 fitting to Gamma

Lets try to fit a Gamma on the data to see what we get
Make some random data from Normal and plot its histogram (see appendix for function to make histogram)

In[37]:=

"method_of_moments_44.gif"

Take a small sample with replacement and obtain the first and second moments from the sample

In[42]:=

"method_of_moments_45.gif"

Solve the method of moments equations (this solves equations (5) above)

In[46]:=

"method_of_moments_46.gif"

Out[48]=

"method_of_moments_47.gif"

Plot the Gamma PDF using the above estimated parameters on top of the data

In[58]:=

"method_of_moments_48.gif"

Out[59]=

Graphics:None

Appendix

A function to plot histogram

In[1]:=

"method_of_moments_50.gif"


Created by Wolfram Mathematica 6.0 for Students - Personal Use Only  (17 November 2007) Valid XHTML 1.1!