Tutorials › VEE Mathematical Statistics › The Shortcut Formula for Covariance

VEE Mathematical Statistics · Covariance and Correlation · Tutorial 307 of 880

The Shortcut Formula for Covariance

Learn when and how to calculate covariance using the expected product minus the product of the means.

Intermediate 10 min read

What You'll Learn

  • Derive the shortcut formula from the definition of covariance.
  • State the moment conditions needed for the formula to be valid.
  • Calculate covariance from raw moments without expanding centered products.
  • Use conditional expectations to find a product moment.
  • Recognize when a product of means is not the expected product.
  • Check covariance calculations by comparing both equivalent forms.

Why Use a Shortcut Formula?

Covariance measures how two random variables vary together. Its definition uses deviations from the variables’ means, which is useful for interpretation but can make direct calculations cumbersome. A simpler calculation is often possible when the raw moments \(E[XY]\), \(E[X]\), and \(E[Y]\) are easy to obtain.

The shortcut does not change what covariance means. It is an algebraically equivalent way to calculate the same quantity. The central task is therefore to identify the model correctly and calculate the expected product \(E[XY]\) without confusing it with the product of the means.

Definition: For random variables \(X\) and \(Y\) with finite second moments, their covariance is $$ \operatorname{Cov}(X,Y)=E[(X-E[X])(Y-E[Y])]. $$ The shortcut formula is $$ \operatorname{Cov}(X,Y)=E[XY]-E[X]E[Y]. $$ Here \(E[XY]\) is the expected value of the product \(XY\), while \(E[X]E[Y]\) is the product of two separate expectations.

Deriving the Shortcut

The derivation is an expansion of the centered product, followed by linearity of expectation. The finite-second-moment assumption ensures that the expectations involved are well-defined: in particular, \(E[|XY|]\) is finite by the Cauchy–Schwarz inequality.

1
Expand the product.
Write \(m_X=E[X]\) and \(m_Y=E[Y]\). Then \((X-m_X)(Y-m_Y)=XY-m_XY-m_YX+m_Xm_Y\).
2
Take expectations term by term.
Linearity gives \(E[XY]-m_XE[Y]-m_YE[X]+m_Xm_Y\).
3
Substitute the means.
Since \(E[X]=m_X\) and \(E[Y]=m_Y\), the expression becomes \(E[XY]-m_Xm_Y-m_Ym_X+m_Xm_Y=E[XY]-m_Xm_Y\).

Thus the two middle terms and the final constant combine to leave \(E[XY]-E[X]E[Y]\). This argument requires no special distribution, but it does require the relevant expectations to exist. If a model has no finite second moments, the centered-product definition or the shortcut may fail to be a finite covariance.

Using Raw Moments Carefully

A reliable calculation separates the work into three quantities: the mean of \(X\), the mean of \(Y\), and the expected product \(XY\). The first two may come from marginal information. The third depends on how \(X\) and \(Y\) occur together, so it must be calculated from the joint model or a valid conditioning argument.

In particular, knowing both means does not by itself determine \(E[XY]\). Substituting \(E[X]E[Y]\) for \(E[XY]\) without an additional justification silently changes the model and often makes the covariance incorrectly equal to zero.

Worked Example: A Continuous Variable and Its Square

Let \(X\sim \operatorname{Uniform}(0,1)\), and define \(Y=X^2\). Find \(\operatorname{Cov}(X,Y)\) using the shortcut formula. The uniform density is \(f_X(x)=1\) for \(0<x<1\), and zero otherwise.

Method. Calculate the two means and the expected product as moments of \(X\), then substitute into \(E[XY]-E[X]E[Y]\). Since \(Y=X^2\), the product \(XY\) is \(X^3\).

First, $$ E[X]=\int_0^1 x\,dx=\left[\frac{x^2}{2}\right]_0^1=\frac12. $$

Next, $$ E[Y]=E[X^2]=\int_0^1 x^2\,dx=\left[\frac{x^3}{3}\right]_0^1=\frac13. $$

