2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 13:33:13 +00:00
This commit is contained in:
2023-04-20 15:55:20 +03:00
parent 0ceaee88aa
commit a95b68c610
46 changed files with 2361 additions and 14 deletions

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Analysis.Statistics
{
public static class Distributions
{
public static int Factorial(this int n)
{
var rt = 1;
while (n > 1)
rt *= n--;
return rt;
}
public static int Permutation(int n, int k)
{
var rt = 1;
var l = n - k;
while (n > l)
rt *= n--;
return n;
}
public static int Combination(int n, int k)
{
var rt = 1;
while (n > k)
rt *= n--;
rt /= Factorial(n - k);
return rt;
}
public static Probability Binomial(Probability p, int n, int x)
{
return Combination(n, x) * p.Power(x) * (1 - p.Power(n - x));
}
public static Probability Poisson(double l, int x)
{
return Math.Exp(-l) * Math.Pow(l, x) / Factorial(x);
}
public static Probability Geometric(Probability p, int x)
{
// joint probability of failures and one success
return p.Inverse().Power(x - 1) * p;
}
}
}

View File

@ -12,6 +12,7 @@ namespace Esiur.Analysis.Statistics
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)
@ -27,5 +28,15 @@ namespace Esiur.Analysis.Statistics
{
return (Math.Round(Value * 10000) / 100) + "%";
}
public Probability Power(double exponent)
{
return Math.Pow(Value, exponent);
}
public Probability Inverse()
{
return 1 - Value;
}
}
}

View File

@ -28,8 +28,11 @@ namespace Esiur.Analysis.Statistics
double rt = 0;
for (var i = 0; i < n; i++)
{
rt += (x[i] - X) * (y[i] - Y);
rt += (x[i] * y[i] - Y * x[i] - X * y[i] + X * Y);// - X) * (y[i] - Y);
}
return rt / n;
}