Calculate Variance

Variance is a measure of how spread out a data set is. It useful when creating statistical models, since low variance can be a sign that you are over-fitting your data.[1] This article will teach you how to calculate variance.

10 Second Summary

1. Write down your sample data set. More ↓
2. Write down the sample variance formula.
3. Calculate the mean of the sample.
4. Subtract the mean from each data point.
5. Square each result.
6. Find the sum of the squared values.
7. Divide by n - 1, where n is the number of data points.

Steps

Help Calculating Variance

Doc:Variance

Calculating Variance of a Sample

  1. Write down your sample data set. In most cases, statisticians only have access to a sample, or a subset of the population they're studying. For example, instead of analyzing the population "cost of every car in Germany," a statistician could find the cost of a random sample of a few thousand cars. He can use this sample to get a good estimate of German car costs, but it will likely not match the actual numbers exactly.
    • Example: Analyzing the number of muffins sold each day at a cafeteria, you sample six days at random and get these results: 17, 15, 23, 7, 9, 13. This is a sample, not a population, since you don't have data on every single day the cafeteria was open.
    • If you have every data point in a population, skip down to the method below instead.
  2. Write down the sample variance formula. The variance of a data set tells you how spread out the data points are. The closer the variance is to zero, the more closely the data points are clustered together. When working with sample data sets, use the following formula to calculate variance:[2]
    • <math>s^2</math> = ∑[(<math>x_i</math> - x̅)<math>^2</math>]/(n - 1)
    • <math>s^2</math> is the variance. Variance is always measured in squared units.
    • <math>x_i</math> represents a term in your data set.
    • ∑, meaning "sum," tells you to calculate the following terms for each value of <math>x_i</math>, then add them together.
    • x̅ is the mean of the sample.
    • n is the number of data points.
  3. Calculate the mean of the sample. The symbol x̅ or "x-bar" refers to the mean of a sample.[3] Calculate this as you would any mean: add all the data points together, then divide by the number of data points.
    • Example: First, add your data points together: 17 + 15 + 23 + 7 + 9 + 13 = 84
      Next, divide your answer by the number of data points, in this case six: 84 ÷ 6 = 14.
      Sample mean = x̅ = 14.
    • You can think of the mean as the "centre-point" of the data. If the data clusters around the mean, variance is low. If it is spread out far from the mean, variance is high.
  4. Subtract the mean from each data point. Now it's time to calculate <math>x_i</math> - x̅, where <math>x_i</math> is each number in your data set. Each answer tells you that number's deviation from the mean, or in plain language, how far away it is from the mean.[4].
    • Example:
      <math>x_1</math> - x̅ = 17 - 14 = 3
      <math>x_2</math> - x̅ = 15 - 14 = 1
      <math>x_3</math> - x̅ = 23 - 14 = 9
      <math>x_4</math> - x̅ = 7 - 14 = -7
      <math>x_5</math> - x̅ = 9 - 14 = -5
      <math>x_6</math> - x̅ = 13 - 14 = -1
    • It's easy to check your work, as your answers should add up to zero. This is due to the definition of mean, since the negative answers (distance from mean to smaller numbers) exactly cancel out the positive answers (distance from mean to larger numbers).
  5. Square each result. As noted above, your current list of deviations (<math>x_i</math> - x̅) sum up to zero. This means the "average deviation" will always be zero as well, so that doesn't tell use anything about how spread out the data is. To solve this problem, find the square of each deviation. This will make them all positive numbers, so the negative and positive values no longer cancel out to zero.[5]
    • Example:
      (<math>x_1</math> - x̅)<math>^2 = 3^2 = 9</math>
      <math>(x_2</math> - x̅)<math>^2 = 1^2 = 1</math>
      92 = 81
      (-7)2 = 49
      (-5)2 = 25
      (-1)2 = 1
    • You now have the value (<math>x_i</math> - x̅)<math>^2</math> for each data point in your sample.
  6. Find the sum of the squared values. Now it's time to calculate the entire numerator of the formula: ∑[(<math>x_i</math> - x̅)<math>^2</math>]. The upper-case sigma, ∑, tells you to sum the value of the following term for each value of <math>x_i</math>. You've already calculated (<math>x_i</math> - x̅)<math>^2</math> for each value of <math>x_i</math> in your sample, so all you need to do is add the results together.
    • Example: 9 + 1 + 81 + 49 + 25 + 1 = 166.
  7. Divide by n - 1, where n is the number of data points. A long time ago, statisticians just divided by n when calculating the variance of the sample. This gives you the average value of the squared deviation, which is a perfect match for the variance of that sample. But remember, a sample is just an estimate of a larger population. If you took another random sample and made the same calculation, you would get a different result. As it turns out, dividing by n - 1 instead of n gives you a better estimate of variance of the larger population, which is what you're really interested in. This correction is so common that it is now the accepted definition of a sample's variance.[5]
    • Example: There are six data points in the sample, so n = 6.
      Variance of the sample = <math>s^2 = \frac{166}{6 - 1} =</math> 33.2
  8. Understand variance and standard deviation. Note that, since there was an exponent in the formula, variance is measured in the squared unit of the original data. This can make it difficult to understand intuitively. Instead, it's often useful to use the standard deviation. You didn't waste your effort, though, as the standard deviation is defined as the square root of the variance. This is why the variance of a sample is written <math>s^2</math>, and the standard deviation of a sample is <math>s</math>.
    • For example, the standard deviation of the sample above = s = √33.2 = 5.76.

