# finding variance from a piecewise function

How do you calculate the variance of a piecewise function? For example, what would be the variance of the probability density function $f_x(x)= \frac{3}{4}, 0\leq x\leq 1; \frac{1}{4}, 2\leq x \leq 3; 0$ otherwise?

-
The ordinary way, you integrate, find say $\int_0^3 x^2f(x)\,dx$, subtract the square of $\int_0^3 xf(x)\,dx$. Now when you are doing the integrations, you have to break them up, $0$ to $1$, and $2$ to $3$. –  André Nicolas Mar 13 '12 at 19:59
Let $U$ be a continuous uniform random variable on a unit interval. It's mean and variance are $$\mathbb{E}(U) = \frac{1}{2} \qquad \qquad \mathbb{Var}(U) = \mathbb{E}(U^2) - \left(\mathbb{E}(U)\right)^2 = \frac{1}{12}$$ Now, your continuous random variable $X$ is a component mixture of a uniform $U$ and shifted uniform $2+U$ with weights $w_1 = \frac{3}{4}$ and $w_2 = \frac{1}{4}$.
Then $$\mathbb{Var}(X) = \mathbb{E}(X^2) - \left(\mathbb{E}(X)\right)^2 = \left( w_1 \mathbb{E}(U^2) + w_2 \mathbb{E}((2+U)^2) \right) - \left( w_1 \mathbb{E}(U) + w_2 \mathbb{E}(2+U) \right)^2$$ Since $\mathbb{E}(U^2) = \mathbb{Var}(U) + \left(\mathbb{E}(U)\right)^2 = \frac{1}{3}$, $\mathbb{E}((2+U)^2) = 4 + 4 \mathbb{E}(U) + \mathbb{E}(U^2) = \frac{19}{3}$. Therefore $$\mathbb{Var}(X) = \left( \frac{3}{4} \cdot \frac{1}{3} + \frac{1}{4} \cdot \frac{19}{3} \right) - \left( \frac{3}{4} \cdot \frac{1}{2} + \frac{1}{4} \cdot \frac{5}{2} \right)^2 = \frac{11}{6} - 1^2 = \frac{5}{6}$$