Esempio n. 1
0
void M2MFstAligner::expectation( ){
  /*
    Here we compute the arc posteriors.  This routine is almost 
    fun to implement in the FST paradigm.
  */
  for( unsigned int i=0; i<fsas.size(); i++ ){
    //Compute Forward and Backward probabilities
    ShortestDistance( fsas.at(i), &alpha );
    ShortestDistance( fsas.at(i), &beta, true );

    //Compute the normalized Gamma probabilities and 
    // update our running tally
    for( StateIterator<VectorFst<LogArc> > siter(fsas.at(i)); !siter.Done(); siter.Next() ){
      LogArc::StateId q = siter.Value();
      for( ArcIterator<VectorFst<LogArc> > aiter(fsas.at(i),q); !aiter.Done(); aiter.Next() ){
	const LogArc&      arc = aiter.Value();
	const LogWeight& gamma = Divide(Times(Times(alpha[q], arc.weight), beta[arc.nextstate]), beta[0]); 
	//Check for any BadValue results, otherwise add to the tally.
        //We call this 'prev_alignment_model' which may seem misleading, but
        // this conventions leads to 'alignment_model' being the final version.
	if( gamma.Value()==gamma.Value() ){
	  prev_alignment_model[arc.ilabel] = Plus(prev_alignment_model[arc.ilabel], gamma);
	  total = Plus(total, gamma);
	}
      }
    }
    alpha.clear();
    beta.clear();
  }
}
ExoticBSEngine::ExoticBSEngine(const Wrapper<PathDependent>& TheProduct_,
                                    const Parameters& R_,
                                    const Parameters& D_,
                                    const Parameters& Vol_,
                                    const Wrapper<RandomBase>& TheGenerator_,
                                    double Spot_)
                                    :
                                    ExoticEngine(TheProduct_,R_),
                                    TheGenerator(TheGenerator_)
{
    MJArray Times(TheProduct_->GetLookAtTimes());
    NumberOfTimes = Times.size();

    TheGenerator->ResetDimensionality(NumberOfTimes);
	// Here is the place for change of models for different products
    Drifts.resize(NumberOfTimes);
    StandardDeviations.resize(NumberOfTimes);

    double Variance = Vol_.IntegralSquare(0,Times[0]);

    Drifts[0] = R_.Integral(0.0,Times[0]) - D_.Integral(0.0,Times[0]) - 0.5 * Variance;
    StandardDeviations[0] = sqrt(Variance);
	// here calculates the r*dt sigma*dw
    for (unsigned long j=1; j < NumberOfTimes; ++j)
    {   
        double thisVariance = Vol_.IntegralSquare(Times[j-1],Times[j]);
        Drifts[j] = R_.Integral(Times[j-1],Times[j]) - D_.Integral(Times[j-1],Times[j]) 
                    - 0.5 * thisVariance;
        StandardDeviations[j] = sqrt(thisVariance);
    }

    LogSpot = log(Spot_);                                   
    Variates.resize(NumberOfTimes);
}
 ///functor:  Run composition itself, separate from load times (e.g. which may include an fst expansion).
 VectorFst<Arc> * operator() () {
   if (!fst_.NumStates() ) {
     LWARN ("Empty lattice. ... Skipping LM application!");
     return NULL;
   }
   while ( qc_.size() ) {
     StateId s = qc_.front();
     qc_.pop();
     pair<StateId, const typename KenLMModelT::State> p = get ( s );
     StateId& s1 = p.first;
     const typename KenLMModelT::State s2 = p.second;
     for ( ArcIterator< VectorFst<Arc> > arc1 ( fst_, s1 ); !arc1.Done();
           arc1.Next() ) {
       const Arc& a1 = arc1.Value();
       float w = 0;
       float wp = wp_;
       typename KenLMModelT::State nextlmstate;
       if ( epsilons_.find ( a1.olabel ) == epsilons_.end() ) {
         w = lmmodel_.Score ( s2, idbridge_.map (a1.olabel), nextlmstate ) * natlog10_;
         //silly hack
         if ( a1.olabel <= 2  )  {
           wp = 0;
           if (a1.olabel == 1 ) w = 0; //get same result as srilm
         }
       } else {
         nextlmstate = s2;
         wp = 0; //We don't count epsilon labels
       }
       pair<StateId, bool> nextp = add ( nextlmstate
                                         , a1.nextstate
                                         , fst_.Final ( a1.nextstate ) );
       StateId& newstate = nextp.first;
       bool visited = nextp.second;
       composed_->AddArc ( s
                           , Arc ( a1.ilabel, a1.olabel
                                   , Times ( a1.weight, Times (mw_ ( w ) , mw_ (wp) ) )
                                   , newstate ) );
       //Finally, only add newstate to the queue if it hasn't been visited previously
       if ( !visited ) {
         qc_.push ( newstate );
       }
     }
   }
   LINFO ( "Done! Number of states=" << composed_->NumStates() );
   return composed_;
 };
