Example usage for org.apache.commons.math3.distribution FDistribution FDistribution

List of usage examples for org.apache.commons.math3.distribution FDistribution FDistribution

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution FDistribution FDistribution.

Prototype

public FDistribution(double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom)
        throws NotStrictlyPositiveException 

Source Link

Document

Creates an F distribution using the given degrees of freedom.

Usage

From source file:dase.timeseries.analysis.GrangerTest.java

/**
 * Returns p-value for Granger causality test.
 *
 * @param y/*  w  ww  . j  a  v  a 2 s  .  c o m*/
 *            - predictable variable
 * @param x
 *            - predictor
 * @param L
 *            - lag, should be 1 or greater.
 * @return p-value of Granger causality
 */
public static double granger(double[] y, double[] x, int L) {
    OLSMultipleLinearRegression h0 = new OLSMultipleLinearRegression();
    OLSMultipleLinearRegression h1 = new OLSMultipleLinearRegression();

    double[][] laggedY = createLaggedSide(L, y);

    double[][] laggedXY = createLaggedSide(L, x, y);

    int n = laggedY.length;

    h0.newSampleData(strip(L, y), laggedY);
    h1.newSampleData(strip(L, y), laggedXY);

    double rs0[] = h0.estimateResiduals();
    double rs1[] = h1.estimateResiduals();

    double RSS0 = sqrSum(rs0);
    double RSS1 = sqrSum(rs1);

    double ftest = ((RSS0 - RSS1) / L) / (RSS1 / (n - 2 * L - 1));

    System.out.println(RSS0 + " " + RSS1);
    System.out.println("F-test " + ftest);

    FDistribution fDist = new FDistribution(L, n - 2 * L - 1);

    double pValue = 1.0 - fDist.cumulativeProbability(ftest);
    System.out.println("P-value " + pValue);
    return pValue;
}

From source file:com.itemanalysis.psychometrics.reliability.AbstractScoreReliability.java

public double[] confidenceInterval() {
    double numberOfExaminees = matrix.getMaxSampleSize();
    double[] confidenceInterval = new double[2];
    double numberOfItems = (double) nItems;
    double df1 = numberOfExaminees - 1.0;
    double df2 = (numberOfExaminees - 1.0) * (numberOfItems - 1.0);
    FDistribution fDist = new FDistribution(df1, df2);
    try {//  w  w  w . j  av a  2 s. c  o m
        confidenceInterval[0] = 1.0 - ((1.0 - this.value()) * fDist.inverseCumulativeProbability(0.975));
        confidenceInterval[1] = 1.0 - ((1.0 - this.value()) * fDist.inverseCumulativeProbability(0.025));
    } catch (Exception ex) {
        confidenceInterval[0] = Double.NaN;
        confidenceInterval[1] = Double.NaN;
    }
    return confidenceInterval;
}

From source file:com.itemanalysis.psychometrics.reliability.ReliabilityInterval.java

public double[] confidenceInterval() {
    double N = sampleSize;
    double nI = numberOfVariables;
    double df1 = N - 1.0;
    double df2 = (N - 1.0) * (nI - 1.0);
    double[] ci = new double[2];
    FDistribution fDist = new FDistribution(df1, df2);
    try {/*from  w w w  .j  a  v a 2  s .  c  o m*/
        ci[0] = 1.0 - ((1.0 - reliability.value()) * fDist.inverseCumulativeProbability(0.975));
        ci[1] = 1.0 - ((1.0 - reliability.value()) * fDist.inverseCumulativeProbability(0.025));
    } catch (Exception ex) {
        ci[0] = Double.NaN;
        ci[1] = Double.NaN;
    }

    return ci;
}

From source file:com.rapidminer.operator.learner.functions.linear.IterativeTTestLinearRegressionMethod.java

