/// <summary> /// Create a truncated Gamma equivalent to a Gamma, i.e. with no truncation. /// </summary> /// <param name="Gamma"></param> public static TruncatedGamma FromGamma(Gamma Gamma) { return(new TruncatedGamma(Gamma)); }
/// <summary> /// Sets this Gamma instance to have the parameter values of that Gamma instance /// </summary> /// <param name="that">That Gamma</param> public void SetTo(Gamma that) { Rate = that.Rate; Shape = that.Shape; }
/// <summary> /// Create a truncated Gamma equivalent to a Gamma, i.e. with no truncation. /// </summary> /// <param name="that"></param> public TruncatedGamma(Gamma that) { this.Gamma = that; LowerBound = 0; UpperBound = Double.PositiveInfinity; }
/// <summary> /// Get the derivatives of the log-pdf at a point. /// </summary> /// <param name="dist"></param> /// <param name="x"></param> /// <param name="dlogp">On exit, the first derivative.</param> /// <param name="ddlogp">On exit, the second derivative.</param> public static void GetDerivatives(Gamma dist, double x, out double dlogp, out double ddlogp) { dist.GetDerivatives(x, out dlogp, out ddlogp); }
/// <summary> /// Construct a Gamma distribution whose pdf has the given derivatives at a point. /// </summary> /// <param name="x">Cannot be negative</param> /// <param name="dLogP">Desired derivative of log-density at x</param> /// <param name="ddLogP">Desired second derivative of log-density at x</param> /// <param name="forceProper">If true and both derivatives cannot be matched by a proper distribution, match only the first.</param> /// <returns></returns> public static Gamma FromDerivatives(double x, double dLogP, double ddLogP, bool forceProper) { if (x < 0) { throw new ArgumentOutOfRangeException(nameof(x), x, "x < 0"); } double a; if (double.IsPositiveInfinity(x)) { if (ddLogP < 0) { return(Gamma.PointMass(x)); } else if (ddLogP == 0) { a = 0.0; } else if (forceProper) { if (dLogP <= 0) { return(Gamma.FromShapeAndRate(1, -dLogP)); } else { return(Gamma.PointMass(x)); } } else { return(Gamma.FromShapeAndRate(-x, -x - dLogP)); } } else { a = -x * x * ddLogP; if (a + 1 > double.MaxValue) { return(Gamma.PointMass(x)); } } if (forceProper) { if (dLogP <= 0) { if (a < 0) { a = 0; } } else { double amin = x * dLogP; if (a < amin) { return(Gamma.FromShapeAndRate(amin + 1, 0)); } } } double b = ((a == 0) ? 0 : (a / x)) - dLogP; if (forceProper) { // correct roundoff errors that might make b negative b = Math.Max(b, 0); } return(Gamma.FromShapeAndRate(a + 1, b)); }
/// <summary> /// Copy constructor. /// </summary> /// <param name="that"></param> public Gamma(Gamma that) { Shape = that.Shape; Rate = that.Rate; }
/// <summary> /// Set the mean and variance to match the moments of a mixture of two Gammas. /// </summary> /// <param name="weight1">The first weight</param> /// <param name="dist1">The first Gamma</param> /// <param name="weight2">The second weight</param> /// <param name="dist2">The second Gamma</param> public void SetToSum(double weight1, Gamma dist1, double weight2, Gamma dist2) { SetTo(Gaussian.WeightedSum <Gamma>(this, weight1, dist1, weight2, dist2)); }
/// <summary> /// Constructs a GammaPower distribution from a Gamma distribution. /// </summary> /// <param name="gamma"></param> /// <param name="power"></param> /// <returns></returns> public static GammaPower FromGamma(Gamma gamma, double power) { return(FromShapeAndRate(gamma.Shape, gamma.Rate, power)); }