Last active
March 25, 2021 11:30
-
-
Save 13xforever/1130205 to your computer and use it in GitHub Desktop.
Some statistics functions
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Numerics; | |
namespace Math.Statistics | |
{ | |
public static class Statistics | |
{ | |
public static long Mean(this IEnumerable<long> data) | |
{ | |
if (data == null) throw new ArgumentNullException("data"); | |
BigInteger sum = 0; | |
int itemCount = 0; | |
foreach (var value in data) | |
{ | |
sum += value; | |
itemCount ++; | |
} | |
if (itemCount == 0) throw new ArgumentException("data"); | |
return (long)(sum / itemCount); | |
} | |
public static double StdDev(this IEnumerable<long> data) | |
{ | |
if (data == null) throw new ArgumentNullException("data"); | |
BigInteger σx = 0, σx2 = 0; | |
int n = 0; | |
foreach (var value in data) | |
{ | |
σx += value; | |
σx2 += (BigInteger)value * value; | |
n++; | |
} | |
if (n == 0) throw new ArgumentException("data"); | |
BigInteger σ2 = σx * σx; | |
return System.Math.Sqrt((double)((n * σx2) - σ2) / ((n - 1) * n)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment