NMath Stats User's Guide

TOC | Previous | Next | Index

3.6 Central Tendency (.NET, C#, CSharp, VB, Visual Basic, F#)

Measures of central tendency are measures of the location of the middle or the center of a data set. For example, the static Mean() method on class StatsFunctions computes the arithmetic mean (average) of the elements in a data set:

Code Example – C#

double mean = StatsFunctions.Mean( data );

Median() calculates the median of the elements in a data set:

Code Example – C#

double median = StatsFunctions.Median( data );

The median is the middle of the set—half the values are above the median and half are below the median. If there are an even number of elements, Median() returns the average of the middle two elements.

Mode() determines the most frequently occurring value in a data set:

Code Example – C#

double mode = StatsFunctions.Mode( data );

GeometricMean() calculates the geometric mean.

HarmonicMean() calculates the harmonic mean.

TrimmedMean() calculates the mean of a data set after the specified trimming. A trimmed mean is calculated by discarding a certain percentage of the lowest and the highest values and then computing the mean of the remaining values. For example, a mean trimmed 50% is computed by discarding the lower and higher 25% of the values and taking the mean of the remaining values. TrimmedMean() takes a trimming parameter, which is a value between 0.0 and 1.0. For example, this code computes the mean trimmed 50%:

Code Example – C#

double trimMean = StatsFunctions.TrimmedMean( data, 0.50 );

The median is the mean trimmed 1.0, and the arithmetic mean is the mean trimmed 0.0.

WeightedMean() calculates the weighted average of all the elements in a data set using a given set of corresponding weights. The weighted mean is calculated as

For instance:

Code Example – C#

var v = new DoubleVector( "-0.3 -0.03 4 2.8 -12.3 -5 3 10" );
var weights = new DoubleVector( "1 2 3 4 2 1 3 4" );
double weightedMean = StatsFunctions.WeightedMean( v, weights ))

A MismatchedSizeException is raised if the number of weights does not equal the number of elements in the data set. Note that if all the weights are equal, the weighted mean is the same as the arithmetic mean.

Lastly, RMS() calculates the root mean square of the elements in a data set. RMS, sometimes called the quadratic mean, is the square root of the mean squared value.


Top

Top