6.2 The Variance
How spread out are the data values from the "center"? The most common measures of variation, or spread, of data are the standard deviation and variance.
The variance of a data set is the average of the squared deviations(distances) of the data values from the mean.
- The sample variance of the data set is given by
- The population variance of the data set is given by
numpy syntax for sample variance:
import numpy as np
x = [x1,x2,x3,...]
np.var(x,ddof=1) is (n-ddof)
numpy syntax for population variance:
import numpy as np
x = [x1,x2,x3,...]
np.var(x)
Note: ddof degrees of freedom for numerator is 1 for sample and 0 for population variance. ddof=0 by default in numpy.
Example: find the sample variance of the dataset 1,2,3,4,5
- Click
+ Code - Type
import numpy as np - TYpe
x = [1,2,3,4,5] - Type
np.var(x,ddof=1) - Click
playbutton
import numpy as np
x = [1,2,3,4,5]
np.var(x,ddof=1)
Show expected output
2.5Interpretation: The sample variance is 2.5
Example: find the population variance of the data set 1,2,3,4,5
import numpy as np
x = [1,2,3,4,5]
np.var(x) #in numpy ddof=0 by default
Show expected output
2.0Interpretation: The population variance is 2.0
Example: The salaries for the eight Board of Ethics workers for the City of Chicago are listed below. Write a numpy program to calculate the population variance.
139740, 100716, 80844, 124056, 79812, 87564, 96096, 96096
import numpy as np
x = [139740, 100716, 80844, 124056, 79812, 87564, 96096, 96096]
np.var(x,ddof=0)
Show expected output
389385609.75Interpretation: The population variance for the given salaries is 389385609.75
Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.