For the product moment, $$ E[XY]=E[X^3]=\int_0^1 x^3\,dx=\left[\frac{x^4}{4}\right]_0^1=\frac14. $$

Therefore, $$ \operatorname{Cov}(X,Y)=\frac14-\left(\frac12\right)\left(\frac13\right) =\frac14-\frac16=\frac{3}{12}-\frac{2}{12}=\frac1{12}. $$

Check. Directly, \(E[(X-\tfrac12)(X^2-\tfrac13)]\) is the integral of \(x^3-\tfrac12x^2-\tfrac13x+\tfrac16\) from 0 to 1. Its value is \(\tfrac14-\tfrac16-\tfrac16+\tfrac16=\tfrac1{12}\), agreeing with the shortcut. The common mistake to avoid is treating \(E[XY]\) as \(E[X]E[Y]\); here those values are \(\tfrac14\) and \(\tfrac16\), respectively.

Using Conditional Expectations to Find the Product Moment

Sometimes the model specifies the conditional distribution of one variable given the other. Then the law of iterated expectation gives a convenient route to the product moment: $$ E[XY]=E\big[E[XY\mid X]\big]=E\big[XE[Y\mid X]\big]. $$ The second equality holds because, once \(X\) is given, it is fixed inside the conditional expectation. This approach can avoid listing every possible pair of values.

Worked Example: A Conditional Follow-Up Event

In a hypothetical monitoring program, let \(X\) be the number of alerts recorded in a review period. Assume \(P(X=0)=P(X=2)=\tfrac12\). Let \(Y\) equal 1 if a follow-up visit occurs and 0 otherwise. Conditional on \(X=0\), let \(Y\sim\operatorname{Bernoulli}(0.2)\); conditional on \(X=2\), let \(Y\sim\operatorname{Bernoulli}(0.8)\). Find \(\operatorname{Cov}(X,Y)\).

Method. Find \(E[X]\) from the distribution of alerts, then use conditional probabilities to find \(E[Y]\) and \(E[XY]\). Do not replace the expected product with a product of means.

The mean alert count is $$ E[X]=0\left(\frac12\right)+2\left(\frac12\right)=1. $$

By conditioning on \(X\), the probability of a follow-up visit is $$ E[Y]=P(Y=1)=\frac12(0.2)+\frac12(0.8)=0.1+0.4=0.5. $$

For the product moment, use \(E[XY]=E[XE[Y\mid X]]\). The conditional means of \(Y\) are 0.2 when \(X=0\) and 0.8 when \(X=2\), so $$ E[XY]=\frac12(0)(0.2)+\frac12(2)(0.8)=0+0.8=0.8. $$

It follows that $$ \operatorname{Cov}(X,Y)=0.8-(1)(0.5)=0.3. $$

Check. The expected centered product is also 0.3: conditional on \(X=0\), its expectation is \((-1)(0.2-0.5)=0.3\), weighted by \(\tfrac12\), giving 0.15; conditional on \(X=2\), it is \((1)(0.8-0.5)=0.3\), also weighted by \(\tfrac12\), giving 0.15. The total is 0.30. A common error is to calculate \(E[XY]\) as \(E[X]E[Y]=0.5\); the conditional model shows the actual expected product is 0.8.

When One Variable Is Defined by an Event

The shortcut also works when a variable records whether a condition is met. For an event \(A\), its indicator \(I_A\) is 1 when \(A\) occurs and 0 otherwise. Thus \(X I_A\) is zero when the condition fails and equals \(X\) when it holds. This can make \(E[XY]\) a sum over only the outcomes where the indicator is 1.

Worked Example: A Binomial Count Above a Threshold

Suppose \(X\sim\operatorname{Binomial}(3,0.5)\), so \(X\) counts successes in three independent trials, each with success probability 0.5. Define \(Y=I_{\{X\ge 2\}}\), which equals 1 when there are at least two successes and 0 otherwise. Find \(\operatorname{Cov}(X,Y)\).

