Skip to main content

Distributions

The CSharpNumerics.Statistics.Distributions namespace provides probability distributions with a common IDistribution interface.

using CSharpNumerics.Statistics.Distributions;
using CSharpNumerics.Statistics.Random;

πŸ“‹ Available Distributions​

DistributionClassParameters
UniformUniformDistributiona,ba, b
NormalNormalDistributionΞΌ,Οƒ\mu, \sigma
ExponentialExponentialDistributionΞ»\lambda
PoissonPoissonDistributionΞ»\lambda
BernoulliBernoulliDistributionpp
BinomialBinomialDistributionn,pn, p
Student's tStudentTDistributionΞ½\nu (degrees of freedom)
Chi-squaredChiSquaredDistributionkk (degrees of freedom)
FFDistributiond1,d2d_1, d_2 (degrees of freedom)

Every distribution exposes Mean, Variance, StandardDeviation, Pdf(x), Cdf(x), Sample(rng) and Samples(rng, count).


πŸ”” Normal Distribution​

var normal = new NormalDistribution(mu: 100, sigma: 15);

double pdf = normal.Pdf(100); // peak value
double cdf = normal.Cdf(115); // P(X ≀ 115) β‰ˆ 0.8413
double q = normal.InverseCdf(0.95); // z such that P(X ≀ z) = 0.95

var rng = new RandomGenerator(42);
double[] samples = normal.Samples(rng, 10_000);

🎯 Poisson Distribution​

var poisson = new PoissonDistribution(lambda: 3.0);
double pmf = poisson.Pdf(2); // P(X = 2)
double cumul = poisson.Cdf(4); // P(X ≀ 4)

πŸͺ™ Binomial Distribution​

var binomial = new BinomialDistribution(n: 10, p: 0.3);
// PMF sums to 1
double total = Enumerable.Range(0, 11).Sum(k => binomial.Pdf(k));

πŸ“ Student's t, Chi-squared & F Distributions​

Used by the hypothesis testing methods, but can also be used directly:

var t = new StudentTDistribution(degreesOfFreedom: 29);
double pTwoTail = t.TwoTailedPValue(2.045); // two-tailed p-value
double quantile = t.InverseCdf(0.975); // critical value

var chi2 = new ChiSquaredDistribution(degreesOfFreedom: 5);
double pUpper = chi2.UpperTailPValue(11.07); // P(X β‰₯ 11.07)

var f = new FDistribution(d1: 3, d2: 20);
double pF = f.UpperTailPValue(3.10); // P(F β‰₯ 3.10)