Esempio n. 4
0
 StdArc operator() (const LexStdArc& arc) const {
   W w;
   if (i_ == 0)
     w = Times ( arc.weight.Value1(), arc.weight.Value2() );
   if (i_ == 1)
     w = arc.weight.Value1();
   if (i_ == 2)
     w = arc.weight.Value2();
   return StdArc (arc.ilabel, arc.olabel, w, arc.nextstate);
 }
Foam::instantList Foam::Time::findTimes
(
    const fileName& directory,
    const word& constantName
)
{
    if (debug)
    {
        InfoInFunction << "Finding times in directory " << directory << endl;
    }

    // Read directory entries into a list
    fileNameList dirEntries
    (
        fileHandler().readDir
        (
            directory,
            fileName::DIRECTORY
        )
    );

    // Initialise instant list
    instantList Times(dirEntries.size() + 1);
    label nTimes = 0;

    // Check for "constant"
    bool haveConstant = false;
    forAll(dirEntries, i)
    {
        if (dirEntries[i] == constantName)
        {
            Times[nTimes].value() = 0;
            Times[nTimes].name() = dirEntries[i];
            nTimes++;
            haveConstant = true;
            break;
        }
    }

    // Read and parse all the entries in the directory
    forAll(dirEntries, i)
    {
        IStringStream timeStream(dirEntries[i]);
        token timeToken(timeStream);

        if (timeToken.isNumber() && timeStream.eof())
        {
            Times[nTimes].value() = timeToken.number();
            Times[nTimes].name() = dirEntries[i];
            nTimes++;
        }
    }
/*
 * Calculates the observed counts (without parallelization)
 *
 * @param exemplar_num - the training examplar number
 * @param fst - the FST in with FeatureArcs
 * @param fst_log - the FST with the actual weights in log space
 * @param alpha - the alpha values
 * @param beta - the beta values
 *
 */
void WFST_Trainer_Local::calculate_observed_counts(int exemplar_num, 
					    VectorFst<FeatureArc> *fst, 
					    VectorFst<LogArc> *fst_log, 
					    vector<LogWeight> *alpha, 
					    vector<LogWeight> *beta) 
{
  //normalizing constant
  LogWeight Z = Times((*alpha)[0], (*beta)[0]);
  
  int arc_id = 0;
  for (StateIterator<VectorFst<FeatureArc> > siter(*fst); 
       !siter.Done(); siter.Next()) {
    int state_id = siter.Value();
    map<int,int> arc_mapper;
    int tmp = arc_id;
    
    for (ArcIterator<VectorFst<FeatureArc> > aiter1(*fst,state_id); 
	 !aiter1.Done(); aiter1.Next()) {
      const FeatureArc &arc = aiter1.Value();
      int old_arc_id = round(exp(-arc.weight.Value2().Value()));
      arc_mapper[tmp] = old_arc_id - 1;
      ++tmp;
    }

    for (ArcIterator<VectorFst<LogArc> > aiter2(*fst_log,state_id); 
	 !aiter2.Done(); aiter2.Next()) {
      const LogArc &arc = aiter2.Value();
      int old_arc_id2 = arc_mapper[arc_id];
      LogWeight val = ((Times(Times((*alpha)[state_id],arc.weight.Value()), 
			      (*beta)[arc.nextstate])));

      double log_val = Divide(val,Z).Value();
      this->observed_counts[old_arc_id2] = logadd(this->observed_counts[old_arc_id2],-log_val);
      ++arc_id;
    }
  }
}
Esempio n. 7
0
StdArc::Weight ARPA2WFST::_get_final_bow( StdArc::StateId st ){
  if( arpafst.Final(st) != StdArc::Weight::Zero() )
    return arpafst.Final(st);

  for( ArcIterator<VectorFst<StdArc> > aiter(arpafst,st); 
       !aiter.Done(); 
       aiter.Next() ){
    StdArc arc = aiter.Value();
    //Assume there is never more than 1 <eps> transition
    if( arc.ilabel == 0 ){
      StdArc::Weight w = Times(arc.weight, _get_final_bow(arc.nextstate));
      arpafst.SetFinal(st, w);
      return w;
    }
  }

  //Should never reach this place
  return StdArc::Weight::Zero();
}
Esempio n. 8
0
int main()
{
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&mod,&m);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1,opt,l,r,x;i<=m;i++)
		{
			scanf("%d%d%d",&opt,&l,&r);
			if(opt==3) printf("%d\n",Quary(l,r));
			else
			{
				scanf("%d",&x);
				if(opt==1) Add(l,r,x);
				else Times(l,r,x);
			}
		}
	}
}
Esempio n. 9
0
 Integer&  operator*=(const Integer& t)	{ return *this = Times(t); }
Esempio n. 10
0
 ///Takes arc as input parameter and returns modified arc
 Arc operator() ( const Arc& arc ) const {
   if (epsilons_.find (arc.ilabel) != epsilons_.end() )
     return Arc ( arc.ilabel, arc.olabel, arc.weight, arc.nextstate );
   return Arc ( arc.ilabel, arc.olabel, Times (arc.weight, wp_), arc.nextstate );
 }
Esempio n. 11
0
Times operator "" _times(unsigned long long t)
{
  return Times(t);
}