mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 13:33:13 +00:00
eBook
This commit is contained in:
55
Esiur.Analysis/Statistics/Distributions.cs
Normal file
55
Esiur.Analysis/Statistics/Distributions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user