@Override
public LinearRegressionResult applyMethod(LinearRegression regression, boolean useBias, double ridge,
        ExampleSet exampleSet, boolean[] isUsedAttribute, int numberOfExamples, int numberOfUsedAttributes,
        double[] means, double labelMean, double[] standardDeviations, double labelStandardDeviation,
        double[] coefficientsOnFullData, double errorOnFullData)
        throws UndefinedParameterError, ProcessStoppedException {
    int maxIterations = regression.getParameterAsInt(PARAMETER_MAX_ITERATIONS);
    double alphaForward = regression.getParameterAsDouble(PARAMETER_FORWARD_SELECTION_THRESHOLD);
    double alphaBackward = regression.getParameterAsDouble(PARAMETER_BACKWARD_SELECTION_THRESHOLD);

    FDistribution fdistribution;/*from w  w w. ja  v a 2 s .  c om*/
    // check if the F-distribution can be calculated
    int secondDegreeOfFreedom = exampleSet.size() - coefficientsOnFullData.length;
    if (secondDegreeOfFreedom > 0) {
        fdistribution = new FDistribution(1, secondDegreeOfFreedom);
    } else {
        fdistribution = null;
    }

    double generalCorrelation = regression.getCorrelation(exampleSet, isUsedAttribute, coefficientsOnFullData,
            useBias);
    generalCorrelation *= generalCorrelation;

    // building data structures
    boolean[] isAllowedToUse = isUsedAttribute;
    // initialize array for checking for change
    boolean[] isLastRoundUsed = new boolean[isUsedAttribute.length];
    boolean[] isToUseNextRound = new boolean[isUsedAttribute.length];
    isUsedAttribute = new boolean[isUsedAttribute.length];

    // do until nothing changes or max rounds exceeded
    int iteration = 0;
    while (iteration == 0
            || iteration < maxIterations && isSelectionDiffering(isUsedAttribute, isLastRoundUsed)) {
        System.arraycopy(isUsedAttribute, 0, isLastRoundUsed, 0, isUsedAttribute.length);

        // first do forward selection for all single non-selected and
        // allowed attributes
        int coefficientIndex = 0;
        for (int i = 0; i < isAllowedToUse.length; i++) {
            if (isAllowedToUse[i] && !isUsedAttribute[i]) {
                // check if this not selected one will receive significant coefficient
                isUsedAttribute[i] = true;
                double[] coefficients = regression.performRegression(exampleSet, isUsedAttribute, means,
                        labelMean, ridge, useBias);
                // only if it is possible to calculate the probabilities, the p-value for this
                // attribute is checked
                if (fdistribution != null) {
                    double pValue = getPValue(coefficients[coefficientIndex], i, regression, useBias, ridge,
                            exampleSet, isUsedAttribute, standardDeviations, labelStandardDeviation,
                            fdistribution, generalCorrelation);
                    if (1.0d - pValue <= alphaForward) {
                        isToUseNextRound[i] = true;
                    }
                }
                isUsedAttribute[i] = false;
            } else if (isUsedAttribute[i]) {
                coefficientIndex++;
            }
        }

        // now add all that we have remembered to use
        for (int i = 0; i < isUsedAttribute.length; i++) {
            isUsedAttribute[i] |= isToUseNextRound[i];
            isToUseNextRound[i] = false;
        }

        // now we have to deselect all that do not fulfill t-test in combination
        {
            double[] coefficients = regression.performRegression(exampleSet, isUsedAttribute, means, labelMean,
                    ridge, useBias);
            isUsedAttribute = filterByPValue(regression, useBias, ridge, exampleSet, isUsedAttribute, means,
                    labelMean, standardDeviations, labelStandardDeviation, coefficients,
                    alphaBackward).isUsedAttribute;
        }

        iteration++;
    }

    // calculate result
    LinearRegressionResult result = new LinearRegressionResult();
    result.isUsedAttribute = isUsedAttribute;
    result.coefficients = regression.performRegression(exampleSet, isUsedAttribute, means, labelMean, ridge,
            useBias);
    result.error = regression.getSquaredError(exampleSet, isUsedAttribute, result.coefficients, useBias);
    return result;
}

