Search in sources :

Example 16 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project vcell by virtualcell.

the class TimeSeriesMultitrialData method kolmogorovSmirnovTest.

public static double kolmogorovSmirnovTest(double[] rawData1, double[] rawData2) {
    try {
        int numBins = 1 + (int) Math.ceil(Math.sqrt(rawData1.length));
        // rawData2 = ramp(0,10,rawData2.length);
        Max max = new Max();
        max.incrementAll(rawData1);
        max.incrementAll(rawData2);
        Min min = new Min();
        min.incrementAll(rawData1);
        min.incrementAll(rawData2);
        double[] cdf1 = calculateCDF(rawData1, min.getResult(), max.getResult(), numBins);
        double[] cdf2 = calculateCDF(rawData2, min.getResult(), max.getResult(), numBins);
        KolmogorovSmirnovTest test = new KolmogorovSmirnovTest();
        return test.kolmogorovSmirnovStatistic(cdf1, cdf2);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return -1;
    }
}
Also used : Min(org.apache.commons.math3.stat.descriptive.rank.Min) Max(org.apache.commons.math3.stat.descriptive.rank.Max) KolmogorovSmirnovTest(org.apache.commons.math3.stat.inference.KolmogorovSmirnovTest) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 17 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project vcell by virtualcell.

the class TimeSeriesMultitrialData method chiSquaredTest.

public static double chiSquaredTest(double[] rawData1, double[] rawData2) {
    try {
        int numBins = 1 + (int) Math.ceil(Math.sqrt(rawData1.length));
        // rawData2 = ramp(0,10,rawData2.length);
        TimeSeriesMultitrialData.MinMaxHelp minMaxHelp1 = new TimeSeriesMultitrialData.MinMaxHelp(rawData1);
        TimeSeriesMultitrialData.MinMaxHelp minMaxHelp2 = new TimeSeriesMultitrialData.MinMaxHelp(rawData2);
        double min = Math.min(minMaxHelp1.min, minMaxHelp2.min);
        double max = Math.max(minMaxHelp1.max, minMaxHelp2.max);
        long[] histogram1 = calcHistogram(rawData1, min, max, numBins);
        long[] histogram2 = calcHistogram(rawData2, min, max, numBins);
        // 
        // remove histogram indices where both bins are zero
        // 
        ArrayList<Long> histogram1List = new ArrayList<Long>();
        ArrayList<Long> histogram2List = new ArrayList<Long>();
        for (int i = 0; i < histogram1.length; i++) {
            if (histogram1[i] != 0 || histogram2[i] != 0) {
                histogram1List.add(histogram1[i]);
                histogram2List.add(histogram2[i]);
            // }else{
            // histogram1List.add(new Long(1));
            // histogram2List.add(new Long(1));
            }
        }
        histogram1 = new long[histogram1List.size()];
        histogram2 = new long[histogram2List.size()];
        for (int i = 0; i < histogram1List.size(); i++) {
            histogram1[i] = histogram1List.get(i);
            histogram2[i] = histogram2List.get(i);
        }
        if (histogram1.length == 1) {
            return 0.0;
        }
        ChiSquareTest chiSquareTest = new ChiSquareTest();
        return chiSquareTest.chiSquareTestDataSetsComparison(histogram1, histogram2);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return -1;
    }
}
Also used : ArrayList(java.util.ArrayList) ChiSquareTest(org.apache.commons.math3.stat.inference.ChiSquareTest) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 18 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project tetrad by cmu-phil.

the class SemOptimizerPowell method optimize.

// =========================PUBLIC METHODS==========================//
public void optimize(SemIm semIm) {
    double min = Double.POSITIVE_INFINITY;
    double[] point = null;
    for (int count = 0; count < numRestarts + 1; count++) {
        // System.out.println("Trial " + (count + 1));
        SemIm _sem2 = new SemIm(semIm);
        List<Parameter> freeParameters = _sem2.getFreeParameters();
        double[] p = new double[freeParameters.size()];
        for (int i = 0; i < freeParameters.size(); i++) {
            if (freeParameters.get(i).getType() == ParamType.VAR) {
                p[i] = RandomUtil.getInstance().nextUniform(0, 1);
            } else {
                p[i] = RandomUtil.getInstance().nextUniform(-1, 1);
            }
        }
        _sem2.setFreeParamValues(p);
        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
        PointValuePair pair = search.optimize(new InitialGuess(_sem2.getFreeParamValues()), new ObjectiveFunction(fittingFunction(semIm)), GoalType.MINIMIZE, new MaxEval(100000));
        double chisq = _sem2.getChiSquare();
        if (chisq < min) {
            min = chisq;
            point = pair.getPoint();
        }
    }
    if (point == null) {
        throw new NullPointerException("Point could not be found.");
    }
    System.arraycopy(point, 0, semIm.getFreeParamValues(), 0, point.length);
}
Also used : MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PowellOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Example 19 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project FSensor by KalebKE.

