-
Notifications
You must be signed in to change notification settings - Fork 6
Calculate mean, standard deviation, variance and covariance
Martijn Koopman edited this page Jun 6, 2019
·
2 revisions
Given a list of values:
std::vector<double> values = {2,4,4,4,5,5,7,9};
Compute mean
double mean = stats::mean(values); // 5
Compute variance
double var = stats::variance(values); // 4 (population, default)
double varSample = stats::variance(values, true); // (sample)
Compute standard deviation
double sd = stats::stdDev(values); // 2 (population, default)
double sdSample = stats::stdDev(values, true); // (sample)
Given 2 lists of values:
std::vector<double> x = {1, 6, 3, 4, 4, 2, 5, 3, 2, 5};
std::vector<double> y = {1, 4, 2, 3, 2, 2, 3, 3, 1, 4};
Compute covariance
double cov = stats::covariance(x,y); // 1.35 (population, default)
double covSample = stats::covariance(x,y,true); // 1.50 (sample)
Function stats::covariance()
also accepts a matrix as argument. This function returns the symmetric covariance matrix.