Statistics module provides very powerful tools, which can be used to compute anything related to Statistics.
variance() is one such function. This function helps to calculate the variance from a sample of data (sample is a subset of populated data).
variance() function should only be used when variance of a sample needs to be calculated. There’s another function known as
pvariance(), which is used to calculate the variance of an entire population.
In pure statistics,
variance is the squared deviation of a variable from its mean. Basically, it measures the spread of random data in a set from its mean or median value. A low value for variance indicates that the data are clustered together and are not spread apart widely, whereas a high value would indicate that the data in the given set are much more spread apart from the average value.
Variance is an important tool in the sciences, where statistical analysis of data is common. It is the square of standard deviation of the given data-set and is also known as second central moment of a distribution. It is usually represented by in pure Statistics.
Variance is calculated by the following formula :
It’s calculated by mean of square minus square of mean
Syntax :variance( [data], xbar )
[data] : An iterable with real valued numbers.
xbar (Optional) : Takes actual mean of data-set as value.
Returnype :Returns the actual variance of the values passed as parameter.
StatisticsError is raised for data-set less than 2-values passed as parameter.
Throws impossible values when the value provided as xbar doesn’t match actual mean of the data-set.
Code #1 :
Variance of sample set is 0.40924
Code #2 : Demonstrates variance() on a range of data-types
Variance of Sample 1 is 15.80952380952381 Variance of Sample 2 is 3.5 Variance of Sample 3 is 61.125 Variance of Sample 4 is 1/45 Variance of Sample 5 is 0.17613000000000006
Code #3 : Demonstrates the use of xbar parameter
Variance of Sample set is 0.3656666666666667
Code #4 : Demonstrates the Error when value of xbar is not same as the mean/average value
Note : It is different in precision from the output in Code #3
Code #4 : Demonstrates StatisticsError
Traceback (most recent call last): File "/home/64bf6d80f158b65d2b75c894d03a7779.py", line 10, in print(statistics.variance(sample)) File "/usr/lib/python3.5/statistics.py", line 555, in variance raise StatisticsError('variance requires at least two data points') statistics.StatisticsError: variance requires at least two data points
Variance is a very important tool in Statistics and handling huge amounts of data. Like, when the omniscient mean is unknown (sample mean) then variance is used as biased estimator. Real world observations like the value of increase and decrease of all shares of a company throughout the day cannot be all sets of possible observations. As such, variance is calculated from a finite set of data, although it won’t match when calculated taking the whole population into consideration, but still it will give the user an estimate which is enough to chalk out other calculations.
- Python | Variance of List
- sympy.stats.variance() function in Python
- Calculate the average, variance and standard deviation in Python using NumPy
- Compute the mean, standard deviation, and variance of a given NumPy array
- Python statistics | pvariance()
- Use Pandas to Calculate Statistics in Python
- Python statistics | mean() function
- Python - Moyal Distribution in Statistics
- Python - Maxwell Distribution in Statistics
- Python - Lomax Distribution in Statistics
- Python - Log Normal Distribution in Statistics
- Python - Log Laplace Distribution in Statistics
- Python - Logistic Distribution in Statistics
- Python - Log Gamma Distribution in Statistics
- Python - Levy_stable Distribution in Statistics
- Python - Left-skewed Levy Distribution in Statistics
- Python - Laplace Distribution in Statistics
- Python - Levy Distribution in Statistics
- Python - Kolmogorov-Smirnov Distribution in Statistics
- Python - ksone Distribution in Statistics
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai