Search in sources :

Example 1 with NotStrictlyPositiveException

use of org.apache.commons.math3.exception.NotStrictlyPositiveException in project knime-core by knime.

the class RegressionContent method getPValueMatrix.

/**
 * Computes the two-tailed p-values of the t-test.
 */
private RealMatrix getPValueMatrix() {
    RealMatrix tValues = getTValueMatrix();
    // p-value
    RealMatrix pValueMatrix = MatrixUtils.createRealMatrix(1, m_covMat.getRowDimension());
    try {
        for (int i = 0; i < m_covMat.getRowDimension(); i++) {
            TDistribution distribution = new TDistribution(getN() - m_covMat.getRowDimension());
            double t = Math.abs(tValues.getEntry(0, i));
            pValueMatrix.setEntry(0, i, 2 * (1 - distribution.cumulativeProbability(t)));
        }
    } catch (NotStrictlyPositiveException e) {
        for (int i = m_covMat.getRowDimension(); i-- > 0; ) {
            pValueMatrix.setEntry(0, i, Double.NaN);
        }
    }
    return pValueMatrix;
}
Also used : NotStrictlyPositiveException(org.apache.commons.math3.exception.NotStrictlyPositiveException) RealMatrix(org.apache.commons.math3.linear.RealMatrix) TDistribution(org.apache.commons.math3.distribution.TDistribution)

Example 2 with NotStrictlyPositiveException

use of org.apache.commons.math3.exception.NotStrictlyPositiveException in project narchy by automenta.

the class MyCMAESOptimizer method initializeCMA.

/**
 * Initialization of the dynamic search parameters
 *
 * @param guess Initial guess for the arguments of the fitness function.
 */
private void initializeCMA(double[] guess) {
    if (lambda <= 0) {
        throw new NotStrictlyPositiveException(lambda);
    }
    // initialize sigma
    final double[][] sigmaArray = new double[guess.length][1];
    for (int i = 0; i < guess.length; i++) {
        sigmaArray[i][0] = inputSigma[i];
    }
    final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
    // overall standard deviation
    sigma = max(insigma);
    // initialize termination criteria
    stopTolUpX = oNEtHOUSAND * max(insigma);
    stopTolX = epsilonWTF11 * max(insigma);
    this.stopTolFun = EPSILON_WTF12;
    this.stopTolHistFun = epsilonwtf13;
    // initialize selection strategy parameters
    // number of parents/points for recombination
    mu = lambda / 2;
    /* log(mu + 0.5), stored for efficiency. */
    double logMu2 = Math.log(mu + 0.5);
    weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2);
    double sumw = 0;
    double sumwq = 0;
    for (int i = 0; i < mu; i++) {
        double w = weights.getEntry(i, 0);
        sumw += w;
        sumwq += w * w;
    }
    weights = weights.scalarMultiply(1 / sumw);
    // variance-effectiveness of sum w_i x_i
    mueff = sumw * sumw / sumwq;
    // initialize dynamic strategy parameters and constants
    cc = (4 + mueff / dimension) / (dimension + 4 + 2 * mueff / dimension);
    cs = (mueff + 2) / (dimension + mueff + 3.0);
    damps = (1 + 2 * Math.max(0, Math.sqrt((mueff - 1) / (dimension + 1)) - 1)) * Math.max(0.3, 1 - dimension / (epsilon6WTF + maxIterations)) + // minor increment
    cs;
    ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff);
    ccovmu = Math.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) / ((dimension + 2) * (dimension + 2) + mueff));
    ccov1Sep = Math.min(1, ccov1 * (dimension + 1.5) / 3);
    ccovmuSep = Math.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3);
    chiN = Math.sqrt(dimension) * (1 - 1 / (4.0 * dimension) + 1 / (21.0 * dimension * dimension));
    // intialize CMA internal values - updated each generation
    // objective variables
    xmean = MatrixUtils.createColumnRealMatrix(guess);
    diagD = insigma.scalarMultiply(1 / sigma);
    diagC = square(diagD);
    // evolution paths for C and sigma
    pc = zeros(dimension, 1);
    // B defines the coordinate system
    ps = zeros(dimension, 1);
    normps = ps.getFrobeniusNorm();
    B = eye(dimension, dimension);
    // diagonal D defines the scaling
    D = ones(dimension, 1);
    BD = times(B, repmat(diagD.transpose(), dimension, 1));
    // covariance
    C = B.multiply(diag(square(D)).multiply(B.transpose()));
    /* Size of history queue of best values. */
    int historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
    // history of fitness values
    fitnessHistory = new double[historySize];
    for (int i = 0; i < historySize; i++) {
        fitnessHistory[i] = Double.POSITIVE_INFINITY;
    }
}
Also used : NotStrictlyPositiveException(org.apache.commons.math3.exception.NotStrictlyPositiveException) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix)

