/* * minValue: es el minimo de personas que llegan al andén * maxValue: es el máximo de personas que llegan al andén * lambda: es el deltaTiempo, intervalo de tiempo (en horas) * */ public static int Gamma(int minValue, int maxValue, double lambda) { double r2, m, x, y, fx, k; if ((minValue >= maxValue) || (minValue < 0) || (maxValue < 0)) { throw new System.ArgumentException("Valores incorrectos"); } k = 9.0; GammaDistribution f = new GammaDistribution(k, lambda); m = (1 / lambda) * (k - 1); do { x = Rand(); r2 = Rand(); y = m * r2; fx = f.ProbabilityDensity(x); } while (y < fx); return Convert.ToInt32(minValue + (maxValue - minValue) * x); }
/// <summary> /// Initializes a new χ<sup>2</sup> distribution. /// </summary> /// <param name="nu">The number of degrees of freedom, which must be positive.</param> public ChiSquaredDistribution(double nu) { if (nu < 1.0) { throw new ArgumentOutOfRangeException(nameof(nu)); } this.nu = nu; this.gamma = new GammaDistribution(nu / 2.0); }
/// <summary> /// Initializes a new χ<sup>2</sup> distribution. /// </summary> /// <param name="nu">The number of degrees of freedom, which must be positive.</param> public ChiSquaredDistribution(int nu) { if (nu < 1) { throw new ArgumentOutOfRangeException("nu"); } this.nu = nu; this.gamma = new GammaDistribution(nu / 2.0); }
public void GammaFitTest() { double k0 = 0.05; double t0 = 2.0; GammaDistribution G = new GammaDistribution(k0, t0); Random rng = new Random(1); Sample S = new Sample(); for (int i = 0; i < 1000; i++) { S.Add(G.GetRandomValue(rng)); } Console.WriteLine("mean sample={0} distribution={1}", S.Mean, G.Mean); Console.WriteLine("variance sample={0} distribution={1}", S.Variance, G.Variance); Console.WriteLine("moment estimate k={0} t={1}", S.Mean * S.Mean / S.Variance, S.Variance / S.Mean); double q = 0; foreach (double x in S) { q += Math.Log(x / S.Mean); } q /= S.Count; q = -q; Console.WriteLine("q = {0}, Log(k)-Psi(k)={1}", q, Math.Log(k0) - AdvancedMath.Psi(k0)); double ke0 = S.Mean * S.Mean / S.Variance; Console.WriteLine("ke0={0} Log(ke0)-Psi(ke0)={1}", ke0, Math.Log(ke0) - AdvancedMath.Psi(ke0)); double ke1 = 1.0 / (2.0 * q - 2.0 / 3.0 * q * q + 4.0 / 9.0 * q * q * q - 14.0 / 135.0 * q * q * q * q); Console.WriteLine("ke1={0} Log(ke1)-Psi(ke1)={1}", ke1, Math.Log(ke1) - AdvancedMath.Psi(ke1)); double ke2 = 1.0 / (q - AdvancedMath.EulerGamma); Console.WriteLine("ke2={0} Log(ke2)-Psi(ke2)={1}", ke2, Math.Log(ke2) - AdvancedMath.Psi(ke2)); }
public void GammaFromExponential() { // test that x_1 + x_2 + ... + x_n ~ Gamma(n) when z ~ Exponential() Random rng = new Random(1); ExponentialDistribution eDistribution = new ExponentialDistribution(); // pick some low values of n so distribution is not simply normal foreach (int n in new int[] { 2, 3, 4, 5 }) { Sample gSample = new Sample(); for (int i = 0; i < 100; i++) { double sum = 0.0; for (int j = 0; j < n; j++) { sum += eDistribution.GetRandomValue(rng); } gSample.Add(sum); } GammaDistribution gDistribution = new GammaDistribution(n); TestResult result = gSample.KolmogorovSmirnovTest(gDistribution); Assert.IsTrue(result.LeftProbability < 0.95); } }
public void CumulantTest() { Distribution d = new GammaDistribution(2.0); double[] K = new double[8]; K[0] = 0.0; for (int r = 1; r < K.Length; r++) { K[r] = 2.0 * AdvancedIntegerMath.Factorial(r - 1); } double[] M = MomentMath.CumulantToRaw(K); for (int r = 0; r < M.Length; r++) { Console.WriteLine("{0} {1}", M[r], d.Moment(r)); } Console.WriteLine("---"); double[] C = MomentMath.CumulantToCentral(K); for (int r = 0; r < C.Length; r++) { Console.WriteLine("{0} {1}", C[r], d.MomentAboutMean(r)); } Console.WriteLine("---"); double[] KP = MomentMath.RawToCumulant(M); for (int r = 0; r < KP.Length; r++) { Console.WriteLine("{0} {1}", KP[r], K[r]); } }
public void GammaFitUncertainty() { // check that the uncertainty in reported fit parameters is actually meaningful // it should be the standard deviation of fit parameter values in a sample of many fits // define a population distribution Distribution distribution = new GammaDistribution(1.5, 2.0); // draw a lot of samples from it; fit each sample and // record the reported parameter value and error of each BivariateSample values = new BivariateSample(); BivariateSample uncertainties = new BivariateSample(); for (int i = 0; i < 100; i++) { Sample sample = CreateSample(distribution, 50, i); FitResult fit = GammaDistribution.FitToSample(sample); UncertainValue a = fit.Parameter(0); UncertainValue b = fit.Parameter(1); values.Add(a.Value, b.Value); uncertainties.Add(a.Uncertainty, b.Uncertainty); } // the reported errors should agree with the standard deviation of the reported parameters Assert.IsTrue(values.X.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(uncertainties.X.Mean)); Assert.IsTrue(values.Y.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(uncertainties.Y.Mean)); }
public void GammaFit() { // Create a gamma distribution GammaDistribution B = new GammaDistribution(2.75, 2.0); // Sample from it Sample S = CreateSample(B, 100); // Fit the sample to a gamma distribution // The fit should agree with the population and the fit should be good FitResult R = GammaDistribution.FitToSample(S); Assert.IsTrue(R.Parameter(0).ConfidenceInterval(0.95).ClosedContains(B.ShapeParameter)); Assert.IsTrue(R.Parameter(1).ConfidenceInterval(0.95).ClosedContains(B.ScaleParameter)); Assert.IsTrue(R.GoodnessOfFit.LeftProbability < 0.95); }
/// <summary> /// Initializes a new χ<sup>2</sup> distribution. /// </summary> /// <param name="nu">The number of degrees of freedom, which must be positive.</param> public ChiSquaredDistribution(int nu) { if (nu < 1) throw new ArgumentOutOfRangeException("nu"); this.nu = nu; this.gamma = new GammaDistribution(nu / 2.0); }
public void TimeGammaGenerators() { double alpha = 1.0; Random rng = new Random(1); //IDeviateGenerator nRng = new AhrensDieterGammaGenerator(alpha); IDeviateGenerator nRng = new MarsagliaTsangGammaGenerator(new PolarRejectionNormalDeviateGenerator(), alpha); Distribution d = new GammaDistribution(alpha); //double sum = 0.0; Sample sample = new Sample(); Stopwatch timer = Stopwatch.StartNew(); for (int i = 0; i < 1000000; i++) { //double x = nRng.GetNext(rng); double x = d.InverseLeftProbability(rng.NextDouble()); //sum += x; sample.Add(x); } timer.Stop(); Console.WriteLine(sample.KolmogorovSmirnovTest(d).RightProbability); //Console.WriteLine(sum); Console.WriteLine(timer.ElapsedMilliseconds); }