From source file:com.rapidminer.operator.learner.functions.linear.TTestLinearRegressionMethod.java

/**
 * This method filters the selected attributes depending on their p-value in respect to the
 * significance niveau alpha.//from  www  . jav a  2s  . c om
 *
 * @throws ProcessStoppedException
 */
protected LinearRegressionResult filterByPValue(LinearRegression regression, boolean useBias, double ridge,
        ExampleSet exampleSet, boolean[] isUsedAttribute, double[] means, double labelMean,
        double[] standardDeviations, double labelStandardDeviation, double[] coefficientsOnFullData,
        double alpha) throws UndefinedParameterError, ProcessStoppedException {

    FDistribution fdistribution;
    // check if the F-distribution can be calculated
    int secondDegreeOfFreedom = exampleSet.size() - coefficientsOnFullData.length;
    if (secondDegreeOfFreedom > 0) {
        fdistribution = new FDistribution(1, secondDegreeOfFreedom);
    } else {
        fdistribution = null;
    }

    double generalCorrelation = regression.getCorrelation(exampleSet, isUsedAttribute, coefficientsOnFullData,
            useBias);
    generalCorrelation *= generalCorrelation;

    int index = 0;
    for (int i = 0; i < isUsedAttribute.length; i++) {
        if (isUsedAttribute[i]) {
            double coefficient = coefficientsOnFullData[index];

            // only if it is possible to calculate the probabilities, the alpha value for this
            // attribute is checked
            if (fdistribution != null) {
                double probability = getPValue(coefficient, i, regression, useBias, ridge, exampleSet,
                        isUsedAttribute, standardDeviations, labelStandardDeviation, fdistribution,
                        generalCorrelation);
                if (1.0d - probability > alpha) {
                    isUsedAttribute[i] = false;
                }
                index++;
            } else {
                isUsedAttribute[i] = false;
            }
        }
    }
    LinearRegressionResult result = new LinearRegressionResult();
    result.isUsedAttribute = isUsedAttribute;
    result.coefficients = regression.performRegression(exampleSet, isUsedAttribute, means, labelMean, ridge,
            useBias);
    result.error = regression.getSquaredError(exampleSet, isUsedAttribute, result.coefficients, useBias);
    return result;
}

From source file:gedi.util.math.stat.testing.OneWayAnova.java

public double getPvalue() {
    FDistribution fdist = new FDistribution(getFactorDof(), getErrorDof());
    if (getFactorDof() < 1 || getErrorDof() < 1)
        return Double.NaN;
    return 1 - fdist.cumulativeProbability(getFStatistic());
}

From source file:com.rapidminer.operator.validation.significance.TTestSignificanceTestOperator.java

private double getProbability(PerformanceCriterion pc1, PerformanceCriterion pc2) {
    double totalDeviation = ((pc1.getAverageCount() - 1) * pc1.getVariance()
            + (pc2.getAverageCount() - 1) * pc2.getVariance())
            / (pc1.getAverageCount() + pc2.getAverageCount() - 2);
    double factor = 1.0d / (1.0d / pc1.getAverageCount() + 1.0d / pc2.getAverageCount());
    double diff = pc1.getAverage() - pc2.getAverage();
    double t = factor * diff * diff / totalDeviation;
    int secondDegreeOfFreedom = pc1.getAverageCount() + pc2.getAverageCount() - 2;
    double prob;/*from w w  w. j  a  va2  s.  co m*/
    // make sure the F-distribution is well defined
    if (secondDegreeOfFreedom > 0) {
        FDistribution fDist = new FDistribution(1, secondDegreeOfFreedom);
        prob = 1 - fDist.cumulativeProbability(t);
    } else {
        // in this case the probability cannot calculated correctly and a 1 is returned, as
        // this result is not significant
        prob = 1;
    }

    return prob;
}

