public void BetaInversion() { // test that beta distribution is accurately inverted over a wide range of a, b, P foreach (double a in TestUtilities.GenerateRealValues(0.01, 100.0, 8)) { foreach (double b in cs) { BetaDistribution B = new BetaDistribution(a, b); foreach (double P in probabilities) { Console.WriteLine("a={0} b={1} P={2}", a, b, P); double x = B.InverseLeftProbability(P); Console.WriteLine(" x={0} P(x)={1}", x, B.LeftProbability(x)); // we would like to test that P(x) = P, but floating point limitations prevent us from meeting this standard // P(x) changes so fast at extremes that sometimes even the minimal change in x causes a change // in P(x) larger than our target precision; so instead we test that our routine gets us // as close as it can, by checking that P(x-e) < P < P(x+e) double Px = B.LeftProbability(x); double Pxm = B.LeftProbability(Math.Min(0.0, x * (1.0 - TestUtilities.TargetPrecision))); double Pxp = B.LeftProbability(Math.Max(x * (1.0 + TestUtilities.TargetPrecision), 1.0)); Assert.IsTrue((Pxm <= P) && (P <= Pxp)); } } } }
/// <inheritdoc /> public override double InverseLeftProbability(double P) { if ((P < 0.0) || (P > 1.0)) { throw new ArgumentOutOfRangeException(nameof(P)); } BetaDistribution b = new BetaDistribution(nu / 2.0, 1.0 / 2.0); if (P <= 0.5) { double x = b.InverseLeftProbability(2.0 * P); return(-Math.Sqrt(nu * (1.0 - x) / x)); } else { double Q = 1.0 - P; double x = b.InverseLeftProbability(2.0 * Q); return(Math.Sqrt(nu * (1.0 - x) / x)); } }
public void TransformedBetaMoments() { // For x ~ B(a,b), kth cumulant of \ln\left(\frac{x}{1-x}\right) = \psi^{(k-1)}(a) \pm \psi^{(k-1)}(b) BetaDistribution D = new BetaDistribution(2.0, 3.0); double m = D.ExpectationValue(x => Math.Log(x / (1.0 - x))); Assert.IsTrue(TestUtilities.IsNearlyEqual(m, AdvancedMath.Psi(D.Alpha) - AdvancedMath.Psi(D.Beta))); double c2 = D.ExpectationValue(x => MoreMath.Pow(Math.Log(x / (1.0 - x)) - m, 2)); Assert.IsTrue(TestUtilities.IsNearlyEqual(c2, AdvancedMath.Psi(1, D.Alpha) + AdvancedMath.Psi(1, D.Beta))); double c3 = D.ExpectationValue(x => MoreMath.Pow(Math.Log(x / (1.0 - x)) - m, 3)); Assert.IsTrue(TestUtilities.IsNearlyEqual(c3, AdvancedMath.Psi(2, D.Alpha) - AdvancedMath.Psi(2, D.Beta))); double c4 = D.ExpectationValue(x => MoreMath.Pow(Math.Log(x / (1.0 - x)) - m, 4)); Assert.IsTrue(TestUtilities.IsNearlyEqual(c4 - 3.0 * c2 * c2, AdvancedMath.Psi(3, D.Alpha) + AdvancedMath.Psi(3, D.Beta))); }
public void Repro() { double I = 0.9998063099306; double a = 0.00034509911609819255; double b = 6.8453983996634218; var bd = new BetaDistribution(a, b); double x = bd.InverseLeftProbability(I); Console.WriteLine(x); Console.WriteLine(bd.LeftProbability(x)); Console.WriteLine(I); }
public void BetaExercise() { double[] c = new double[] { 1.0 / 32768.0, 1.0 / 1024.0, 1.0 / 64.0, 1.0 / 8.0, 1.0 / 2.0, 1.0, 2.0, 8.0, 64.0, 1024.0, 32768.0 }; double[] p = new double[] { 1.0 / 65536.0, 1.0 / 256.0, 1.0 / 16.0, 1.0 / 4.0, 1.0 - 1.0 / 4.0, 1.0 - 1.0 / 16.0, 1.0 - 1.0 / 256.0 }; Stopwatch s = Stopwatch.StartNew(); double sum = 0.0; foreach (double a in c) { foreach (double b in c) { BetaDistribution BD = new BetaDistribution(a, b); foreach (double x in p) { //Console.WriteLine("a={0} b={1} P={2}", a, b, x); //Console.WriteLine(BD.InverseLeftProbability(x)); sum += BD.InverseLeftProbability(x); } } } s.Stop(); Console.WriteLine(sum); Console.WriteLine(s.ElapsedMilliseconds); }
public void BetaFitUncertainty() { // 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 BetaDistribution(1.0 / 3.0, 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 < 50; i++) { Sample sample = CreateSample(distribution, 10, i); FitResult fit = BetaDistribution.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 BetaFit() { // Create a very non-normal Beta distribution BetaDistribution B = new BetaDistribution(0.25, 3.0); // Sample from it Sample S = CreateSample(B, 128); // Fit the sample to a beta distribution // The fit should agree with the population and the fit should be good FitResult RB = BetaDistribution.FitToSample(S); Assert.IsTrue(RB.Parameter(0).ConfidenceInterval(0.99).ClosedContains(B.Alpha)); Assert.IsTrue(RB.Parameter(1).ConfidenceInterval(0.99).ClosedContains(B.Beta)); Assert.IsTrue(RB.GoodnessOfFit.LeftProbability < 0.99); // Fit to a normal distribution should be bad FitResult RN = NormalDistribution.FitToSample(S); Assert.IsTrue(RN.GoodnessOfFit.LeftProbability > 0.99); }
public void Bug7788() { // Beta with high parameters used to have incorrect inverse probabilty, // which in this case resulted in zero median and consistently zero random values. Distribution beta = new BetaDistribution(1713.0, 58743.0); Console.WriteLine(beta.Mean); Console.WriteLine(beta.StandardDeviation); Assert.IsTrue(beta.Median != 0.0); Assert.IsTrue(Math.Abs(beta.Median - beta.Median) <= beta.StandardDeviation); Random rng = new Random(1); for (int i = 0; i < 10; i++) { Assert.IsTrue(beta.GetRandomValue(rng) != 0.0); } }
public void Bug7684() { // These values caused incomplete Beta to be called with argument outside the interval [0,1]. double I1 = 0.9999902; double a1 = 0.0000434313636267175; double b1 = 18474.36071078790000; BetaDistribution D1 = new BetaDistribution(a1, b1); double x1 = D1.InverseLeftProbability(I1); Console.WriteLine("{0} {1} {2}", x1, D1.LeftProbability(x1), I1); double I2 = 0.9998063099306; double a2 = 0.00034509911609819255; double b2 = 6.8453983996634218; BetaDistribution D2 = new BetaDistribution(a2, b2); double x2 = D2.InverseLeftProbability(I2); Console.WriteLine("{0} {1} {2}", x2, D2.LeftProbability(x2), I2); }
/// <inheritdoc /> public override double InverseLeftProbability(double P) { if ((P < 0.0) || (P > 1.0)) throw new ArgumentOutOfRangeException("P"); BetaDistribution b = new BetaDistribution(nu / 2.0, 1.0 / 2.0); if (P <= 0.5) { double x = b.InverseLeftProbability(2.0 * P); return (-Math.Sqrt(nu * (1.0 - x) / x)); } else { double Q = 1.0 - P; double x = b.InverseLeftProbability(2.0 * Q); return (Math.Sqrt(nu * (1.0 - x) / x)); } }