MATHEMATICS FOR AI • LESSON 5

Variance

Variance tells us how spread out a group of numbers is. It helps us understand whether the values are close together or far away from the average.

CORE IDEA

Variance measures how much the values differ from the mean.

A small variance means the values are close to the average. A large variance means the values are more spread out.

01

What Is Variance?

Imagine you have a group of numbers. Variance tells you how much those numbers are spread around their average value.

For example:

10, 10, 10, 10, 10

All the numbers are exactly the same. They are not spread out at all.

Mean = 10
Variance = 0

Now consider:

2, 6, 10, 14, 18

These numbers are much more spread out, so their variance is larger.

02

Why Do We Need Variance?

The mean tells us where the center of the data is. But the mean does not tell us how the data is distributed around that center.

Consider these two datasets:

Dataset A

48, 49, 50, 51, 52

Mean = 50
Dataset B

10, 30, 50, 70, 90

Mean = 50

Both datasets have the same mean:

Mean = 50

But they are clearly different. Dataset A is tightly grouped around 50, while Dataset B is much more spread out.

Variance helps us measure this difference.

03

Step 1: Find the Mean

To understand variance, we first need to calculate the mean.

Let's use a simple dataset:

2, 4, 6, 8, 10

Calculate the mean:

Mean = (2 + 4 + 6 + 8 + 10) / 5

     = 30 / 5

     = 6

So the average value is:

Mean = 6
04

Step 2: Find the Difference From the Mean

Next, we find how far each number is from the mean.

Our mean is 6.

Number     Difference from Mean

2          2 - 6  = -4
4          4 - 6  = -2
6          6 - 6  =  0
8          8 - 6  =  2
10         10 - 6 =  4

These differences tell us how far each value is from the center of the data.

05

Step 3: Square the Differences

Some differences are negative and some are positive. If we simply added them together, they would cancel each other.

So we square each difference.

Difference     Squared Difference

-4             (-4)² = 16
-2             (-2)² = 4
 0              (0)² = 0
 2              (2)² = 4
 4              (4)² = 16

Squaring makes every value positive and gives more weight to values that are farther away from the mean.

06

Step 4: Calculate the Variance

Now we calculate the average of the squared differences.

Squared differences:

16, 4, 0, 4, 16

Variance = (16 + 4 + 0 + 4 + 16) / 5

         = 40 / 5

         = 8

Therefore:

Variance = 8

This means the data has a certain amount of spread around its mean of 6.

07

The Complete Process

Variance can look complicated at first, but the process is actually a simple sequence of steps.

Original Data
      ↓
Find the Mean
      ↓
Find Difference From Mean
      ↓
Square Each Difference
      ↓
Find Average of Squared Differences
      ↓
Variance

Remember this flow. You will see the same idea again when learning standard deviation.

08

Small Variance vs Large Variance

The easiest way to understand variance is to compare datasets with different amounts of spread.

Dataset A

9, 10, 10, 10, 11

Values are close together
→ Small variance
Dataset B

1, 5, 10, 15, 20

Values are more spread out
→ Larger variance

Therefore:

Small Variance
→ Values are close to the mean

Large Variance
→ Values are far from the mean
09

Why Is Variance Used in AI?

AI models work with large amounts of numerical data. Understanding how that data is distributed is important.

For example, imagine a machine learning dataset containing customer ages:

25, 26, 27, 28, 29

The ages are very close together. The variance will be relatively small.

Now consider:

18, 25, 40, 65, 90

These values are much more spread out. The variance will be larger.

AI and machine learning algorithms use this type of statistical information to understand and process data.

10

Variance in Machine Learning Data

Suppose a machine learning model uses two features:

Feature 1: Age

25, 26, 27, 28, 29


Feature 2: Income

20,000
50,000
80,000
1,50,000
3,00,000

Age has relatively little spread. Income has much more spread.

Variance helps us quantify this difference instead of simply looking at the numbers and guessing.

11

Variance in Python

Python can calculate variance using the statistics module.

from statistics import pvariance

numbers = [2, 4, 6, 8, 10]

result = pvariance(numbers)

print(result)

Output:

8

Here we are using population variance, because we are treating all five values as the complete dataset.

12

Population Variance vs Sample Variance

There are two common ways to calculate variance.

Population Variance

Divide by N

where N = total number of values
Sample Variance

Divide by N - 1

where N = number of values in the sample

The important point for now is not to memorize every statistical detail. Understand that the calculation changes depending on whether your data represents the entire population or only a sample of it.

13

Variance and Squaring

You may wonder why we square the differences instead of simply using the differences themselves.

Consider:

Mean = 10

Value 8  → Difference = -2
Value 12 → Difference = +2

If we add the differences:

-2 + 2 = 0

The spread would incorrectly appear to be zero.

Squaring solves this problem:

(-2)² = 4
(+2)² = 4

4 + 4 = 8

Now the distance from the mean is properly represented.

14

Variance vs Mean

These two concepts answer different questions.

Mean

"Where is the center?"

Example:

10, 20, 30

Mean = 20
Variance

"How spread out are the values?"

Example:

10, 20, 30

Variance tells us
how far the values are
from the mean.

In statistics and AI, we often need both pieces of information: the center and the spread.

15

Variance and Standard Deviation

Variance is closely connected to standard deviation.

Standard deviation is simply the square root of variance.

Variance = 8

Standard Deviation = √8

                    ≈ 2.83

We will study standard deviation in the next lesson. For now, remember that the two concepts are directly connected.

WHAT TO REMEMBER

Variance measures how spread out the data is around its mean.

First find the mean, calculate how far each value is from the mean, square those differences, and then find their average. Small variance means the values are close together, while large variance means they are more spread out. Variance is important in AI because machine learning models work with numerical data whose spread and distribution matter.