Search in sources :

Example 51 with RealVector

use of org.apache.commons.math3.linear.RealVector in project narchy by automenta.

the class HordeSchedulerTest method testScheduler.

@Test
public void testScheduler() {
    RealVector o_tp1 = new ArrayRealVector(1);
    FakeDemon d1 = new FakeDemon(), d2 = new FakeDemon();
    final List<FakeDemon> demons = Lists.newArrayList(d1, d2);
    final FakeFunction[] beforeFunctions = { new FakeFunction(d1), new FakeFunction(d2) };
    final FakeFunction[] afterFunctions = { new FakeFunction(d1), new FakeFunction(d2) };
    Horde horde = new Horde(new HordeScheduler(3));
    horde.beforeFunctions().addAll(Lists.newArrayList(beforeFunctions));
    horde.demons().addAll(demons);
    horde.afterFunctions().addAll(Lists.newArrayList(afterFunctions));
    final RealVector x0 = new ArrayRealVector(1), x1 = new ArrayRealVector(1);
    final Integer a0 = 0;
    checkFunction(beforeFunctions, null, null, null, null, false);
    checkFunction(afterFunctions, null, null, null, null, false);
    horde.update(o_tp1, x0, a0, x1);
    checkFunction(beforeFunctions, x0, a0, o_tp1, x1, false);
    checkFunction(afterFunctions, x0, a0, o_tp1, x1, true);
    checkDemon(d1, x0, a0, x1);
    checkDemon(d2, x0, a0, x1);
}
Also used : RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) Horde(nars.rl.horde.Horde) HordeScheduler(nars.rl.horde.HordeScheduler) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) Test(org.junit.jupiter.api.Test)

Example 52 with RealVector

use of org.apache.commons.math3.linear.RealVector in project narchy by automenta.

the class HordeTest method runExperiment.

protected void runExperiment(PredictionDemon predictionDemon, PredictionDemonVerifier demonVerifier, TimeToState timeToState, int maxStep) {
    RealVector x_t = null;
    int time = 0;
    PredictionDemonVerifier.TDErrorMonitor verifier = demonVerifier.errorMonitor();
    while (!verifier.errorComputed() || Math.abs(verifier.error()) >= verifier.precision()) {
        RealVector x_tp1 = timeToState.get(time);
        ((RewardFunctionTest) predictionDemon.rewardFunction()).update(time);
        predictionDemon.update(x_t, null, x_tp1);
        demonVerifier.update(false);
        x_t = x_tp1;
        time++;
        assertTrue(time < maxStep);
    }
}
Also used : RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) PredictionDemonVerifier(nars.rl.horde.demons.PredictionDemonVerifier)

Example 53 with RealVector

use of org.apache.commons.math3.linear.RealVector in project narchy by automenta.

the class ThreadVectorPool method newVector.

public ArrayRealVector newVector(RealVector v) {
    assert dimension == v.getDimension();
    ArrayRealVector w = vectorCached();
    w.setSubVector(0, v);
    return w;
}
Also used : ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector)

Example 54 with RealVector

use of org.apache.commons.math3.linear.RealVector in project pyramid by cheng-li.

the class GaussianDistribution method logDensity.

public double logDensity(RealVector x) {
    RealVector diff = x.subtract(mean);
    int dim = mean.getDimension();
    return -0.5 * dim * Math.log(2 * Math.PI) - 0.5 * logDeterminant - 0.5 * (inverseCovariance.preMultiply(diff).dotProduct(diff));
}
Also used : RealVector(org.apache.commons.math3.linear.RealVector)

Example 55 with RealVector

use of org.apache.commons.math3.linear.RealVector in project GDSC-SMLM by aherbert.

the class TrackPopulationAnalysisTest method canComputeBrownianDiffusionFunction1.

@Test
void canComputeBrownianDiffusionFunction1() {
    final int size = 10;
    final double delta = 1e-6;
    final DoubleDoubleBiPredicate test = TestHelper.doublesAreClose(1e-5);
    for (final double t : new double[] { 0.8, 1, 1.2 }) {
        final MultivariateJacobianFunction f = new BrownianDiffusionFunction(size, t);
        for (final double d : new double[] { 0.8, 0.9, 1, 1.1, 1.2 }) {
            for (final double s : new double[] { 2, 20 }) {
                // Check the value and Jacobian
                final RealVector point = new ArrayRealVector(new double[] { d, s }, false);
                final Pair<RealVector, RealMatrix> p = f.value(point);
                final double[] value = p.getFirst().toArray();
                Assertions.assertEquals(size, value.length);
                for (int n = 1; n <= size; n++) {
                    // MSD = 4Dt * (n - 1/3) + 4s^2
                    final double msd = 4 * d * t * (n - 1.0 / 3) + 4 * s * s;
                    TestAssertions.assertTest(msd, value[n - 1], test, "value");
                }
                // Columns of the Jacobian
                final double[] dfda1 = p.getSecond().getColumn(0);
                final double[] dfdb1 = p.getSecond().getColumn(1);
                point.setEntry(0, d - delta);
                RealVector v1 = f.value(point).getFirst();
                point.setEntry(0, d + delta);
                RealVector v2 = f.value(point).getFirst();
                final double[] dfda = v2.subtract(v1).mapDivide(2 * delta).toArray();
                point.setEntry(0, d);
                point.setEntry(1, s - delta);
                v1 = f.value(point).getFirst();
                point.setEntry(1, s + delta);
                v2 = f.value(point).getFirst();
                final double[] dfdb = v2.subtract(v1).mapDivide(2 * delta).toArray();
                // Element-by-element relative error
                TestAssertions.assertArrayTest(dfda, dfda1, test, "jacobian dfda");
                TestAssertions.assertArrayTest(dfdb, dfdb1, test, "jacobian dfdb");
            }
        }
    }
}
Also used : BrownianDiffusionFunction(uk.ac.sussex.gdsc.smlm.ij.plugins.TrackPopulationAnalysis.BrownianDiffusionFunction) DoubleDoubleBiPredicate(uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate) RealMatrix(org.apache.commons.math3.linear.RealMatrix) RealVector(org.apache.commons.math3.linear.RealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) ArrayRealVector(org.apache.commons.math3.linear.ArrayRealVector) MultivariateJacobianFunction(org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction) Test(org.junit.jupiter.api.Test)

Aggregations

RealVector (org.apache.commons.math3.linear.RealVector)51 ArrayRealVector (org.apache.commons.math3.linear.ArrayRealVector)36 RealMatrix (org.apache.commons.math3.linear.RealMatrix)29 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)11 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)7 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)7 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)6 DecompositionSolver (org.apache.commons.math3.linear.DecompositionSolver)5 SingularValueDecomposition (org.apache.commons.math3.linear.SingularValueDecomposition)5 Test (org.junit.jupiter.api.Test)5 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)4 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)4 VectorPool (nars.rl.horde.math.VectorPool)3 LeastSquaresBuilder (org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)3 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)3 CombinedAttributeValues (org.knime.base.node.mine.treeensemble2.data.BinaryNominalSplitsPCA.CombinedAttributeValues)3 DoubleDoubleBiPredicate (uk.ac.sussex.gdsc.test.api.function.DoubleDoubleBiPredicate)3 File (java.io.File)2 Comparator (java.util.Comparator)2 DimensionMismatchException (org.apache.commons.math3.exception.DimensionMismatchException)2