9.3. Moments#

Moments are numerical values that are commonly used to characterize the distribution of a random variable. They are expected values of powers of the random variable. The most common moments are the mean, which is the first moment, and the variance, which is the second central moment.

In this section of Foundations of Data Science with Python, you will learn:

  • the definition of moments and central moments,

  • how to interpret moments as integrals that applying weighting functions to different parts of the probability density,

  • the definition of variance and standard deviation for random variables,

  • why variance measures the “spread” of a distribution,

  • how to use SymPy to compute means and variances of continuous random variables, and

  • important properties of variance that are used in later sections of the book.

9.3.1. Terminology and Properties Review#

Use the flashcards below to help you review the terminology introduced in this chapter. \(~~~~ ~~~~ ~~~~\)

9.3.2. Code and Proof from Section 9.3#

Code for Fig 9.6

../_images/2d1d62c57f712cb725b06f9aab48a7602003747999fb6e1d1788fbcfdb018474.png ../_images/f5099c9b0f2df6d8e3eec2a8e863428ca1f7ac480b6e15b4aad52aeae79b6691.png

Proof that the mean of a Normal random variable is the parameter \(\mu\)

\[\begin{align*} E[X] = \int_{-\infty}^{\infty} x \cdot \frac{1}{\sigma \sqrt{2 \pi}} \exp \left ( -\frac 1 2 \left[ \frac{x-\mu}{\sigma} \right]^2 \right). \end{align*}\]

Now we add and subtract a term,

\[\begin{align*} E[X] = &\int_{-\infty}^{\infty} (x- \mu) \cdot \frac{1}{\sigma \sqrt{2 \pi}} \exp \left ( -\frac 1 2 \left[ \frac{x-\mu}{\sigma} \right]^2 \right)\ &+ \int_{-\infty}^{\infty} \mu \cdot \frac{1}{\sigma \sqrt{2 \pi}} \exp \left ( -\frac 1 2 \left[ \frac{x-\mu}{\sigma} \right]^2 \right). \end{align*}\]

The first integral is 0 because of odd symmetry around \(\mu\). This may be confusing to some who are used to only odd symmetry around 0, so let’s do the change of variable \(u = x-\mu\). With this change of variable, the region of integration is still \((-\infty, \infty)\):

\[\begin{align*} E[X] =& \int_{-\infty}^{\infty} u \cdot \frac{1}{\sigma \sqrt{2 \pi}} \exp \left ( -\frac 1 2 \left[ \frac{u}{\sigma} \right]^2 \right)\\ &+ \mu \int_{-\infty}^{\infty} \cdot \frac{1}{\sigma \sqrt{2 \pi}} \exp \left ( -\frac 1 2 \left[ \frac{x-\mu}{\sigma} \right]^2 \right). \end{align*}\]

In addition to the change of variable, I pulled the constant term \(\mu\) outside the second integral. Then the first integral is 0 because of odd symmetry around 0. The second integral is 1 because it is the integral of the Normal density function. So, \(E[X] = \mu\).

Code for Figure 9.7

../_images/02cbd56dd2ff32d6a313cd26d4e9c3005de0b04b301094f23a125256c44c85ad.png ../_images/a1092a9f132eb446904ec1a9d628840ba9e55b457099dbb0909b6f5a7a933718.png

Code for Fig. 9.8

../_images/5494ebe629b0d85dcb927e01b7b7cf6e852f86333fd5b12383aa464045c1b214.png ../_images/e5917b6b9b2c100117bf4ddda75413543e28a35ed98a4958de7b4945ff656cf5.png

Code fog Fig. 9.9

../_images/325a2280982f6c8017f86b999f8b28ee2eca885a7b2c4a9cb1887b9361a3b212.png ../_images/107995c123a87e6d8ef7917c7400c38e3cab07fe3a35e3e656726007b0163cca.png

Code for Fig. 9.10

Y0 = stats.norm(loc = -4, scale =3 )
Y1 = stats.norm(loc = 0, scale = 1)
Y2 = stats.norm(loc = 2, scale = 1/2)

y=np.linspace(-12,10, 201)

plt.plot(y, Y0.pdf(y), 
         label = 'μ0=−4, σ=3' )
plt.plot(y, Y1.pdf(y), 
         label = 'μ1=0, σ=1' )
plt.plot(y, Y2.pdf(y), 
         label = 'μ2=2, σ=1/2' )

plt.title('Normal densities various means and standard deviations') 
plt.xlabel('y')
plt.ylabel('fY(y)')
plt.legend();
../_images/189dbbde0cf300279c7deda66d818a36f600c9c7ab61bf8db0d86832514215ef.png