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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Drebin

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

    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!


  • Kyle Kaitan

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

    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.Collecti ons;

    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 (DictionaryEntr y e in h)
    {
    if (e.Value >= count) modeValues.Add( e.Key);
    }

    return (float[])modeValues.ToA rray(typeof(Sys tem.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" <thedrebin@hotm ail.com> wrote in message
    news:nqJ4d.7901 $Qv5.3849@newss vr33.news.prodi gy.com...[color=blue]
    > 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!
    >
    >[/color]


    Comment

    • Drebin

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

      hehehe - excellent!!! Thanks much Kyle!! :-)


      "Kyle Kaitan" <kkaitan [at] gmail [dot] com> wrote in message
      news:e92MNfdoEH A.4032@TK2MSFTN GP15.phx.gbl...[color=blue]
      > 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.Collecti ons;
      >
      > 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 (DictionaryEntr y e in h)
      > {
      > if (e.Value >= count) modeValues.Add( e.Key);
      > }
      >
      > return (float[])modeValues.ToA rray(typeof(Sys tem.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" <thedrebin@hotm ail.com> wrote in message
      > news:nqJ4d.7901 $Qv5.3849@newss vr33.news.prodi gy.com...[color=green]
      >> 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!
      >>
      >>[/color]
      >
      >[/color]


      Comment

      Working...