Calculating Variance of a Population

  1. Start with a population data set. The term "population" refers to the total set of relevant observations. For example, if you're studying the age of Texas residents, your population would include the age of every single Texas resident. You would normally Make-a-Spreadsheet for a large data set like that, but here's a smaller example data set:
    • Example: There are exactly six fish tanks in a room of the aquarium. The six tanks contain the following numbers of fish:
      <math>x_1 = 5</math>
      <math>x_2 = 5</math>
      <math>x_3 = 8</math>
      <math>x_4 = 12</math>
      <math>x_5 = 15</math>
      <math>x_6 = 18</math>
  2. Write down the population variance formula. Since a population contains all the data you need, this formula gives you the exact variance of the population. In order to distinguish it from sample variance (which is only an estimate), statisticians use different variables:[2]
    • σ<math>^2</math> = (∑(<math>x_i</math> - μ)<math>^2</math>)/n
    • σ<math>^2</math> = population variance. This is a lower-case sigma, squared. Variance is measured in squared units.
    • <math>x_i</math> represents a term in your data set.
    • The terms inside ∑ will be calculated for each value of <math>x_i</math>, then summed.
    • μ is the population mean
    • n is the number of data points in the population
  3. Find the mean of the population. When analyzing a population, the symbol μ ("mu") represents the arithmetic mean. To find the mean, add all the data points together, then divide by the number of data points.
    • You can think of the mean as the "average," but be careful, as that word has multiple definitions in mathematics.
    • Example: mean = μ = <math>\frac{5 + 5 + 8 + 12 + 15 + 18}{6}</math> = 10.5
  4. Subtract the mean from each data point. Data points close to the mean will result in a difference closer to zero. Repeat the subtraction problem for each data point, and you might start to get a sense of how spread out the data is.
    • Example:
      <math>x_1</math> - μ = 5 - 10.5 = -5.5
      <math>x_2</math> - μ = 5 - 10.5 = -5.5
      <math>x_3</math> - μ = 8 - 10.5 = -2.5
      <math>x_4</math> - μ = 12 - 10.5 = 1.5
      <math>x_5</math> - μ = 15 - 10.5 = 4.5
      <math>x_6</math> - μ = 18 - 10.5 = 7.5
  5. Square each answer. Right now, some of your numbers from the last step will be negative, and some will be positive. If you picture your data on a number line, these two categories represent numbers to the left of the mean, and numbers to the right of the mean. This is no good for calculating variance, since these two groups will cancel each other out. Square each number so they are all positive instead.
    • Example:
      (<math>x_i</math> - μ)<math>^2</math> for each value of i from 1 to 6:
      (-5.5)<math>^2</math> = 30.25
      (-5.5)<math>^2</math> = 30.25
      (-2.5)<math>^2</math> = 6.25
      (1.5)<math>^2</math> = 2.25
      (4.5)<math>^2</math> = 20.25
      (7.5)<math>^2</math> = 56.25
  6. Find the mean of your results. Now you have a value for each data point, related (indirectly) to how far that data point is from the mean. Take the mean of these values by adding them all together, then dividing by the number of values.
    • Example:
      Variance of the population = <math>\frac{30.25 + 30.25 + 6.25 + 2.25 + 20.25 + 56.25}{6} = \frac{145.5}{6} =</math> 24.25
  7. Relate this back to the formula. If you're not sure how this matches the formula at the beginning of this method, try writing out the whole problem in longhand:
    • After finding the difference from the mean and squaring, you have the value (<math>x_1</math> - μ)<math>^2</math>, (<math>x_2</math> - μ)<math>^2</math>, and so on up to (<math>x_n</math> - μ)<math>^2</math>, where <math>x_n</math> is the last data point in the set.
    • To find the mean of these values, you sum them up and divide by n: ( (<math>x_1</math> - μ)<math>^2</math> + (<math>x_2</math> - μ)<math>^2</math> + ... + (<math>x_n</math> - μ)<math>^2</math> ) / n
    • After rewriting the numerator in sigma notation, you have (∑(<math>x_i</math> - μ)<math>^2</math>)/n, the formula for variance.



Tips

  • Since it is difficult to interpret the variance, this value is usually calculated as a starting point for calculating the standard deviation.
  • Using "n-1" instead of "n" in the denominator when analyzing samples is a technique called Bessel's correction. The sample is only an estimate of the full population, and the mean of the sample is biased to fit that estimate. This correction removes this bias.[6] This is related to the fact that, once you've listed n - 1 data points, the final nth point is already constrained, since only certain values will result in the sample mean (x̅) used in the variance formula.[7]

Related Articles

Sources and Citations