Calculates the statistics for a set of data that may change size frequently.

You're viewing an older version of this page (#108). View the current version.

View versions (1)

Interface

Overview

This class allows the calculation of key statistics for a set of data that may change its size frequently. The main advantage of this class is the ability of updating the key statistics while storing minimum information about previous data into a set of main variables given below, rather than storing the actual data. Based on the next main variables, that are recalculated with each new value that is added to the population,

S=\sum_{i=1}^N x_i \qquad
    S_1=\sum_{i=1}^N \frac{1}{x_i} \qquad
    S_2=\sum_{i=1}^N x_i^2
    \f]
    \f[
    S_3=\sum_{i=1}^N x_i^3 \qquad
    S_4=\sum_{i=1}^N x_i^4 \qquad
    prod=\prod_{i=1}^N x_i
(1)

it calculates the statistics using the following formulas

mean=\frac{S}{N}\f]
    \f[geomean=\sqrt[N]{prod}\f]
    \f[harmean=\frac{N}{S_1}\f]
    \f[var=\frac{S_2-2\overline{x}S+N\overline{x}^2}{N-1}\f]
    \f[stdev=\sqrt{var}
(2)

where mean is the arithmetic mean, geomean is the geometric mean, harmean is the harmonic mean,var </em> is the variance and stdev is the standard deviation. The skewness and kurtosis are calculated using similar formulas.

This part is only visible to registered users. Sign in to see it.

References

Example 1

#include <codecogs/statistics/moments/statistics.h>
    #include <iostream>
    int main()
    {
      double a[15];
      for (int i = 0; i < 15; i++)
        a[i]=i+1;
      a[1]=5;
      Stats::Moments::Statistics<double> stats(15, a);
      stats.print();
      return 0;
    }

Output:

Total = 123.0000000000
    Arithmetic Mean = 8.2000000000
     Geometric Mean = 6.8280387111
      Harmonic Mean = 4.9698018386
           Variance = 18.0285714286
 Standard Deviation = 4.2460065271
           Skewness = 0.0403100873
           Kurtosis = -1.0487842858
GPL Licence — free for non commercial use. See Licence details.

Members of Statistics

CLASS METHOD

Statistics

Default constructor which resets all variables

CLASS METHOD

Statistics

This constructor takes an array as input, which is analyzes to compute the various statistical parameters

Parameters

data
A pointer to the data array
size
The number of elements within the data array to analyze

CLASS METHOD

add

This function adds a new value to the statistical collection. Additional values are appended in constant time very quickly, enabling you to call this function thousands of times in quick succession with no penalty.

Parameters

x
value to be added to the accumulating statistics

CLASS METHOD

calcStats

One this class is loaded with the values that you want analysed, this function must be run to compute the final values such as mean, variance etc. This function need only be run once before all statistics can be retrieved.

CLASS METHOD

print

Calculates and prints the key statistics of the current population.

CLASS METHOD

getTotal

total = \sum_{i=1}^N x_i
(3)

CLASS METHOD

getMean

\overline{x} = \frac{1}{N} \sum_{i=1}^N x_i
(4)

CLASS METHOD

getGeometricMean

geom=\sqrt[N] {\prod_{i=1}^N x_i}
(5)

CLASS METHOD

getHarmonicMean

harm=\frac{N}{\sum_{i=1}^N\frac{1}{x_i}}
(6)

CLASS METHOD

getVariance

var=\frac{1}{N-1}\sum_{i=1}^N(x_i-\overline{x})^2
(7)

CLASS METHOD

getStdev

\sigma = \sqrt{ \frac {1}{N-1} \sum_{i=1}^N (x_i - \overline{x})^2 }
(8)

CLASS METHOD

getSkewness

skew= \frac{N}{(N-1)(N-2) \sigma^3} \sum_{i=1}^N (x_i - \overline{x})^3
(9)

CLASS METHOD

getKurtosis

kurt= \frac{1}{(N-2)(N-3)}\left( \frac{N(N+1)}{(N-1) \sigma^4}
    \sum_{i=1}^N (x_i - \overline{x})^4 - 3(N-1)^2 \right)
(10)

CLASS METHOD

getCV

CV= \frac{\sigma}{\overline{x}}
(11)

CLASS METHOD

getSize

Calculates the size of the set of data.

CLASS METHOD

operator+

Combines the statistics from two independent Statistics objects. This is equivalent to having added all the data to just one container, with all the statistical measures maintained accurately. i.e

Stats::Moments::Statistics<double> A(15, somearray_a);
  Stats::Moments::Statistics<double> B(51, somearray_b);

  //This contains the combined statistics 
  Stats::Moments::Statistics<double> C = A+B;

Parameters

B
the Statistics container to add to the current object