/** Test of setParameters method, of class MultivariatePolyaDistribution. */ public void testSetParameters() { System.out.println("setParameters"); MultivariatePolyaDistribution instance = this.createInstance(); Vector p = instance.getParameters().scale(2.0); instance.setParameters(p); assertEquals(p, instance.getParameters()); p.setElement(0, 0.0); try { instance.setParameters(p); fail("Parameters must be > 0.0"); } catch (Exception e) { System.out.println("Good: " + e); } try { instance.setParameters(null); fail("Cannot have null parameters"); } catch (Exception e) { System.out.println("Good: " + e); } try { instance.setParameters(VectorFactory.getDefault().copyValues(1.0)); fail("Must have dimensionality >= 2"); } catch (Exception e) { System.out.println("Good: " + e); } }
public static ArrayList<Vector> readDataAsVector(final String fileName, final int dimensionality) throws IOException { final ArrayList<Vector> result = new ArrayList<Vector>(); final BufferedReader in = new BufferedReader(new FileReader(fileName)); try { String line; while ((line = in.readLine()) != null) { final String[] parts = line.split("\\s"); final int elements = Integer.parseInt(parts[0]); final Vector v = VectorFactory.getSparseDefault().createVectorCapacity(dimensionality, elements); for (int i = 1; i < parts.length; i++) { final String part = parts[i]; final int split = part.indexOf(':'); final int index = Integer.parseInt(part.substring(0, split)); final int value = Integer.parseInt(part.substring(split + 1)); v.setElement(index, value); } result.add(v); } } finally { in.close(); } return result; }
@Test public void testPathStateConvert3() { final Path startPath = TestUtils.makeTmpPath(graph, false, new Coordinate(0, 0), new Coordinate(10, 0)); final Matrix covar = MatrixFactory.getDefault() .copyArray(new double[][] {new double[] {126.56, 8.44}, new double[] {8.44, 0.56}}); final MultivariateGaussian startBelief = new AdjMultivariateGaussian(VectorFactory.getDefault().createVector2D(2.5d, 1d), covar); final PathStateDistribution currentBelief = new PathStateDistribution(startPath, startBelief); final Vector groundLoc = MotionStateEstimatorPredictor.getOg() .times(currentBelief.getGroundDistribution().getMean()); AssertJUnit.assertEquals("initial state x", 2.5d, groundLoc.getElement(0), 0d); AssertJUnit.assertEquals("initial state y", 0d, groundLoc.getElement(1), 0d); final Path newPath = TestUtils.makeTmpPath( graph, false, new Coordinate(0, 0), new Coordinate(10, 0), new Coordinate(10, -10), new Coordinate(20, -10)); final PathStateDistribution result = currentBelief.convertToPath(newPath); AssertJUnit.assertEquals("distance", 2.5d, result.getMean().getElement(0), 0d); AssertJUnit.assertEquals("velocity", 1d, result.getMean().getElement(1), 0d); }
/** * Simulates the Markov chain into the future, given the transition Matrix and the given current * state-probability distribution, for the given number of time steps into the future. * * @param current Current distribution of probabilities of the various states. * @param numSteps Number of steps into the future to simulate. * @return State-probability distribution for numSteps into the future, starting from the given * state-probability distribution. */ public Vector getFutureStateDistribution(Vector current, int numSteps) { Vector predicted = current; for (int n = 0; n < numSteps; n++) { predicted = this.transitionProbability.times(predicted); } return predicted.scale(1.0 / predicted.norm1()); }
@Override public void convertFromVector(final Vector parameters) { if (parameters.getDimensionality() != 2) { throw new IllegalArgumentException("Parameters must have dimensionality 2"); } this.setV1(parameters.getElement(0)); this.setV2(parameters.getElement(1)); }
public Coordinate getCoordOnEdge(Vector obsPoint) { if (this == InferredEdge.emptyEdge) return null; final Coordinate revObsPoint = new Coordinate(obsPoint.getElement(1), obsPoint.getElement(0)); final LinearLocation here = locationIndexedLine.project(revObsPoint); final Coordinate pointOnLine = locationIndexedLine.extractPoint(here); final Coordinate revOnLine = new Coordinate(pointOnLine.y, pointOnLine.x); return revOnLine; }
@Override public void testKnownConvertToVector() { System.out.println("Known convertToVector"); MultivariatePolyaDistribution instance = this.createInstance(); Vector p = instance.convertToVector(); assertEquals(instance.getInputDimensionality(), p.getDimensionality()); assertNotSame(p, instance.getParameters()); assertEquals(p, instance.getParameters()); }
@Override public void testKnownConvertToVector() { System.out.println("CDF.convertToVector"); UniformDistribution instance = this.createInstance(); Vector p = instance.convertToVector(); assertEquals(2, p.getDimensionality()); assertEquals(instance.getMinSupport(), p.getElement(0)); assertEquals(instance.getMaxSupport(), p.getElement(1)); }
public static Vector diag(Matrix mat) { Vector ret; if (mat.getNumColumns() > mat.getNumRows()) { ret = mat.getRow(0); } else { ret = mat.getColumn(0); } int rowcol = ret.getDimensionality(); for (int rc = 0; rc < rowcol; rc++) { ret.setElement(rc, mat.getElement(rc, rc)); } return ret; }
/** * Setter for initialProbability * * @param initialProbability Initial probability Vector over the states. Each entry must be * nonnegative and the Vector must sum to 1. */ public void setInitialProbability(Vector initialProbability) { final int k = initialProbability.getDimensionality(); double sum = 0.0; for (int i = 0; i < k; i++) { double value = initialProbability.getElement(i); if (value < 0.0) { throw new IllegalArgumentException("Initial Probabilities must be >= 0.0"); } sum += value; } if (sum != 1.0) { initialProbability.scaleEquals(1.0 / sum); } this.initialProbability = initialProbability; }
@Override protected boolean step() { // Reset the number of errors for the new iteration. this.setErrorCount(0); // Loop over all the training instances. for (InputOutputPair<? extends Vectorizable, ? extends Boolean> example : this.getData()) { if (example == null) { continue; } // Compute the predicted classification and get the actual // classification. final Vector input = example.getInput().convertToVector(); final boolean actual = example.getOutput(); final double prediction = this.result.evaluateAsDouble(input); if ((actual && prediction <= this.marginPositive) || (!actual && prediction >= -this.marginNegative)) { // The classification was incorrect so we need to update // the perceptron. this.setErrorCount(this.getErrorCount() + 1); final Vector weights = this.result.getWeights(); double bias = this.result.getBias(); if (actual) { // Update for a positive example so add to the // weights and the bias. weights.plusEquals(input); bias += 1.0; } else { // Update for a negative example so subtract from // the weights and the bias. weights.minusEquals(input); bias -= 1.0; } // The weights are updated by side-effect. // Update the bias directly. this.result.setBias(bias); } // else - The classification was correct, no need to update. } // Keep going while the error count is positive. return this.getErrorCount() > 0; }
/** * Returns the steady-state distribution of the state distribution. This is also the largest * eigenvector of the transition-probability matrix, which has an eigenvalue of 1. * * @return Steady-state probability distribution of state distribution. */ public Vector getSteadyStateDistribution() { final double tolerance = 1e-5; final int maxIterations = 100; Vector p = EigenvectorPowerIteration.estimateEigenvector( this.initialProbability, this.transitionProbability, tolerance, maxIterations); // We do the manual sum (instead of norm1) in case the EVD found // the negative of the eigenvector. double sum = 0.0; for (int i = 0; i < p.getDimensionality(); i++) { sum += p.getElement(i); } p.scaleEquals(1.0 / sum); return p; }
public void convertFromVector(Vector parameters) { int M = this.getNumRows(); int N = this.getNumColumns(); if ((M * N) != parameters.getDimensionality()) { throw new IllegalArgumentException("Elements in this does not equal elements in parameters"); } int index = 0; for (int j = 0; j < N; j++) { for (int i = 0; i < M; i++) { this.setElement(i, j, parameters.getElement(index)); index++; } } }
@Override protected boolean step() { // Search along the gradient for the minimum value Vector xold = this.result.getInput(); this.result = this.lineMinimizer.minimizeAlongDirection( this.lineFunction, this.result.getOutput(), this.gradient); Vector xnew = this.result.getInput(); double fnew = this.result.getOutput(); this.lineFunction.setVectorOffset(xnew); // Let's cache some values for speed Vector gradientOld = this.gradient; // See if I've already computed the gradient information // NOTE: It's possible that there's still an inefficiency here. // For example, we could have computed the gradient for "xnew" // previous to the last evaluation. But this would require a // tremendous amount of bookkeeping and memory. if ((this.lineFunction.getLastGradient() != null) && (this.lineFunction.getLastGradient().getInput().equals(xnew))) { this.gradient = this.lineFunction.getLastGradient().getOutput(); } else { this.gradient = this.data.differentiate(xnew); } // Start caching vectors and dot products for the BFGS update... // this notation is taken from Wikipedia Vector gamma = this.gradient.minus(gradientOld); Vector delta = xnew.minus(xold); // If we've converged on zero slope, then we're done! if (MinimizationStoppingCriterion.convergence( xnew, fnew, this.gradient, delta, this.getTolerance())) { return false; } // Call the particular Quasi-Newton update rule this.updateHessianInverse(this.hessianInverse, delta, gamma); this.lineFunction.setDirection(this.hessianInverse.times(this.gradient).scale(-1.0)); return true; }
/** Test conversion from a positive state to a negative one with an opposite geom. */ @Test public void testGetStateOnPath() { final Path startPath = TestUtils.makeTmpPath(graph, false, new Coordinate(0, 0), new Coordinate(0, 60)); final PathState startState = new PathState(startPath, VectorFactory.getDefault().createVector2D(12d, -1)); final Path newPath = TestUtils.makeTmpPath(graph, true, new Coordinate(0, 60), new Coordinate(0, 0)); final Vector result = startState.convertToPath(newPath); AssertJUnit.assertEquals("dist", -12d, result.getElement(0), 0d); AssertJUnit.assertEquals("dist", 1d, result.getElement(1), 0d); }
/** * Creates a new instance of ContinuousDensityHiddenMarkovModel * * @param initialProbability Initial probability Vector over the states. Each entry must be * nonnegative and the Vector must sum to 1. * @param transitionProbability Transition probability matrix. The entry (i,j) is the probability * of transition from state "j" to state "i". As a corollary, all entries in the Matrix must * be nonnegative and the columns of the Matrix must sum to 1. */ public MarkovChain(Vector initialProbability, Matrix transitionProbability) { if (!transitionProbability.isSquare()) { throw new IllegalArgumentException("transitionProbability must be square!"); } final int k = transitionProbability.getNumRows(); initialProbability.assertDimensionalityEquals(k); this.setTransitionProbability(transitionProbability); this.setInitialProbability(initialProbability); }
/** Test around the start of the edge, positive. */ @Test public void testPathEdge1() { final Path path1 = TestUtils.makeTmpPath(graph, false, new Coordinate(0, 10.11d), new Coordinate(10.11d, 20d)); final PathEdge edge1 = Iterables.getFirst(path1.getPathEdges(), null); final Vector testVec1 = VectorFactory.getDenseDefault().createVector2D(-1e-12d, -1d); final Vector result1 = edge1.getCheckedStateOnEdge(testVec1, 1e-7d, false); AssertJUnit.assertEquals(0d, result1.getElement(0), 0d); final Vector testVec2 = VectorFactory.getDenseDefault().createVector2D(1e-12d, -1d); final Vector result2 = edge1.getCheckedStateOnEdge(testVec2, 1e-7d, false); AssertJUnit.assertEquals(1e-12d, result2.getElement(0), 0d); final Vector testVec3 = VectorFactory.getDenseDefault().createVector2D(-1e-6d, -1d); final Vector result3 = edge1.getCheckedStateOnEdge(testVec3, 1e-7d, false); AssertJUnit.assertEquals(null, result3); }
@Override public void measure(MultivariateGaussian belief, Vector observation) { final Matrix C = this.model.getC(); // Figure out what the model says the observation should be final Vector xpred = belief.getMean(); final Vector ypred = C.times(xpred); // Update step... compute the difference between the observation // and what the model says. // Then compute the Kalman gain, which essentially indicates // how much to believe the observation, and how much to believe model final Vector innovation = observation.minus(ypred); this.computeMeasurementBelief(belief, innovation, C); // XXX covariance was set in the previous call // if (!checkPosDef((DenseMatrix)belief.getCovariance())) // return; }
/** * Test of learn method, of class gov.sandia.cognition.learning.pca.PrincipalComponentsAnalysis. * * <p>The example data is based on: http://www.kernel-machines.org/code/kpca_toy.m */ public void testPCALearn() { System.out.println("PCA.learn"); int num = random.nextInt(100) + 10; ArrayList<Vector> data = new ArrayList<Vector>(num); final double r1 = random.nextDouble(); final double r2 = r1 / random.nextDouble(); for (int i = 0; i < num; i++) { data.add(VectorFactory.getDefault().createUniformRandom(INPUT_DIM, r1, r2, random)); } Vector mean = MultivariateStatisticsUtil.computeMean(data); DenseMatrix X = DenseMatrixFactoryMTJ.INSTANCE.createMatrix(INPUT_DIM, num); for (int n = 0; n < num; n++) { X.setColumn(n, data.get(n).minus(mean)); } final ArrayList<Vector> dataCopy = ObjectUtil.cloneSmartElementsAsArrayList(data); long startsvd = System.currentTimeMillis(); SingularValueDecomposition svd = SingularValueDecompositionMTJ.create(X); long stopsvd = System.currentTimeMillis(); long start = System.currentTimeMillis(); PrincipalComponentsAnalysis instance = this.createPCAInstance(); PrincipalComponentsAnalysisFunction f = instance.learn(data); long stop = System.currentTimeMillis(); assertEquals(dataCopy, data); System.out.println("Uhat:\n" + f.getDimensionReducer().getDiscriminant().transpose()); System.out.println("U:\n" + svd.getU()); System.out.println("Time taken: SVD = " + (stopsvd - startsvd) + ", PCA = " + (stop - start)); // Make sure the PCA algorithm subtracted off the sample mean if (mean.equals(f.getMean(), 1e-5) == false) { assertEquals(mean, f.getMean()); } assertEquals(OUTPUT_DIM, instance.getNumComponents()); assertEquals(instance.getNumComponents(), f.getOutputDimensionality()); assertEquals(INPUT_DIM, f.getInputDimensionality()); if (mean.equals(f.getMean(), 1e-5) == false) { assertEquals(mean, f.getMean()); } double absnorm = 0.0; int nc = instance.getNumComponents() * INPUT_DIM; for (int i = 0; i < instance.getNumComponents(); i++) { Vector uihat = f.getDimensionReducer().getDiscriminant().getRow(i); for (int j = 0; j < i; j++) { Vector ujhat = f.getDimensionReducer().getDiscriminant().getRow(j); assertEquals( "Dot product between " + i + " and " + j + " is too large!", 0.0, uihat.dotProduct(ujhat), 1e-2); } assertEquals(1.0, uihat.norm2(), 1e-5); Vector ui = svd.getU().getColumn(i); absnorm += Math.min(ui.minus(uihat).norm2(), ui.minus(uihat.scale(-1)).norm2()); } absnorm /= nc; System.out.println("U 1-norm: " + absnorm); assertEquals(0.0, absnorm, 1e-1); }
@Override public void convertFromVector(final Vector parameters) { parameters.assertDimensionalityEquals(2); this.setMean(parameters.getElement(0)); this.setScale(parameters.getElement(1)); }