Skip to content

Deriving the Normal Moment-Generating Function by Completing the Square

Original question

How can I calculate the moment generating function of a normally distributed random variable and use it to derive expectations?
For X∼N(μ,σ2)X\sim N(\mu,\sigma^2), the moment generating function is MX(t)=exp⁡(μt+σ2t2/2)M_X(t)=\exp(\mu t+\sigma^2t^2/2). Complete the square inside the normal-density integral to derive it. Differentiate at zero to obtain ordinary moments, or substitute a nonzero argument to evaluate an expectation such as E[eX]E[e^X].

Complete the square around a shifted normal mean

Let μ\mu be the mean, σ2\sigma^2 the variance, and tt the argument of the moment-generating function. The variable of integration xx represents a possible value of XX. For σ>0\sigma>0, write E[etX]E[e^{tX}] using the normal density.
MX(t)=1σ2π∫−∞∞exp⁡(tx−(x−μ)22σ2) dx.M_X(t)=\frac{1}{\sigma\sqrt{2\pi}}\int_{-\infty}^{\infty} \exp\left(tx-\frac{(x-\mu)^2}{2\sigma^2}\right)\,dx.

Only the linear coefficient in xx changes when you multiply the density by etxe^{tx}. The coefficient is now μ/σ2+t\mu/\sigma^2+t. A normal kernel with mean μ+σ2t\mu+\sigma^2t has that coefficient, and its quadratic coefficient remains −1/(2σ2)-1/(2\sigma^2). You can keep track of the constant by writing tx=t(x−μ)+tμtx=t(x-\mu)+t\mu.

tx−(x−μ)22σ2=−(x−μ)2−2σ2t(x−μ)2σ2+μt=−(x−μ−σ2t)22σ2+μt+σ2t22.\begin{aligned} tx-\frac{(x-\mu)^2}{2\sigma^2} &=-\frac{(x-\mu)^2-2\sigma^2t(x-\mu)}{2\sigma^2}+\mu t\\ &=-\frac{(x-\mu-\sigma^2t)^2}{2\sigma^2}+\mu t+\frac{\sigma^2t^2}{2}. \end{aligned}

The constant μt+σ2t2/2\mu t+\sigma^2t^2/2 is independent of xx, so its exponential comes outside the integral. What remains is the density of a normal with mean μ+σ2t\mu+\sigma^2t and variance σ2\sigma^2, integrated over the whole real line. Its integral is one. When σ=0\sigma=0, X=μX=\mu almost surely and the formula reduces directly to eμte^{\mu t}.

Use the formula to get moments

The normal tails allow differentiation under the expectation near zero. Each derivative brings down one factor of XX, so MX′(0)=E[X]M'_X(0)=E[X] and MX′′(0)=E[X2]M''_X(0)=E[X^2]. Differentiating the closed form gives

MX′(0)=μ,MX′′(0)=μ2+σ2,Var⁡(X)=MX′′(0)−MX′(0)2=σ2.M_X'(0)=\mu,\qquad M_X''(0)=\mu^2+\sigma^2,\qquad \operatorname{Var}(X)=M_X''(0)-M_X'(0)^2=\sigma^2.

For an exponential moment such as E[eX]E[e^X], evaluate MX(1)M_X(1) directly. It is a different calculation from taking a derivative at zero.

Three moments from one formula

Let X∼N(2,9)X\sim N(2,9). Its first moment is 2, its second raw moment is 22+9=132^2+9=13, and its exponential moment is E[eX]=MX(1)=e6.5E[e^X]=M_X(1)=e^{6.5}. Exponentiating the mean alone would give e2e^2, which misses the contribution from dispersion.

In a stochastic calculus exercise, suppose BT∼N(0,T)B_T\sim N(0,T) and define ZT=eσBT−σ2T/2Z_T=e^{\sigma B_T-\sigma^2T/2}. Using the normal moment generating function at argument σ\sigma gives
E[ZT]=e−σ2T/2E[eσBT]=e−σ2T/2eσ2T/2=1.E[Z_T]=e^{-\sigma^2T/2}E[e^{\sigma B_T}] =e^{-\sigma^2T/2}e^{\sigma^2T/2}=1.
The same idea applied to a Brownian increment gives the corresponding conditional expectation in a martingale proof.

Verify the exponential moment numerically

This example computes the three quantities for the normal variable in the exercise and checks the first derivative with a centred difference.

from math import exp mu, variance = 2.0, 9.0 def mgf(t): return exp(mu * t + variance * t**2 / 2) h = 1e-5 print("Mean by centred difference:", (mgf(h) - mgf(-h)) / (2 * h)) print("Second raw moment:", mu**2 + variance) print("Exponential moment:", mgf(1))
Numerical differentiation is a useful check on an analytic result. If you try the second derivative numerically, a very small step can cause cancellation because both nearby function values are close to one.

References and next steps