/**
   * Standard 2D tracking model with the following state equation: {@latex[ D_ x_t = G x_ t-1} + A
   * \epsilon_t} Also, when angle != null, a constraint matrix is created for the state covariance,
   * with perpendicular variance a0Variance.
   *
   * @param gVariance
   * @param aVariance
   * @param a0Variance
   * @param angle
   */
  public Standard2DTrackingFilter(
      double gVariance, double aVariance, double a0Variance, Double angle) {

    super(
        VectorFactory.getDefault().createVector(4),
        createStateCovarianceMatrix(1d, aVariance, a0Variance, angle),
        MatrixFactory.getDefault().createIdentity(2, 2).scale(gVariance));

    this.aVariance = aVariance;
    this.gVariance = gVariance;
    this.a0Variance = a0Variance;
    this.angle = angle;

    final LinearDynamicalSystem model = new LinearDynamicalSystem(0, 4);

    final Matrix Gct = createStateTransitionMatrix(currentTimeDiff);
    final Matrix G = MatrixFactory.getDefault().createIdentity(4, 4);
    G.setSubMatrix(0, 0, Gct);

    model.setA(G);
    model.setB(MatrixFactory.getDefault().createMatrix(4, 4));
    model.setC(O);

    this.model = model;
  }
  /** Test of createUniformRandom method, of class MatrixFactory. */
  public void testCreateUniformRandom() {
    System.out.println("createUniformRandom");

    Matrix m = this.createRandomMatrix();
    MatrixFactory<?> factory = this.createFactory();

    int M = m.getNumRows();
    int N = m.getNumColumns();
    Matrix mr = factory.createUniformRandom(M, N, -RANGE, RANGE, random);
    assertNotNull(mr);
    assertNotSame(m, mr);
    assertEquals(M, mr.getNumRows());
    assertEquals(N, mr.getNumColumns());

    boolean nonzero = false;
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++) {
        double value = mr.getElement(i, j);
        if (value != 0.0) {
          nonzero = true;
          if ((value < -RANGE) || (value > RANGE)) {
            fail("Nonzero value outside the given range!");
          }
        }
      }
    }

    if (!nonzero) {
      fail("I didn't find any nonzero values in your random matrix!!");
    }
  }
  public void testCreateMatrixWithInitialValue() {
    Matrix matrix = this.createRandomMatrix();
    int M = matrix.getNumRows();
    int N = matrix.getNumColumns();

    MatrixFactory<?> factory = this.createFactory();
    double initialValue = this.random.nextGaussian();
    Matrix m3 = factory.createMatrix(M, N, initialValue);
    assertNotNull(m3);
    assertEquals(M, m3.getNumRows());
    assertEquals(N, m3.getNumColumns());

    boolean hasNonZero = false;
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++) {
        double value = m3.getElement(i, j);

        if (value != 0.0) {
          hasNonZero = true;
          assertEquals(initialValue, value, 0.0);
        }
      }
    }

    assertTrue(hasNonZero);
  }
  /** Test of copyArray method, of class MatrixFactory. */
  public void testCopyArray() {
    System.out.println("copyArray");

    MatrixFactory<?> instance = this.createFactory();
    Matrix matrix = this.createRandomMatrix();
    int M = matrix.getNumRows();
    int N = matrix.getNumColumns();
    double[][] values = new double[M][N];
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++) {
        values[i][j] = matrix.getElement(i, j);
      }
    }

    Matrix copy = instance.copyArray(values);
    assertNotNull(copy);
    assertNotSame(matrix, copy);
    assertEquals(matrix, copy);

    matrix.scaleEquals(random.nextDouble());
    assertFalse(matrix.equals(copy));

    Matrix m00 = instance.copyArray(new double[0][0]);
    assertEquals(0, m00.getNumRows());
    assertEquals(0, m00.getNumColumns());
  }
 @Override
 public Matrix init(int rows, int cols) {
   final SparseMatrix rand = (SparseMatrix) smf.createUniformRandom(rows, cols, min, max, random);
   final Matrix ret = smf.createMatrix(rows, cols);
   for (int i = 0; i < rows; i++) {
     if (this.random.nextDouble() > sparcity) {
       ret.setRow(i, rand.getRow(i));
     }
   }
   return ret;
 }
  /** Test of copyMatrix method, of class MatrixFactory. */
  public void testCopyMatrix() {
    System.out.println("copyMatrix");
    MatrixFactory<?> instance = this.createFactory();

    Matrix matrix = this.createRandomMatrix();
    assertNotNull(matrix);
    Matrix copy = instance.copyMatrix(matrix);
    assertNotNull(copy);
    assertNotSame(matrix, copy);
    assertEquals(matrix, copy);

    matrix.scaleEquals(random.nextDouble());
    assertFalse(matrix.equals(copy));
  }
  @Override
  protected boolean initializeAlgorithm() {
    // Due to bizarre inheretance, this.data is the function to minimize...
    DifferentiableEvaluator<? super Vector, Double, Vector> f = this.data;

    this.result =
        new DefaultInputOutputPair<Vector, Double>(
            this.initialGuess.clone(), f.evaluate(this.initialGuess));

    this.gradient = f.differentiate(this.initialGuess);

    // Load up the line function with the current direction and
    // the search direction, which is the negative gradient, in other words
    // the direction of steepest descent
    this.lineFunction =
        new DirectionalVectorToDifferentiableScalarFunction(
            f, this.result.getInput(), this.gradient.scale(-1.0));

    this.dimensionality = this.initialGuess.getDimensionality();
    this.hessianInverse =
        MatrixFactory.getDefault()
            .createIdentity(this.dimensionality, this.dimensionality)
            .scale(0.5)
    //            .scale( Math.sqrt(this.getTolerance()) )
    ;

    return true;
  }
