Method_of_Moments

Previous exercise

  • In Exercise 5.9 you comp ared a sampling distribution to a bootstrap distribution.
  • In both, the underlying population was assumed to have a gamma distribution with shape \( r=5 \) and rate \( \lambda=\frac{1}{4} \).

Huh?

  • Plot this distribution with ggdistribution.
  • Adjust the values on the horizontal axis to make sure you see enough of the distribution.
  • Describe the distribution.
library(ggfortify)
ggdistribution(dgamma, seq(0, 50, 0.1), rate=1/4, shape=5)

plot of chunk unnamed-chunk-1

  • This distribution is skewed right.
  • Roughly, most of the values lie between 8 and 30.

Gamma and Exponential distributions

  • We discussed the exponential distribution:

    • it is a “waiting time” distribution.
    • How long until the next volcano eruption?, etc.
  • The gamma distribution is similar - but more general.

    • The exponential distribution is a gamma distribution with \( r=1 \).

Service times

  • Wait times at a snack bar are in the data set Service.

  • Load this data set and plot a histogram.

    • Use prob=T in your histogram.
library(resampledata)
hist(Service$Times, prob=T)

plot of chunk unnamed-chunk-2

with a gamma distribution superimposed

Plot a gamma distribution on the same plot with:

hist(Service$Times, prob=T)
curve(dgamma(x, shape=10, rate=10), add=TRUE)

plot of chunk unnamed-chunk-3

What should the parameters be?

Experiment with the parameters a little and see if you can get the gamma distribution to match the distribution of the sample data (histogram).

What are the best parameters to use?

We'll use a method called the Method of Moments to determine the parameters to use.

Read the first part of section 6.2.

  • What values of \( k \) will we use?
  • What is \( f(x) \)?
  • What are the \( X_i \) s?
  • What is \( n \)?

The calculus part first

  • \( f \) is the pdf. You'll find this function in the Appendix C
  • What does the Appendix indicate is the domain of the gamma function pdf?
  • How will this affect the integrals shown on page 162?

Evaluate the integrals!

A few hints:

  • Look up \( \Gamma(r) \). It is in Appendix B.
  • The integrals are with respect to \( x \). Pull any constants outside the integral.
  • Start with a substitution: \( w=\lambda x \).
  • Next: integration by parts
  • It would be really nice if the factor of \( \Gamma(r) \) in the denominator canceled! Watch out for a factor of \( \Gamma(r) \) in the numerator.
  • When you get to the second integral, pay attention. At a certain point you can use work you already did for the first integral.

\[ \begin{align*} \Gamma(r)&=\int_0^\infty x^{r-1}e^{- x} dx \\ f(x,r,\lambda)&=\frac{1}{\Gamma(r)}\lambda^rx^{r-1}e^{-\lambda x} \end{align*} \]

\[ \begin{align*} E[X]&=\int_0^\infty x f(x,r,\lambda) dx\\ &= \int_0^\infty x \frac{1}{\Gamma(r)}\lambda^rx^{r-1}e^{-\lambda x} dx\\ &=\frac{\lambda^r}{\Gamma(r)}\int_0^\infty x^r e^{-\lambda x} dx && w=\lambda x, dw=\lambda dx\\ &=\frac{\lambda^{r-1}}{\Gamma(r)}\int_0^\infty \left(\frac{w}{\lambda}\right)^r e^{-w} dw \\ &=\frac{1}{\lambda \Gamma(r)} \int_0^\infty w^r e^{-w} dw && u=w^r, dv=e^{-w}dw\\ & && du = r w^{r-1} dw, v= -e^{-w}\\ \end{align*} \]

\[ \begin{align*} E[X]&=\\ &=\frac{1}{\lambda \Gamma(r)}\left[-w^re^{-w} \biggr|_0^\infty+r\int_0^\infty w^{r-1}e^{-w} dw \right]\\ &=\frac{1}{\lambda \Gamma(r)}\left[0+r\Gamma(r) \right]\\ &=\frac{r}{\lambda} \end{align*} \]

Status

  • You should have the left hand side for two of the equations on page 162.
  • They should be expressions in \( r \) and \( \lambda \).

Right hand side

You could compute these with a loop, but R's vector structure provides an elegant way to compute the right hand side.

Here's an example. Say my \( X_i \) s are:

Xs <- c(12, 14, 17, 2, 3, 19, 7, 12)

(I just made them up.)

To compute \[ \sum_{i=1}^n X_i^3 \] I will cube each of the \( X_i \) s and then add them together.

Xs^3
[1] 1728 2744 4913    8   27 6859  343 1728
sum(Xs^3)
[1] 18350

All that's left to do is divide by \( n \).