Skip to content

Instantly share code, notes, and snippets.

@exceedsystem
Last active November 21, 2022 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exceedsystem/5c9988aa9c5027e281ef8e14ddd46251 to your computer and use it in GitHub Desktop.
Save exceedsystem/5c9988aa9c5027e281ef8e14ddd46251 to your computer and use it in GitHub Desktop.
Samples of calculating standard deviation in .NET(C#/VB.NET).
static class Extensions
{
public static double StdDev(this IEnumerable<double> src)
{
IList<double> lst = src is IList<double> ? (IList<double>)src : src.ToArray();
int n = lst.Count();
double ave = lst.Average();
double s = 0;
for (int i = 0; i < n; ++i)
s = s + (lst[i] - ave) * (lst[i] - ave);
return Math.Sqrt(s / n);
}
}
Module Extensions
<Extension()>
Public Function StdDev(src As IEnumerable(Of Double)) As Double
Dim lst As IList(Of Double) = If(TypeOf src Is IList(Of Double), CType(src, IList(Of Double)), src.ToArray())
Dim n As Integer = lst.Count()
Dim ave As Double = lst.Average()
Dim s As Double = 0
For i As Integer = 0 To n - 1
s = s + (lst(i) - ave) * (lst(i) - ave)
Next
Return Math.Sqrt(s / n)
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment