Beispiel #1
0
void
HMM::cacheProbabilities(const arma::mat & data) {
  for (unsigned int i = 0; i < N_; ++i) {
    arma::vec weights = BModels_[i].getWeights();
    for (unsigned int l = 0; l < weights.n_elem; ++l) {
      const GM & gm = BModels_[i].getGM(l);
      gammaLts_[i].col(l) = weights(l) * arma::trans(gm.getDataProb(data));
    }
    B_.col(i) = arma::sum(gammaLts_[i],1);
  }
  if (!B_.is_finite()) {
    //B_.print("B");
    for (unsigned int i = 0; i < N_; ++i) {
      arma::vec test = B_.col(i);
      if (!test.is_finite()) {
        arma::vec weights = BModels_[i].getWeights();
        for (unsigned l = 0; l < weights.n_elem; ++l) {
        arma::vec test2 = gammaLts_[i].col(l); 
          if (!test2.is_finite()) {
            const GM & gm = BModels_[i].getGM(l);
            gm.print("gm");
            arma::vec test3 = arma::trans(gm.getDataProb(data));
            //test3.print("test3");
          }
        }
      }
    }
    throw std::runtime_error("probabilities not finite");
  }
}
Beispiel #2
0
 /** For debugging reasons*/
 bool sanityCheck() const {
   bool sane = false;
   if (D_ == 0 || arma::det(sigma_) == 0 || !invSigma_.is_finite()) sane = false;
   if (!sane) {
     std::cout << "coeff: " << coeff_ << std::endl;
     std::cout << "D: " << D_ << std::endl;
     sigma_.print("sigma");
     invSigma_.print("invSigma");
     throw std::logic_error("Something is wrong with the GM");
   }
   return sane;
 }
Beispiel #3
0
unsigned int optCoef(arma::mat& weights, const arma::icube& obs, const arma::cube& emission,
    const arma::mat& initk, const arma::cube& beta, const arma::mat& scales, arma::mat& coef,
    const arma::mat& X, const arma::ivec& cumsumstate, const arma::ivec& numberOfStates,
    int trace) {

  int iter = 0;
  double change = 1.0;
  while ((change > 1e-10) & (iter < 100)) {
    arma::vec tmpvec(X.n_cols * (weights.n_rows - 1));
    bool solve_ok = arma::solve(tmpvec, hCoef(weights, X),
        gCoef(obs, beta, scales, emission, initk, weights, X, cumsumstate, numberOfStates));
    if (solve_ok == false) {
      return (4);
    }

    arma::mat coefnew(coef.n_rows, coef.n_cols - 1);
    for (unsigned int i = 0; i < (weights.n_rows - 1); i++) {
      coefnew.col(i) = coef.col(i + 1) - tmpvec.subvec(i * X.n_cols, (i + 1) * X.n_cols - 1);
    }
    change = arma::accu(arma::abs(coef.submat(0, 1, coef.n_rows - 1, coef.n_cols - 1) - coefnew))
        / coefnew.n_elem;
    coef.submat(0, 1, coef.n_rows - 1, coef.n_cols - 1) = coefnew;
    iter++;
    if (trace == 3) {
      Rcout << "coefficient optimization iter: " << iter;
      Rcout << " new coefficients: " << std::endl << coefnew << std::endl;
      Rcout << " relative change: " << change << std::endl;
    }
    weights = exp(X * coef).t();
    if (!weights.is_finite()) {
      return (5);
    }
    weights.each_row() /= sum(weights, 0);

  }
  return (0);
}