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

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

Introduction

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

Prototype

public ChiSquaredDistributionImpl(double df) 

Source Link

Document

Create a Chi-Squared distribution with the given degrees of freedom.

Usage

From source file:org.renjin.Distributions.java

public static double dchisq(final double x, final double df, boolean log) {
    return d(new ChiSquaredDistributionImpl(df), x, log);
}

From source file:org.renjin.Distributions.java

public static double pchisq(final double q, final double df, boolean lowerTail, boolean logP) {
    if (df == 0) {
        return p(new ChisquareZeroDfDistribution(), q, lowerTail, logP);
    } else {//from   www  .j  a v  a 2  s  .c  o m
        return p(new ChiSquaredDistributionImpl(df), q, lowerTail, logP);
    }
}

From source file:org.renjin.Distributions.java

public static double qchisq(final double p, final double df, boolean lowerTail, boolean logP) {
    if (df == 0) {
        return q(new ChisquareZeroDfDistribution(), p, lowerTail, logP);
    } else {//from w  w  w .j a va  2s  . com
        return q(new ChiSquaredDistributionImpl(df), p, lowerTail, logP);
    }
}

From source file:org.renjin.primitives.random.Distributions.java

public static double dchisq(@Recycle double x, @Recycle double df, boolean log) {
    return d(new ChiSquaredDistributionImpl(df), x, log);
}

From source file:org.renjin.primitives.random.Distributions.java

public static double pchisq(@Recycle double q, @Recycle double df, boolean lowerTail, boolean logP) {
    return p(new ChiSquaredDistributionImpl(df), q, lowerTail, logP);
}

From source file:org.renjin.primitives.random.Distributions.java

public static double qchisq(@Recycle double p, @Recycle double df, boolean lowerTail, boolean logP) {
    return q(new ChiSquaredDistributionImpl(df), p, lowerTail, logP);
}

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

@DataParallel
@Internal//  w  w w .  j a  v a  2 s .  co m
public static double dchisq(@Recycle double x, @Recycle double df, boolean log) {
    return d(new ChiSquaredDistributionImpl(df), x, log);
}

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

@DataParallel
@Internal/*from   ww w . ja  v  a 2 s. c  om*/
public static double pchisq(@Recycle double q, @Recycle double df, boolean lowerTail, boolean logP) {
    if (df == 0) {
        return p(new ChisquareZeroDfDistribution(), q, lowerTail, logP);
    } else {
        return p(new ChiSquaredDistributionImpl(df), q, lowerTail, logP);
    }
}

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

@DataParallel
@Internal/*from  ww w  . ja  v  a 2 s .c o m*/
public static double qchisq(@Recycle double p, @Recycle double df, boolean lowerTail, boolean logP) {
    if (df == 0) {
        return q(new ChisquareZeroDfDistribution(), p, lowerTail, logP);
    } else {
        return q(new ChiSquaredDistributionImpl(df), p, lowerTail, logP);
    }
}

From source file:ubic.gemma.analysis.preprocess.batcheffects.BatchConfound.java

/**
 * @param ee//from www .  j a  v a  2s  .  c o  m
 * @param bioMaterialFactorMap
 * @param svdo
 */
