Last active
November 21, 2022 08:57
-
-
Save exceedsystem/5c9988aa9c5027e281ef8e14ddd46251 to your computer and use it in GitHub Desktop.
Samples of calculating standard deviation in .NET(C#/VB.NET).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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