示例#8
0
  /** Path & edge are the same, directions are reverse, neg. to pos. */
  @Test
  public void testPathStateConvert7() {

    final Path startPath =
        TestUtils.makeTmpPath(
            graph,
            true,
            new Coordinate(20, -10),
            new Coordinate(10, -10),
            new Coordinate(10, 0),
            new Coordinate(0, 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 Path newPath =
        TestUtils.makeTmpPath(graph, false, new Coordinate(10, 0), new Coordinate(0, 0));

    final PathStateDistribution result = currentBelief.convertToPath(newPath);

    AssertJUnit.assertEquals("distance", 7.5d, result.getMean().getElement(0), 0d);
    AssertJUnit.assertEquals("velocity", 1d, result.getMean().getElement(1), 0d);
  }
示例#9
0
  @Test
  public void testPathStateConvert2() {
    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.getDenseDefault().createVector2D(0d, 1d), covar);
    final PathStateDistribution currentBelief = new PathStateDistribution(startPath, startBelief);

    final Vector groundLoc =
        MotionStateEstimatorPredictor.getOg()
            .times(currentBelief.getGroundDistribution().getMean());
    AssertJUnit.assertEquals("initial state x", 0d, groundLoc.getElement(0), 0d);
    AssertJUnit.assertEquals("initial state y", 0d, groundLoc.getElement(1), 0d);

    final Path newPath =
        TestUtils.makeTmpPath(
            graph,
            true,
            new Coordinate(20, -10),
            new Coordinate(10, -10),
            new Coordinate(10, 0),
            new Coordinate(0, 0));

    final PathStateDistribution result = currentBelief.convertToPath(newPath);

    AssertJUnit.assertEquals("distance", -0d, result.getMean().getElement(0), 0d);
    AssertJUnit.assertEquals("velocity", -1d, result.getMean().getElement(1), 0d);
  }
public class SparseRowRandomInitStrategy implements InitStrategy {
  MatrixFactory<? extends Matrix> smf = MatrixFactory.getSparseDefault();
  private double min;
  private double max;
  private Random random;
  private double sparcity;

  public SparseRowRandomInitStrategy(double min, double max, double sparcity, Random random) {
    this.min = min;
    this.max = max;
    this.random = random;
    this.sparcity = sparcity;
  }

  @Override
  public Matrix init(int rows, int cols) {
    final SparseMatrix rand = (SparseMatrix) smf.createUniformRandom(rows, cols, min, max, random);
    final Matrix ret = smf.createMatrix(rows, cols);
    for (int i = 0; i < rows; i++) {
      if (this.random.nextDouble() > sparcity) {
        ret.setRow(i, rand.getRow(i));
      }
    }
    return ret;
  }
}
  private static Matrix getVarianceConstraintMatrix(
      double aVariance, double a0Variance, Double angle) {

    if (angle == null) return MatrixFactory.getDefault().createIdentity(2, 2).scale(aVariance);

    final Matrix rotationMatrix = MatrixFactory.getDefault().createIdentity(2, 2);
    rotationMatrix.setElement(0, 0, Math.cos(angle));
    rotationMatrix.setElement(0, 1, -Math.sin(angle));
    rotationMatrix.setElement(1, 0, Math.sin(angle));
    rotationMatrix.setElement(1, 1, Math.cos(angle));

    final Matrix temp =
        MatrixFactory.getDefault()
            .createDiagonal(
                VectorFactory.getDefault().copyArray(new double[] {a0Variance, aVariance}));
    return rotationMatrix.times(temp).times(rotationMatrix.transpose());
  }
  private static Matrix createStateTransitionMatrix(double timeDiff) {

    final Matrix Gct = MatrixFactory.getDefault().createIdentity(4, 4);
    Gct.setElement(0, 1, timeDiff);
    Gct.setElement(2, 3, timeDiff);

    return Gct;
  }
 @Override
 public Matrix init(int rows, int cols) {
   final Matrix ret = smf.createMatrix(rows, cols);
   for (int i = 0; i < rows; i++) {
     for (int j = 0; j < cols; j++) {
       if (this.random.nextDouble() > sparcity) ret.setElement(i, j, 1d);
     }
   }
   return ret;
 }
示例#14
0
 /**
  * Creates a uniform transition-probability Matrix
  *
  * @param numStates Number of states to create the Matrix for
  * @return Uniform probability Matrix.
  */
 protected static Matrix createUniformTransitionProbability(int numStates) {
   Matrix A = MatrixFactory.getDefault().createMatrix(numStates, numStates);
   final double p = 1.0 / numStates;
   for (int i = 0; i < numStates; i++) {
     for (int j = 0; j < numStates; j++) {
       A.setElement(i, j, p);
     }
   }
   return A;
 }
    /**
     * Computes the pair-wise confidence test results
     *
     * @param data Data from each treatment to consider
     * @param pairwiseTest Confidence test used for pair-wise null-hypothesis tests.
     */
    protected void computePairwiseTestResults(
        final Collection<? extends Collection<? extends Number>> data,
        final NullHypothesisEvaluator<Collection<? extends Number>> pairwiseTest) {

      ArrayList<? extends Collection<? extends Number>> treatments =
          CollectionUtil.asArrayList(data);

      final int K = treatments.size();
      Matrix Z = MatrixFactory.getDefault().createMatrix(K, K);
      Matrix P = MatrixFactory.getDefault().createMatrix(K, K);
      ArrayList<ArrayList<ConfidenceStatistic>> stats =
          new ArrayList<ArrayList<ConfidenceStatistic>>(K);
      for (int i = 0; i < K; i++) {
        ArrayList<ConfidenceStatistic> comp = new ArrayList<ConfidenceStatistic>(K);
        for (int j = 0; j < K; j++) {
          comp.add(null);
        }
        stats.add(comp);
      }

      for (int i = 0; i < K; i++) {
        // Comparisons to ourselves are perfect
        Z.setElement(i, i, 0.0);
        P.setElement(i, i, 1.0);
        Collection<? extends Number> datai = treatments.get(i);
        for (int j = i + 1; j < K; j++) {
          Collection<? extends Number> dataj = treatments.get(j);
          ConfidenceStatistic comparison = pairwiseTest.evaluateNullHypothesis(datai, dataj);
          final double pij = comparison.getNullHypothesisProbability();
          final double zij = comparison.getTestStatistic();
          Z.setElement(i, j, zij);
          Z.setElement(j, i, zij);
          P.setElement(i, j, pij);
          P.setElement(j, i, pij);
          stats.get(i).set(j, comparison);
          stats.get(j).set(i, comparison);
        }
      }

      this.testStatistics = Z;
      this.nullHypothesisProbabilities = P;
      this.pairwiseTestStatistics = stats;
    }
  /** Test of createMatrix method, of class MatrixFactory. */
  public void testCreateMatrix() {
    System.out.println("createMatrix");

    Matrix matrix = this.createRandomMatrix();
    int M = matrix.getNumRows();
    int N = matrix.getNumColumns();

    MatrixFactory<?> factory = this.createFactory();
    Matrix m2 = factory.createMatrix(M, N);
    assertNotNull(m2);
    assertNotSame(matrix, m2);

    assertEquals(M, m2.getNumRows());
    assertEquals(N, m2.getNumColumns());
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++) {
        assertEquals(0.0, m2.getElement(i, j));
      }
    }
  }
  @Override
  public void performExperiment() throws IOException {
    BilinearLearnerParameters params = new BilinearLearnerParameters();
    params.put(BilinearLearnerParameters.ETA0_U, 0.02);
    params.put(BilinearLearnerParameters.ETA0_W, 0.02);
    params.put(BilinearLearnerParameters.LAMBDA, 0.001);
    params.put(BilinearLearnerParameters.BICONVEX_TOL, 0.01);
    params.put(BilinearLearnerParameters.BICONVEX_MAXITER, 10);
    params.put(BilinearLearnerParameters.BIAS, true);
    params.put(BilinearLearnerParameters.ETA0_BIAS, 0.5);
    params.put(BilinearLearnerParameters.WINITSTRAT, new SingleValueInitStrat(0.1));
    params.put(BilinearLearnerParameters.UINITSTRAT, new SparseZerosInitStrategy());
    BillMatlabFileDataGenerator bmfdg =
        new BillMatlabFileDataGenerator(new File(BILL_DATA()), 98, true);
    prepareExperimentLog(params);
    for (int i = 0; i < bmfdg.nFolds(); i++) {
      logger.debug("Fold: " + i);
      BilinearSparseOnlineLearner learner = new BilinearSparseOnlineLearner(params);
      learner.reinitParams();

      bmfdg.setFold(i, Mode.TEST);
      List<Pair<Matrix>> testpairs = new ArrayList<Pair<Matrix>>();
      while (true) {
        Pair<Matrix> next = bmfdg.generate();
        if (next == null) break;
        testpairs.add(next);
      }
      logger.debug("...training");
      bmfdg.setFold(i, Mode.TRAINING);
      int j = 0;
      while (true) {
        Pair<Matrix> next = bmfdg.generate();
        if (next == null) break;
        logger.debug("...trying item " + j++);
        learner.process(next.firstObject(), next.secondObject());
        Matrix u = learner.getU();
        Matrix w = learner.getW();
        Matrix bias = MatrixFactory.getDenseDefault().copyMatrix(learner.getBias());
        BilinearEvaluator eval = new RootMeanSumLossEvaluator();
        eval.setLearner(learner);
        double loss = eval.evaluate(testpairs);
        logger.debug(String.format("Saving learner, Fold %d, Item %d", i, j));
        File learnerOut = new File(FOLD_ROOT(i), String.format("learner_%d", j));
        IOUtils.writeBinary(learnerOut, learner);
        logger.debug("W row sparcity: " + SandiaMatrixUtils.rowSparcity(w));
        logger.debug("U row sparcity: " + SandiaMatrixUtils.rowSparcity(u));
        Boolean biasMode = learner.getParams().getTyped(BilinearLearnerParameters.BIAS);
        if (biasMode) {
          logger.debug("Bias: " + SandiaMatrixUtils.diag(bias));
        }
        logger.debug(String.format("... loss: %f", loss));
      }
    }
  }
 /** Test of createDiagonal method, of class MatrixFactory. */
 public void testCreateDiagonal() {
   System.out.println("createDiagonal");
   Vector diagonal =
       VectorFactory.getDefault()
           .copyValues(random.nextGaussian(), random.nextGaussian(), random.nextGaussian());
   int M = diagonal.getDimensionality();
   MatrixFactory<?> instance = this.createFactory();
   Matrix diag = instance.createDiagonal(diagonal);
   assertNotNull(diag);
   assertEquals(M, diag.getNumRows());
   assertEquals(M, diag.getNumColumns());
   for (int i = 0; i < M; i++) {
     for (int j = 0; j < M; j++) {
       if (i == j) {
         assertEquals(diagonal.getElement(i), diag.getElement(i, j));
       } else {
         assertEquals(0.0, diag.getElement(i, j));
       }
     }
   }
 }
  /** Test of copyColumnVectors method, of class MatrixFactory. */
  public void testCopyColumnVectors_VectorizableArr() {
    System.out.println("copyColumnVectors");
    Matrix m = this.createRandomMatrix();
    int M = m.getNumRows();
    int N = m.getNumColumns();

    Vector[] cols = new Vector[N];
    for (int j = 0; j < N; j++) {
      cols[j] = m.getColumn(j);
    }

    MatrixFactory<?> factory = this.createFactory();
    @SuppressWarnings("unchecked")
    Matrix mr = factory.copyColumnVectors(cols);
    assertNotNull(mr);
    assertNotSame(m, mr);
    assertEquals(m, mr);
    for (int j = 0; j < N; j++) {
      assertEquals(m.getColumn(j), mr.getColumn(j));
    }
  }
  /** Test of copyColumnVectors method, of class MatrixFactory. */
  public void testCopyColumnVectors_Collection() {
    System.out.println("copyColumnVectors");
    Matrix m = this.createRandomMatrix();
    int M = m.getNumRows();
    int N = m.getNumColumns();

    ArrayList<Vector> cols = new ArrayList<Vector>(M);
    for (int j = 0; j < N; j++) {
      cols.add(m.getColumn(j));
    }

    MatrixFactory<?> factory = this.createFactory();
    @SuppressWarnings("unchecked")
    Matrix mr = factory.copyColumnVectors(cols);
    assertNotNull(mr);
    assertNotSame(m, mr);
    assertEquals(m, mr);
    for (int j = 0; j < N; j++) {
      assertEquals(m.getColumn(j), mr.getColumn(j));
    }
  }
  /** Test of copyRowVectors method, of class MatrixFactory. */
  public void testCopyRowVectors_VectorizableArr() {
    System.out.println("copyRowVectors");
    Matrix m = this.createRandomMatrix();
    int M = m.getNumRows();
    int N = m.getNumColumns();

    Vector[] rows = new Vector[M];
    for (int i = 0; i < M; i++) {
      rows[i] = m.getRow(i);
    }

    MatrixFactory<?> factory = this.createFactory();
    @SuppressWarnings("unchecked")
    Matrix mr = factory.copyRowVectors(rows);
    assertNotNull(mr);
    assertNotSame(m, mr);
    assertEquals(m, mr);
    for (int i = 0; i < M; i++) {
      assertEquals(m.getRow(i), mr.getRow(i));
    }
  }
 /** Test of createIdentity method, of class MatrixFactory. */
 public void testCreateIdentity() {
   System.out.println("createIdentity");
   Matrix matrix = this.createRandomMatrix();
   int M = matrix.getNumRows();
   int N = matrix.getNumColumns();
   MatrixFactory<?> instance = this.createFactory();
   Matrix ident = instance.createIdentity(M, N);
   assertNotNull(ident);
   assertNotSame(matrix, ident);
   assertEquals(M, ident.getNumRows());
   assertEquals(N, ident.getNumColumns());
   for (int i = 0; i < M; i++) {
     for (int j = 0; j < N; j++) {
       if (i == j) {
         assertEquals(1.0, ident.getElement(i, j));
       } else {
         assertEquals(0.0, ident.getElement(i, j));
       }
     }
   }
 }
  private static Matrix createStateCovarianceMatrix(
      double timeDiff, double aVariance, double a0Variance, Double angle) {
    final Matrix A_half = MatrixFactory.getDefault().createMatrix(4, 2);
    A_half.setElement(0, 0, Math.pow(timeDiff, 2) / 2d);
    A_half.setElement(1, 0, timeDiff);
    A_half.setElement(2, 1, Math.pow(timeDiff, 2) / 2d);
    A_half.setElement(3, 1, timeDiff);

    final Matrix Q = getVarianceConstraintMatrix(aVariance, a0Variance, angle);
    final Matrix A = A_half.times(Q).times(A_half.transpose());
    return A;
  }