private static Collection<BatchConfoundValueObject> factorBatchConfoundTest(ExpressionExperiment ee,
        Map<ExperimentalFactor, Map<Long, Double>> bioMaterialFactorMap) throws IllegalArgumentException {

    Map<Long, Long> batchMembership = new HashMap<Long, Long>();
    ExperimentalFactor batchFactor = null;
    Map<Long, Integer> batchIndexes = new HashMap<Long, Integer>();
    for (ExperimentalFactor ef : bioMaterialFactorMap.keySet()) {
        if (ExperimentalDesignUtils.isBatch(ef)) {
            batchFactor = ef;

            Map<Long, Double> bmToFv = bioMaterialFactorMap.get(batchFactor);

            if (bmToFv == null) {
                log.warn("No biomaterial --> factor value map for batch factor: " + batchFactor);
                continue;
            }

            int index = 0;
            for (FactorValue fv : batchFactor.getFactorValues()) {
                batchIndexes.put(fv.getId(), index++);
            }

            for (Long bmId : bmToFv.keySet()) {
                batchMembership.put(bmId, bmToFv.get(bmId).longValue());
            }
            break;
        }
    }

    HashSet<BatchConfoundValueObject> result = new HashSet<BatchConfoundValueObject>();
    if (batchFactor == null) {
        return result;
    }

    /*
     * Compare other factors to batches to look for confounds.
     */

    for (ExperimentalFactor ef : bioMaterialFactorMap.keySet()) {

        if (ef.equals(batchFactor))
            continue;

        Map<Long, Double> bmToFv = bioMaterialFactorMap.get(ef);
        int numBioMaterials = bmToFv.keySet().size();

        assert numBioMaterials > 0 : "No biomaterials for " + ef;

        double p = Double.NaN;
        double chiSquare = Double.NaN;
        int df;

        int numBatches = batchFactor.getFactorValues().size();
        if (ExperimentalDesignUtils.isContinuous(ef)) {

            DoubleArrayList factorValues = new DoubleArrayList(numBioMaterials);
            factorValues.setSize(numBioMaterials);

            IntArrayList batches = new IntArrayList(numBioMaterials);
            batches.setSize(numBioMaterials);

            int j = 0;
            for (Long bmId : bmToFv.keySet()) {

                assert factorValues.size() > 0 : "Biomaterial to factorValue is empty for " + ef;

                factorValues.set(j, bmToFv.get(bmId));
                long batch = batchMembership.get(bmId);
                batches.set(j, batchIndexes.get(batch));
                j++;
            }

            p = KruskalWallis.test(factorValues, batches);
            df = KruskalWallis.dof(factorValues, batches);
            chiSquare = KruskalWallis.kwStatistic(factorValues, batches);

            log.debug("KWallis\t" + ee.getId() + "\t" + ee.getShortName() + "\t" + ef.getId() + "\t"
                    + ef.getName() + "\t" + String.format("%.2f", chiSquare) + "\t" + df + "\t"
                    + String.format("%.2g", p) + "\t" + numBatches);
        } else {

            Map<Long, Integer> factorValueIndexes = new HashMap<Long, Integer>();
            int index = 0;
            for (FactorValue fv : ef.getFactorValues()) {
                factorValueIndexes.put(fv.getId(), index++);
            }
            Map<Long, Long> factorValueMembership = new HashMap<Long, Long>();

            for (Long bmId : bmToFv.keySet()) {
                factorValueMembership.put(bmId, bmToFv.get(bmId).longValue());
            }

            long[][] counts = new long[numBatches][ef.getFactorValues().size()];

            for (int i = 0; i < batchIndexes.size(); i++) {
                for (int j = 0; j < factorValueIndexes.size(); j++) {
                    counts[i][j] = 0;
                }
            }

            for (Long bm : bmToFv.keySet()) {
                long fv = factorValueMembership.get(bm);
                Long batch = batchMembership.get(bm);
                if (batch == null) {
                    log.warn("No batch membership for : " + bm);
                    continue;
                }
                int batchIndex = batchIndexes.get(batch);
                int factorIndex = factorValueIndexes.get(fv);
                counts[batchIndex][factorIndex]++;
            }

            ChiSquareTest cst = new ChiSquareTestImpl();

            try {
                chiSquare = cst.chiSquare(counts);
            } catch (IllegalArgumentException e) {
                log.warn("IllegalArgumentException exception computing ChiSq for : " + ef + "; Error was: "
                        + e.getMessage());
                chiSquare = Double.NaN;
            }

            df = (counts.length - 1) * (counts[0].length - 1);
            ChiSquaredDistribution distribution = new ChiSquaredDistributionImpl(df);

            if (!Double.isNaN(chiSquare)) {
                try {
                    p = 1.0 - distribution.cumulativeProbability(chiSquare);
                } catch (MathException e) {
                    log.warn("Math exception computing ChiSq probability for " + chiSquare + ": "
                            + e.getMessage());
                    p = Double.NaN;
                }
            }

            log.debug("ChiSq\t" + ee.getId() + "\t" + ee.getShortName() + "\t" + ef.getId() + "\t"
                    + ef.getName() + "\t" + String.format("%.2f", chiSquare) + "\t" + df + "\t"
                    + String.format("%.2g", p) + "\t" + numBatches);
        }

        BatchConfoundValueObject summary = new BatchConfoundValueObject(ee, ef, chiSquare, df, p, numBatches);

        result.add(summary);
    }
    return result;
}