/**
   * 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;
  }
  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;
  }
 /**
  * Evaluates the predictive observation likelihood given the predictive state distribution.
  *
  * @param obs
  * @param belief
  * @return
  */
 public double predictiveLikelihood(Vector obs, MultivariateGaussian belief) {
   Matrix Q = belief.getCovariance();
   Q = this.model.getC().times(Q).times(this.model.getC().transpose());
   Q.plusEquals(this.measurementCovariance);
   final MultivariateGaussian.PDF pdf =
       new MultivariateGaussian.PDF(this.model.getC().times(belief.getMean()), Q);
   return pdf.evaluate(obs);
 }
Пример #4
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;
 }
Пример #5
0
 public static Matrix plusInplace(Matrix mat, double etat) {
   int nrows = mat.getNumRows();
   int ncols = mat.getNumColumns();
   for (int r = 0; r < nrows; r++) {
     for (int c = 0; c < ncols; c++) {
       mat.setElement(r, c, mat.getElement(r, c) + etat);
     }
   }
   return mat;
 }
Пример #6
0
 public static double colSparcity(Matrix mat) {
   double ncols = mat.getNumColumns();
   double nsparse = 0;
   for (int c = 0; c < ncols; c++) {
     if (mat.getColumn(c).sum() == 0) {
       nsparse++;
     }
   }
   return nsparse / ncols;
 }
Пример #7
0
 public static double rowSparcity(Matrix mat) {
   double nrows = mat.getNumRows();
   double nsparse = 0;
   for (int r = 0; r < nrows; r++) {
     if (mat.getRow(r).sum() == 0) {
       nsparse++;
     }
   }
   return nsparse / nrows;
 }
 @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;
 }
Пример #9
0
 public static double absSum(Matrix mat) {
   double tot = 0;
   int nrows = mat.getNumRows();
   int ncols = mat.getNumColumns();
   for (int r = 0; r < nrows; r++) {
     for (int c = 0; c < ncols; c++) {
       tot += Math.abs(mat.getElement(r, c));
     }
   }
   return tot;
 }
 @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;
 }
Пример #11
0
  /**
   * 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);
  }
  /**
   * Creates a new {@code MatrixBasedTermSimilarityNetwork}.
   *
   * @param termIndex The index of terms that contains the nodes of the network.
   * @param similarities The square matrix of similarities between terms. Must have a number of rows
   *     and columns equal to the number of terms in the term index.
   */
  public MatrixBasedTermSimilarityNetwork(final TermIndex termIndex, final Matrix similarities) {
    super();

    if (similarities.getNumRows() != termIndex.getTermCount()
        || similarities.getNumColumns() != termIndex.getTermCount()) {
      throw new DimensionalityMismatchException(
          "the number of terms in the term index must match the "
              + "dimensions of the square similarities matrix");
    }

    this.setTermIndex(termIndex);
    this.setSimilarities(similarities);
  }
Пример #13
0
  public static Matrix asMat(MLArray mlArray) {
    MLDouble mlArrayDbl = (MLDouble) mlArray;
    int rows = mlArray.getM();
    int cols = mlArray.getN();

    Matrix mat = SparseMatrixFactoryMTJ.INSTANCE.createMatrix(rows, cols);

    for (int r = 0; r < rows; r++) {
      for (int c = 0; c < cols; c++) {
        mat.setElement(r, c, mlArrayDbl.get(r, c));
      }
    }
    return mat;
  }
Пример #14
0
  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;
  }
Пример #15
0
 /**
  * Setter for transitionProbability.
  *
  * @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 void setTransitionProbability(Matrix transitionProbability) {
   if (!transitionProbability.isSquare()) {
     throw new IllegalArgumentException("Transition Probability must be square");
   }
   this.normalizeTransitionMatrix(transitionProbability);
   this.transitionProbability = transitionProbability;
 }
Пример #16
0
 /**
  * Normalizes a column of the transition-probability matrix
  *
  * @param A Transition probability matrix to normalize, modified by side effect
  * @param j Column of the matrix to normalize
  */
 protected static void normalizeTransitionMatrix(Matrix A, final int j) {
   double sum = 0.0;
   final int k = A.getNumRows();
   for (int i = 0; i < k; i++) {
     final double value = A.getElement(i, j);
     if (value < 0.0) {
       throw new IllegalArgumentException("Transition Probabilities must be >= 0.0");
     }
     sum += A.getElement(i, j);
   }
   if (sum <= 0.0) {
     sum = 1.0;
   }
   if (sum != 1.0) {
     for (int i = 0; i < k; i++) {
       A.setElement(i, j, A.getElement(i, j) / sum);
     }
   }
 }
  @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;

  }
  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;
  }
  public double sumLoss(
      List<Pair<Matrix>> pairs, Matrix u, Matrix w, Matrix bias, BilinearLearnerParameters params) {
    LossFunction loss = params.getTyped(BilinearLearnerParameters.LOSS);
    loss = new MatLossFunction(loss);
    double total = 0;
    int i = 0;
    int ntasks = 0;
    for (Pair<Matrix> pair : pairs) {
      Matrix X = pair.firstObject();
      Matrix Y = pair.secondObject();
      SparseMatrix Yexp = BilinearSparseOnlineLearner.expandY(Y);
      Matrix expectedAll = u.transpose().times(X.transpose()).times(w);
      loss.setY(Yexp);
      loss.setX(expectedAll);
      if (bias != null) loss.setBias(bias);
      logger.debug("Testing pair: " + i);
      total += loss.eval(null); // Assums an identity w.
      i++;
      ntasks += Y.getNumColumns();
    }
    total /= ntasks;

    return Math.sqrt(total);
  }
Пример #20
0
 public static Matrix abs(Matrix mat) {
   Matrix ret = mat.clone();
   int nrows = ret.getNumRows();
   int ncols = ret.getNumColumns();
   for (int r = 0; r < nrows; r++) {
     for (int c = 0; c < ncols; c++) {
       ret.setElement(r, c, Math.abs(mat.getElement(r, c)));
     }
   }
   return ret;
 }
Пример #21
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;
 }
  @Test
  public void testMatlabFile() throws IOException {
    MatlabFileDataGenerator gen = new MatlabFileDataGenerator(matfile);
    int nusers = -1;
    int nwords = -1;
    int ntasks = -1;
    for (int i = 0; i < gen.size(); i++) {
      Pair<Matrix> XY = gen.generate();
      Matrix X = XY.firstObject();
      Matrix Y = XY.secondObject();

      if (nusers == -1) {
        nusers = X.getNumColumns();
        nwords = X.getNumRows();
        ntasks = Y.getNumColumns();
      } else {
        assertTrue(nusers == X.getNumColumns());
        assertTrue(nwords == X.getNumRows());
        assertTrue(ntasks == Y.getNumColumns());
      }
    }
  }
  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());
  }
    /**
     * 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;
    }
 static {
   O = MatrixFactory.getDefault().createMatrix(2, 4);
   O.setElement(0, 0, 1);
   O.setElement(1, 2, 1);
 }
Пример #26
0
 // Note this method isn't static, because we want to override to make
 // it parallelized --krdixon
 protected void normalizeTransitionMatrix(Matrix A) {
   final int k = A.getNumColumns();
   for (int j = 0; j < k; j++) {
     normalizeTransitionMatrix(A, j);
   }
 }