Method. Use the binomial probabilities to calculate \(E[X]\), \(E[Y]\), and \(E[XY]\). Since \(Y=0\) for \(X=0,1\), only the outcomes \(X=2,3\) contribute to \(E[XY]\).

The probability mass function is \(P(X=k)=\binom{3}{k}(0.5)^k(0.5)^{3-k}=\binom{3}{k}/8\). Thus the probabilities for \(k=0,1,2,3\) are \(1/8,3/8,3/8,1/8\), respectively. The mean is $$ E[X]=3(0.5)=1.5. $$

The event \(X\ge2\) has probability $$ E[Y]=P(X\ge2)=\frac38+\frac18=\frac12. $$

Because \(XY=0\) for \(X=0,1\), and \(XY=X\) for \(X=2,3\), $$ E[XY]=2\left(\frac38\right)+3\left(\frac18\right) =\frac68+\frac38=\frac98=1.125. $$

Therefore, $$ \operatorname{Cov}(X,Y)=\frac98-\left(\frac32\right)\left(\frac12\right) =\frac98-\frac34=\frac98-\frac68=\frac38. $$

Check. The mean product is 1.125, while the product of the means is 0.75; their difference is 0.375. The common mistake is to include \(X=1\) in \(E[XY]\) as a contribution of \(1\cdot P(X=1)\). At \(X=1\), the indicator is zero, so \(XY=0\).

Exam Traps and a Practical Checklist

The shortcut is brief, but its notation can hide errors. In a time-limited calculation, write each of the three required quantities on a separate line before subtracting. That makes it easier to check that the product moment comes from the joint behavior of the variables, rather than from their separate means.

  • Confusing the two products. \(E[XY]\) is an expectation of a random product. \(E[X]E[Y]\) is a product of fixed numbers. They are not interchangeable by definition.
  • Forgetting the subtraction. The shortcut is \(E[XY]-E[X]E[Y]\), not a sum and not \(E[XY]\) alone.
  • Using the wrong event values. If a variable is an indicator, multiply by zero on outcomes where the event does not occur.
  • Ignoring moment conditions. The formula is a finite covariance calculation when the variables have finite second moments. If those moments do not exist, do not assume the covariance is defined.
  • Dropping context too early. Keep track of what \(X\) and \(Y\) represent while computing \(XY\); the product’s contribution can differ substantially across conditional outcomes.

A compact check is to substitute the calculated means into the formula only after verifying the product moment independently. When practical, a direct centered-product calculation on a small model provides a useful cross-check, but the shortcut is usually more efficient for continuous moments, conditional models, or variables that are functions of another random variable.

Key takeaway: With finite second moments, calculate covariance by finding \(E[XY]\), \(E[X]\), and \(E[Y]\), then subtract \(E[X]E[Y]\) from \(E[XY]\). The central exam skill is calculating the expected product from the stated model correctly.

Check Your Understanding

For each problem, identify the product moment before applying the shortcut.

  1. Let \(U\sim\operatorname{Uniform}(0,2)\) and \(V=U^2\). Find \(\operatorname{Cov}(U,V)\). (Answer: \(\tfrac23\))
  2. Let \(X\) be Bernoulli with \(P(X=1)=0.4\). Given \(X=0\), let \(Y\sim\operatorname{Bernoulli}(0.1)\); given \(X=1\), let \(Y\sim\operatorname{Bernoulli}(0.7)\). Find \(\operatorname{Cov}(X,Y)\). (Answer: \(0.144\))
  3. Let \(N\sim\operatorname{Binomial}(4,0.5)\) and \(Z=I_{\{N\ge3\}}\). Find \(\operatorname{Cov}(N,Z)\). (Answer: \(\tfrac38\))
  4. A random pair \((R,S)\) takes values \((0,3),(1,1),(2,2),(3,0)\), each with probability \(1/4\). Find \(\operatorname{Cov}(R,S)\) using the shortcut formula. (Answer: \(-1\))
Clinical Biostats