示例#24
0
 public static Matrix vstack(MatrixFactory<? extends Matrix> matrixFactory, Matrix... matricies) {
   int nrows = 0;
   int ncols = 0;
   for (Matrix matrix : matricies) {
     nrows += matrix.getNumRows();
     ncols = matrix.getNumColumns();
   }
   Matrix ret = matrixFactory.createMatrix(nrows, ncols);
   int currentRow = 0;
   for (Matrix matrix : matricies) {
     ret.setSubMatrix(currentRow, 0, matrix);
     currentRow += matrix.getNumRows();
   }
   return ret;
 }
public class SparseOnesInitStrategy implements InitStrategy {
  MatrixFactory<? extends Matrix> smf = MatrixFactory.getSparseDefault();
  private Random random;
  private double sparcity;

  public SparseOnesInitStrategy(double sparcity, Random random) {
    this.random = random;
    this.sparcity = sparcity;
  }

  @Override
  public Matrix init(int rows, int cols) {
    final Matrix ret = smf.createMatrix(rows, cols);
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        if (this.random.nextDouble() > sparcity) ret.setElement(i, j, 1d);
      }
    }
    return ret;
  }
}
示例#26
0
  @Test(enabled = false, expectedExceptions = IllegalStateException.class)
  public void testBadConversion() {
    final Path startPath =
        TestUtils.makeTmpPath(graph, true, new Coordinate(20, -10), new Coordinate(10, -10));

    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(-0d, -5d / 30d), covar);
    final PathStateDistribution currentBelief = new PathStateDistribution(startPath, startBelief);

    final Path newPath =
        TestUtils.makeTmpPath(
            graph,
            true,
            new Coordinate(20, -10),
            new Coordinate(10, -10),
            new Coordinate(10, 0),
            new Coordinate(0, 0));

    PathUtils.checkAndGetConvertedBelief(currentBelief, newPath);
  }
 /** Test of getDiagonalDefault method, of class MatrixFactory. */
 public void testGetDiagonalDefault() {
   System.out.println("getDiagonalDefault");
   MatrixFactory<? extends DiagonalMatrix> result = MatrixFactory.getDiagonalDefault();
   assertSame(MatrixFactory.DEFAULT_DIAGONAL_INSTANCE, result);
 }
 /** Test of getSparseDefault method, of class MatrixFactory. */
 public void testGetSparseDefault() {
   System.out.println("getDefault");
   MatrixFactory<? extends Matrix> result = MatrixFactory.getSparseDefault();
   assertSame(MatrixFactory.DEFAULT_SPARSE_INSTANCE, result);
 }
 static {
   O = MatrixFactory.getDefault().createMatrix(2, 4);
   O.setElement(0, 0, 1);
   O.setElement(1, 2, 1);
 }
示例#30
0
 public static Matrix vstack(Matrix... matricies) {
   return vstack(MatrixFactory.getDefault(), matricies);
 }