Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard deviation of generic list? [duplicate]

I need to calculate the standard deviation of a generic list. I will try to include my code. Its a generic list with data in it. The data is mostly floats and ints. Here is my code that is relative to it without getting into to much detail:

namespace ValveTesterInterface {     public class ValveDataResults     {         private List<ValveData> m_ValveResults;          public ValveDataResults()         {             if (m_ValveResults == null)             {                 m_ValveResults = new List<ValveData>();             }         }          public void AddValveData(ValveData valve)         {             m_ValveResults.Add(valve);         } 

Here is the function where the standard deviation needs to be calculated:

        public float LatchStdev()         {              float sumOfSqrs = 0;             float meanValue = 0;             foreach (ValveData value in m_ValveResults)             {                 meanValue += value.LatchTime;             }             meanValue = (meanValue / m_ValveResults.Count) * 0.02f;              for (int i = 0; i <= m_ValveResults.Count; i++)              {                    sumOfSqrs += Math.Pow((m_ValveResults - meanValue), 2);               }             return Math.Sqrt(sumOfSqrs /(m_ValveResults.Count - 1));          }     } } 

Ignore whats inside the LatchStdev() function because I'm sure its not right. Its just my poor attempt to calculate the st dev. I know how to do it of a list of doubles, however not of a list of generic data list. If someone had experience in this, please help.

like image 875
Tom Hangler Avatar asked Jun 29 '10 14:06

Tom Hangler


People also ask

How do you find the standard deviation of a list?

Step 1: Find the mean. Step 2: For each data point, find the square of its distance to the mean. Step 3: Sum the values from Step 2. Step 4: Divide by the number of data points.

Can you get a standard deviation from two values?

2. The standard deviation may be thought of as the average difference between any two data values, ignoring the sign. where N is the number of items in the population, X is the variable being measured, and µ is the mean of X. This formula indicates that the standard deviation is the square root of an average.


2 Answers

The example above is slightly incorrect and could have a divide by zero error if your population set is 1. The following code is somewhat simpler and gives the "population standard deviation" result. (http://en.wikipedia.org/wiki/Standard_deviation)

using System; using System.Linq; using System.Collections.Generic;  public static class Extend {     public static double StandardDeviation(this IEnumerable<double> values)     {         double avg = values.Average();         return Math.Sqrt(values.Average(v=>Math.Pow(v-avg,2)));     } } 
like image 56
Jonathan DeMarks Avatar answered Sep 23 '22 15:09

Jonathan DeMarks


This article should help you. It creates a function that computes the deviation of a sequence of double values. All you have to do is supply a sequence of appropriate data elements.

The resulting function is:

private double CalculateStandardDeviation(IEnumerable<double> values) {      double standardDeviation = 0;    if (values.Any())    {            // Compute the average.           double avg = values.Average();       // Perform the Sum of (value-avg)_2_2.            double sum = values.Sum(d => Math.Pow(d - avg, 2));       // Put it all together.            standardDeviation = Math.Sqrt((sum) / (values.Count()-1));      }      return standardDeviation; } 

This is easy enough to adapt for any generic type, so long as we provide a selector for the value being computed. LINQ is great for that, the Select funciton allows you to project from your generic list of custom types a sequence of numeric values for which to compute the standard deviation:

List<ValveData> list = ... var result = list.Select( v => (double)v.SomeField )                  .CalculateStdDev(); 
like image 28
LBushkin Avatar answered Sep 22 '22 15:09

LBushkin