From source file:edu.cudenver.bios.power.glmm.GLMMTest.java

/**
 * Fit the model/*from  www  .  j  a v  a  2 s.co m*/
 * @return model fit object
 */
public ModelFit getModelFit() {
    double fobs = getObservedF(GLMMTest.DistributionType.DATA_ANALYSIS_NULL);

    // get the p-value from a central F distribution
    double ndf = getNumeratorDF(GLMMTest.DistributionType.DATA_ANALYSIS_NULL);
    if (Double.isNaN(ndf)) {
        throw new IllegalArgumentException("numerator DF is NaN");
    }
    double ddf = getDenominatorDF(GLMMTest.DistributionType.DATA_ANALYSIS_NULL);
    if (Double.isNaN(ddf)) {
        throw new IllegalArgumentException("denominator DF is NaN");
    }

    FDistribution fdist = new FDistribution(ndf, ddf);
    double pvalue = 1 - fdist.cumulativeProbability(fobs);

    return new ModelFit(pvalue, fobs, ndf, ddf, sigmaError, beta);
}

From source file:edu.cudenver.bios.power.glmm.GLMMTest.java

/**
 * Calculate the critical F value under the specified distribution
 *
 * @param type distribution type//from w w w.  j av a  2  s. co m
 * @param alpha type I error level
 * @return critical F
 *
 */
public double getCriticalF(DistributionType type, double alpha) throws IllegalArgumentException {
    double ndf = getNumeratorDF(type);
    if (Double.isNaN(ndf)) {
        throw new IllegalArgumentException("numerator DF is NaN");
    }
    double ddf = getDenominatorDF(type);
    if (Double.isNaN(ddf)) {
        throw new IllegalArgumentException("denominator DF is NaN");
    }

    FDistribution centralFDist = new FDistribution(ndf, ddf);
    double fcrit = centralFDist.inverseCumulativeProbability(1 - alpha);
    return fcrit;
}

From source file:edu.stanford.cfuller.imageanalysistools.filter.VariableSizeMeanFilter.java

protected boolean shouldSubDivide(OcttreeNode node, Image im, Image laplacianFiltered) {

    im.setBoxOfInterest(node.getBoxMin(), node.getBoxMax());
    laplacianFiltered.setBoxOfInterest(node.getBoxMin(), node.getBoxMax());

    double l_sum = 0;
    double sum = 0;
    double count = 0;

    for (ImageCoordinate ic : im) {
        l_sum += laplacianFiltered.getValue(ic);
        sum += im.getValue(ic);//from   w  w w  . j  a v  a  2 s . c om
        count++;

    }

    if (count == 1)
        return false;

    l_sum /= count;
    sum /= count;

    double l_var = 0;
    double var = 0;

    for (ImageCoordinate ic : im) {

        l_var += Math.pow(laplacianFiltered.getValue(ic) - l_sum, 2);
        var += Math.pow(im.getValue(ic) - sum, 2);

    }

    l_var /= (count - 1);
    var /= (count - 1);

    im.clearBoxOfInterest();
    laplacianFiltered.clearBoxOfInterest();

    double cutoff = 0.0001;

    double smallerVar = var < l_var ? var : l_var;
    double largerVar = var > l_var ? var : l_var;
    try {

        FDistribution f = new FDistribution(count - 1, count - 1);
        double valueAtLowerCutoff = f.inverseCumulativeProbability(cutoff);
        double valueAtUpperCutoff = f.inverseCumulativeProbability(1 - cutoff);
        boolean result = (smallerVar / largerVar > valueAtUpperCutoff
                || smallerVar / largerVar < valueAtLowerCutoff);
        return result;

    } catch (MathIllegalArgumentException e) {
        LoggingUtilities.getLogger()
                .severe("Exception while calculating variable size mean QO partition: " + e.getMessage());
        e.printStackTrace();
        return false;
    }
}