## 2 replies

Last post Feb 02, 2015 09:38 AM by mdehghani

• mdehghani

Member

94 Points

278 Posts

### caclulating standard deviation

hi everybody.. whats the easiest way to calculate the standard deviation of a query column with webmatrix? any predefined method, etc?

if there isn't any easy way.. how can i utilize this?

``````public static double StandardDeviation(List<double> valueList)
{
double M = 0.0;
double S = 0.0;
int k = 1;
foreach (double value in valueList)
{
double tmpM = M;
M += (value - tmpM) / k;
S += (value - tmpM) * (value - M);
k++;
}
return Math.Sqrt(S / (k-2));
}``````
• GmGregori

Contributor

4946 Points

873 Posts

### Re: caclulating standard deviation

You can make use of your Standard Deviation function as follows:

1. create a new directory in the root of your site and name it App_Code;

2. in App_Code create a new cshtml file and name it Functions.cshtml;

3. replace the content of Functions.cshtml with

```@functions {
public static double StandardDeviation(List<dynamic> recList)
{
double M = 0.0;
double S = 0.0;
int k = 1;
foreach (var rec in recList)
{
double value = (double)rec[0];
double tmpM = M;
M += (value - tmpM) / k;
S += (value - tmpM) * (value - M);
k++;
}
return Math.Sqrt(S / (k-2));
}
}
```

Now you can get the standard deviation with the following two lines of code:

```var std = db1.Query("SELECT field_with_values FROM table_name").ToList();
var stdvalue = Functions.StandardDeviation(std);
```

• mdehghani

Member

94 Points

278 Posts