/** * <p>Generates a random value from the * {@link org.apache.commons.math3.distribution.GammaDistribution Gamma Distribution}.</p> * * <p>This implementation uses the following algorithms: </p> * * <p>For 0 < shape < 1: <br/> * Ahrens, J. H. and Dieter, U., <i>Computer methods for * sampling from gamma, beta, Poisson and binomial distributions.</i> * Computing, 12, 223-246, 1974.</p> * * <p>For shape >= 1: <br/> * Marsaglia and Tsang, <i>A Simple Method for Generating * Gamma Variables.</i> ACM Transactions on Mathematical Software, * Volume 26 Issue 3, September, 2000.</p> * * @param shape the median of the Gamma distribution * @param scale the scale parameter of the Gamma distribution * @return random value sampled from the Gamma(shape, scale) distribution * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ public double nextGamma(double shape, double scale) throws NotStrictlyPositiveException { return new GammaDistribution(getRandomGenerator(),shape, scale, GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** {@inheritDoc} */ public double cumulativeProbability(double x) { return gamma.cumulativeProbability(x); }
/** * Access the number of degrees of freedom. * * @return the degrees of freedom. */ public double getDegreesOfFreedom() { return gamma.getShape() * 2.0; }
/** * Create a Chi-Squared distribution with the given degrees of freedom and * inverse cumulative probability accuracy. * * @param rng Random number generator. * @param degreesOfFreedom Degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @since 3.1 */ public ChiSquaredDistribution(RandomGenerator rng, double degreesOfFreedom, double inverseCumAccuracy) { super(rng); gamma = new GammaDistribution(degreesOfFreedom / 2, 2); solverAbsoluteAccuracy = inverseCumAccuracy; }
static protected double gammaTestP(float ixy, int binx, int biny, int count) { double shapePar = (binx - 1) * (biny - 1) / 2d; double scalePar = 1d / count; try { GammaDistribution gammaDist = new GammaDistribution(shapePar, scalePar); return 1 - gammaDist.cumulativeProbability(ixy); } catch (Exception ex) { return 1; } } }
@Test public void logProbMatchesKnownLogDensityOfVector() { GammaDistribution distribution = new GammaDistribution(5.0, 1.0); double expectedLogDensity = distribution.logDensity(1) + distribution.logDensity(3); GammaVertex tensorGamma = new GammaVertex(1., 5.); ProbabilisticDoubleTensorContract.matchesKnownLogDensityOfVector(tensorGamma, new double[]{1., 3.}, expectedLogDensity); }
static protected boolean gammaTest(float ixy, int binx, int biny, int count, float pvalue) { double shapePar = (binx - 1) * (biny - 1) / 2d; double scalePar = 1d / count; try { GammaDistribution gammaDist = new GammaDistribution(shapePar, scalePar); double c = gammaDist.inverseCumulativeProbability(1 - pvalue); return ixy <= c; } catch (Exception ex) { return true; } }
/** * @param param * parameter beta * @param param2 * parameter alpha * @return gamma distribution */ protected GammaDistribution getGammaDistribution(double param, double param2) { if (gamma == null || gamma.getBeta() != param2 || gamma.getAlpha() != param) { gamma = new GammaDistribution(param, param2); } return gamma; }
public OnlineLDAModel(int K, float alpha, float eta, long D, double tau0, double kappa, double delta) { super(K); if (tau0 < 0.d) { throw new IllegalArgumentException("tau0 MUST be positive: " + tau0); } if (kappa <= 0.5 || 1.d < kappa) { throw new IllegalArgumentException("kappa MUST be in (0.5, 1.0]: " + kappa); } this._alpha = alpha; this._eta = eta; this._D = D; this._tau0 = tau0; this._kappa = kappa; this._delta = delta; this._isAutoD = (_D <= 0L); // initialize a random number generator this._gd = new GammaDistribution(SHAPE, SCALE); _gd.reseedRandomGenerator(1001); // initialize the parameters this._lambda = new HashMap<String, float[]>(100); }
@Override public double sample() { return numGen.sample(); }
/** {@inheritDoc} **/ @Override public double logDensity(double x) { return gamma.logDensity(x); }
/** {@inheritDoc} */ public double density(double x) { return gamma.density(x); }
/** * Instantiates a new Gamma pseudo random number generator. * * @param shape the shape * @param scale the scale */ public GammaDistr(int shape, double scale) { numGen = new GammaDistribution(shape, scale); }
protected static double getPValue(final double chiSquare, double df) { GammaDistribution gamma = new GammaDistribution(df / 2.0D, 2.0D); double gammaVal = gamma.cumulativeProbability(chiSquare); return 1 - gammaVal; }
@Test public void logProbMatchesKnownLogDensityOfScalar() { GammaDistribution distribution = new GammaDistribution(1.0, 1.5); GammaVertex tensorGamma = new GammaVertex(1.5, 1.0); double expectedDensity = distribution.logDensity(0.5); ProbabilisticDoubleTensorContract.matchesKnownLogDensityOfScalar(tensorGamma, 0.5, expectedDensity); }
@Nonnull public static float[] newRandomFloatArray(@Nonnegative final int size, @Nonnull final GammaDistribution gd) { final float[] ret = new float[size]; for (int i = 0; i < size; i++) { ret[i] = (float) gd.sample(); } return ret; }
/** {@inheritDoc} **/ @Override public double logDensity(double x) { return gamma.logDensity(x); }
/** {@inheritDoc} */ public double density(double x) { return gamma.density(x); }