public static double Sample(Gamma gamma, double lowerBound, double upperBound) { if (gamma.IsUniform()) { return(Rand.UniformBetween(lowerBound, upperBound)); } bool useQuantile = (gamma.Shape == 1); if (useQuantile) { return(GetQuantile(gamma, lowerBound, upperBound, Rand.UniformBetween(0, 1))); } else { double sample; do { sample = gamma.Sample(); } while (sample < lowerBound || sample > upperBound); return(sample); } }
/// <summary> /// Asks whether this instance is uniform. If the upper and lower bounds are finite the distribution /// is not uniform. /// </summary> /// <returns>True if uniform, false otherwise</returns> public bool IsUniform() { return(Gamma.IsUniform() && (LowerBound == 0) && double.IsPositiveInfinity(UpperBound)); }