# Variance of a random variable with a uniform distribution evaluated starting from the moment generating function

expected valuemoment-generating-functionsuniform distributionvariance

I'd like to get the variance of a random variable $$X$$ with an uniform distribution over the interval $$(a, b)$$ starting from the moment generating function $$MGF_X$$. The variance is:

$$Var(X) = \mathbb{E}[X^2] – (\mathbb{E}[X])^2$$

The $$MGF_X(t)$$ is:

$$MGF_X(t) = \frac{e^{t \, b} – e^{t \, a}}{t \, (b – a)}$$

The first derivative is (it's reported also on mathworld):

$$MGF'_X(t) = \frac{b \, e^{t \, b} – a \, e^{t \, a}} {t \, (b – a)} – \frac{e^{t \, b} – e^{t \, a}} {t^2 \, (b – a)}$$

The expected value of $$X$$ is (I got it starting from the $$MGF_X(t)$$; I evaluated the limit for $$t \rightarrow 0$$ of the first derivative of the $$MGF_X(t)$$):

$$\lim_{t \to 0} MGF'_X(t) = \mathbb{E}[X] = \frac{a + b}{2}$$

Now I need to apply the following one in order to get $$\mathbb{E}[X^2]$$:

$$\left( \frac{d^2}{dt^2} MGF_X(t) \right) \Bigr|_{\substack{t = 0}} = \mathbb{E} [X^2]$$

I got the following result:

$$MGF''_X(t) = \frac{b^2 \, e^{t \, b} – a^2 \, e^{t \, a}}{t \, (b -a)}$$

In $$t=0$$ the result is infinity; this is strange, what mistake did I make?

I tried also the evaluated the limit for $$t \rightarrow 0$$ of the second derivative and I got:

$$\lim_{t \to 0} MGF''_X(t) = \frac{3 \, b^3 – 3 \, a^3 + a^2 \, b – a \, b^2}{4 \, (b-a)}$$

The second derivative of the moment generating function is:

\begin{align} \frac{d^2}{dt^2} MGF_X(t) &= \frac{b^2 \, e^{b \, t} - a^2 \, e^{a \, t}} {t \, (b - a)} - \frac{b \, e^{b \, t} - a \, e^{a \, t}} {t^2 \, (b - a)} - \left( \frac{b \, e^{b \, t} - a \, e^{a \, t}} {t^2 \, (b - a)} - \frac{2 \, \left( e^{b \, t} - e^{a \, t} \right)}{t^3 \, (b - a)} \right) = \\ &= \frac{b^2 \, e^{b \, t} - a^2 \, e^{a \, t}} {t \, (b - a)} - \frac{2 \, \left( b \, e^{b \, t} - a \, e^{a \, t} \right)} {t^2 \, (b - a)} + \frac{2 \, \left( e^{b \, t} - e^{a \, t} \right)}{t^3 \, (b - a)} \\ &= \frac{ t^2 \, \left( b^2 \, e^{b \, t} - a^2 \, e^{a \, t} \right) - 2 \, t \, \left( b \, e^{b \, t} - a \, e^{a \, t} \right) + 2 \, \left( e^{b \, t} - e^{a \, t} \right) } { t^3 \, (b - a) } \\ &= \frac{ t^2 \, b^2 \, e^{b \, t} - t^2 \, a^2 \, e^{a \, t} - 2 \, t \, b \, e^{b \, t} - 2 \, t \, a \, e^{a \, t} + 2 \, e^{b \, t} - 2 \, e^{a \, t} } { t^3 \, (b - a) } \\ &= \frac{ e^{b \, t} \, \left( t^2 \, b^2 - 2 \, t \, b + 2 \right) - e^{a \, t} \, \left( t^2 \, a^2 - 2 \, t \, a + 2 \right) } { t^3 \, (b - a) } \end{align}

The limit for $$t$$ tending to zero is (I will apply the Hopital theorem):

\begin{align} \mathbb{E}[X^2] &= \lim_{t \to 0} MGF''_X(t) = \\ &= \lim_{t \to 0} \left( \frac{\frac{d}{dt} e^{b \, t} \, \left( t^2 \, b^2 - 2 \, t \, b + 2 \right) - e^{a \, t} \, \left( t^2 \, a^2 - 2 \, t \, a + 2 \right)} {\frac{d}{dt} t^3 \, (b - a)} \right) = \\ &= \lim_{t \to 0} \left( \frac{ b \, e^{b \, t} \, \left( t^2 \, b^2 - 2 \, t \, b + 2 \right) + e^{b \, t} \, \left( 2 \, t \, b^2 - 2 \, b \right) - a \, e^{a \, t} \, \left( t^2 \, a^2 - 2 \, t \, a + 2 \right) - e^{a \, t} \, \left( 2 \, t \, a^2 - 2 \, a \right) } { 3 \, t^2 \, (b - a) } \right) = \\ &= \lim_{t \to 0} \left( \frac{ e^{b \, t} \, \left( t^2 \, b^3 - 2 \, t \, b^2 + 2 \, b + 2 \, t \, b^2 - 2 \, b \right) - e^{a \, t} \, \left( t^2 \, a^3 - 2 \, t \, a^2 + 2 \, a + 2 \, t \, a^2 - 2 \, a \right) } { 3 \, t^2 \, (b - a) } \right) = \\ &= \lim_{t \to 0} \left( \frac{ e^{b \, t} \, t^2 \, b^3 - e^{a \, t} \, t^2 \, a^3 } { 3 \, t^2 \, (b - a) } \right) = \\ &= \lim_{t \to 0} \left( \frac{ e^{b \, t} \, b^3 - e^{a \, t} \, a^3 } { 3 \, (b - a) } \right) = \\ &= \frac{ b^3 - a^3 } { 3 \, (b - a) } = \\ &= \frac{ (b - a) \, (a^2 + a \, b + b^2) } { 3 \, (b - a) } = \\ &= \frac{ a^2 + a \, b + b^2 } { 3 } \end{align}

The variance is:

\begin{align} Var(X) &= \mathbb{E}[X^2] - \left( \mathbb{E}[X] \right)^2 = \\ &= \frac{a^2 + a \, b + b^2}{3} - \frac{(b - a)^2}{4} = \\ &= \frac{4 \, a^2 + 4 \, a \, b + 4 \, b^2 - 3 \, a^2 - 6 \, a \, b - 3 \, b^2}{12} = \\ &= \frac{a^2 - 2 \, a \, b + b^2}{12} = \\ &= \frac{(b - a)^2}{12} \end{align}