static void mPower (double *A, int eA, double *V, int *eV, int m, int n)
{
  double *B;
  int eB, i;
  if (n == 1) {
    for (i = 0; i < m * m; i++)
      V[i] = A[i];
    *eV = eA;
    return;
  }
  mPower (A, eA, V, eV, m, n / 2);
  B = (double *) malloc ((m * m) * sizeof (double));
  mMultiply (V, V, B, m);
  eB = 2 * (*eV);
  if (B[(m / 2) * m + (m / 2)] > NORM)
    renormalize (B, m, &eB);

  if (n % 2 == 0) {
    for (i = 0; i < m * m; i++)
      V[i] = B[i];
    *eV = eB;
  } else {
    mMultiply (A, B, V, m);
    *eV = eA + eB;
  }

  if (V[(m / 2) * m + (m / 2)] > NORM)
    renormalize (V, m, eV);
  free (B);
}
Example #2
0
bool Application::perturb(const TigrString &ngram)
{
  throw "OBSOLETE";

  // Choose 1 state in each HMM to perturb
  int state1=RandomNumber(numStates1);
  int state2=RandomNumber(numStates2);

  // Get the current emission probabilities for this ngram
  int symbol=hoa->lookup(ngram);
  double p1=hmm1->getEmissionProb(state1,symbol);
  double p2=hmm2->getEmissionProb(state2,symbol);
  if(p1==0 || p2==0) return false;

  // Choose direction & size of perturbation
  const double DELTA=0.1;
  double delta=DELTA;
  if(p1<p2) 
  //if(Random0to1()<0.5) 
    delta=-delta;

  // Install new emission probabilities
  //hmm1->setEmissionProb(state1,symbol,p1+delta);
  //hmm2->setEmissionProb(state2,symbol,p2+delta);

  // Re-normalize to restore sum-to-one constraints
  renormalize(*hmm1,state1,ngram,symbol,p1+delta);
  renormalize(*hmm2,state2,ngram,symbol,p2+delta);

  return true;
}
Example #3
0
qdouble qdadd(qdouble a, qdouble b) {
  double x[8];
  int i=0,j=0,k;
  daccres   tmp;
  unqdouble us;
  for (k=0;k<5;k++) us.v[k] = 0.;
  for (k=0;k<8;k++) {                                                            // merge - sort
    if (i>3) { x[k] = b.v[j]; j++; } else
    if (j>3) { x[k] = a.v[i]; i++; } else
    if (fabs(a.v[i])>=fabs(b.v[j])) { x[k] = a.v[i]; i++; } else { x[k] = b.v[j]; j++; }
  }
//  Rprintf("%g %g %g %g %g %g %g %g\n",x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7]);
  double s=0., u=0., v=0.;
  k = 0; i = 0;
  while ((k<4)&&(i<8)) {
    tmp = doubleacc(u,v,x[i]);
//    Rprintf("-   i=%d, k=%d, u=%f, v=%f, x[i]=%f\n",i,k,u,v,x[i]);
//    Rprintf("+   i=%d, k=%d, s=%f, u=%f, v[i]=%f\n",i,k,tmp.s,tmp.u,tmp.v);
    s = tmp.s; u = tmp.u; v = tmp.v;
    if (s!=0.) {
//      Rprintf("   i=%d, k=%d, v[k]=%f\n",i,k,s);
      us.v[k] = s; k++;
    }
    i++;
  }
  if (k<3) us.v[k+1] = v;
  if (k<4) us.v[k] = u;
//  Rprintf("%g %g %g %g %g\n",us.v[0],us.v[1],us.v[2],us.v[3],us.v[4]);
  return(renormalize(us));
}
Example #4
0
qdouble qdmult (qdouble a, qdouble b) {
  unqdouble us;
  volatile flerr tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9;
  volatile tsares tmpA,tmpB;
  volatile flerr tmpC;
  tmp0    = twoprod(a.v[0],b.v[0]);
  us.v[0] = tmp0.r;
  tmp1    = twoprod(a.v[0],b.v[1]);
  tmp2    = twoprod(a.v[1],b.v[0]);
  tmpA    = threesumsa(tmp0.e,tmp1.r,tmp2.r);
  us.v[1] = tmpA.v[0];
  tmp3    = twoprod(a.v[0],b.v[2]);
  tmp4    = twoprod(a.v[1],b.v[1]);
  tmp5    = twoprod(a.v[2],b.v[0]);
  tmpB    = sixthreesum(tmpA.v[1],tmp1.e,tmp2.e,tmp3.r,tmp4.r,tmp5.r);
  us.v[2] = tmpB.v[0];
  tmp6    = twoprod(a.v[0],b.v[3]);
  tmp7    = twoprod(a.v[1],b.v[2]);
  tmp8    = twoprod(a.v[2],b.v[1]);
  tmp9    = twoprod(a.v[3],b.v[0]);
  tmpC    = ninetwosum(tmpA.v[2],tmpB.v[1],tmp3.e,tmp4.e,tmp5.e,
                       tmp6.r,tmp7.r,tmp8.r,tmp9.r);
  us.v[3] = tmpC.r;
  us.v[4] = tmpB.v[2] + tmpC.e + tmp6.e + tmp7.e + tmp8.e + tmp9.e +
            a.v[1]*b.v[3] + a.v[2]*b.v[2] + a.v[3]*b.v[1];
  return(renormalize(us));
}
Example #5
0
SEXP RENORM1 (SEXP QD) {
  SEXP Res;
  PROTECT (Res=allocVector(REALSXP,4));
  double *res = REAL(Res);
  qdouble tmp;
  unqdouble qd1;
  for (int i=0;i<4;i++) qd1.v[i] = REAL(QD)[i];
  qd1.v[4] = 0.0;
  tmp = renormalize(qd1);
  for (int i=0;i<4;i++) res[i] = tmp.v[i];
  UNPROTECT(1);
  return(Res);
}
Example #6
0
qdouble qdplusd(qdouble a, double b) {
  unqdouble us;
  flerr tmp;
  tmp = twosum(a.v[0],b);
  us.v[0] = tmp.r;
  tmp = twosum(a.v[1],tmp.e);
  us.v[1] = tmp.r;
  tmp = twosum(a.v[2],tmp.e);
  us.v[2] = tmp.r;
  tmp = twosum(a.v[3],tmp.e);
  us.v[3] = tmp.r;
  us.v[4] = tmp.e;
//  Rprintf(" sum of %g + %g + %g + %g and %g\n",a.v[0],a.v[1],a.v[2],a.v[3],b);
  return(renormalize(us));
}
Example #7
0
qdouble qdtimesd (qdouble a, double b) {
  unqdouble us;
  volatile flerr tmp1, tmp2, tmp3;
  tmp1 = twoprod(a.v[0],b);
  tmp2 = twoprod(a.v[1],b);
  tmp3 = twoprod(a.v[2],b);
  us.v[0] = tmp1.r;
  tmp1 = twosum(tmp2.r,tmp1.e);
  us.v[1] = tmp1.r;
  volatile tsares tmp4 = threesumsa(tmp3.r,tmp2.e,tmp1.e);
  us.v[2] = tmp4.v[0];
  volatile tsbres tmp5 = threesumsb(a.v[3]*b,tmp3.e,tmp4.v[1]);
  us.v[3] = tmp5.v[0];
  us.v[4] = tmp5.v[1]+tmp4.v[2];
  return(renormalize(us));
}
Example #8
0
qdouble qddiv (qdouble a, qdouble b) {
  volatile double q0, q1, q2, q3, q4;
  unqdouble us;
  volatile qdouble r;
  
  q0 = a.v[0] / b.v[0];
  r  = qdadd(a,qdtimesd(b,-q0));
  q1 = r.v[0] / b.v[0];
  r  = qdadd(r,qdtimesd(b,-q1));
  q2 = r.v[0] / b.v[0];
  r  = qdadd(r,qdtimesd(b,-q2));
  q3 = r.v[0] / b.v[0];
  r  = qdadd(r,qdtimesd(b,-q3));
  q4 = r.v[0] / b.v[0];
  us.v[0] = q0; us.v[1] = q1; us.v[2] = q2; us.v[3] = q3; us.v[4] = q4;
  return(renormalize(us));
}
Example #9
0
qdouble qdadd2 (qdouble a, qdouble b) {
  unqdouble us;
  flerr tmp1, tmp2, tmp3, tmp4;
  tsares tmpA;
  tsbres tmpB;
  tmp1 = twosum(a.v[0],b.v[0]);
  us.v[0] = tmp1.r;
  tmp2 = twosum(a.v[1],b.v[1]);
  tmp1 = twosum(tmp2.r,tmp1.e);
  us.v[1] = tmp1.r;
  tmp3 = twosum(a.v[2],b.v[2]);
  tmpA = threesumsa(tmp3.r,tmp1.e,tmp2.e);
  us.v[2] = tmpA.v[0];
  tmp4 = twosum(a.v[3],b.v[3]);
  tmpB = threesumsb(tmp4.r,tmp3.e,tmpA.v[1]);
  us.v[3] = tmpB.v[0];
  us.v[4] = tmpB.v[1]+tmpA.v[2]+tmp4.e;
  return(renormalize(us));
}
Example #10
0
/*********************
*  python interface  *
*********************/
static PyObject* eeint(PyObject* self, PyObject* args){

  /*** input variables ***/
  /* dictionary */
  PyObject *in_dict;
  PyObject *dictList;
  PyObject *dictList_fast;
  PyObject *pyStr;
  PyObject *item;
  /* numpy array */
  PyArrayObject *in_array1, *in_array2;
  NpyIter *in_iter;
  NpyIter_IterNextFunc *in_iternext;
  /* C data type for input */
  int Ngo, Nao;
  int N, itr;
  /* basis function variables */
  double **in_ptr; // address to numpy pointer
  double *center;  // gaussian center
  double *cef;     // contraction coefficients
  int *ng;         // number of gaussians per AO
  double *exp;     // gaussian exponents
  int *lm_xyz;     // angular momentum

  /* python output variables */
  PyObject *py_out;
  PyObject *py_out2;
  double *data, *overlap;
  double element;
  int i, j, k, l;
  int s, t, u, v, w;
  int mat_dim[4];

  /*  parse numpy array and two integers as argument */
  if (!PyArg_ParseTuple(args, "OO!O!",
                        &in_dict, 
                        &PyArray_Type, &in_array1,
                        &PyArray_Type, &in_array2
                       )) return NULL;
  if(in_array1 == NULL) return NULL;

  /***********************
  * Construct input data *
  ***********************/
  /*** access dict_list data ***/
  /* exponents */
  pyStr = PyString_FromString("exponents");
  dictList = PyDict_GetItem(in_dict, pyStr);
  dictList_fast = PySequence_Fast(
                    dictList, "expected a sequence");
  Ngo = PySequence_Size(dictList);
  exp = (double*) malloc(Ngo * sizeof(double));
  for(i=0;i<Ngo;i++){
    item = PySequence_Fast_GET_ITEM(dictList_fast, i);
    exp[i] = PyFloat_AsDouble(item);
  }
  Py_DECREF(dictList_fast);
  /* coefficients */
  pyStr = PyString_FromString("coefficients");
  dictList = PyDict_GetItem(in_dict, pyStr);
  dictList_fast = PySequence_Fast(
                    dictList, "expected a sequence");
  cef = (double*) malloc(Ngo * sizeof(double));
  for(i=0;i<Ngo;i++){
    item = PySequence_Fast_GET_ITEM(dictList_fast, i);
    cef[i] = PyFloat_AsDouble(item);
  }
  Py_DECREF(dictList_fast);
  /* number of gaussians per shell */
  pyStr = PyString_FromString("n_gaussians");
  dictList = PyDict_GetItem(in_dict, pyStr);
  dictList_fast = PySequence_Fast(
                    dictList, "expected a sequence");
  Nao = PySequence_Size(dictList);
  ng = (int*) malloc(Nao * sizeof(int));
  for(i=0;i<Nao;i++){
    item = PySequence_Fast_GET_ITEM(dictList_fast, i);
    ng[i] = PyFloat_AsDouble(item);
  }
  Py_DECREF(dictList_fast);
  /*** end of dict_list data ***/

  /*** create the iterators, necessary to access numpy array ***/
  /* center */
  in_iter = NpyIter_New(in_array1, NPY_ITER_READONLY,
                         NPY_KEEPORDER, NPY_NO_CASTING, NULL);
  if (in_iter == NULL) goto fail;
  in_iternext = NpyIter_GetIterNext(in_iter, NULL);
  if (in_iternext == NULL){
    NpyIter_Deallocate(in_iter);
    goto fail;
  }
  /* interator pointer to actual numpy data */
  in_ptr = (double **) NpyIter_GetDataPtrArray(in_iter);
  center = (double*) malloc(3 * Nao * sizeof(double));
  itr=0;
  do {
    center[itr++] = **in_ptr;
  } while(in_iternext(in_iter));
  NpyIter_Deallocate(in_iter);

  /* lm_xyz */
  in_iter = NpyIter_New(in_array2, NPY_ITER_READONLY,
                         NPY_KEEPORDER, NPY_NO_CASTING, NULL);
  if (in_iter == NULL) goto fail;
  in_iternext = NpyIter_GetIterNext(in_iter, NULL);
  if (in_iternext == NULL){
    NpyIter_Deallocate(in_iter);
    goto fail;
  }
  /* interator pointer to actual numpy data */
  int **in_ptr2 = (int **) NpyIter_GetDataPtrArray(in_iter);
  lm_xyz = (int*) malloc(3 * Nao * sizeof(int));
  itr=0;
  do {
    lm_xyz[itr++] = **in_ptr2;
  } while(in_iternext(in_iter));
  NpyIter_Deallocate(in_iter);

  /*** end of numpy input data ***/

  /***** end of input data construction *****/


  /*******************
  * construct output *
  *******************/
  for(i=0;i<4;i++) mat_dim[i] = Nao;
  py_out = (PyArrayObject*) 
           PyArray_FromDims(4, mat_dim, NPY_DOUBLE);
  data = pyvector_to_Carrayptrs(py_out);

  /* renormalization */
  renormalize(center, exp, cef, ng, lm_xyz, Nao);

  /* orthogonalize */
  // no effect at the moment, for debug purpose
  //py_out2 = (PyArrayObject*) 
  //          PyArray_FromDims(2, mat_dim, NPY_DOUBLE);
  //overlap = pyvector_to_Carrayptrs(py_out2);
  //orthogonalize(overlap, center, exp, cef, ng, lm_xyz, Nao);

#pragma omp parallel private(i, j, k, l, s, t, u, v, w)\
shared(data)
{
  #pragma omp for schedule(dynamic)
  for(i=0;i<Nao;i++){
    for(j=i;j<Nao;j++){
      for(k=0;k<Nao;k++){
        for(l=k;l<Nao;l++){
          if(l+k >= i+j){
            s = l + k*Nao + j*Nao*Nao + i*Nao*Nao*Nao;
            element = eeMatrix(center, exp, cef, ng, lm_xyz,
                                Nao, i, j, k, l);
            data[s] = element;

            // symmetry for (ij|kl)=(ij|lk)=(ji|kl)=(ji|lk)
            t = k + l*Nao + j*Nao*Nao + i*Nao*Nao*Nao; //(ij|lk)
            u = l + k*Nao + i*Nao*Nao + j*Nao*Nao*Nao; //(ji|kl)
            v = k + l*Nao + i*Nao*Nao + j*Nao*Nao*Nao; //(ji|lk)
            data[t] = data[s];
            data[u] = data[s];
            data[v] = data[s];
    
            // symmetry for (ij|kl)=(kl|ij)=(kl|ji)=(lk|ij)=(lk|ji)
            t = j + i*Nao + l*Nao*Nao + k*Nao*Nao*Nao; //(kl|ij)
            u = i + j*Nao + l*Nao*Nao + k*Nao*Nao*Nao; //(kl|ji)
            v = j + i*Nao + k*Nao*Nao + l*Nao*Nao*Nao; //(lk|ij)
            w = i + j*Nao + k*Nao*Nao + l*Nao*Nao*Nao; //(lk|ji)
            data[t] = data[s];
            data[u] = data[s];
            data[v] = data[s];
            data[w] = data[s];
          }
        }
      }
    }
  }
} // end of omp loop

  /*********************************
  * clean up and return the result *
  *********************************/
  free(exp);
  free(cef);
  free(ng);
  free(center);
  free(lm_xyz);
  //free(overlap);

  Py_INCREF(py_out);
  return Py_BuildValue("O", py_out);

  /*  in case bad things happen */
  fail:
    Py_XDECREF(py_out);
    return NULL;
} // end of eeint function
Example #11
0
// examines weights for filtering failure
// computes log likelihood and effective sample size
// computes (if desired) prediction mean, prediction variance, filtering mean.
// it is assumed that ncol(x) == ncol(params).
// weights are used in filtering mean computation.
// if length(weights) == 1, an unweighted average is computed.
// returns all of the above in a named list
SEXP pfilter2_computations (SEXP x, SEXP params, SEXP Np,
			   SEXP rw, SEXP rw_sd,
			   SEXP predmean, SEXP predvar,
			   SEXP filtmean, SEXP onepar,
			   SEXP weights, SEXP tol)
{
  int nprotect = 0;
  SEXP pm = R_NilValue, pv = R_NilValue, fm = R_NilValue;
  SEXP rw_names, ess, fail, loglik;
  SEXP newstates = R_NilValue, newparams = R_NilValue;
  SEXP retval, retvalnames;
  double *xpm = 0, *xpv = 0, *xfm = 0, *xw = 0, *xx = 0, *xp = 0, *xpw=0;
  int *xpa=0;
  SEXP dimX, dimP, newdim, Xnames, Pnames, pindex;
  SEXP pw=R_NilValue,pa=R_NilValue, psample=R_NilValue;
  int *dim, *pidx, lv, np;
  int nvars, npars = 0, nrw = 0, nreps, offset, nlost;
  int do_rw, do_pm, do_pv, do_fm, do_par_resamp, all_fail = 0;
  double sum, sumsq, vsq, ws, w, toler;
  int j, k;

  PROTECT(dimX = GET_DIM(x)); nprotect++;
  dim = INTEGER(dimX);
  nvars = dim[0]; nreps = dim[1];
  xx = REAL(x);
  PROTECT(Xnames = GET_ROWNAMES(GET_DIMNAMES(x))); nprotect++;

  PROTECT(dimP = GET_DIM(params)); nprotect++;
  dim = INTEGER(dimP);
  npars = dim[0];
  if (nreps != dim[1])
    error("'states' and 'params' do not agree in second dimension");
  PROTECT(Pnames = GET_ROWNAMES(GET_DIMNAMES(params))); nprotect++;

  np = INTEGER(AS_INTEGER(Np))[0]; // number of particles to resample

  PROTECT(rw_names = GET_NAMES(rw_sd)); nprotect++; // names of parameters undergoing random walk

  do_rw = *(LOGICAL(AS_LOGICAL(rw))); // do random walk in parameters?
  do_pm = *(LOGICAL(AS_LOGICAL(predmean))); // calculate prediction means?
  do_pv = *(LOGICAL(AS_LOGICAL(predvar)));  // calculate prediction variances?
  do_fm = *(LOGICAL(AS_LOGICAL(filtmean))); // calculate filtering means?
  do_par_resamp = *(LOGICAL(AS_LOGICAL(onepar))); // are all cols of 'params' the same?
  do_par_resamp = !do_par_resamp || do_rw || (np != nreps); // should we do parameter resampling?

  PROTECT(ess = NEW_NUMERIC(1)); nprotect++; // effective sample size
  PROTECT(loglik = NEW_NUMERIC(1)); nprotect++; // log likelihood
  PROTECT(fail = NEW_LOGICAL(1)); nprotect++;	// particle failure?

  xw = REAL(weights); 
  toler = *(REAL(tol));		// failure tolerance
  
    
  // check the weights and compute sum and sum of squares
  for (k = 0, w = 0, ws = 0, nlost = 0; k < nreps; k++) {
    
    if (xw[k] >= 0) {	
     
      w += xw[k];
      ws += xw[k]*xw[k];
    } else {			// this particle is lost
      xw[k] = 0;
      nlost++;
    }
  }
  if (nlost >= nreps) all_fail = 1; // all particles are lost
  if (all_fail) {
    *(REAL(loglik)) = log(toler); // minimum log-likelihood
    *(REAL(ess)) = 0;		  // zero effective sample size
  } else {
    *(REAL(loglik)) = log(w/((double) nreps)); // mean of weights is likelihood
    *(REAL(ess)) = w*w/ws;	// effective sample size
  }
  *(LOGICAL(fail)) = all_fail;

  if (do_rw) {
    // indices of parameters undergoing random walk
    PROTECT(pindex = matchnames(Pnames,rw_names,"parameters")); nprotect++; 
    xp = REAL(params);
    pidx = INTEGER(pindex);
    nrw = LENGTH(rw_names);
    lv = nvars+nrw;
  } else {
    pidx = NULL;
    lv = nvars;
  }

  if (do_pm || do_pv) {
    PROTECT(pm = NEW_NUMERIC(lv)); nprotect++;
    xpm = REAL(pm);
  }

  if (do_pv) {
    PROTECT(pv = NEW_NUMERIC(lv)); nprotect++;
    xpv = REAL(pv);
  }

  if (do_fm) {
    if (do_rw) {
      PROTECT(fm = NEW_NUMERIC(nvars+npars)); nprotect++;
    } else {
      PROTECT(fm = NEW_NUMERIC(nvars)); nprotect++;
    }
    xfm = REAL(fm);
  }
  
  PROTECT(pa = NEW_INTEGER(np)); nprotect++;
  xpa = INTEGER(pa);
  
  
  
  for (j = 0; j < nvars; j++) {	// state variables

    // compute prediction mean
    if (do_pm || do_pv) {
      for (k = 0, sum = 0; k < nreps; k++) sum += xx[j+k*nvars];
      sum /= ((double) nreps);
      xpm[j] = sum;
    }

    // compute prediction variance
    if (do_pv) {	
      for (k = 0, sumsq = 0; k < nreps; k++) {
	vsq = xx[j+k*nvars]-sum;
	sumsq += vsq*vsq;
      }
      xpv[j] = sumsq / ((double) (nreps - 1));
      
    }

    //  compute filter mean
    if (do_fm) {
      if (all_fail) {		// unweighted average
	for (k = 0, ws = 0; k < nreps; k++) ws += xx[j+k*nvars]; 
	xfm[j] = ws/((double) nreps);
      } else { 			// weighted average
	for (k = 0, ws = 0; k < nreps; k++) ws += xx[j+k*nvars]*xw[k]; 
	xfm[j] = ws/w;
      }
    }

  }

  // compute means and variances for parameters (if needed)
  if (do_rw) {
    for (j = 0; j < nrw; j++) {
      offset = pidx[j];		// position of the parameter

      if (do_pm || do_pv) {
	for (k = 0, sum = 0; k < nreps; k++) sum += xp[offset+k*npars];
	sum /= ((double) nreps);
	xpm[nvars+j] = sum;
      }

      if (do_pv) {
	for (k = 0, sumsq = 0; k < nreps; k++) {
	  vsq = xp[offset+k*npars]-sum;
	  sumsq += vsq*vsq;
	}
	xpv[nvars+j] = sumsq / ((double) (nreps - 1));
      }

    }

    if (do_fm) {
      for (j = 0; j < npars; j++) {
	if (all_fail) {		// unweighted average
	  for (k = 0, ws = 0; k < nreps; k++) ws += xp[j+k*npars];
	  xfm[nvars+j] = ws/((double) nreps);
	} else {		// weighted average
	  for (k = 0, ws = 0; k < nreps; k++) ws += xp[j+k*npars]*xw[k];
	  xfm[nvars+j] = ws/w;
	}
      }
    }
  }

  GetRNGstate();

  if (!all_fail) { // resample the particles unless we have filtering failure
    int xdim[2];
    //int sample[np];
    double *ss = 0, *st = 0, *ps = 0, *pt = 0;

    // create storage for new states
    xdim[0] = nvars; xdim[1] = np;
    PROTECT(newstates = makearray(2,xdim)); nprotect++;
    setrownames(newstates,Xnames,2);
    ss = REAL(x);
    st = REAL(newstates);

    // create storage for new parameters
    if (do_par_resamp) {
      xdim[0] = npars; xdim[1] = np;
      PROTECT(newparams = makearray(2,xdim)); nprotect++;
      setrownames(newparams,Pnames,2);
      ps = REAL(params);
      pt = REAL(newparams);
    }
    
    PROTECT(pw = NEW_NUMERIC(nreps)); nprotect++;
    xpw = REAL(pw);
    for (k = 0; k < nreps; k++)
      xpw[k]=REAL(weights)[k];
    nosort_resamp(nreps,REAL(weights),np,xpa,0);
    for (k = 0; k < np; k++) { // copy the particles
      for (j = 0, xx = ss+nvars*xpa[k]; j < nvars; j++, st++, xx++) 
	*st = *xx;
      
          
	        
      if (do_par_resamp) {
	for (j = 0, xp = ps+npars*xpa[k]; j < npars; j++, pt++, xp++){
    *pt = *xp;
   
	} 
	  
      }
    }

  } else { // don't resample: just drop 3rd dimension in x prior to return
    
    PROTECT(newdim = NEW_INTEGER(2)); nprotect++;
    dim = INTEGER(newdim);
    dim[0] = nvars; dim[1] = nreps;
    SET_DIM(x,newdim);
    setrownames(x,Xnames,2);

  }
    
  if (do_rw) { // if random walk, adjust prediction variance and move particles
    xx = REAL(rw_sd);
    xp = (all_fail || !do_par_resamp) ? REAL(params) : REAL(newparams);
    nreps = (all_fail) ? nreps : np;

    for (j = 0; j < nrw; j++) {
      offset = pidx[j];
      vsq = xx[j];
      if (do_pv) {
	xpv[nvars+j] += vsq*vsq;
      }
      for (k = 0; k < nreps; k++)
	xp[offset+k*npars] += rnorm(0,vsq);
    }
  }
  
  renormalize(xpw,nreps,0);
  PutRNGstate();

  PROTECT(retval = NEW_LIST(10)); nprotect++;
  PROTECT(retvalnames = NEW_CHARACTER(10)); nprotect++;
  SET_STRING_ELT(retvalnames,0,mkChar("fail"));
  SET_STRING_ELT(retvalnames,1,mkChar("loglik"));
  SET_STRING_ELT(retvalnames,2,mkChar("ess"));
  SET_STRING_ELT(retvalnames,3,mkChar("states"));
  SET_STRING_ELT(retvalnames,4,mkChar("params"));
  SET_STRING_ELT(retvalnames,5,mkChar("pm"));
  SET_STRING_ELT(retvalnames,6,mkChar("pv"));
  SET_STRING_ELT(retvalnames,7,mkChar("fm"));
  SET_STRING_ELT(retvalnames,8,mkChar("weight"));
  SET_STRING_ELT(retvalnames,9,mkChar("pa"));
  
  SET_NAMES(retval,retvalnames);

  SET_ELEMENT(retval,0,fail);
  SET_ELEMENT(retval,1,loglik);
  SET_ELEMENT(retval,2,ess);
  
  if (all_fail) {
    SET_ELEMENT(retval,3,x);
  } else {
    SET_ELEMENT(retval,3,newstates);
  }

  if (all_fail || !do_par_resamp) {
    SET_ELEMENT(retval,4,params);
  } else {
    SET_ELEMENT(retval,4,newparams);
  }

  if (do_pm) {
    SET_ELEMENT(retval,5,pm);
  }
  if (do_pv) {
    SET_ELEMENT(retval,6,pv);
  }
  if (do_fm) {
    SET_ELEMENT(retval,7,fm);
  }
  SET_ELEMENT(retval,8,pw);
  SET_ELEMENT(retval,9,pa);
  UNPROTECT(nprotect);
  return(retval);
}
Example #12
0
double
thermo_sa(double temp_init, double temp_end, double temp_sig, double initstate,
          double bm_sigma, double k, FILE * log)
{
   double  energy, energy_new, energy_delta, energy_variation;
   int    *path;
   double  temp, temp_old;
   double  prob;
   double  rot_old, best_rot;
   double  entropy_variation;
   double  energy_best;
   gsl_rng *acpt_rng;
   unsigned long time = 0;
   double  BM;

   /*
    * Initialize the random number generators. 
    */
   _bm_rng = gsl_rng_alloc(gsl_rng_taus);
   acpt_rng = gsl_rng_alloc(gsl_rng_taus);

   temp = temp_init;
   rotation = initstate;

   /*
    * Compute the first path. 
    */
   path = renormalize();
   energy = route_length(path, tsp->dimension);
	free(path);
   energy_best = energy;

   entropy_variation = 0;
   energy_variation = 0;

   /*
    * Print the log headers. 
    */
   if (log != NULL)
      (void) fprintf(log, "time T E_n E_d E_v E_b S_v rb r rv bm\n");

   do {
      temp_old = temp;
      rot_old = rotation;
      if (log != NULL)
         (void) fprintf(log, "%lu ", time);

      BM = neighbour_rot(temp, temp_end, temp_init, bm_sigma);

      if(fpclassify(rotation) == FP_NAN)
          errx(EX_DATAERR, "Rotation can not be NaN");
      path = renormalize();
      energy_new = route_length(path, tsp->dimension);
		free(path);
      energy_delta = energy_new - energy;

      prob = exp(-energy_delta / temp);

      if (log != NULL)
			(void)fprintf(log, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n",
							temp, energy_new, energy_delta, energy_variation,
							energy_best, entropy_variation, best_rot, rotation,
                     (rotation - rot_old), BM);

      if (gsl_rng_uniform(acpt_rng) < prob) {
         if (energy_best > energy) {
            energy_best = energy;
            best_rot = rot_old;
         }
         energy = energy_new;
         energy_variation += energy_delta;
      } else
         rotation = rot_old;

      if (energy_delta > 0)
         entropy_variation -= energy_delta / temp;

      if ((energy_variation >= 0) || fabs(entropy_variation) < 0.000001)
         temp = temp_init;
      else {
         temp = k * (energy_variation / entropy_variation);
         //rotation = best_rot;
      }
      time++;
   } while ((temp > temp_end) || (fabs(temp - temp_old) > temp_sig));

   gsl_rng_free(acpt_rng);
   gsl_rng_free(_bm_rng);

   return energy_best;
}
Example #13
0
/*********************
*  python interface  *
*********************/
static PyObject* eekernel(PyObject* self, PyObject* args){

  /*** input variables ***/
  /* dictionary */
  PyObject *in_dict;
  PyObject *dictList;
  PyObject *dictList_fast;
  PyObject *pyStr;
  PyObject *item;
  /* list */
  PyObject *in_list;
  PyObject *list_fast;
  /* numpy array */
  PyArrayObject *in_array1, *in_array2, *in_array3;
  NpyIter *in_iter;
  NpyIter_IterNextFunc *in_iternext;
  /* C data type for input */
  int Ngo, Nao;
  int N, itr;
  double *Z;
  double *R;
  /* basis function variables */
  double **in_ptr; // address to numpy pointer
  double *center;  // gaussian center
  double *cef;     // contraction coefficients
  int *ng;         // number of gaussians per AO
  double *exp;     // gaussian exponents
  int *lm_xyz;     // angular momentum

  /* python output variables */
  PyObject *py_out;
  PyObject *py_out2;
  double *data, *overlap;
  int i, j, k;
  int mat_dim[3];

  /*  parse numpy array and two integers as argument */
  if (!PyArg_ParseTuple(args, "OO!O!O!O", 
                        &in_dict, 
                        &PyArray_Type, &in_array1,
                        &PyArray_Type, &in_array2,
                        &PyArray_Type, &in_array3,
                        &in_list
                       )) return NULL;
  if(in_array1 == NULL) return NULL;

  /***********************
  * Construct input data *
  ***********************/
  /*** access dict_list data ***/
  /* exponents */
  pyStr = PyString_FromString("exponents");
  dictList = PyDict_GetItem(in_dict, pyStr);
  dictList_fast = PySequence_Fast(
                    dictList, "expected a sequence");
  Ngo = PySequence_Size(dictList);
  exp = (double*) malloc(Ngo * sizeof(double));
  for(i=0;i<Ngo;i++){
    item = PySequence_Fast_GET_ITEM(dictList_fast, i);
    exp[i] = PyFloat_AsDouble(item);
  }
  Py_DECREF(dictList_fast);
  /* coefficients */
  pyStr = PyString_FromString("coefficients");
  dictList = PyDict_GetItem(in_dict, pyStr);
  dictList_fast = PySequence_Fast(
                    dictList, "expected a sequence");
  cef = (double*) malloc(Ngo * sizeof(double));
  for(i=0;i<Ngo;i++){
    item = PySequence_Fast_GET_ITEM(dictList_fast, i);
    cef[i] = PyFloat_AsDouble(item);
  }
  Py_DECREF(dictList_fast);
  /* number of gaussians per shell */
  pyStr = PyString_FromString("n_gaussians");
  dictList = PyDict_GetItem(in_dict, pyStr);
  dictList_fast = PySequence_Fast(
                    dictList, "expected a sequence");
  Nao = PySequence_Size(dictList);
  ng = (int*) malloc(Nao * sizeof(int));
  for(i=0;i<Nao;i++){
    item = PySequence_Fast_GET_ITEM(dictList_fast, i);
    ng[i] = PyFloat_AsDouble(item);
  }
  Py_DECREF(dictList_fast);
  /*** end of dict_list data ***/

  /*** access python list data ***/
  list_fast = PySequence_Fast(in_list, "expected a sequence");
  N = PySequence_Size(in_list);
  Z = (double*) malloc(N * sizeof(double));
  for(i=0;i<N;i++){
    item = PySequence_Fast_GET_ITEM(list_fast, i);
    Z[i] = PyFloat_AsDouble(item);
  }
  Py_DECREF(list_fast);
  /*** end of list input data ***/

  /*** create the iterators, necessary to access numpy array ***/
  /* center */
  in_iter = NpyIter_New(in_array1, NPY_ITER_READONLY,
                         NPY_KEEPORDER, NPY_NO_CASTING, NULL);
  if (in_iter == NULL) goto fail;
  in_iternext = NpyIter_GetIterNext(in_iter, NULL);
  if (in_iternext == NULL){
    NpyIter_Deallocate(in_iter);
    goto fail;
  }
  /* interator pointer to actual numpy data */
  in_ptr = (double **) NpyIter_GetDataPtrArray(in_iter);
  center = (double*) malloc(3 * Nao * sizeof(double));
  itr=0;
  do {
    center[itr++] = **in_ptr;
  } while(in_iternext(in_iter));
  NpyIter_Deallocate(in_iter);

  /* lm_xyz */
  in_iter = NpyIter_New(in_array2, NPY_ITER_READONLY,
                         NPY_KEEPORDER, NPY_NO_CASTING, NULL);
  if (in_iter == NULL) goto fail;
  in_iternext = NpyIter_GetIterNext(in_iter, NULL);
  if (in_iternext == NULL){
    NpyIter_Deallocate(in_iter);
    goto fail;
  }
  /* interator pointer to actual numpy data */
  int **in_ptr2 = (int **) NpyIter_GetDataPtrArray(in_iter);
  lm_xyz = (int*) malloc(3 * Nao * sizeof(int));
  itr=0;
  do {
    lm_xyz[itr++] = **in_ptr2;
  } while(in_iternext(in_iter));
  NpyIter_Deallocate(in_iter);

  /* R */
  in_iter = NpyIter_New(in_array3, NPY_ITER_READONLY,
                         NPY_KEEPORDER, NPY_NO_CASTING, NULL);
  if (in_iter == NULL) goto fail;
  in_iternext = NpyIter_GetIterNext(in_iter, NULL);
  if (in_iternext == NULL){
    NpyIter_Deallocate(in_iter);
    goto fail;
  }
  /* interator pointer to actual numpy data */
  in_ptr = (double **) NpyIter_GetDataPtrArray(in_iter);
  R = (double*) malloc(3 * N * sizeof(double));
  itr=0;
  do {
    R[itr++] = **in_ptr;
  } while(in_iternext(in_iter));
  NpyIter_Deallocate(in_iter);
  /*** end of numpy input data ***/

  /***** end of input data construction *****/


  /*******************
  * construct output *
  *******************/
  mat_dim[0] = Nao;
  mat_dim[1] = Nao;
  mat_dim[2] = N;
  py_out = (PyArrayObject*) 
           PyArray_FromDims(3, mat_dim, NPY_DOUBLE);
  data = pyvector_to_Carrayptrs(py_out);

  /* renormalization */
  renormalize(center, exp, cef, ng, lm_xyz, Nao);

  /* orthogonalize */
  // no effect at the moment, for debug purpose
  //py_out2 = (PyArrayObject*) 
  //          PyArray_FromDims(2, mat_dim, NPY_DOUBLE);
  //overlap = pyvector_to_Carrayptrs(py_out2);
  //orthogonalize(overlap, center, exp, cef, ng, lm_xyz, Nao);

#pragma omp parallel private(i, j, k) shared(data)
{
  #pragma omp for schedule(dynamic)
  for(i=0;i<Nao;i++){
    for(j=i;j<Nao;j++){
      eeKernel(R, Z, center, exp, cef, &data[Nao*N*i + N*j],
      ng, lm_xyz, Nao, N, i, j);
      if(j!=i){
        for(k=0;k<N;k++)
          data[i*N+j*Nao*N + k] = data[j*N+i*Nao*N + k];
      }
    }
  }
} // end of omp loop

  /*********************************
  * clean up and return the result *
  *********************************/
  free(exp);
  free(cef);
  free(ng);
  free(center);
  free(R);
  free(Z);
  free(overlap);

  Py_INCREF(py_out);
  return Py_BuildValue("O", py_out);

  /*  in case bad things happen */
  fail:
    Py_XDECREF(py_out);
    return NULL;
}
Example #14
0
void HMM::trainGibbsFromFile(char* inputFile)
{

	double **emit_count;
	double **trans_count;
	double *state_count;
	double *sequence_count;
	double *init_count;
	double *obs_count;

	emit_count = createMatrix(_numStates, _numObs);
	trans_count = createMatrix(_maxState, _numStates);
	state_count = new double[_numStates];
	zeroArray(state_count, _numStates);
	obs_count = new double[_numObs];
	zeroArray(obs_count, _numObs);
	sequence_count = new double[_maxState];
	zeroArray(sequence_count, _maxState);
	init_count = new double[_maxState];
	zeroArray(init_count, _maxState);

	ifstream trainFile(inputFile);
	string line;
	int count = 0;
	while (getline(trainFile, line))
	{ // for every sentence
		vector<int> words;
		stringstream ss(line);
		string buf;

		while (ss >> buf)
		{
			words.push_back(atoi(buf.c_str()));
		}
		int len = words.size();
		count++;
		int *stateArray;
		stateArray = new int[len];
		for (int i = 0; i < len; i++)
		{
			int origState = (rand() % (_numStates - 1)) + 1;
			int obs = words[i];
			int prev_sequence = 0;
			int r = 0;
			stateArray[i] = origState;
			while ((r < _order) && (i - 1 - r) >= 0)
			{
				prev_sequence += stateArray[(i - 1) - r] * int(pow(_numStates, r));
				r++;
			}
			obs_count[obs]++;
			state_count[origState]++;
			if (i == 0)
				init_count[origState]++;
			else
			{
				trans_count[prev_sequence][origState]++;
				sequence_count[prev_sequence]++;
			}
			emit_count[origState][obs]++;

		}
		int sampleN = rand() % len;
		for (int i = 0; i < sampleN; i++)
		{
			int k = rand() % (len - 1);
			int obs = words[k];
			int prev_sequence = 0;
			int r = 0;
			//	cout << "Compute prev_seq" << endl;
			while ((r < _order) && (k - 1 - r) >= 0)
			{
				prev_sequence += stateArray[(k - 1) - r] * int(pow(_numStates, r));
				r++;
			}
			//	cout << "Done Compute prev_seq" << endl;
			int origState = stateArray[k];
			int nextState = stateArray[k + 1];
			int next_sequence = _numStates * (prev_sequence % int(pow(_numStates, _order - 1))) + origState;
			double *dist = new double[_numStates];
			double totalp = 0;
			for (int state = 0; state < _numStates; state++)
			{
				int state_sequence = _numStates * (prev_sequence % int(pow(_numStates, _order - 1))) + state;
				if (prev_sequence == 0)
					dist[state] = _pObservation[state][obs] * initial_probability[state]
							* _pTransition[state_sequence][nextState];
				else
					dist[state] = _pObservation[state][obs] * _pTransition[prev_sequence][state]
							* _pTransition[state_sequence][nextState];
				totalp += dist[state];
			}
			renormalize(dist, _numStates);
			Distribution d(dist, _numStates);
			int sample = d.generate_sample();
			delete[] dist;

			//	cout << "Update params" << endl;
			state_count[origState]--;
			if (k == 0)
			{
				init_count[origState]--;
				init_count[sample]++;
			}
			else
			{
				trans_count[prev_sequence][origState]--;
				trans_count[prev_sequence][sample]++;
			}
			trans_count[next_sequence][nextState]--;
			sequence_count[next_sequence]--;
			emit_count[origState][obs]--;
			stateArray[k] = sample;
			next_sequence = _numStates * (prev_sequence % int(pow(_numStates, _order - 1))) + sample;
			state_count[sample]++;
			trans_count[next_sequence][nextState]++;
			sequence_count[next_sequence]++;
			emit_count[sample][obs]++;

			//	cout << "Done Update params" << endl;

		}
	}//end for every sentence
	trainFile.close();
	updateHMM(emit_count, trans_count, state_count, sequence_count, init_count, obs_count);

	freeMatrix(trans_count, _maxState, _numStates);
	freeMatrix(emit_count, _numStates, _numObs);
	delete[] state_count;
	delete[] obs_count;
	delete[] sequence_count;

}
Example #15
0
void RigidBody::rotate(Quaternion rot){
	mQ = rot * mQ;
	renormalize();
}
//------------------------------------------------------------------------------
bool ComplexTimeRungeKuttaFehlberg::stepForward()
{
    cx_vec k1, k2, k3, k4, k5, k6;
    cx_mat m1, m2, m3, m4, m5, m6;
    cx_vec A_, A_1;
    cx_mat C_, C_1;

    bool accepted = false;

    // Computing Runge-Kutta weights
    V->computeNewElements(C);
    h->computeNewElements(C);

    k1 = -i*dt*slater->computeRightHandSideComplexTime(A);
    m1 = -i*dt*orbital->computeRightHandSide(C, A);

    V->computeNewElements(C + m1/4.0);
    h->computeNewElements(C + m1/4.0);

    k2 = -i*dt*slater->computeRightHandSideComplexTime(A + k1/4.0);
    m2 = -i*dt*orbital->computeRightHandSide(C + m1/4.0, A + k1/4.0);

    V->computeNewElements(C + 3.0/32*m1 + 9.0/32*m2);
    h->computeNewElements(C + 3.0/32*m1 + 9.0/32*m2);

    k3 = -i*dt*slater->computeRightHandSideComplexTime(A + 3.0/32*k1 + 9.0/32*k2);
    m3 = -i*dt*orbital->computeRightHandSide(C + 3.0/32*m1 + 9.0/32*m2, A + 3.0/32*k1 + 9.0/32*k2);

    V->computeNewElements(C + 1932.0/2197*m1 - 7200.0/2197*m2 + 7296.0/2197*m3);
    h->computeNewElements(C + 1932.0/2197*m1 - 7200.0/2197*m2 + 7296.0/2197*m3);

    k4 = -i*dt*slater->computeRightHandSideComplexTime(A + 1932.0/2197*k1 - 7200.0/2197*k2 + 7296.0/2197*k3);
    m4 = -i*dt*orbital->computeRightHandSide(C + 1932.0/2197*m1 - 7200.0/2197*m2 + 7296.0/2197*m3,
                                             A + 1932.0/2197*k1 - 7200.0/2197*k2 + 7296.0/2197*k3);

    V->computeNewElements(C + 439.0/216*m1 - 8.0*m2 + 3680.0/513*m3 - 845.0/4104*m4);
    h->computeNewElements(C + 439.0/216*m1 - 8.0*m2 + 3680.0/513*m3 - 845.0/4104*m4);

    k5 = -i*dt*slater->computeRightHandSideComplexTime(A + 439.0/216*k1 - 8.0*k2 + 3680.0/513*k3 - 845.0/4104*k4);
    m5 = -i*dt*orbital->computeRightHandSide(C + 439.0/216*m1 - 8.0*m2 + 3680.0/513*m3 - 845.0/4104*m4,
                                             A + 439.0/216*k1 - 8.0*k2 + 3680.0/513*k3 - 845.0/4104*k4);

    V->computeNewElements(C - 8.0/27*m1 + 2.0*m2 - 3544.0/2565*m3 + 1859.0/4104*m4 - 11.0/40*m5);
    h->computeNewElements(C - 8.0/27*m1 + 2.0*m2 - 3544.0/2565*m3 + 1859.0/4104*m4 - 11.0/40*m5);

    k6 = -i*dt*slater->computeRightHandSideComplexTime(A - 8.0/27*k1 + 2.0*k2 - 3544.0/2565*k3 + 1859.0/4104*k4 - 11.0/40*k5);
    m6 = -i*dt*orbital->computeRightHandSide(C - 8.0/27*m1 + 2.0*m2 - 3544.0/2565*m3 + 1859.0/4104*m4 - 11.0/40*m5,
                                             A - 8.0/27*k1 + 2.0*k2 - 3544.0/2565*k3 + 1859.0/4104*k4 - 11.0/40*k5);


    // Computing new states
    A_  = A + 16.0/135*k1 + 6656.0/12825*k3 + 28561.0/56430*k4 - 9.0/50*k5 + 2.0/55*k6;
    A_1 = A + 25.0/216*k1 + 1408.0/2565*k3 + 2197.0/4104*k4 - 1.0/5*k5;

    C_  = C + 16.0/135*m1 + 6656.0/12825*m3 + 28561.0/56430*m4 - 9.0/50*m5 + 2.0/55*m6;
    C_1 = C + 25.0/216*m1 + 1408.0/2565*m3 + 2197.0/4104*m4 - 1.0/5*m5;

    vec R;
    double maxR;
    double rTmp;

    R = 1.0/dt*abs(A_ - A_1);
    maxR = max(R);
    for(uint i=0; i<C.n_cols; i++){
        R = 1.0/dt*abs(C_.col(i) - C_1.col(i));
        rTmp = max(R);
        if(rTmp > maxR)
            maxR = rTmp;
    }

    double delta = 0.84*pow(epsilon/maxR, 0.25);

    if(maxR <= epsilon){
        C = C_1;
        A = A_1;
        t += dt;
        dt = delta*dt;

        // Normalizing
        renormalize(C);
        A = A/sqrt(cdot(A,A));

        accepted = true;
    }else
    {
        dt = delta*dt;
        step--;
    }
    return accepted;
}
Example #17
0
void HMM::trainGibbsParallel(vector<string> files_list)
{

	int count = files_list.size();
	int rank = MPI::COMM_WORLD.Get_rank();
	int size = MPI::COMM_WORLD.Get_size();
	const int root = 0;
	int dist = count / size;
	int start = rank * dist;
	int end = rank * dist + dist;

	double **emit_count;
	double **trans_count;
	double *state_count;
	double *sequence_count;
	double *init_count;
	double *obs_count;
	emit_count = createMatrix(_numStates, _numObs);
	trans_count = createMatrix(_maxState, _numStates);

	state_count = new double[_numStates];
	zeroArray(state_count, _numStates);

	obs_count = new double[_numObs];
	zeroArray(obs_count, _numObs);

	sequence_count = new double[_maxState];
	zeroArray(sequence_count, _maxState);

	init_count = new double[_maxState];
	zeroArray(init_count, _maxState);

	double **temit_count;
	double **ttrans_count;
	double *tstate_count;
	double *tsequence_count;
	double *tinit_count;
	double *tobs_count;

	temit_count = createMatrix(_numStates, _numObs);
	ttrans_count = createMatrix(_maxState, _numStates);

	tstate_count = new double[_numStates];
	zeroArray(tstate_count, _numStates);

	tobs_count = new double[_numObs];
	zeroArray(tobs_count, _numObs);

	tsequence_count = new double[_maxState];
	zeroArray(tsequence_count, _maxState);

	tinit_count = new double[_maxState];
	zeroArray(tinit_count, _maxState);

	for (int i = start; i < end; i++)
	{
		const char* inputFile = files_list[i].c_str();
		ifstream trainFile(inputFile);
		string line;
		while (getline(trainFile, line))
		{ // for every sentence

			// Read in training sequence
			vector<int> words;
			stringstream ss(line);
			string buf;

			while (ss >> buf)
			{
				words.push_back(atoi(buf.c_str()));
			}
			int len = words.size();
			count++;
			int *stateArray;
			stateArray = new int[len];
			for (int i = 0; i < len; i++)
			{
				int origState = (rand() % (_numStates - 1)) + 1;
				int obs = words[i];
				int prev_sequence = 0;
				int r = 0;
				stateArray[i] = origState;

				if (i == 0)
					init_count[origState]++;
				else
				{
					while ((r < _order) && (i - 1 - r) >= 0)
					{
						prev_sequence += stateArray[(i - 1) - r] * int(pow(_numStates, r));
						r++;
					}
					trans_count[prev_sequence][origState]++;
					sequence_count[prev_sequence]++;
				}
				obs_count[obs]++;
				state_count[origState]++;
				emit_count[origState][obs]++;

			}
			int sampleN = rand() % len;
			for (int i = 0; i < sampleN; i++)
			{
				int k = rand() % (len - 1);
				int obs = words[k];
				int prev_sequence = 0;
				int r = 0;
				//		cout << "Compute seq " <<endl;
				while ((r < _order) && (k - 1 - r) >= 0)
				{
					prev_sequence += stateArray[(k - 1) - r] * int(pow(_numStates, r));
					r++;
				}
				//		cout << "Done Compute seq " <<endl;
				int origState = stateArray[k];
				int nextState = stateArray[k + 1];
				int next_sequence = _numStates * (prev_sequence % int(pow(_numStates, _order - 1))) + nextState;
				double *dist = new double[_numStates];
				double totalp = 0;
				for (int state = 0; state < _numStates; state++)
				{
					int state_sequence = _numStates * (prev_sequence % int(pow(_numStates, _order - 1))) + state;
					if (prev_sequence == 0)
						dist[state] = _pObservation[state][obs] * initial_probability[state]
								* _pTransition[state_sequence][nextState];
					else
						dist[state] = _pObservation[state][obs] * _pTransition[prev_sequence][state]
								* _pTransition[state_sequence][nextState];
					totalp += dist[state];
				}
				renormalize(dist, _numStates);
				Distribution d(dist, _numStates);
				int sample = d.generate_sample();
				delete[] dist;

				if (k == 0)
				{
					init_count[origState]--;
					init_count[sample]++;
				}
				else
				{
					trans_count[prev_sequence][origState]--;
					trans_count[prev_sequence][sample]++;
				}
				state_count[origState]--;
				//			trans_count[next_sequence][nextState]--;
				//			sequence_count[next_sequence]--;
				emit_count[origState][obs]--;
				stateArray[k] = sample;
				//			next_sequence = _numStates*(prev_sequence  % int(pow(_numStates, _order-1))) + sample;
				state_count[sample]++;
				//			trans_count[next_sequence][nextState]++;
				//			sequence_count[next_sequence]++;
				emit_count[sample][obs]++;
				//		cout << "Done Update parameters" << endl;
			}
		}//end for every sentence

		trainFile.close();

	} // for every file

	//Collect parameters on root
	for (int state_sequence = 0; state_sequence < _maxState; state_sequence++)
	{
		MPI_Reduce(trans_count[state_sequence], ttrans_count[state_sequence], _numStates, MPI_DOUBLE, MPI_SUM, root,
				MPI_COMM_WORLD);
	}
	MPI_Reduce(sequence_count, tsequence_count, _maxState, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
	MPI_Reduce(init_count, tinit_count, _maxState, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
	for (int state = 0; state < _numStates; state++)
	{
		MPI_Reduce(emit_count[state], temit_count[state], _numObs, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
	}
	MPI_Reduce(state_count, tstate_count, _numStates, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
	MPI_Reduce(obs_count, tobs_count, _numObs, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);

	//Send updated parameters too all children
	for (int state_sequence = 0; state_sequence < _maxState; state_sequence++)
	{
		MPI_Bcast(ttrans_count[state_sequence], _numStates, MPI_DOUBLE, root, MPI_COMM_WORLD);
	}
	MPI_Bcast(tsequence_count, _maxState, MPI_DOUBLE, root, MPI_COMM_WORLD);
	MPI_Bcast(tinit_count, _maxState, MPI_DOUBLE, root, MPI_COMM_WORLD);
	for (int state = 0; state < _numStates; state++)
	{
		MPI_Bcast(temit_count[state], _numObs, MPI_DOUBLE, root, MPI_COMM_WORLD);
	}
	MPI_Bcast(tstate_count, _numStates, MPI_DOUBLE, root, MPI_COMM_WORLD);
	MPI_Bcast(tobs_count, _numObs, MPI_DOUBLE, root, MPI_COMM_WORLD);

	//cout << "Update Step" << endl;
	updateHMM(temit_count, ttrans_count, tstate_count, tsequence_count, tinit_count, tobs_count);

	freeMatrix(trans_count, _maxState, _numStates);
	freeMatrix(emit_count, _numStates, _numObs);
	delete[] state_count;
	delete[] sequence_count;
	delete[] init_count;
	delete[] obs_count;

	freeMatrix(temit_count, _numStates, _numObs);
	freeMatrix(ttrans_count, _maxState, _numStates);
	delete[] tstate_count;
	delete[] tsequence_count;
	delete[] tinit_count;
	delete[] tobs_count;
}