discord-bot/CompatBot/Utils/Statistics.cs

40 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
namespace CompatBot.Utils
{
public static class Statistics
{
public static long Mean(this IEnumerable<long> data)
{
System.Numerics.BigInteger sum = 0;
var itemCount = 0;
foreach (var value in data)
{
sum += value;
itemCount ++;
}
if (itemCount == 0)
throw new ArgumentException("Sequence must contain elements", nameof(data));
return (long)(sum / itemCount);
}
public static double StdDev(this IEnumerable<long> data)
{
System.Numerics.BigInteger σx = 0, σx2 = 0;
var n = 0;
foreach (var value in data)
{
σx += value;
σx2 += (System.Numerics.BigInteger)value * value;
n++;
}
if (n == 0)
throw new ArgumentException("Sequence must contain elements", nameof(data));
var σ2 = σx * σx;
return Math.Sqrt((double)((n * σx2) - σ2) / ((n - 1) * n));
}
}
}