mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 13:33:13 +00:00
Analysis
This commit is contained in:
31
Esiur.Analysis/Statistics/Probability.cs
Normal file
31
Esiur.Analysis/Statistics/Probability.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using Esiur.Analysis.Units;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Analysis.Statistics
|
||||
{
|
||||
public struct Probability
|
||||
{
|
||||
public double Value;
|
||||
|
||||
public static implicit operator Probability(double v) => new Probability(v);
|
||||
public static implicit operator double(Probability v) => v.Value;
|
||||
|
||||
public Probability(double value)
|
||||
{
|
||||
if (value > 1 || value < 0)
|
||||
throw new Exception("0 >= Probability <= 1");
|
||||
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public double ToPercentage() => Value * 100;
|
||||
public Probability FromPercentage(double value) => new Probability(value / 100);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return (Math.Round(Value * 10000) / 100) + "%";
|
||||
}
|
||||
}
|
||||
}
|
44
Esiur.Analysis/Statistics/StatisticalFunctions.cs
Normal file
44
Esiur.Analysis/Statistics/StatisticalFunctions.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Analysis.Statistics
|
||||
{
|
||||
public static class Statistics
|
||||
{
|
||||
public static double Mean(this double[] values) => values.Sum() / values.Length;
|
||||
|
||||
public static double Variance(this double[] x)
|
||||
{
|
||||
var mean = x.Mean();
|
||||
return x.Sum(x => Math.Pow(x - mean, 2)) / x.Length;
|
||||
}
|
||||
|
||||
|
||||
public static double StdDiv(this double[] x) => Math.Sqrt(x.Variance());
|
||||
|
||||
public static double Covariance(this double[] x, double[] y)
|
||||
{
|
||||
var n = x.Length < y.Length ? x.Length : y.Length;
|
||||
var X = x.Mean();
|
||||
var Y = y.Mean();
|
||||
|
||||
double rt = 0;
|
||||
|
||||
for (var i = 0; i < n; i++)
|
||||
rt += (x[i] - X) * (y[i] - Y);
|
||||
|
||||
return rt / n;
|
||||
}
|
||||
|
||||
|
||||
public static double RMS(this double[] x) => Math.Sqrt(x.Sum(x => x * x) / x.Length);
|
||||
|
||||
|
||||
public static double Correlation(this double[] x, double[] y)
|
||||
{
|
||||
return x.Covariance(y) / (x.StdDiv() * y.StdDiv());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user