Anyone have a mean, median, mode, stddev routines in C#??

  • Thread starter Thread starter Drebin
  • Start date Start date
D

Drebin

I'm looking for mean, median, mode and standard deviation - maybe a couple
methods that take in an array of floats or something??

Thanks much!
 
Drebin:

This is untested, but feel free to use as you see fit. For more extensive
statistical/math computations like Bessel functions, likelihood estimators,
and so forth, you might want to check out NMath. I've favored readability
over raw speed here, but this should be very fast.

HTH,
- k^2

///// Begin code.
using System;
using System.Collections;

public class MathAverages
{
// Computes the median x~ of a set of values X. O(1) implementation..
public static float Median(float[] values)
{
int size = values.Length - 1;
return ((values[size/2] == values[size/2 + 1]) / 2);
}

// Computes the mode set x{} of a set of values X. O(n) implementation.
public static float[] Mode(float[] values)
{
Hashtable h = new Hashtable();
int count = 0;
foreach (float value in values)
{
h[value]++;
if (h[value] > count)
count = h[value];
}

ArrayList modeValues = new ArrayList();
foreach (DictionaryEntry e in h)
{
if (e.Value >= count) modeValues.Add(e.Key);
}

return (float[])modeValues.ToArray(typeof(System.Single));
}

// Computes the arithmetic mean x-bar of a set of values X. O(n)
implementation.
public static float Mean(float[] values)
{
float sum = 0;
foreach (float value in values)
{
sum += value;
}
return sum/(values.Length);
}
}
///// End code.
 
hehehe - excellent!!! Thanks much Kyle!! :-)


Kyle Kaitan said:
Drebin:

This is untested, but feel free to use as you see fit. For more extensive
statistical/math computations like Bessel functions, likelihood
estimators,
and so forth, you might want to check out NMath. I've favored readability
over raw speed here, but this should be very fast.

HTH,
- k^2

///// Begin code.
using System;
using System.Collections;

public class MathAverages
{
// Computes the median x~ of a set of values X. O(1) implementation..
public static float Median(float[] values)
{
int size = values.Length - 1;
return ((values[size/2] == values[size/2 + 1]) / 2);
}

// Computes the mode set x{} of a set of values X. O(n) implementation.
public static float[] Mode(float[] values)
{
Hashtable h = new Hashtable();
int count = 0;
foreach (float value in values)
{
h[value]++;
if (h[value] > count)
count = h[value];
}

ArrayList modeValues = new ArrayList();
foreach (DictionaryEntry e in h)
{
if (e.Value >= count) modeValues.Add(e.Key);
}

return (float[])modeValues.ToArray(typeof(System.Single));
}

// Computes the arithmetic mean x-bar of a set of values X. O(n)
implementation.
public static float Mean(float[] values)
{
float sum = 0;
foreach (float value in values)
{
sum += value;
}
return sum/(values.Length);
}
}
///// End code.


Drebin said:
I'm looking for mean, median, mode and standard deviation - maybe a
couple
methods that take in an array of floats or something??

Thanks much!
 
Back
Top