Example usage for org.apache.commons.math.distribution GammaDistributionImpl GammaDistributionImpl

List of usage examples for org.apache.commons.math.distribution GammaDistributionImpl GammaDistributionImpl

Introduction

In this page you can find the example usage for org.apache.commons.math.distribution GammaDistributionImpl GammaDistributionImpl.

Prototype

public GammaDistributionImpl(double alpha, double beta) 

Source Link

Document

Create a new gamma distribution with the given alpha and beta values.

Usage

From source file:org.renjin.stats.internals.Distributions.java

@DataParallel
@Internal/*from  ww w .  java  2s . c  o  m*/
public static double pgamma(@Recycle double q, @Recycle double shape, @Recycle double scale, boolean lowerTail,
        boolean logP) {
    return p(new GammaDistributionImpl(shape, scale), q, lowerTail, logP);
}

From source file:org.renjin.stats.internals.Distributions.java

@DataParallel
@Internal// ww w. jav a 2 s  .  c o  m
public static double qgamma(@Recycle double p, @Recycle double shape, @Recycle double scale, boolean lowerTail,
        boolean logP) {
    return q(new GammaDistributionImpl(shape, scale), p, lowerTail, logP);
}

From source file:timsaddons.modelselection.ModelSwitchOperator.java

@Override
public double proposal() {

    // Uniformly select new model from available models:
    int oldModel = modelNumberInput.get().getValue();
    int newModel;
    do {//from ww  w .ja v a 2s  . c  o m
        newModel = Randomizer.nextInt(nModels);
    } while (newModel == oldModel);

    // Update model number
    modelNumberInput.get().setValue(newModel);

    // Record selection probability of old parameters in HR:
    double logHR = 0;

    for (int i = 0; i < parameterModelIndicesInput.get().getDimension(); i++) {
        int m = parameterModelIndicesInput.get().getValue(i);

        if (m == oldModel) {
            double shape = proposalShapesInput.get().getValue(i);
            double scale = proposalMeansInput.get().getValue(i) / shape;
            double x = parametersInput.get().get(i).getValue();

            logHR += (new GammaDistributionImpl(shape, scale)).density(x);
        }

        if (m == newModel) {
            double shape = proposalShapesInput.get().getValue(i);
            double scale = proposalMeansInput.get().getValue(i) / shape;
            double xprime = Randomizer.nextGamma(shape, 1.0 / scale);

            logHR -= (new GammaDistributionImpl(shape, scale)).density(xprime);

            parametersInput.get().get(i).setValue(xprime);
        }
    }

    return logHR;
}

From source file:timsaddons.modelselection.ModelSwitchOperator.java

/**
 * Main for debugging.//w w  w.  j  a  v a 2s. co  m
 * 
 * @param args 
 */
public static void main(String[] args) throws FileNotFoundException {

    // Check that RNG is working as expected
    PrintStream outf = new PrintStream("rngtest.txt");
    for (int i = 0; i < 10000; i++) {
        outf.println(Randomizer.nextGamma(2.0, 3.0));
    }
    outf.close();

    // Check that Gamma distribution PDF is behaving as expected:
    outf = new PrintStream("pdftest.txt");
    outf.println("x p");
    for (double x = 0; x < 10; x += 0.01) {
        outf.println(x + " " + (new GammaDistributionImpl(2.0, 1.0 / 3.0)).density(x));
    }
    outf.close();
}

From source file:yabby.evolution.sitemodel.SiteModel.java

/**
 * discretisation of gamma distribution with equal proportions in each
 * category/*w  ww .j a  v a2  s. c  om*/
 * @param node
 */
protected void calculateCategoryRates(final Node node) {
    double propVariable = 1.0;
    int cat = 0;

    if (/*invarParameter != null && */invarParameter.getValue() > 0) {
        if (hasPropInvariantCategory) {
            categoryRates[0] = 0.0;
            categoryProportions[0] = invarParameter.getValue();
        }
        propVariable = 1.0 - invarParameter.getValue();
        if (hasPropInvariantCategory) {
            cat = 1;
        }
    }

    if (shapeParameter != null) {

        final double a = shapeParameter.getValue();
        double mean = 0.0;
        final int gammaCatCount = categoryCount - cat;

        final GammaDistribution g = new GammaDistributionImpl(a, 1.0 / a);
        for (int i = 0; i < gammaCatCount; i++) {
            try {
                // RRB: alternative implementation that seems equally good in
                // the first 5 significant digits, but uses a standard distribution object
                if (useBeast1StyleGamma) {
                    categoryRates[i + cat] = GammaDistributionQuantile((2.0 * i + 1.0) / (2.0 * gammaCatCount),
                            a, 1.0 / a);
                } else {
                    categoryRates[i + cat] = g
                            .inverseCumulativeProbability((2.0 * i + 1.0) / (2.0 * gammaCatCount));
                }

            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("Something went wrong with the gamma distribution calculation");
                System.exit(-1);
            }
            mean += categoryRates[i + cat];

            categoryProportions[i + cat] = propVariable / gammaCatCount;
        }

        mean = (propVariable * mean) / gammaCatCount;

        for (int i = 0; i < gammaCatCount; i++) {

            categoryRates[i + cat] /= mean;
        }
    } else {
        categoryRates[cat] = 1.0 / propVariable;
        categoryProportions[cat] = propVariable;
    }

    ratesKnown = true;
}