Example 3 with NotStrictlyPositiveException

use of org.apache.commons.math3.exception.NotStrictlyPositiveException in project Gemma by PavlidisLab.

the class ExpressionExperimentServiceImpl method getBatchConfound.

@Override
@Transactional(readOnly = true)
public String getBatchConfound(ExpressionExperiment ee) {
    ee = this.thawBioAssays(ee);
    if (!this.checkHasBatchInfo(ee)) {
        return null;
    }
    Collection<BatchConfoundValueObject> confounds;
    try {
        confounds = BatchConfound.test(ee);
    } catch (NotStrictlyPositiveException e) {
        AbstractService.log.error("Batch confound test threw a NonStrictlyPositiveException! Returning null.");
        return null;
    }
    StringBuilder result = new StringBuilder("");
    for (BatchConfoundValueObject c : confounds) {
        if (c.getP() < ExpressionExperimentServiceImpl.BATCH_CONFOUND_THRESHOLD) {
            String factorName = c.getEf().getName();
            if (!result.toString().isEmpty()) {
                result.append(", ");
            }
            result.append("Factor '").append(factorName).append("' may be confounded with batches; p=").append(String.format("%.2g", c.getP()));
        }
    }
    return Strings.emptyToNull(result.toString());
}
Also used : NotStrictlyPositiveException(org.apache.commons.math3.exception.NotStrictlyPositiveException) BatchConfoundValueObject(ubic.gemma.core.analysis.preprocess.batcheffects.BatchConfoundValueObject) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with NotStrictlyPositiveException

use of org.apache.commons.math3.exception.NotStrictlyPositiveException in project PhenomeScape by soulj.

the class GOTermAnalyser method calculateGOTermPValues.

public void calculateGOTermPValues(PhenomeExpressSubnetwork subnetwork, ProteinNetwork proteinNetwork) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException, Exception {
    Map<GOTerm, Integer> subnetworkGoTerms = goTermsinSubnetwork(proteinNetwork, subnetwork);
    int networkSize = proteinNetwork.getNodeCount();
    double minProb = 2.0;
    GOTerm bestGOTerm = null;
    for (GOTerm goterm : subnetworkGoTerms.keySet()) {
        HypergeometricDistribution hypergeometricDistribution = new HypergeometricDistribution(networkSize, goterm.getNumAnnotation(), subnetwork.getNodeList().size());
        double prob = 1.0 - hypergeometricDistribution.cumulativeProbability(subnetworkGoTerms.get(goterm));
        if (prob < minProb) {
            minProb = prob;
            bestGOTerm = goterm;
        } else if (prob == minProb) {
            if (subnetworkGoTerms.get(goterm) > subnetworkGoTerms.get(bestGOTerm)) {
                minProb = prob;
                bestGOTerm = goterm;
            }
        }
    }
    subnetwork.setBestGOTerm(bestGOTerm);
    subnetwork.setGoTermPvalue(minProb);
    if (bestGOTerm == null) {
        throw new Exception("No matching GOTerms - Wrong species selected?");
    }
}
Also used : HypergeometricDistribution(org.apache.commons.math3.distribution.HypergeometricDistribution) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NumberIsTooLargeException(org.apache.commons.math3.exception.NumberIsTooLargeException) NotStrictlyPositiveException(org.apache.commons.math3.exception.NotStrictlyPositiveException) NotPositiveException(org.apache.commons.math3.exception.NotPositiveException)

Aggregations

NotStrictlyPositiveException (org.apache.commons.math3.exception.NotStrictlyPositiveException)4 RealMatrix (org.apache.commons.math3.linear.RealMatrix)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HypergeometricDistribution (org.apache.commons.math3.distribution.HypergeometricDistribution)1 TDistribution (org.apache.commons.math3.distribution.TDistribution)1 NotPositiveException (org.apache.commons.math3.exception.NotPositiveException)1 NumberIsTooLargeException (org.apache.commons.math3.exception.NumberIsTooLargeException)1 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)1 Transactional (org.springframework.transaction.annotation.Transactional)1 BatchConfoundValueObject (ubic.gemma.core.analysis.preprocess.batcheffects.BatchConfoundValueObject)1