Exemple #1
0
    public double apply(int first, int second, double third) {
      //            System.out.println("checking = " + min_val + " versus " +
      // (third/count_matrix.getQuick(first,second)));
      if (third / count_matrix.getQuick(first, second) > max_val) {
        max_m = first;
        max_n = second;
        max_val = third / count_matrix.getQuick(first, second);
      }

      return third;
    }
Exemple #2
0
  private static double[] solution(DoubleMatrix2D X, DoubleMatrix2D Y, int k) {
    // Solve X * Beta = Y for Beta
    // Only the first column of Y is used
    // k is number of beta coefficients

    QRDecomposition qr = new QRDecomposition(X);

    if (qr.hasFullRank()) {
      DoubleMatrix2D B = qr.solve(Y);
      return B.viewColumn(0).toArray();

    } else {
      DoubleMatrix1D Y0 = Y.viewColumn(0); // first column of Y
      SingularValueDecomposition svd = new SingularValueDecomposition(X);
      DoubleMatrix2D S = svd.getS();
      DoubleMatrix2D V = svd.getV();
      DoubleMatrix2D U = svd.getU();
      Algebra alg = new Algebra();
      DoubleMatrix2D Ut = alg.transpose(U);
      DoubleMatrix1D g = alg.mult(Ut, Y0); // Ut*Y0

      for (int j = 0; j < k; j++) {
        // solve S*p = g for p;  S is a diagonal matrix
        double x = S.getQuick(j, j);
        if (x > 0.) {
          x = g.getQuick(j) / x; // p[j] = g[j]/S[j]
          g.setQuick(j, x); // overwrite g by p
        } else g.setQuick(j, 0.);
      }
      DoubleMatrix1D beta = alg.mult(V, g); // V*p
      return beta.toArray();
    }
  }
  /**
   * Computes the density function <SPAN CLASS="MATH"><I>f</I> (<I>x</I>)</SPAN>, with <SPAN
   * CLASS="MATH"><I>&#955;</I><SUB>i</SUB> =</SPAN> <TT>lambda[<SPAN CLASS="MATH"><I>i</I> -
   * 1</SPAN>]</TT>, <SPAN CLASS="MATH"><I>i</I> = 1,&#8230;, <I>k</I></SPAN>.
   *
   * @param lambda rates of the hypoexponential distribution
   * @param x value at which the density is evaluated
   * @return density at <SPAN CLASS="MATH"><I>x</I></SPAN>
   */
  public static double density(double[] lambda, double x) {
    testLambda(lambda);
    if (x < 0) return 0;
    DoubleMatrix2D Ax = buildMatrix(lambda, x);
    DoubleMatrix2D M = DMatrix.expBidiagonal(Ax);

    int k = lambda.length;
    return lambda[k - 1] * M.getQuick(0, k - 1);
  }
Exemple #4
0
  static boolean computeLogMi(
      FeatureGenerator featureGen,
      double lambda[],
      DoubleMatrix2D Mi_YY,
      DoubleMatrix1D Ri_Y,
      boolean takeExp,
      boolean reuseM,
      boolean initMDone) {

    if (reuseM && initMDone) {
      Mi_YY = null;
    } else initMDone = false;
    if (Mi_YY != null) Mi_YY.assign(0);
    Ri_Y.assign(0);
    while (featureGen.hasNext()) {
      Feature feature = featureGen.next();
      int f = feature.index();
      int yp = feature.y();
      int yprev = feature.yprev();
      float val = feature.value();
      //	    System.out.println(feature.toString());

      if (yprev < 0) {
        // this is a single state feature.
        double oldVal = Ri_Y.getQuick(yp);
        Ri_Y.setQuick(yp, oldVal + lambda[f] * val);
      } else if (Mi_YY != null) {
        Mi_YY.setQuick(yprev, yp, Mi_YY.getQuick(yprev, yp) + lambda[f] * val);
        initMDone = true;
      }
    }
    if (takeExp) {
      for (int r = Ri_Y.size() - 1; r >= 0; r--) {
        Ri_Y.setQuick(r, expE(Ri_Y.getQuick(r)));
        if (Mi_YY != null)
          for (int c = Mi_YY.columns() - 1; c >= 0; c--) {
            Mi_YY.setQuick(r, c, expE(Mi_YY.getQuick(r, c)));
          }
      }
    }
    return initMDone;
  }
  /**
   * Constructs and returns a new eigenvalue decomposition object; The decomposed matrices can be
   * retrieved via instance methods of the returned decomposition object. Checks for symmetry, then
   * constructs the eigenvalue decomposition.
   *
   * @param A A square matrix.
   * @return A decomposition object to access <tt>D</tt> and <tt>V</tt>.
   * @throws IllegalArgumentException if <tt>A</tt> is not square.
   */
  public EigenvalueDecomposition(DoubleMatrix2D A) {
    Property.DEFAULT.checkSquare(A);

    n = A.columns();
    V = new double[n][n];
    d = new double[n];
    e = new double[n];

    issymmetric = Property.DEFAULT.isSymmetric(A);

    if (issymmetric) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          V[i][j] = A.getQuick(i, j);
        }
      }

      // Tridiagonalize.
      tred2();

      // Diagonalize.
      tql2();

    } else {
      H = new double[n][n];
      ort = new double[n];

      for (int j = 0; j < n; j++) {
        for (int i = 0; i < n; i++) {
          H[i][j] = A.getQuick(i, j);
        }
      }

      // Reduce to Hessenberg form.
      orthes();

      // Reduce Hessenberg to real Schur form.
      hqr2();
    }
  }
  /**
   * Computes the complementary distribution <SPAN CLASS="MATH">bar(F)(<I>x</I>)</SPAN>, with <SPAN
   * CLASS="MATH"><I>&#955;</I><SUB>i</SUB> =</SPAN> <TT>lambda[<SPAN CLASS="MATH"><I>i</I> -
   * 1</SPAN>]</TT>, <SPAN CLASS="MATH"><I>i</I> = 1,&#8230;, <I>k</I></SPAN>.
   *
   * @param lambda rates of the hypoexponential distribution
   * @param x value at which the complementary distribution is evaluated
   * @return complementary distribution at <SPAN CLASS="MATH"><I>x</I></SPAN>
   */
  public static double barF(double[] lambda, double x) {
    testLambda(lambda);
    if (x <= 0.0) return 1.0;
    if (x >= Double.MAX_VALUE) return 0.0;
    DoubleMatrix2D M = buildMatrix(lambda, x);
    M = DMatrix.expBidiagonal(M);

    // prob is first row of final matrix
    int k = lambda.length;
    double sum = 0;
    for (int j = 0; j < k; j++) sum += M.getQuick(0, j);
    return sum;
  }
  @Override
  public DoubleMatrix getEigenvectors() {
    DoubleMatrix2D V = myDecomposition.getV();
    int nrows = V.rows();
    int ncols = V.columns();
    DoubleMatrix dm = DoubleMatrixFactory.DEFAULT.make(nrows, ncols);

    for (int r = 0; r < nrows; r++) {
      for (int c = 0; c < ncols; c++) {
        dm.set(r, c, V.getQuick(r, c));
      }
    }

    return dm;
  }