private static double GetGaussianQuantile(double probability, double x0, double p0, double x1, double p1) { double mean, stddev; GetGaussianFromQuantiles(x0, p0, x1, p1, out mean, out stddev); return(MMath.NormalCdfInv(probability) * stddev + mean); }
/// <summary> /// Returns the value x such that GetProbLessThan(x) == probability. /// </summary> /// <param name="probability">A real number in [0,1].</param> /// <returns></returns> public double GetQuantile(double probability) { if (probability < 0) { throw new ArgumentOutOfRangeException("probability < 0"); } if (probability > 1) { throw new ArgumentOutOfRangeException("probability > 1"); } if (this.IsPointMass) { return((probability == 1.0) ? MMath.NextDouble(this.Point) : this.Point); } else if (Precision <= 0.0) { throw new ImproperDistributionException(this); } else { double mean = MeanTimesPrecision / Precision; double sqrtPrec = Math.Sqrt(Precision); return(MMath.NormalCdfInv(probability) / sqrtPrec + mean); } }
public static int Histogramize(double value, double mean, double deviation, int numberOfBins) { for (int ii = 1; ii <= numberOfBins; ii++) { double above = MMath.NormalCdfInv(((double)ii) / ((double)numberOfBins)); if (((value - mean) / deviation) < above) { return(ii - 1); } } return(numberOfBins - 1); }
private static void GetGaussianFromQuantiles(double x0, double p0, double x1, double p1, out double mean, out double deviation) { double z0 = MMath.NormalCdfInv(p0); double z1 = MMath.NormalCdfInv(p1); // solve for the equivalent Gaussian mean and stddev Matrix Z = new Matrix(new double[, ] { { 1, z0 }, { 1, z1 } }); DenseVector X = DenseVector.FromArray(x0, x1); DenseVector A = DenseVector.Zero(2); A.SetToLeastSquares(X, Z); mean = A[0]; deviation = A[1]; }
/// <summary> /// Returns the value x such that GetProbLessThan(x) == probability. /// </summary> /// <param name="probability">A real number in [0,1].</param> /// <returns></returns> public double GetQuantile(double probability) { if (this.IsPointMass) { return((probability == 1.0) ? (this.Point + MMath.Ulp(this.Point)) : this.Point); } else if (Precision <= 0.0) { throw new ImproperDistributionException(this); } else { double mean = MeanTimesPrecision / Precision; double sqrtPrec = Math.Sqrt(Precision); return(MMath.NormalCdfInv(probability) / sqrtPrec + mean); } }