variance (Descriptive Statistics)


Import

#include <archr/stats/variance.h>
#include <archr/stats/variance.hpp>
import archr.stats
atomsLoads('archr')

Synopsis

 float archr_stats_variance_f32(const float *first, size_t nbelem);
(1)
 double archr_stats_variance_f64(const double *first, size_t nbelem);
(2)
 float archr_stats_variance2_f32(const float *first, size_t nbelem, float m);
(3)
 double archr_stats_variance2_f64(const double *first, size_t nbelem, double m);
(4)
 float archr_stats_varp_f32(const float *first, size_t nbelem);
(5)
 double archr_stats_varp_f64(const double *first, size_t nbelem);
(6)
 float archr_stats_varp2_f32(const float *first, size_t nbelem, float m);
(7)
 double archr_stats_varp2_f64(const double *first, size_t nbelem, double m);
(8)
 float variance(const float *first, size_t nbelem);
(1)
 double variance(const double *first, size_t nbelem);
(2)
 float variance(const float *first, size_t nbelem, float m);
(3)
 double variance(const double *first, size_t nbelem, double m);
(4)
 float variance(const float *first, const float *last);
(5)
 double variance(const double *first, const double *last);
(6)
 float variance(const float *first, const float *last, float m);
(7)
 double variance(const double *first, const double *last, double m);
(8)
template <typename Range> typename Range::value_type variance(const Range& data);
(9)
template <typename Range> typename Range::value_type variance(const Range& data, float m);
(10)
template <typename Range> typename Range::value_type variance(const Range& data, double m);
(11)
 float varp(const float *first, size_t nbelem);
(12)
 double varp(const double *first, size_t nbelem);
(13)
 float varp(const float *first, size_t nbelem, float m);
(14)
 double varp(const double *first, size_t nbelem, double m);
(15)
 float varp(const float *first, const float *last);
(16)
 double varp(const double *first, const double *last);
(17)
 float varp(const float *first, const float *last, float m);
(18)
 double varp(const double *first, const double *last, double m);
(19)
template <typename Range> typename Range::value_type varp(const Range& data);
(20)
template <typename Range> typename Range::value_type varp(const Range& data, float m);
(21)
template <typename Range> typename Range::value_type varp(const Range& data, double m);
(22)
subroutine archr_stats_variance_f32(real(4) :: r, real(4), dimension(*), parameter :: first, integer(4) :: nbelem)
(1)
subroutine archr_stats_variance_f64(real(8) :: r, real(8), dimension(*), parameter :: first, integer(4) :: nbelem)
(2)
subroutine archr_stats_variance2_f32(real(4) :: r, real(4), dimension(*), parameter :: first, integer(4) :: nbelem, real(4) :: m)
(3)
subroutine archr_stats_variance2_f64(real(8) :: r, real(8), dimension(*), parameter :: first, integer(4) :: nbelem, real(8) :: m)
(4)
subroutine archr_stats_varp_f32(real(4) :: r, real(4), dimension(*), parameter :: first, integer(4) :: nbelem)
(5)
subroutine archr_stats_varp_f64(real(8) :: r, real(8), dimension(*), parameter :: first, integer(4) :: nbelem)
(6)
subroutine archr_stats_varp2_f32(real(4) :: r, real(4), dimension(*), parameter :: first, integer(4) :: nbelem, real(4) :: m)
(7)
subroutine archr_stats_varp2_f64(real(8) :: r, real(8), dimension(*), parameter :: first, integer(4) :: nbelem, real(8) :: m)
(8)
def variance(first):
    return r
(1)
def variance(first, m):
    return r
(2)
def varp(first):
    return r
(3)
def varp(first, m):
    return r
(4)
function r = archr_stats_variance(first)
(1)
function r = archr_stats_variance(first, m)
(2)
function r = archr_stats_varp(first)
(3)
function r = archr_stats_varp(first, m)
(4)

Description

This function computes the population variance (or Pearson's variance) of the elements stored in:

Note that the varp_* function variant s are provided as a convenience alias for migration from spreadsheet or Matlab like code.

Parameters

first

the beginning of the range of elements to process.

last

the end of the range of elements to process.

nbelem

the number of elements to process.

m

the mean of the range of elements.

Return value

The Pearson's variance of the given range or container using the following formula:

$$\textrm{V(X)} = \frac{1}{n}\sum_{i=1}^{n} (X[i] - m)^{2}$$

If m is not provided, the mean of the elements range is computed beforehand.

Example

#include <archr/stats/variance.h>
#include <stdio.h>

int main()
{
  float v[] = {1,-2,3,-4,5,-6};

  printf("variance(1,-2,3,-4,5,-6) = %f\n", archr_stats_variance_f32(&v[0],6) );

  puts("");

  printf("variance(1,-2,3,-4,5,-6) with m = -0.5 = %f\n", archr_stats_variance2_f32(&v[0], 6, -0.5));

  return 0;
}

Possible Output

variance(1,-2,3,-4,5,-6) = 14.916667
variance(1,-2,3,-4)      = 7.250000

variance(1,-2,3,-4,5,-6) with m = -0.5 = 14.916667
variance(1,-2,3,-4)      with m =  0.6 = 8.460000
#include <archr/stats/variance.hpp>
#include <iostream>
#include <vector>

int main()
{
  std::vector<float> v{1,-2,3,-4,5,-6};

  std::cout << "variance(1,-2,3,-4,5,-6) = " << archr::stats::variance(v) << "\n";
  std::cout << "variance(1,-2,3,-4,5)    = " << archr::stats::variance(v.data(), 5) << "\n";
  std::cout << "variance(1,-2,3)         = " << archr::stats::variance(v.data(), v.data() + 3) << "\n";
  std::cout << "\n";

  std::cout << "variance(1,-2,3,-4,5,-6) with m = -0.5 = " << archr::stats::variance(v,-0.5) << "\n";
  std::cout << "variance(1,-2,3,-4,5)    with m =  0.6 = " << archr::stats::variance(v.data(), 5, 0.6) << "\n";
  std::cout << "variance(1,-2,3)         with m =  2/3 = " << archr::stats::variance(v.data(), v.data() + 3, 2./3) << "\n";

  return 0;
}

Possible Output

variance(1,-2,3,-4,5,-6) = 14.9167
variance(1,-2,3,-4,5)    = 10.64
variance(1,-2,3)         = 4.22222

variance(1,-2,3,-4,5,-6) with m = -0.5 = 14.9167
variance(1,-2,3,-4,5)    with m =  0.6 = 10.64
variance(1,-2,3)         with m =  2/3 = 4.22222

Possible Output