FUNCTION
Variance
Calculates the variance of a given set of data.
Interface
#include <codecogs/statistics/moments/variance.h>
using namespace Statistics::Moments;
Consider a discrete random variable . The variance
of is defined as
Note that is a new random variable (it's a function of
). The variance is also denoted as
. A useful formula that follows inmediately from the definition is that
In words, the variance of is the second moment of
minus the first moment squared. The variance of a random variable determines a level of variation of the possible values of
around its mean. However, as this measure is squared, the standard deviation is used instead when one wants to talk about how much a random variable varies around its expected value.
If we cannot analyze a whole population but we have to take a sample, we define its variance (denoted as ) with the formula:
where is the aritmetic mean . The value for
is an estimator for
.
If the value of the boolean argument <em> total </em> is true, then the variance is computed using the following formula:
References
PlanetMath, http:planetmath.org/encyclopedia/Variance.html
Example 1
#include <codecogs/statistics/moments/variance.h>
#include <iostream>
int main()
{
int x[5] = {3 , 1 , 5 , 6 , 9};
double var = Statistics::Moments::variance<int>(5, x);
std::cout << "The population variance is: " << var << std::endl;
return 0;
}Output:
The population variance is: 9.2