/** * 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; }
/** * <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(); }
@Override protected void setup() throws LibrecException { super.setup(); // get hyper-parameters a = conf.getDouble("rec.a", 0.3); aPrime = conf.getDouble("rec.a.prime", 0.3); bPrime = conf.getDouble("rec.b.prime", 1.0); c = conf.getDouble("rec.c", 0.3); cPrime = conf.getDouble("rec.c.prime", 0.3); dPrime = conf.getDouble("rec.d.prime", 1.0); // init user factors GammaDistribution userGammaDis = new GammaDistribution(a, 1/bPrime); for (int u=0; u<numUsers; u++) { for (int k=0; k<numFactors; k++) userFactors.set(u, k, userGammaDis.sample()); } // init item factors GammaDistribution itemGammaDis = new GammaDistribution(c, 1/dPrime); for (int i=0; i<numItems; i++) { for (int k=0; k<numFactors; k++) itemFactors.set(i, k, itemGammaDis.sample()); } }
GammaDistribution gammaPre = new GammaDistribution(aPrime, bPrime/aPrime); DenseMatrix gammaRte = new DenseMatrix(numUsers, numFactors); for (int u=0; u<numUsers; u++) { GammaDistribution lambdaPre = new GammaDistribution(cPrime, dPrime/cPrime); DenseMatrix lambdaRte = new DenseMatrix(numItems, numFactors); for (int i=0; i<numItems; i++) {
/** * Instantiates a new Gamma pseudo random number generator. * * @param seed the seed * @param shape the shape * @param scale the scale */ public GammaDistr(long seed, int shape, double scale) { super(new GammaDistribution(shape, scale), seed); }
/** * 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); }
/** * 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; }
/** * @param shape The shape parameter. * @param scale The scale parameter. * @return Ibid. */ public double nextGamma(double shape, double scale) { return new GammaDistribution(randomGenerator, shape, scale).sample(); }
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; } }
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; } } }
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; }
@Override public Object doWork(Object first, Object second) throws IOException{ if(null == first){ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the first value",toExpression(constructingFactory))); } if(null == second){ throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory))); } Number shape = (Number)first; Number scale = (Number)second; return new GammaDistribution(shape.doubleValue(), scale.doubleValue()); } }
/** * @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 static double[] staticDraw(double[] alpha) { double dist[] = new double[alpha.length]; // For each dimension, draw a sample from Gamma(mp_i, 1). for (int i = 0; i < dist.length; i++) { GammaDistribution gammaDist = new GammaDistribution(rng, alpha[i], 1, GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); dist[i] = gammaDist.sample(); if (dist[i] <= 0) { dist[i] = EPSILON; } } // Normalize the distribution. Multinomials.normalizeProps(dist); return dist; }
public RealDistribution getRealDistribution(Context context) { RandomGenerator randomGenerator = RandomUtil.getInstance().getRandomGenerator(); double e1 = getExpressions().get(0).evaluate(context); double e2 = getExpressions().get(1).evaluate(context); return new GammaDistribution(randomGenerator, e1, e2); } };
public double evaluate(Context context) { RandomGenerator randomGenerator = RandomUtil.getInstance().getRandomGenerator(); double e1 = getExpressions().get(0).evaluate(context); double e2 = getExpressions().get(1).evaluate(context); final GammaDistribution distribution = new GammaDistribution(randomGenerator, e1, e2); return distribution.sample(); }
@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); }
@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); }
@Test public void logProbGraphMatchesKnownLogDensityOfScalar() { DoubleVertex theta = ConstantVertex.of(1.5); DoubleVertex k = ConstantVertex.of(1.); GammaVertex tensorGamma = new GammaVertex(theta, k); LogProbGraph logProbGraph = tensorGamma.logProbGraph(); LogProbGraphValueFeeder.feedValue(logProbGraph, theta, theta.getValue()); LogProbGraphValueFeeder.feedValue(logProbGraph, k, k.getValue()); LogProbGraphValueFeeder.feedValue(logProbGraph, tensorGamma, DoubleTensor.scalar(0.5)); GammaDistribution distribution = new GammaDistribution(1., 1.5); double expectedDensity = distribution.logDensity(0.5); LogProbGraphContract.matchesKnownLogDensity(logProbGraph, expectedDensity); }
@Test public void logProbGraphMatchesKnownLogDensityOfVector() { DoubleVertex theta = ConstantVertex.of(1.0, 1.0); DoubleVertex k = ConstantVertex.of(5., 5.); GammaVertex tensorGamma = new GammaVertex(theta, k); LogProbGraph logProbGraph = tensorGamma.logProbGraph(); LogProbGraphValueFeeder.feedValue(logProbGraph, theta, theta.getValue()); LogProbGraphValueFeeder.feedValue(logProbGraph, k, k.getValue()); LogProbGraphValueFeeder.feedValue(logProbGraph, tensorGamma, DoubleTensor.create(1., 3.)); GammaDistribution distribution = new GammaDistribution(5., 1.); double expectedDensity = distribution.logDensity(1.) + distribution.logDensity(3.); LogProbGraphContract.matchesKnownLogDensity(logProbGraph, expectedDensity); }