the class CalibrationUtil method getCalibration.

/**
 * Transforms the ellipsoid into a sphere with the offset vector = [0,0,0]
 * and the radii vector = [1,1,1].
 *
 * @param fitPoints the representation of the calibration ellipsoid
 */
public static Calibration getCalibration(FitPoints fitPoints) {
    // The scalar values to transform the radii vector into [1,1,1]
    RealMatrix scalar = new Array2DRowRealMatrix(3, 3);
    // RIV determines the magnitude of the radii. We have to know the
    // magnitudes because the eigenvalues, and thus the radii, are returned
    // in ascending order. Without knowing the magnitudes, we wouldn't know
    // what radii to apply to what axis.
    // Find the max and minimum magnitudes.
    double max = fitPoints.riv.getEntry(0);
    double min = fitPoints.riv.getEntry(0);
    // The indexes of the maximum, median, and minimum radii.
    // Note that these are the opposite of the max and min
    // because a smaller riv value means a greater magnitude.
    int maxi = 0, midi = 0, mini = 0;
    // Find max and min radii
    for (int i = 0; i < fitPoints.riv.getDimension(); i++) {
        if (fitPoints.riv.getEntry(i) > max) {
            max = fitPoints.riv.getEntry(i);
            mini = i;
        }
        if (fitPoints.riv.getEntry(i) < min) {
            min = fitPoints.riv.getEntry(i);
            maxi = i;
        }
    }
    // Find median radii
    for (int i = 0; i < fitPoints.riv.getDimension(); i++) {
        if (fitPoints.riv.getEntry(i) < max && fitPoints.riv.getEntry(i) > min) {
            midi = i;
        }
    }
    // Create the scalar vector in the correct orientation.
    scalar.setEntry(0, 0, 1 / fitPoints.radii.getEntry(mini));
    scalar.setEntry(1, 1, 1 / fitPoints.radii.getEntry(midi));
    scalar.setEntry(2, 2, 1 / fitPoints.radii.getEntry(maxi));
    return new Calibration(scalar, fitPoints.center);
}
Also used : Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix)

Example 20 with Min

use of org.apache.commons.math3.stat.descriptive.rank.Min in project narchy by automenta.

the class Optimize method solve.

public void solve(int dim, ObjectiveFunction func, double[] mid, double[] min, double[] max, double[] inc, int maxIterations) {
    if (dim == 1) {
        // use a solver capable of 1 dim
        new SimplexOptimizer(1e-10, 1e-30).optimize(new MaxEval(maxIterations), func, GoalType.MAXIMIZE, new InitialGuess(mid), // new NelderMeadSimplex(inc)
        new MultiDirectionalSimplex(inc));
    } else {
        int popSize = // 4 + 3 ln(n)
        (int) Math.ceil(4 + 3 * Math.log(tweaks.size()));
        double[] sigma = MathArrays.scale(1f, inc);
        MyCMAESOptimizer m = new MyCMAESOptimizer(maxIterations, Double.NaN, true, 0, 1, new MersenneTwister(System.nanoTime()), true, null, popSize, sigma);
        m.optimize(func, GoalType.MAXIMIZE, new MaxEval(maxIterations), new SimpleBounds(min, max), new InitialGuess(mid));
        m.print(System.out);
    // final int numIterpolationPoints = 3 * dim; //2 * dim + 1 + 1;
    // new BOBYQAOptimizer(numIterpolationPoints,
    // dim * 2.0,
    // 1.0E-4D /* ? */).optimize(
    // MaxEval.unlimited(), //new MaxEval(maxIterations),
    // new MaxIter(maxIterations),
    // func,
    // GoalType.MAXIMIZE,
    // new SimpleBounds(min, max),
    // new InitialGuess(mid));
    }
}
Also used : MaxEval(org.apache.commons.math3.optim.MaxEval) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MultiDirectionalSimplex(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.MultiDirectionalSimplex) SimpleBounds(org.apache.commons.math3.optim.SimpleBounds) SimplexOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer) MersenneTwister(org.apache.commons.math3.random.MersenneTwister)

Aggregations

ArrayList (java.util.ArrayList)16 List (java.util.List)10 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)8 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)7 Map (java.util.Map)6 UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)6 MaxEval (org.apache.commons.math3.optim.MaxEval)6 Collectors (java.util.stream.Collectors)5 ExpressionException (cbit.vcell.parser.ExpressionException)4 Plot2 (ij.gui.Plot2)4 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)4 InitialGuess (org.apache.commons.math3.optim.InitialGuess)4 PointValuePair (org.apache.commons.math3.optim.PointValuePair)4 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)3 HashMap (java.util.HashMap)3 SimpsonIntegrator (org.apache.commons.math3.analysis.integration.SimpsonIntegrator)3 BrentOptimizer (org.apache.commons.math3.optim.univariate.BrentOptimizer)3 SearchInterval (org.apache.commons.math3.optim.univariate.SearchInterval)3