Esempio n. 1
0
int main(){
    uint i = 0;
    uint double_count = 0;
    vector<uint> probs(40, 0);
    uint limit = 1000000;
    for(uint j = 0; j < limit; j++){
        i = next_move(i, double_count);
        probs[i]++;
    }

    //Find 3 largest values
    uint min = limit;
    for(uint k = 0; k < 3; k++){
        uint max = 0;
        uint tmp = 0;
        for(uint j = 0; j < 40; j++){
            if(probs[j] > max && probs[j] < min){
                max = probs[j];
                tmp = j;
            }
        }
        cout << setfill('0') << setw(2) << tmp;
        min = max;
    }
        cout << endl;
}
int NaiveBayesGibbs::predict(int fid)
{
    std::vector<std::string> words = testWords_[fid];
    std::vector<double> probs(numCategories_, 0.0);
    for (int i=words.size()-1; i>=0; i--)
    {
        if (thetaHistory_.find(words[i]) == thetaHistory_.end())
            continue;

        std::vector<double> thetaList = thetaHistory_[words[i]];
        for (int c=0; c<numCategories_; c++)
            probs[c] += thetaList[c];
    }
    double max = -DBL_MAX;
    int label = -1;
    for (int c=0; c<numCategories_; c++)
    {
        if (probs[c] > max)
        {
            max = probs[c];
            label = c;
        }
    }
    return label;
}
int NaiveBayesGibbs::predictMLE(int fid)
{
    std::vector<std::string> words = testWords_[fid];
    std::vector<double> probs(numCategories_, 0.0);
    for (int i=words.size()-1; i>=0; i--)
    {
        if (wordCount_.find(words[i]) == wordCount_.end())
            continue;

        std::vector<int> counter = wordCount_[words[i]];
        for (int c=0; c<numCategories_; c++)
            probs[c] += log(counter[c]+0.01);
    }
    double max = -DBL_MAX;
    int label = -1;
    for (int c=0; c<numCategories_; c++)
    {
        if (probs[c] > max)
        {
            max = probs[c];
            label = c;
        }
    }
    return label;
}
Esempio n. 4
0
std::vector<float> Router::measure_probs(float reltol) const {
    POMAGMA_INFO("Measuring ob probs");
    const size_t item_count = m_carrier.item_count();
    std::vector<float> probs(1 + item_count, 0);
    const float max_increase = 1.0 + reltol;

    bool changed = true;
    while (changed) {
        changed = false;

        POMAGMA_DEBUG("accumulating route probabilities");

        // The following three cannot be mixed: openmp, gcc, fork.
        // see http://bisqwit.iki.fi/story/howto/openmp/#OpenmpAndFork
        //# pragma omp parallel for schedule(dynamic, 1)
        for (size_t i = 0; i < item_count; ++i) {
            Ob ob = 1 + i;

            float prob = 0;
            for (const Segment& segment : iter_val(ob)) {
                prob += get_prob(segment, probs);
            }

            if (prob > probs[ob] * max_increase) {
                //#pragma omp atomic
                changed = true;
            }
            probs[ob] = prob;  // relaxed memory order
        }
    }

    return probs;
}
Esempio n. 5
0
string BayesNet::predict(const Instance* instance, double* probability) const {
    int numOfClasses = metadata->numOfClasses;
    int numOfFeatures = metadata->numOfFeatures;
    
    Instance inst = *instance;
    double probSum = 0.0;
    vector<double> probs(numOfClasses);
    for (int y = 0; y < numOfClasses; ++y) {
        inst.classLabel = y;
        probs[y] = probabilityTables.back()->computeCondProb(&inst);
        for (int x = 0; x < numOfFeatures; ++x) {
            probs[y] *= probabilityTables[x]->computeCondProb(&inst);
        }
        probSum += probs[y];
    }
    
    double maxProb = -1.0;
    int maxClass = -1;
    for (int y = 0; y < numOfClasses; ++y) {
        probs[y] /= probSum;
        if (probs[y] > maxProb) {
            maxProb = probs[y];
            maxClass = y;
        }
    }
    
    if (probability)
        *probability = maxProb;
    return metadata->classVariable->convertInternalToValue(maxClass);
}
Esempio n. 6
0
QList<CloserNodesGuesser::NodeProbability> CloserNodesGuesser::doIterativeRandomWalk(
        const QList<FeatureAbstractPtr> &hints, unsigned int walks) const {

    QList<FeaturesComplexNetwork::Node> ids;
    for (const FeatureAbstractPtr &s: hints) {
        FeaturesComplexNetwork::Node id = cn.getNodeFromFeature(s);
        if (cn.valid(id)) {
            ids.append(id);
            puts("Encontrei");
        } else {
            //puts("Node not found");
        }
    }

    FeaturesComplexNetwork::NodeMap <double> final(cn);
    GraphUtilities::fillMap(cn, final, 1.0);


            foreach(FeaturesComplexNetwork::Node
                            id, ids) {
            rw.Execute(id, walks);
            FeaturesComplexNetwork::NodeMap <double> probs(cn);
            rw.getAllProbs(probs);
            for (FeaturesComplexNetwork::NodeIt it(cn); it != INVALID; ++it) {
                final[it] = final[it] * (1 - probs[it]);
            }
        }
Esempio n. 7
0
bool doublestratmatch::takeDouble( strategyprob& strat, const board& b, int cube )
{
    // need probabilities when the opponent holds the dice; that's what the strategy's
    // boardProbabilities returns so just use that.
    
    gameProbabilities probs( boardProbabilities(strat,b,false) );
    
    // calculate the equity at the doubled cube level assuming the player holds the dice
    
    interpMEdata deadME( equityInterpFn( probs, b.perspective(), 2*cube, b.perspective(), true ) );
    interpMEdata liveME( equityInterpFn( probs, b.perspective(), 2*cube, b.perspective(), false ) );
    
    double equityDouble = cubeLifeIndex*liveME(probs.probWin) + (1-cubeLifeIndex)*deadME(probs.probWin);
    
    // calculate the equity we'd give up if we passed. Respect the Crawford rule.
    
    int n = b.perspective() == 0 ? currMatch->getTarget() - currMatch->playerScore() : currMatch->getTarget() - currMatch->opponentScore();
    int m = b.perspective() == 0 ? currMatch->getTarget() - currMatch->opponentScore() : currMatch->getTarget() - currMatch->playerScore();
    
    double singleLossME;
    if( n == 1 )
        singleLossME = MET.matchEquityPostCrawford(m-cube);
    else if( m == 1 )
        singleLossME = -1;
    else
        singleLossME = MET.matchEquity(n, m-cube);

    return equityDouble > singleLossME;
}
Esempio n. 8
0
double doublestratmatch::equity( strategyprob& strat, const board& b, int cube, bool ownsCube, bool holdsDice )
{
    gameProbabilities probs( boardProbabilities(strat, b, holdsDice) );
    int cubeOwner = ownsCube ? b.perspective() : 1-b.perspective();
    interpMEdata deadME( equityInterpFn(probs, b.perspective(), cube, cubeOwner,true) );
    interpMEdata liveME( equityInterpFn(probs, b.perspective(), cube, cubeOwner,false) );
    
    return cubeLifeIndex*liveME(probs.probWin) + (1-cubeLifeIndex)*deadME(probs.probWin);
}
Esempio n. 9
0
bool small_test() {
    const int alphabet_size = 5;
    const int T = 2;

    std::vector<float> activations = {0.1, 0.6, 0.1, 0.1, 0.1,
                                      0.1, 0.1, 0.6, 0.1, 0.1};

    // Calculate the score analytically
    float expected_score;
    {
        std::vector<float> probs(activations.size());
        softmax(activations.data(), alphabet_size, T, probs.data());

        // Score calculation is specific to the given activations above
        expected_score = probs[1] * probs[7];
    }

    std::vector<int> labels = {1, 2};
    std::vector<int> label_lengths = {2};

    std::vector<int> lengths;
    lengths.push_back(T);

    float score;

    ctcComputeInfo info;
    info.loc = CTC_CPU;
    info.num_threads = 1;

    size_t cpu_alloc_bytes;
    throw_on_error(get_workspace_size(label_lengths.data(), lengths.data(),
                                      alphabet_size, lengths.size(), info,
                                      &cpu_alloc_bytes),
                   "Error: get_workspace_size in small_test");

    void* ctc_cpu_workspace = malloc(cpu_alloc_bytes);

    throw_on_error(compute_ctc_loss(activations.data(), NULL,
                                    labels.data(), label_lengths.data(),
                                    lengths.data(),
                                    alphabet_size,
                                    lengths.size(),
                                    &score,
                                    ctc_cpu_workspace,
                                    info),
                   "Error: compute_ctc_loss in small_test");

    free(ctc_cpu_workspace);
    score = std::exp(-score);
    const float eps = 1e-6;

    const float lb = expected_score - eps;
    const float ub = expected_score + eps;

    return (score > lb && score < ub);
}
    cv::vector< double > getMomentMatchBasedProbs( const cv::vector< cv::vector< cv::Point > > &contours )
    {
        cv::vector< double > probs( contours.size() );

        for( int i = 0; i < contours.size(); i++ )
        {
            probs[i] =  1.0 - fmin( matchShapes( contours[i], matchContour_, CV_CONTOURS_MATCH_I2, 0.0 ) / matchThreshold_, 1.0 );
        }
        return( probs );
    }
Esempio n. 11
0
 void MM::mle(){
   const Vec &n(suf()->n());
   double tot = sum(n);
   if(tot==0){
     Vec probs(size(), 1.0/size());
     set_pi(probs);
     return;
   }
   set_pi(n/tot);
 }
    cv::vector< double > getSurfMatchBasedProbs( const cv::vector< cv::vector< cv::Point > > &contours, cv_bridge::CvImagePtr cvPtr )
    {
        cv::vector< double > probs( contours.size() );

        for( int i = 0; i < contours.size(); i++ )
        {
            probs[i] = getSingleSurfProb( contours[i], cvPtr, i );
        }
        return( probs );
    }
Esempio n. 13
0
double_array ThresholdCurve::getProbabilities(std::vector<Prediction*> predictions, const int classIndex) const {

    // sort by predicted probability of the desired class.
    double_array probs(predictions.size());
    for (int i = 0; i < probs.size(); i++) {
        NominalPrediction *pred = static_cast<NominalPrediction*>(predictions[i]);
        probs[i] = pred->distribution()[classIndex];
    }
    return probs;
}
Esempio n. 14
0
	void LaserSensorModel::weightParticle( Particle& particle, 
											const SensorData& data )
	{
		if(!data.hasScan){
			return;
		}
		
		// get the laser ranges we expect to see for this particle
		std::vector<double> zhat = RayTrace( particle, data );

// 		lastTraces.push_back( zhat );
		
		// If the ray tracing is infeasible (signalled by empty return), we set the particle weight to zero
		if( zhat.empty() ) {
			particle.setW( 0 );
			return;
		}

// 		double cumProb = 0;
		double cumProb = 1; // initialize the cumulative probability of all the laser probabilities for this particle
					
		// find out what the probability of seeing the laser from this position is
		std::vector<double> probs(numPoints);
		for( unsigned int s = 0; s < numPoints; s++ ) {

			double rTrue = data.points[s*laserSubsample];
			double rEst = zhat[s];
			
			double indivProb = CalculateGaussian( rEst, rTrue )
							+ CalculateUniform( rTrue )
							+ CalculateExponential( rTrue )
							+ CalculateMaxRange( rTrue );

			probs[s] = indivProb;
			
			// find the probability of the real laser measurement under this distribution, and mulitply it into the cumulative
// 			cumProb = cumProb * indivProb;
// 			cumProb += indivProb;

// 			std::cout << "rEst: " << rEst << ", rTrue: " << rTrue << std::endl;
// 			std::cout << "indivProb: " << indivProb << ", cumProb: " << cumProb << std::endl;
			
		} // end find-cumlative-probability-of-every-laser-beam-for-this-particle

		std::sort( probs.begin(), probs.end() );
		for( unsigned int i = numProbsToSkip; i < numPoints; i++ ) {
			cumProb = cumProb * probs[i];
		}
// 		for( unsigned int i = 0; i < probs.size(); i++ ) {
// 			cumProb += probs[i];
// 		}
	
		particle.setW( cumProb * particle.getW() ); // update the particle weight
				
	}// end LaserSensorModel::weightParticle
void SpatiallySparseCNN::processDatasetRepeatTest(SpatialDataset &dataset, int batchSize, int nReps, string predictionsFilename,string header) {
  vector<vector<int> >   votes(dataset.pictures.size());
  vector<vector<float> > probs(dataset.pictures.size());
  for (int i=0;i<dataset.pictures.size();++i) {
    votes[i].resize(dataset.nClasses);
    probs[i].resize(dataset.nClasses);
  }
  ofstream f,g;
  for (int rep=1;rep<=nReps;++rep) {
    BatchProducer bp(*this, dataset,inputSpatialSize,batchSize,4);
    while(SpatiallySparseBatch* batch=bp.nextBatch()) {
      processBatch(*batch,0,f,g);
      for (int i=0;i<batch->interfaces[0].batchSize;++i) {
        int ii=batch->sampleNumbers[i];
        votes[ii][batch->predictions[i][0]]++;
        for (int j=0;j<dataset.nClasses;++j)
          probs[ii][j]+=batch->probabilities[i][j];
      }
      delete batch;
    }
    float errors=dataset.pictures.size(),nll=0;
    for (int i=0;i<dataset.pictures.size();++i) {
      vector<int> predictions=vectorTopIndices(probs[i],nTop);
      for (int j=0;j<nTop;j++)
        if (predictions[j]==dataset.pictures[i]->label)
          errors--;
      nll-=log(max(probs[i][dataset.pictures[i]->label]/rep,1.0e-15));
    }
    if (!predictionsFilename.empty()) {
      cout << predictionsFilename << endl;
      f.open(predictionsFilename.c_str());
      f<<header;
      for (int i=0;i<dataset.pictures.size();++i) {
        f << dataset.pictures[i]->identify();
        for (int j=0;j<dataset.nClasses;++j)
          f <<"," << probs[i][j]/rep;
        f <<endl;
      }
      f.close();

    }
    cout << dataset.name
         << " rep " << rep <<"/"<<nReps
         << " Mistakes: " << 100*errors/dataset.pictures.size()
         << "% NLL " << nll/dataset.pictures.size()
         <<endl;
    // // if (dataset.type==UNLABELLEDBATCH and rep==nReps) {
    // //   ofstream f;f.open("unlabelledData.predictions");
    // //   ofstream g;g.open("unlabelledData.probabilities");
    // // }
  }

}
Esempio n. 16
0
bool doublestratmatch::offerDouble( strategyprob& strat, const board& b, int cube )
{
    // no double allowed if the player can already win at this level
    
    int n = b.perspective() == 0 ? currMatch->getTarget() - currMatch->playerScore() : currMatch->getTarget() - currMatch->opponentScore();
    int m = b.perspective() == 0 ? currMatch->getTarget() - currMatch->opponentScore() : currMatch->getTarget() - currMatch->playerScore();
    
    if( n-cube<=0 ) return false;
    
    // automatic double case: if the opponent is definitely going to win the match on a win
    // the player should double.
    
    if( m-cube<=0 and n-cube>0 ) return true;
    
    // get the various game probabilities. For this we want game probabilities
    // before the dice are thrown. The strategy's boardProbabilities returns the
    // game probs *after* the dice are thrown, so we need to flip the board around,
    // get the probs, then flip their perspective back.
    
    gameProbabilities probs(boardProbabilities(strat, b, true));
    
    // calculate the dead cube equity - assuming there's no value to holding the cube
    
    interpMEdata deadME1( equityInterpFn( probs, b.perspective(), cube, b.perspective(), true ) );
    interpMEdata deadME2( equityInterpFn( probs, b.perspective(), 2*cube, 1-b.perspective(), true ) );
    
    // get the live cube match equity for this cube level and for a doubled cube. Current cube this player
    // owns the cube; doubled the opponent owns it.
    
    interpMEdata liveME1( equityInterpFn( probs, b.perspective(), cube, b.perspective(), false ) );
    interpMEdata liveME2( equityInterpFn( probs, b.perspective(), 2*cube, 1-b.perspective(), false ) );
    
    // double if that equity is larger.
    
    double equityDouble   = cubeLifeIndex * liveME2(probs.probWin) + ( 1 - cubeLifeIndex ) * deadME2(probs.probWin);
    double equityNoDouble = cubeLifeIndex * liveME1(probs.probWin) + ( 1 - cubeLifeIndex ) * deadME1(probs.probWin);
    
    // the doubled equity is never more than the pass equity. Respect the Crawford rule.
    
    double singleWinME;
    if( n == 1 )
        singleWinME = 1;
    else if( m == 1 )
        singleWinME = -MET.matchEquityPostCrawford(n-cube);
    else
        singleWinME = MET.matchEquity(n-cube, m);
    
    if( equityDouble > singleWinME ) equityDouble = singleWinME;
    
    // Leave a little threshold
    
    return equityDouble > equityNoDouble + 1e-6;
}
QList<QString> LabelGuesserExperiment::guessByIterativeRandomWalk(IterativeRandomWalk &walk,
                                                                  const QList<FeatureAbstractPtr> &hints) {
    setlocale(LC_ALL, "en_US");
    char buffer[100];

    QList<FeaturesComplexNetwork::Node> ids;
    for (const FeatureAbstractPtr &s: hints) {
        FeaturesComplexNetwork::Node id = cn.getNodeFromFeature(s);
        if (cn.valid(id))
            ids.append(id);
    }

    FeaturesComplexNetwork::NodeMap <double> final(cn);
    switch (m) {
        case XorProbabilities:
            GraphUtilities::fillMap(cn, final, 1.0);
            break;
        case MultProbabilities:
            GraphUtilities::fillMap(cn, final, 1.0);
            break;
        case SumProbabilities:
            GraphUtilities::fillMap(cn, final, 0.0);
            break;
    }


            foreach(FeaturesComplexNetwork::Node
                            id, ids) {
            walk.Execute(id, (unsigned int) walkLenght);
            FeaturesComplexNetwork::NodeMap <double> probs(cn);
            walk.getAllProbs(probs);
            for (FeaturesComplexNetwork::NodeIt it(cn); it != INVALID; ++it) {
                switch (m) {
                    case XorProbabilities:
                        final[it] = final[it] * (1 - probs[it]);
                        break;
                    case MultProbabilities:
                        final[it] = final[it] * probs[it];
                        break;
                    case SumProbabilities:
                        final[it] = final[it] + probs[it];
                        break;
                }

            }
        }
Esempio n. 18
0
  MM::MultinomialModel(const StringVec &names)
    : ParamPolicy(new VectorParams(1)),
      DataPolicy(new MS(1)),
      ConjPriorPolicy(),
      logp_current_(false)
  {
    std::vector<Ptr<CD> >
      dvec(make_catdat_ptrs(names));

    uint nlev= dvec[0]->nlevels();
    Vec probs(nlev, 1.0/nlev);
    set_pi(probs);

    set_data(dvec);
    mle();
    set_observer();
  }
Esempio n. 19
0
CRef<CGumbelParamsOptions> 
CGumbelParamsOptionsFactory::CreateStandard20AAOptions(
                                    CGeneralScoreMatrix::EScoreMatrixName smat)
{
    const size_t kNumResidues = 20;
    CRef<CGeneralScoreMatrix> 
        smatrix(new CGeneralScoreMatrix(smat, kNumResidues));

    CGumbelParamsOptions::TFrequencies probs(kNumResidues);

    probs[ 0 ] = 0.07805 ;
    probs[ 1 ] = 0.05129 ;
    probs[ 2 ] = 0.04487 ;
    probs[ 3 ] = 0.05364 ;
    probs[ 4 ] = 0.01925 ;
    probs[ 5 ] = 0.04264 ;
    probs[ 6 ] = 0.06295 ;
    probs[ 7 ] = 0.07377 ;
    probs[ 8 ] = 0.02199 ;
    probs[ 9 ] = 0.05142 ;
    probs[ 10 ] = 0.09019 ;
    probs[ 11 ] = 0.05744 ;
    probs[ 12 ] = 0.02243 ;
    probs[ 13 ] = 0.03856 ;
    probs[ 14 ] = 0.05203 ;
    probs[ 15 ] = 0.0712 ;
    probs[ 16 ] = 0.05841 ;
    probs[ 17 ] = 0.0133 ;
    probs[ 18 ] = 0.03216 ;
    probs[ 19 ] = 0.06441 ;

    CGumbelParamsOptions* options = new CGumbelParamsOptions();
    options->SetScoreMatrix(smatrix);
    options->SetGapOpening(11);
    options->SetGapExtension(1);
    options->SetLambdaAccuracy(0.001);
    options->SetKAccuracy(0.005);
    options->SetGapped(true);//gapped calculation
    options->SetMaxCalcTime(1.0);
    options->SetMaxCalcMemory(1000.0);
    options->SetSeq1ResidueProbs(probs);
    options->SetSeq2ResidueProbs(probs);

    return CRef<CGumbelParamsOptions>(options);
}
    cv::vector< double > getAreaBasedProbs( const cv::vector< cv::vector< cv::Point > > &contours )
    {
        int largestContour = -1;
        double maxArea = 0.0;
        cv::vector< double > probs( contours.size() );

        for( int i = 0; i < contours.size(); i++ )
        {
            probs[i] = cv::contourArea( contours[i] );
            if( maxArea < probs[i] )
            {
                maxArea = probs[i];
                largestContour = i;
            }
        }

        for( int i = 0; i < contours.size(); i++ )
        {
            probs[i] /= probs[largestContour];
        }

        return( probs );
    }
Esempio n. 21
0
  double DPMM::logp(const Vector &x) const {
    int number_of_components = mixture_components_.size();
    double ans = 0;
    if (number_of_components == 1) {
      ans += mixture_components_[0]->logp(x);
      return ans;
    }
    Vector counts = allocation_counts();
    // The Dirichlet process is the limit of finite mixture models
    // with symmetric Dirichlet priors (with total mass alpha) on the
    // mixing weights.
    Vector probs(number_of_components, alpha() / number_of_components);
    probs += counts;
    probs /= sum(probs);  // Posterior mode of mixing weights.

    Vector log_probs = log(probs);
    Vector wsp = log_probs;
    for (int i = 0; i < number_of_components; ++i) {
      wsp[i] += mixture_components_[i]->logp(x);
    }
    ans += lse(wsp);
    return ans;
  }
	void ExRandomTrees::evaluateTestSet(){
		m_votes.assign(m_evaluation->getNumTestingInstances()*2,0);
		m_workLeft = m_trees.size();
		m_choiceId = 0;

		TM_runFunctionPtr runFunc = TM_runFunctionPtr(new boost::function<void(void)>(std::bind(std::mem_fun(&ExRandomTrees::collectSingleVote),this)));
		TM_callbackFunctionPtr callbackFunc = TM_callbackFunctionPtr(new boost::function<void(int)>(std::bind1st(std::mem_fun(&ExRandomTrees::buildTreeCallback),this)));
		for(size_t treeIdx = 0; treeIdx < m_trees.size(); ++treeIdx) {
			ThreadManager::queueWorkPackage(runFunc,callbackFunc);
		}

		// Start tree building threads
		TimerPtr timer = TimerPtr(new boost::timer);
		ThreadManager::executeWorkQueue();
		m_barrier->wait();
		m_testTimer = timer->elapsed();

		unsigned int correct = 0,wrong = 0;

		std::map<double,std::vector<bool>,std::greater<double>> rankingcl1,rankingcl2;
		bool truePositive;
		std::vector<int> clCorrect(m_document->getNumClassValues(),0),clWrong(m_document->getNumClassValues(),0);
		std::vector<double> probs(2,0);
		for(size_t i = 0; i < m_evaluation->getNumTestingInstances(); i++) {
			unsigned int vote = m_votes[2*i] > m_votes[2*i+1] ? 0 : 1;
			probs[0] = m_votes[2*i];
			probs[1] = m_votes[2*i+1];

			if(vote == m_classSetTest[i]){
				++clCorrect[m_classSetTest[i]];
				truePositive;
				++correct;
			}
			else{
				++clWrong[m_classSetTest[i]];
				++wrong;
			}

			rankingcl1[double(probs[0])/double(probs[0]+probs[1])].push_back((0 == m_classSetTest[i]));
			rankingcl2[double(probs[1])/double(probs[0]+probs[1])].push_back((1 == m_classSetTest[i]));
		}

		double auc = (m_evaluation->calculateAUC(rankingcl1)+m_evaluation->calculateAUC(rankingcl2))/2.0;

		m_outputStream <<
			"	Time used for building trees: " << m_buildTimer << "s\r\n" <<
			"	Time used for evaluation of trees: " << m_testTimer << "s\r\n";

		m_outputStream	<< 
			"\r\n	"; 

		std::vector<std::vector<unsigned int>> confusMatrix(m_document->getNumClassValues(),std::vector<unsigned int>(m_document->getNumClassValues(),0));
		for(unsigned int i=0; i<m_document->getNumClassValues(); ++i){
			for(unsigned int j=0; j<m_document->getNumClassValues(); ++j){
				if(i == j)
					confusMatrix[i][j] = clCorrect[i];
				else
					confusMatrix[i][j] = clWrong[i];
			}
		}

		for(unsigned int i=0; i<m_document->getNumClassValues(); ++i){
			m_outputStream << i << "	";
		}
		for(unsigned int i=0; i<m_document->getNumClassValues(); ++i){
			m_outputStream << "\r\n	";
			for(unsigned int j=0; j<m_document->getNumClassValues(); ++j){
				m_outputStream  << confusMatrix[i][j] << "	";
			}
			m_outputStream << i;
		}
		m_outputStream << "\r\n";

		double accuracy = double(double(correct)/double(wrong+correct))*100;

		m_outputStream << 
			"\r\n	" << "Accuracy: " << accuracy << "%\r\n";

		PROCESS_MEMORY_COUNTERS pmc;
		GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
		size_t memUsageCPU = pmc.WorkingSetSize;

		m_resultWriter->addKeyValue("accuracy",accuracy);
		m_resultWriter->addKeyValue("cl1Correct",clCorrect[0]);
		m_resultWriter->addKeyValue("cl1Wrong",clWrong[0]);
		m_resultWriter->addKeyValue("cl2Correct",clCorrect[1]);
		m_resultWriter->addKeyValue("cl2Wrong",clWrong[1]);
		m_resultWriter->addKeyValue("trainingTime",m_buildTimer);
		m_resultWriter->addKeyValue("testingTime",m_testTimer);
		m_resultWriter->addKeyValue("totalTime",m_testTimer+m_buildTimer);
		m_resultWriter->addKeyValue("testingInstances",m_evaluation->getNumTestingInstances());
		m_resultWriter->addKeyValue("trainingInstances",m_evaluation->getNumTrainingInstances());
		m_resultWriter->addKeyValue("auc",auc);
		m_resultWriter->addKeyValue("memUsageCPU",double(memUsageCPU)/1024.0/1024.0);
	}
Esempio n. 23
0
    void doc_manager::populate(void)
    {
        
        add_class_descriptor(ml::k_base);
        
        add_class_descriptors(ml::k_base, {
            ml::k_classification,
            ml::k_regression
        });
        
        add_class_descriptors(ml::k_regression, {
            ml::k_ann,
            ml::k_linreg,
            ml::k_logreg
        });
        
        add_class_descriptors(ml::k_classification, {
            ml::k_svm,
            ml::k_adaboost,
            ml::k_anbc,
            ml::k_dtw,
            ml::k_hmmc,
            ml::k_softmax,
            ml::k_randforest,
            ml::k_mindist,
            ml::k_knn,
            ml::k_gmm,
            ml::k_dtree
        });
        
        add_class_descriptors(ml::k_feature_extraction, {
            ml::k_peak,
            ml::k_minmax,
            ml::k_zerox
        });
        
        descriptors[ml::k_ann].desc("Artificial Neural Network").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/MLP");
        descriptors[ml::k_linreg].desc("Linear Regression").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/LinearRegression");
        descriptors[ml::k_logreg].desc("Logistic Regression").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/LogisticRegression");
        descriptors[ml::k_peak].desc("Peak Detection").url("").num_outlets(1);
        descriptors[ml::k_minmax].desc("Minimum / Maximum Detection").url("").num_outlets(1);
        descriptors[ml::k_zerox].desc("Zero Crossings Detection").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/ZeroCrossingCounter");
        descriptors[ml::k_svm].desc("Support Vector Machine").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/SVM");
        descriptors[ml::k_adaboost].desc("Adaptive Boosting").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/AdaBoost");
        descriptors[ml::k_anbc].desc("Adaptive Naive Bayes Classifier").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/ANBC");
        descriptors[ml::k_dtw].desc("Dynamic Time Warping").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/DTW");
        descriptors[ml::k_hmmc].desc("Continuous Hidden Markov Model").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/HMM");
        descriptors[ml::k_softmax].desc("Softmax Classifier").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/Softmax");
        descriptors[ml::k_randforest].desc("Random Forests").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/RandomForests");
        descriptors[ml::k_mindist].desc("Minimum Distance").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/MinDist");
        descriptors[ml::k_knn].desc("K Nearest Neighbour").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/KNN");
        descriptors[ml::k_gmm].desc("Gaussian Mixture Model").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/GMMClassifier");
        descriptors[ml::k_dtree].desc("Decision Trees").url("http://www.nickgillian.com/wiki/pmwiki.php/GRT/DecisionTree");
        
        for (auto& desc : {&descriptors[ml::k_hmmc], &descriptors[ml::k_dtw]})
        {
            desc->notes(
                        "add and map messages for time series should be delimited with record messages, e.g. record 1, add 1 40 50, add 1 41 50, record 0"
            );
        }
        
        // base descriptor
        message_descriptor add(
                              "add",
                              "list comprising a class id followed by n features, <class> <feature 1> <feature 2> etc",
                               "1 0.2 0.7 0.3 0.1"
                              );

        
        message_descriptor train(
                                "train",
                                "train the model based on vectors added with 'add'"
                                );
        
        message_descriptor map(
                              "map",
                              "generate the output value(s) for the input feature vector",
                               "0.2 0.7 0.3 0.1"
                              );
        
        message_descriptor write(
                                 "write",
                                 "write training data and / or model, first argument gives path to write file",
                                 "/path/to/my_ml-lib_data"
                                 );
        
        message_descriptor read(
                                "read",
                                "read training data and / or model, first argument gives path to the read file",
                                "/path/to/my_ml-lib_data"
                                );
        
        message_descriptor clear(
                                 "clear",
                                 "clear the stored training data and model"
                                 );
        
        message_descriptor help(
                               "help",
                               "post usage statement to the console"
                               );
        
        valued_message_descriptor<int> scaling(
                                               "scaling",
                                               "sets whether values are automatically scaled",
                                               {0, 1},
                                               1
                                               );
        
        valued_message_descriptor<int> record(
                                              "record",
                                              "start or stop time series recording for a single example of a given class",
                                              {0, 1},
                                              0
                                              );
        
        ranged_message_descriptor<float> training_rate(
                                                       "training_rate",
                                                       "set the learning rate, used to update the weights at each step of learning algorithms such as stochastic gradient descent.",
                                                       0.01,
                                                       1.0,
                                                       0.1
                                                       );
        
        ranged_message_descriptor<float> min_change(
                                                    "min_change",
                                                    "set the minimum change that must be achieved between two training epochs for the training to continue",
                                                    0.0,
                                                    1.0,
                                                    1.0e-5
                                                    );
        
        ranged_message_descriptor<int> max_iterations(
                                                      "max_iterations",
                                                      "set the maximum number of training iterations",
                                                      0,
                                                      1000,
                                                      100
                                                      );
        
        record.insert_before = "add";
        descriptors[ml::k_base].add_message_descriptor(add, write, read, train, clear, map, help, scaling, training_rate, min_change, max_iterations);

        // generic classification descriptor
        valued_message_descriptor<bool> null_rejection(
                                                       "null_rejection",
                                                       "toggle NULL rejection off or on, when 'on' classification results below the NULL-rejection threshold will be discarded",
                                                       {false, true},
                                                       true
                                                       );
        
        ranged_message_descriptor<float> null_rejection_coeff(
                                                              "null_rejection_coeff",
                                                              "set a multiplier for the NULL-rejection threshold ",
                                                              0.1,
                                                              1.0,
                                                              0.9
                                                              );
        
        valued_message_descriptor<int> probs(
                                             "probs",
                                             "determines whether probabilities are sent from the right outlet",
                                             {0, 1},
                                             0
                                             );
        
        descriptors[ml::k_classification].add_message_descriptor(null_rejection_coeff, probs, null_rejection);
        
        // generic feature extraction descriptor
//        descriptors[ml::k_feature_extraction].add_message_descriptor(null_rejection_coeff, null_rejection);

        // generic regression descriptor
       
        
//        descriptors[ml::k_regression].add_message_descriptor(training_rate, min_change, max_iterations);
        
        // Object-specific descriptors
        //-- Regressifiers
        //---- ann
        valued_message_descriptor<ml::data_type> mode("mode",
                                                      "set the mode of the ANN, " + std::to_string(ml::LABELLED_CLASSIFICATION) + " for classification, " + std::to_string(ml::LABELLED_REGRESSION) + " for regression",
                                                      {ml::LABELLED_CLASSIFICATION, ml::LABELLED_REGRESSION, ml::LABELLED_TIME_SERIES_CLASSIFICATION},
                                                      ml::defaults::data_type
                                                      );
        
        
        message_descriptor add_ann(
                              "add",
                              "class id followed by n features, <class> <feature 1> <feature 2> etc when in classification mode or N output values followed by M input values when in regression mode (N = num_outputs)",
                                   "1 0.2 0.7 0.3 0.1"

                              );
      
        ranged_message_descriptor<int> num_outputs(
                                                   "num_outputs",
                                                   "set the number of neurons in the output layer",
                                                   1,
                                                   1000,
                                                   ml::defaults::num_output_dimensions
                                                   );
        
        ranged_message_descriptor<int> num_hidden(
                                                  "num_hidden",
                                                  "set the number of neurons in the hidden layer",
                                                  1,
                                                  1000,
                                                  ml::defaults::num_hidden_neurons
                                                  );
        
        ranged_message_descriptor<int> min_epochs(
                                                  "min_epochs",
                                                  "setting the minimum number of training iterations",
                                                  1,
                                                  1000,
                                                  10
                                                  );
        
        // TODO: check if the "epochs" are still needed or if we can use "iterations" as inherited from ml_regression
        ranged_message_descriptor<int> max_epochs(
                                                  "max_epochs",
                                                  "setting the maximum number of training iterations",
                                                  1,
                                                  10000,
                                                  100
                                                  );

        ranged_message_descriptor<float> momentum(
                                                  "momentum",
                                                  "set the momentum",
                                                  0.0,
                                                  1.0,
                                                  0.5
                                                  );
        
        ranged_message_descriptor<float> gamma(
                                                  "gamma",
                                                  "set the gamma",
                                                  0.0,
                                                  10.0,
                                                  2.0
                                                  );
        
        // TODO: have optional value_labels for value labels
        valued_message_descriptor<int> input_activation_function(
                                                                 "input_activation_function",
                                                                 "set the activation function for the input layer, 0:LINEAR, 1:SIGMOID, 2:BIPOLAR_SIGMOID",
                                                                 {0, 1, 2},
                                                                 0
                                                                 );
        
        valued_message_descriptor<int> hidden_activation_function(
                                                                 "hidden_activation_function",
                                                                 "set the activation function for the hidden layer, 0:LINEAR, 1:SIGMOID, 2:BIPOLAR_SIGMOID",
                                                                 {0, 1, 2},
                                                                 0
                                                                 );
        
        valued_message_descriptor<int> output_activation_function(
                                                                 "output_activation_function",
                                                                 "set the activation function for the output layer, 0:LINEAR, 1:SIGMOID, 2:BIPOLAR_SIGMOID",
                                                                 {0, 1, 2},
                                                                 0
                                                                 );

                                                                 
        ranged_message_descriptor<int> rand_training_iterations(
                                                                 "rand_training_iterations",
                                                                 "set the number of random training iterations",
                                                                 0,
                                                                 1000,
                                                                 10
                                                                 );

        valued_message_descriptor<bool> use_validation_set(
                                                           "use_validation_set",
                                                           "set whether to use a validation training set",
                                                           {false, true},
                                                           true
                                                           );
        
        ranged_message_descriptor<int> validation_set_size(
                                                           "validation_set_size",
                                                           "set the size of the validation set",
                                                           1,
                                                           100,
                                                           20
                                                           );
        
        valued_message_descriptor<bool> randomize_training_order(
                                                           "randomize_training_order",
                                                           "sets whether to randomize the training order",
                                                           {false, true},
                                                           false
                                                           );
        
        
        descriptors[ml::k_ann].add_message_descriptor(add_ann, probs, mode, null_rejection, null_rejection_coeff, num_outputs, num_hidden, min_epochs, max_epochs, momentum, gamma, input_activation_function, hidden_activation_function, output_activation_function, rand_training_iterations, use_validation_set, validation_set_size, randomize_training_order);
        
        
        //-- Classifiers
        //---- ml.svm
        ranged_message_descriptor<int> type(
                                            "type",
                                            "set SVM type,"
                                            " 0:C-SVC (multi-class),"
                                            " 1:nu-SVC (multi-class),"
                                            " 2:one-class SVM,"
                                           // " 3:epsilon-SVR (regression),"
                                           // " 4:nu-SVR (regression)"
                                            ,
                                            0,
                                            2,
                                            0
                                            //        "	0 -- C-SVC		(multi-class classification)\n"
                                            //        "	1 -- nu-SVC		(multi-class classification)\n"
                                            //        "	2 -- one-class SVM\n"
                                            //        "	3 -- epsilon-SVR	(regression)\n"
                                            //        "	4 -- nu-SVR		(regression)\n"

                                            );
        
        ranged_message_descriptor<int> kernel(
                                              "kernel",
                                              "set type of kernel function, "
                                              "0:linear, " // (u'*v),"
                                              "1:polynomial, " // (gamma*u'*v + coef0)^degree,"
                                              "2:radial basis function, " //: exp(-gamma*|u-v|^2),"
                                              "3:sigmoid, " //  tanh(gamma*u'*v + coef0),"
                                              "4:precomputed kernel (kernel values in training_set_file)",
                                              0,
                                              4,
                                              0
                                              //        "	0 -- linear: u'*v\n"
                                              //        "	1 -- polynomial: (gamma*u'*v + coef0)^degree\n"
                                              //        "	2 -- radial basis function: exp(-gamma*|u-v|^2)\n"
                                              //        "	3 -- sigmoid: tanh(gamma*u'*v + coef0)\n"
                                              //        "	4 -- precomputed kernel (kernel values in training_set_file)\n"
                                              );
        
        ranged_message_descriptor<float> degree(
                                              "degree",
                                              "set degree in kernel function",
                                              0,
                                              20,
                                              3
                                              );
        
        ranged_message_descriptor<float> svm_gamma(
                                              "gamma",
                                              "set gamma in kernel function",
                                              0.0,
                                              1.0,
                                              0.5
                                              );
        
        ranged_message_descriptor<float> coef0(
                                               "coef0",
                                               "coef0 in kernel function",
                                               INFINITY * -1.f, INFINITY,
                                               0.0
                                               );
        
        ranged_message_descriptor<float> cost(
                                               "cost",
                                               "set the parameter C of C-SVC, epsilon-SVR, and nu-SVR",
                                               INFINITY * -1.f, INFINITY,
                                               1.0
                                               );
        
        ranged_message_descriptor<float> nu(
                                              "nu",
                                              "set the parameter nu of nu-SVC, one-class SVM, and nu-SVR",
                                              INFINITY * -1.f, INFINITY,
                                              0.5
                                              );
        
        message_descriptor cross_validation(
                                            "cross_validation",
                                            "perform cross validation"
                                            );
        
        ranged_message_descriptor<int> num_folds(
                                                 "num_folds",
                                                 "set the number of folds used for cross validation",
                                                 1, 100,
                                                 10
                                                 );
        
        descriptors[ml::k_svm].add_message_descriptor(cross_validation, num_folds, type, kernel, degree, svm_gamma, coef0, cost, nu);
        
        //---- ml.adaboost        
        ranged_message_descriptor<int> num_boosting_iterations(
                                                                "num_boosting_iterations",
                                                               "set the number of boosting iterations that should be used when training the model",
                                                               0,
                                                               200,
                                                               20
                                                               );
        
        valued_message_descriptor<int> prediction_method(
                                                        "prediction_method",
                                                         "set the Adaboost prediction method, 0:MAX_VALUE, 1:MAX_POSITIVE_VALUE",
                                                         {GRT::AdaBoost::MAX_VALUE, GRT::AdaBoost::MAX_POSITIVE_VALUE},
                                                         GRT::AdaBoost::MAX_VALUE
                                                         
        );
        
        valued_message_descriptor<int> set_weak_classifier(
                                                           "set_weak_classifier",
                                                           "sets the weak classifier to be used by Adaboost, 0:DECISION_STUMP, 1:RADIAL_BASIS_FUNCTION",
                                                           {ml::weak_classifiers::DECISION_STUMP, ml::weak_classifiers::RADIAL_BASIS_FUNCTION},
                                                           ml::weak_classifiers::DECISION_STUMP
                                                           );
        
        valued_message_descriptor<int> add_weak_classifier(
                                                           "add_weak_classifier",
                                                           "add a weak classifier to the list of classifiers used by Adaboost",
                                                           {ml::weak_classifiers::DECISION_STUMP, ml::weak_classifiers::RADIAL_BASIS_FUNCTION},
                                                           ml::weak_classifiers::DECISION_STUMP
                                                           );

        descriptors[ml::k_adaboost].add_message_descriptor(num_boosting_iterations, prediction_method, set_weak_classifier, add_weak_classifier);
        
        //---- ml.anbc
        message_descriptor weights("weights",
                                   "vector of 1 integer and N floating point values where the integer is a class label and the floats are the weights for that class. Sending weights with a vector size of zero clears all weights"
                                   );
        
        descriptors[ml::k_anbc].add_message_descriptor(weights);
        
        //---- ml.dtw
        valued_message_descriptor<int> rejection_mode(
                                                      "rejection_mode",
                                                      "sets the method used for null rejection, 0:TEMPLATE_THRESHOLDS, 1:CLASS_LIKELIHOODS, 2:THRESHOLDS_AND_LIKELIHOODS",
                                                      {GRT::DTW::TEMPLATE_THRESHOLDS, GRT::DTW::CLASS_LIKELIHOODS, GRT::DTW::THRESHOLDS_AND_LIKELIHOODS},
                                                      GRT::DTW::TEMPLATE_THRESHOLDS
                                                      );
        
        ranged_message_descriptor<float> warping_radius(
                                                        "warping_radius",
                                                        "sets the radius of the warping path, which is used if the constrain_warping_path is set to 1",
                                                        0.0,
                                                        1.0,
                                                        0.2
                                                        );
        
        valued_message_descriptor<bool> offset_time_series(
                                                           "offset_time_series",
                                                           "set if each timeseries should be offset by the first sample in the time series",
                                                           {false, true},
                                                           false
                                                           );
        
        valued_message_descriptor<bool> constrain_warping_path(
                                                           "constrain_warping_path",
                                                           "sets the warping path should be constrained to within a specific radius from the main diagonal of the cost matrix",
                                                           {false, true},
                                                           true
                                                           );
        
        valued_message_descriptor<bool> enable_z_normalization(
                                                               "enable_z_normalization",
                                                               "turn z-normalization on or off for training and prediction",
                                                               {false, true},
                                                               false
                                                               );
        
        valued_message_descriptor<bool> enable_trim_training_data(
                                                               "enable_trim_training_data",
                                                               "enabling data trimming prior to training",
                                                               {false, true},
                                                               false
                                                               );
  
        descriptors[ml::k_dtw].insert_message_descriptor(record);
        descriptors[ml::k_dtw].add_message_descriptor(rejection_mode, warping_radius, offset_time_series, constrain_warping_path, enable_z_normalization, enable_trim_training_data);
        
        //---- ml.hmmc
        valued_message_descriptor<int> model_type(
                                                  "model_type",
                                                  "set the model type used, 0:ERGODIC, 1:LEFTRIGHT",
                                                  {HMM_ERGODIC, HMM_LEFTRIGHT},
                                                  HMM_LEFTRIGHT
                                                  );
        
        ranged_message_descriptor<int> delta(
                                             "delta",
                                             "control how many states a model can transition to if the LEFTRIGHT model type is used",
                                             1,
                                             100,
                                             11
                                             );
        
        ranged_message_descriptor<int> max_num_iterations(
                                                          "max_num_iterations",
                                                          "set the maximum number of training iterations",
                                                          1,
                                                          1000,
                                                          100
                                                          );
        
        ranged_message_descriptor<int> committee_size(
                                                      "committee_size",
                                                      "set the committee size for the number of votes combined to make a prediction",
                                                      1,
                                                      1000,
                                                      5
                                                      );
        
        ranged_message_descriptor<int> downsample_factor(
                                                      "downsample_factor",
                                                         "set the downsample factor for the resampling of each training time series. A factor of 5 will result in each time series being resized (smaller) by a factor of 5",
                                                      1,
                                                      1000,
                                                      5
                                                      );
        
        descriptors[ml::k_hmmc].insert_message_descriptor(record);
        descriptors[ml::k_hmmc].add_message_descriptor(model_type, delta, max_num_iterations, committee_size, downsample_factor);
        
        //---- ml.softmax
        
        //---- ml.randforest
        ranged_message_descriptor<int> num_random_splits(
                                                         "num_random_splits",
                                                         "set the number of steps that will be used to search for the best spliting value for each node",
                                                         1,
                                                         1000,
                                                         100
                                                         );
        
        ranged_message_descriptor<int> min_samples_per_node2(
                                                            "min_samples_per_node",
                                                            "set the minimum number of samples that are allowed per node",
                                                            1,
                                                            100,
                                                            5
                                                            );
        
        ranged_message_descriptor<int> max_depth(
                                                 "max_depth",
                                                 "sets the maximum depth of the tree, any node that reaches this depth will automatically become a leaf node",
                                                 1,
                                                 100,
                                                 10
                                                 );

        descriptors[ml::k_randforest].add_message_descriptor(num_random_splits, min_samples_per_node2, max_depth);
        
        //----ml.mindist
        ranged_message_descriptor<int> num_clusters(
                                                    "num_clusters",
                                                    "set how many clusters each model will try to find during the training phase",
                                                    1,
                                                    100,
                                                    10
                                                    );

        descriptors[ml::k_mindist].add_message_descriptor(num_clusters);
                
        //---- ml.knn
//        "best_k_value_search:\tbool (0 or 1) set whether k value search is enabled or not (default 0)\n";

        ranged_message_descriptor<int> k(
                                         "k",
                                         "sets the K nearest neighbours that will be searched for by the algorithm during prediction",
                                         1,
                                         500,
                                         10
                                         );
        
        ranged_message_descriptor<int> min_k_search_value(
                                         "min_k_search_value",
                                         "set the minimum K value to use when searching for the best K value",
                                         1,
                                         500,
                                         1
                                         );
        
        ranged_message_descriptor<int> max_k_search_value(
                                                          "max_k_search_value",
                                                          "set the maximum K value to use when searching for the best K value",
                                                          1,
                                                          500,
                                                          10
                                                          );
        
        valued_message_descriptor<bool> best_k_value_search(
                                                            "best_k_value_search",
                                                            "set whether k value search is enabled or not",
                                                            {false, true},
                                                            false
                                                            );
        
        descriptors[ml::k_knn].add_message_descriptor(k, min_k_search_value, max_k_search_value, best_k_value_search);
        
        //---- ml.gmm
        ranged_message_descriptor<int> num_mixture_models(
                                                          "num_mixture_models",
                                                          "sets the number of mixture models used for class",
                                                          1,
                                                          20,
                                                          2
                                                          );

        descriptors[ml::k_gmm].add_message_descriptor(num_mixture_models);

        //---- ml.dtree
        valued_message_descriptor<bool> training_mode(
                                                      "training_mode",
                                                      "set the training mode",
                                                      {GRT::Tree::BEST_ITERATIVE_SPILT, GRT::Tree::BEST_RANDOM_SPLIT},
                                                      GRT::Tree::BEST_ITERATIVE_SPILT
                                                      );
        
        ranged_message_descriptor<int> num_splitting_steps(
                                                          "num_splitting_steps",
                                                          "set the number of steps that will be used to search for the best spliting value for each node",
                                                          1,
                                                          500,
                                                          100
                                                          );
        
        ranged_message_descriptor<int> min_samples_per_node(
                                                          "min_samples_per_node",
                                                          "sets the minimum number of samples that are allowed per node, if the number of samples at a node is below this value then the node will automatically become a leaf node",
                                                          1,
                                                          100,
                                                          5
                                                          );
        
        ranged_message_descriptor<int> dtree_max_depth(
                                                 "max_depth",
                                                 "sets the maximum depth of the tree, any node that reaches this depth will automatically become a leaf node",
                                                 1,
                                                 100,
                                                 10
                                                 );
        
        valued_message_descriptor<bool> remove_features_at_each_split(
                                                               "remove_features_at_each_split",
                                                               "set if a feature is removed at each spilt so it can not be used again",
                                                               {false, true},
                                                               false
                                                               );
        descriptors[ml::k_dtree].add_message_descriptor(training_mode, num_splitting_steps, min_samples_per_node, dtree_max_depth, remove_features_at_each_split);

        //-- Feature extraction
        
        //---- ml.peak
        ranged_message_descriptor<int> search_window_size(
                                                          "search_window_size",
                                                          "set the search window size in values",
                                                          1,
                                                          500,
                                                          5
                                                          );
        
        ranged_message_descriptor<float> peak(
                                              "float",
                                              "set the current value of the peak detector, a bang will be output when a peak is detected",
                                              INFINITY * -1.f, INFINITY,
                                              1
                                              );
        
        message_descriptor reset(
                                "reset",
                                "reset the peak detector"
                                );
        
        message_descriptor peak_help(
                                 "help",
                                 "post usage statement to the console"
                                 );


        descriptors[ml::k_peak].add_message_descriptor(peak, reset, search_window_size, peak_help);
        
        //---- ml.minmax
        
        message_descriptor input(
                                 "list",
                                 "list of float values in which to find minima and maxima",
                                 "0.1 0.5 -0.3 0.1 0.2 -0.1 0.7 0.1 0.3"
                                 );
        
        ranged_message_descriptor<float> minmax_delta(
                                                      "delta",
                                                      "setting the minmax delta. Input values will be considered to be peaks if they are greater than the previous and next value by at least the delta value",
                                                      0,
                                                      1,
                                                      0.1
                                                      );
        
        descriptors[ml::k_minmax].add_message_descriptor(input, minmax_delta);
        
        //---- ml.zerox
        
        valued_message_descriptor<float> zerox_map(
                                                   "map",
                                                   "a stream of input values in which to detect zero crossings",
                                                   0.5
                                                   );
        
        ranged_message_descriptor<float> dead_zone_threshold(
                                                             "dead_zone_threshold",
                                                             "set the dead zone threshold",
                                                             0.f,
                                                             1.f,
                                                             0.01f
                                                             );
        
        ranged_message_descriptor<int> zerox_search_window_size(
                                                          "search_window_size",
                                                          "set the search window size in values",
                                                          1,
                                                          500,
                                                          20
                                                          );
        
        descriptors[ml::k_zerox].add_message_descriptor(zerox_map, dead_zone_threshold, zerox_search_window_size);
    }
string Classification::toString(){
	string probs("Tuple #" + to_string(id) + " prob of being " + label + " is " + to_string(end()) + "\n");
	return probs;
}
Esempio n. 25
0
// simulate genotypes given observed marker data
// [[Rcpp::export(".sim_geno")]]
IntegerVector sim_geno(const String& crosstype,
                       const IntegerMatrix& genotypes, // columns are individuals, rows are markers
                       const IntegerMatrix& founder_geno, // columns are markers, rows are founder lines
                       const bool is_X_chr,
                       const LogicalVector& is_female, // length n_ind
                       const IntegerMatrix& cross_info, // columns are individuals
                       const NumericVector& rec_frac,   // length nrow(genotypes)-1
                       const IntegerVector& marker_index, // length nrow(genotypes)
                       const double error_prob,
                       const int n_draws) // number of imputations
{
    const int n_ind = genotypes.cols();
    const int n_pos = marker_index.size();
    const int n_mar = genotypes.rows();

    QTLCross* cross = QTLCross::Create(crosstype);

    // check inputs
    if(is_female.size() != n_ind)
        throw std::range_error("length(is_female) != ncol(genotypes)");
    if(cross_info.cols() != n_ind)
        throw std::range_error("ncols(cross_info) != ncol(genotypes)");
    if(rec_frac.size() != n_pos-1)
        throw std::range_error("length(rec_frac) != length(marker_index)-1");

    if(error_prob < 0.0 || error_prob > 1.0)
        throw std::range_error("error_prob out of range");

    for(int i=0; i<rec_frac.size(); i++) {
        if(rec_frac[i] < 0 || rec_frac[i] > 0.5)
            throw std::range_error("rec_frac must be >= 0 and <= 0.5");
    }
    if(!cross->check_founder_geno_size(founder_geno, n_mar))
        throw std::range_error("founder_geno is not the right size");
    // end of checks

    const int mat_size = n_pos*n_draws;
    IntegerVector draws(mat_size*n_ind); // output object

    for(int ind=0; ind<n_ind; ind++) {

        Rcpp::checkUserInterrupt();  // check for ^C from user

        // possible genotypes for this individual
        IntegerVector poss_gen = cross->possible_gen(is_X_chr, is_female[ind], cross_info(_,ind));
        const int n_poss_gen = poss_gen.size();
        NumericVector probs(n_poss_gen);

        // backward equations
        NumericMatrix beta = backwardEquations(cross, genotypes(_,ind), founder_geno, is_X_chr, is_female[ind],
                                               cross_info(_,ind), rec_frac, marker_index, error_prob,
                                               poss_gen);

        // simulate genotypes
        for(int draw=0; draw<n_draws; draw++) {
            // first draw
            // calculate first prob (on log scale)
            probs[0] = cross->init(poss_gen[0], is_X_chr, is_female[ind], cross_info(_,ind)) + beta(0,0);
            if(marker_index[0] >= 0)
                probs[0] += cross->emit(genotypes(marker_index[0],ind), poss_gen[0], error_prob,
                                        founder_geno(_, marker_index[0]), is_X_chr, is_female[ind], cross_info(_,ind));
            double sumprobs = probs[0]; // to contain log(sum(probs))

            // calculate rest of probs
            for(int g=1; g<n_poss_gen; g++) {
                probs[g] = cross->init(poss_gen[g], is_X_chr, is_female[ind], cross_info(_,ind)) + beta(g,0);
                if(marker_index[0] >= 0)
                    probs[g] += cross->emit(genotypes(marker_index[0],ind), poss_gen[g], error_prob,
                                            founder_geno(_, marker_index[0]), is_X_chr, is_female[ind], cross_info(_,ind));
                sumprobs = addlog(sumprobs, probs[g]);
            }

            // re-scale probs
            for(int g=0; g<n_poss_gen; g++)
                probs[g] = exp(probs[g] - sumprobs);

            // make draw, returns a value from 1, 2, ..., n_poss_gen
            int curgeno = random_int(probs);
            draws[draw*n_pos + ind*mat_size] = poss_gen[curgeno];

            // move along chromosome
            for(int pos=1; pos<n_pos; pos++) {

                // calculate probs
                for(int g=0; g<n_poss_gen; g++) {
                    probs[g] = cross->step(poss_gen[curgeno], poss_gen[g], rec_frac[pos-1],
                                           is_X_chr, is_female[ind], cross_info(_,ind)) +
                        beta(g,pos) - beta(curgeno, pos-1);
                    if(marker_index[pos] >= 0)
                        probs[g] += cross->emit(genotypes(marker_index[pos],ind), poss_gen[g], error_prob,
                                                founder_geno(_, marker_index[pos]), is_X_chr, is_female[ind], cross_info(_,ind));
                    probs[g] = exp(probs[g]);
                }

                // make draw
                curgeno = random_int(probs);

                draws[pos + draw*n_pos + ind*mat_size] = poss_gen[curgeno];

            } // loop over positions
        } // loop over draws
    } // loop over individuals

    draws.attr("dim") = Dimension(n_pos, n_draws, n_ind);
    delete cross;
    return draws;
}
Esempio n. 26
0
int main()
{
	std::cout << "Initializing MCMCAlgorithm object---------------" << std::endl;
	int samples = 200;
	int thinning = 10;
	int useSamples = 100;
	std::cout << "\t# Samples: " << samples << "\n";
	std::cout << "\tThinning: " << thinning << "\n";
	std::cout << "\t# Samples used: " << useSamples << "\n";
	MCMCAlgorithm mcmc = MCMCAlgorithm(samples, thinning, 100, false, true, true);
	mcmc.setRestartFileSettings(std::string("test"), 100, true);

	//mcmc.setRestartFileSettings("RestartFile.txt", 20, true);
	std::cout << "Done!-------------------------------\n\n\n";


	std::cout << "initialize Genome object--------------------------" << std::endl;
	bool withPhi = false;

	Genome genome;
	genome.readFasta("/home/clandere/CodonUsageBias/RibosomeModel/RibModelDev/data/twoMixtures/simulatedMutationSharedR.fasta");
	//genome.readFasta("F:/GitHub/RibModelDev/data/twoMixtures/simulatedAllUniqueR.fasta");
	//genome.readFasta("E:/RibosomeModel/RibModelDev/data/twoMixtures/simulatedAllUniqueR_unevenMixtures.fasta");
	if(withPhi)
	{
		genome.readObservedPhiValues("F:/GitHub/RibModelDev/data/twoMixtures/simulatedAllUniqueR_phi_withPhiSet.csv", false);
		//genome.readObservedPhiValues("E:/RibosomeModel/RibModelDev/data/twoMixtures/simulatedAllUniqueR_phi_unevenMixtures.csv", false);
	}

	std::cout << "Done!-------------------------------\n\n\n";
	std::cout << "Initializing shared parameter variables---------------\n";

	std::cout << "Done!-------------------------------\n\n\n";
	std::cout << "Initializing shared parameter variables---------------\n";
	std::vector<unsigned> geneAssignment(genome.getGenomeSize());

	unsigned numMixtures = 2;
	std::vector<double> sphi_init(numMixtures, 1);

	/* For 2 mixture */
	for (unsigned i = 0u; i < genome.getGenomeSize(); i++)
	{
		geneAssignment[i] = ( ((double)rand() / (double)RAND_MAX) < 0.5 ? 0u : 1u );

	}
	std::vector<std::vector<unsigned>> mixtureDefinitionMatrix;
	std::cout << "Done!------------------------\n\n\n";

	std::cout << "initialize ROCParameter object" << std::endl;
	std::string mixDef = ROCParameter::mutationShared;
	ROCParameter parameter(sphi_init, numMixtures, geneAssignment, mixtureDefinitionMatrix, true, mixDef);

	for (unsigned i = 0u; i < numMixtures; i++)
	{
		unsigned selectionCategry = parameter.getSelectionCategory(i);
		std::cout << "Sphi_init for selection category " << selectionCategry << ": " << sphi_init[selectionCategry] << std::endl;
	}
	std::cout << "\t# mixtures: " << numMixtures << "\n";
	std::cout << "\tmixture definition: " << mixDef << "\n";

	std::vector<std::string> files(1);
	//files[0] = std::string("F:/GitHub/RibModelDev/data/twoMixtures/simulated_mutation0.csv");
	//files[1] = std::string("F:/GitHub/RibModelDev/data/twoMixtures/simulated_mutation1.csv");
	files[0] = std::string("/home/clandere/CodonUsageBias/RibosomeModel/RibModelDev/data/twoMixtures/simulated_mutation0.csv");
	//files[1] = std::string("/home/clandere/CodonUsageBias/RibosomeModel/RibModelDev/data/twoMixtures/simulated_mutation1.csv");
	parameter.initMutationCategories(files, parameter.getNumMutationCategories());
	files.resize(2);
	//files[0] = std::string("F:/GitHub/RibModelDev/data/twoMixtures/simulated_selection0.csv");
	//files[1] = std::string("F:/GitHub/RibModelDev/data/twoMixtures/simulated_selection1.csv");
	files[0] = std::string("/home/clandere/CodonUsageBias/RibosomeModel/RibModelDev/data/twoMixtures/simulated_selection0.csv");
	files[1] = std::string("/home/clandere/CodonUsageBias/RibosomeModel/RibModelDev/data/twoMixtures/simulated_selection1.csv");
	parameter.initSelectionCategories(files, parameter.getNumSelectionCategories());

	parameter.InitializeSynthesisRate(genome, sphi_init[0]);
	//std::vector<double> phiVals = parameter.readPhiValues("/home/clandere/CodonUsageBias/RibosomeModel/RibModelFramework/ribModel/data/Skluyveri_ChrA_ChrCleft_phi_est.csv");
	//parameter.InitializeSynthesisRate(phiVals);
	std::cout << "done initialize ROCParameter object" << std::endl;


	std::cout << "Initializing ROCModel object\n";

	ROCModel model(withPhi);
	model.setParameter(parameter);


	std::cout << "starting MCMC for ROC" << std::endl;
	mcmc.run(genome, model, 1, 0);
	std::cout << std::endl << "Finished MCMC for ROC" << std::endl;
	double temp[] = { 0.025, 0.5, 0.975 };
	std::vector<double> probs(temp, temp + sizeof(temp) / sizeof(double));
	//std::vector<double> quants = parameter.getCodonSpecificQuantile(1, 100, std::string("GCA"), 0, probs, true);

	std::string codon = std::string("TTT");
	double a = parameter.getCodonSpecificPosteriorMean(0u, 50u, codon, 0u, true);
	double b = parameter.getCodonSpecificPosteriorMean(0u, 50u, codon, 1u, true);
	double c = parameter.getCodonSpecificPosteriorMean(1u, 50u, codon, 0u, true);
	double d = parameter.getCodonSpecificPosteriorMean(1u, 50u, codon, 1u, true);

	std::cout << std::endl << "Exiting" << std::endl;
}
Esempio n. 27
0
/**
 * \brief Initialize all mutation operators of all active genotypes
 */
bool Mutation::initialize(StateP state)
{
	state_ = state;
	protectedGenotypes_.clear();
	protectedGenotypes_.insert(protectedGenotypes_.begin(), operators.size(), false);
	opProb.clear();

	voidP sptr = state->getRegistry()->getEntry("mutation.indprob");
	indMutProb_ = *((double*)sptr.get());

	sptr = state->getRegistry()->getEntry("mutation.geneprob");
//	geneMutProb_ = *((double*)sptr.get());
//	if(state->getRegistry()->isModified("mutation.geneprob") == false)
//		geneMutProb_ = 0;

	sptr = state->getRegistry()->getEntry("mutation.genotypes");
	std::string mutGen = *((std::string*)sptr.get());

	mutateGenotypes_ = RANDOM_GENOTYPE;
	if(mutGen == "random")
		mutateGenotypes_ = RANDOM_GENOTYPE;
	else if(mutGen == "all")
		mutateGenotypes_ = ALL_GENOTYPES;
	else
		ECF_LOG_ERROR(state, "Warning: invalid parameter value (key: mutation.genotypes)");

	// read protected genotypes
	std::stringstream ss;
	sptr = state->getRegistry()->getEntry("mutation.protected");
	ss << *((std::string*) sptr.get());
	uint genId;
	while(ss >> genId) {	// read all the data from string
		if(genId >= protectedGenotypes_.size()) {
			ECF_LOG_ERROR(state, "Error: invalid genotype index (key: mutation.protected)!");
			throw("");
		}
		protectedGenotypes_[genId] = true;
	}

	// initialize operators for all genotypes
	for(uint gen = 0; gen < operators.size(); gen++) {
		uint nOps = (uint) operators[gen].size();
		// if the genotype doesn't define mutation operators
		if(nOps == 0) {
			protectedGenotypes_[gen] = true;
			std::vector<double> empty;
			opProb.push_back(empty);
			break;
		}
		for(uint i = 0; i < nOps; i++) {
			operators[gen][i]->state_ = state;
			operators[gen][i]->initialize(state);
		}
		// calculate cumulative operator probabilities
		std::vector<double> probs(nOps);
		probs[0] = operators[gen][0]->probability_;
		for(uint i = 1; i < nOps; i++) {
			probs[i] = probs[i - 1] + operators[gen][i]->probability_;
		}
		if(probs[nOps - 1] == 0) {
			std::vector<double> none(1);
			none[0] = -1;
			opProb.push_back(none);
		} else {
			if(probs[nOps - 1] != 1) {
				double normal = probs[nOps - 1];
				ECF_LOG_ERROR(state, "Warning: " + operators[gen][0]->myGenotype_->getName() +
					" mutation operators: cumulative probability not equal to 1 (sum = " + dbl2str(normal) + ")");
				for(uint i = 0; i < probs.size(); i++)
					probs[i] /= normal;
			}
			opProb.push_back(probs);
		}
	}
	return true;
}
Esempio n. 28
0
/**
 * The Stan print function.
 *
 * @param argc Number of arguments
 * @param argv Arguments
 * 
 * @return 0 for success, 
 *         non-zero otherwise
 */
int main(int argc, const char* argv[]) {
  
  if (argc == 1) {
    print_usage();
    return 0;
  }
  
  // Parse any arguments specifying filenames
  std::ifstream ifstream;
  std::vector<std::string> filenames;
  
  for (int i = 1; i < argc; i++) {
    
    if (std::string(argv[i]).find("--autocorr=") != std::string::npos)
      continue;
    
    if (std::string(argv[i]).find("--sig_figs=") != std::string::npos)
      continue;
    
    if (std::string("--help") == std::string(argv[i])) {
      print_usage();
      return 0;
    }
    
    ifstream.open(argv[i]);
    
    if (ifstream.good()) {
      filenames.push_back(argv[i]);
      ifstream.close();
    } else {
      std::cout << "File " << argv[i] << " not found" << std::endl;
    }
    
  }
  
  if (!filenames.size()) {
    std::cout << "No valid input files, exiting." << std::endl;
    return 0;
  }
  
  // Parse specified files
  Eigen::VectorXd warmup_times(filenames.size());
  Eigen::VectorXd sampling_times(filenames.size());
  
  Eigen::VectorXi thin(filenames.size());
  
  ifstream.open(filenames[0].c_str());
  
  stan::io::stan_csv stan_csv = stan::io::stan_csv_reader::parse(ifstream);
  warmup_times(0) = stan_csv.timing.warmup;
  sampling_times(0) = stan_csv.timing.sampling;
  
  stan::mcmc::chains<> chains(stan_csv);
  ifstream.close();
  
  thin(0) = stan_csv.metadata.thin;
  
  for (std::vector<std::string>::size_type chain = 1; 
       chain < filenames.size(); chain++) {
    ifstream.open(filenames[chain].c_str());
    stan_csv = stan::io::stan_csv_reader::parse(ifstream);
    chains.add(stan_csv);
    ifstream.close();
    thin(chain) = stan_csv.metadata.thin;
    
    warmup_times(chain) = stan_csv.timing.warmup;
    sampling_times(chain) = stan_csv.timing.sampling;
    
  }
  
  double total_warmup_time = warmup_times.sum();
  double total_sampling_time = sampling_times.sum();

  // Compute largest variable name length
  const int skip = 0;
  std::string model_name = stan_csv.metadata.model;
  size_t max_name_length = 0;
  for (int i = skip; i < chains.num_params(); i++) 
    if (chains.param_name(i).length() > max_name_length)
      max_name_length = chains.param_name(i).length();
  for (int i = 0; i < 2; i++) 
    if (chains.param_name(i).length() > max_name_length)
      max_name_length = chains.param_name(i).length();


  // Prepare values
  int n = 9;
  
  Eigen::MatrixXd values(chains.num_params(), n);
  values.setZero();
  Eigen::VectorXd probs(3);
  probs << 0.05, 0.5, 0.95;
  
  for (int i = 0; i < chains.num_params(); i++) {
    double sd = chains.sd(i);
    double n_eff = chains.effective_sample_size(i);
    values(i,0) = chains.mean(i);
    values(i,1) = sd / sqrt(n_eff);
    values(i,2) = sd;
    Eigen::VectorXd quantiles = chains.quantiles(i,probs);
    for (int j = 0; j < 3; j++)
      values(i,3+j) = quantiles(j);
    values(i,6) = n_eff;
    values(i,7) = n_eff / total_sampling_time;
    values(i,8) = chains.split_potential_scale_reduction(i);
  }
  
  // Prepare header
  Eigen::Matrix<std::string, Eigen::Dynamic, 1> headers(n);
  headers << 
    "Mean", "MCSE", "StdDev",
    "5%", "50%", "95%", 
    "N_Eff", "N_Eff/s", "R_hat";
  
  // Set sig figs
  Eigen::VectorXi column_sig_figs(n);
  
  int sig_figs = 2;
  
  for (int k = 1; k < argc; k++)
    if (std::string(argv[k]).find("--sig_figs=") != std::string::npos)
      sig_figs = atoi(std::string(argv[k]).substr(11).c_str());
  
  // Compute column widths
  Eigen::VectorXi column_widths(n);
  Eigen::Matrix<std::ios_base::fmtflags, Eigen::Dynamic, 1> formats(n);
  column_widths = calculate_column_widths(values, headers, sig_figs, formats);
  
  // Initial output
  std::cout << "Inference for Stan model: " << model_name << std::endl
            << chains.num_chains() << " chains: each with iter=(" << chains.num_kept_samples(0);
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << "," << chains.num_kept_samples(chain);
  std::cout << ")";
  
  // Timing output
  std::cout << "; warmup=(" << chains.warmup(0);
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << "," << chains.warmup(chain);
  std::cout << ")";
                          
  std::cout << "; thin=(" << thin(0);
  
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << "," << thin(chain);
  std::cout << ")";
                          
  std::cout << "; " << chains.num_samples() << " iterations saved."
            << std::endl << std::endl;

  std::string warmup_unit = "seconds";
  
  if (total_warmup_time / 3600 > 1) {
    total_warmup_time /= 3600;
    warmup_unit = "hours";
  } else if (total_warmup_time / 60 > 1) {
    total_warmup_time /= 60;
    warmup_unit = "minutes";
  }
  
  std::cout << "Warmup took ("
            << std::fixed
            << std::setprecision(compute_precision(warmup_times(0), sig_figs, false))
            << warmup_times(0);
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << ", " << std::fixed
              << std::setprecision(compute_precision(warmup_times(chain), sig_figs, false))
              << warmup_times(chain);
  std::cout << ") seconds, ";
  std::cout << std::fixed
            << std::setprecision(compute_precision(total_warmup_time, sig_figs, false))
            << total_warmup_time << " " << warmup_unit << " total" << std::endl;

  std::string sampling_unit = "seconds";
  
  if (total_sampling_time / 3600 > 1) {
    total_sampling_time /= 3600;
    sampling_unit = "hours";
  } else if (total_sampling_time / 60 > 1) {
    total_sampling_time /= 60;
    sampling_unit = "minutes";
  }

  std::cout << "Sampling took ("
            << std::fixed
            << std::setprecision(compute_precision(sampling_times(0), sig_figs, false))
            << sampling_times(0);
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << ", " << std::fixed
              << std::setprecision(compute_precision(sampling_times(chain), sig_figs, false))
              << sampling_times(chain);
  std::cout << ") seconds, ";
  std::cout << std::fixed
            << std::setprecision(compute_precision(total_sampling_time, sig_figs, false))
            << total_sampling_time << " " << sampling_unit << " total" << std::endl;
  std::cout << std::endl;

  // Header output
  std::cout << std::setw(max_name_length + 1) << "";
  for (int i = 0; i < n; i++) {
    std::cout << std::setw(column_widths(i)) << headers(i);
  }
  std::cout << std::endl;
  
  // Value output
  for (int i = skip; i < chains.num_params(); i++) {
    if (!is_matrix(chains.param_name(i))) {
      std::cout << std::setw(max_name_length + 1) << std::left << chains.param_name(i);
      std::cout << std::right;
      for (int j = 0; j < n; j++) {
        std::cout.setf(formats(j), std::ios::floatfield);
        std::cout << std::setprecision(
                                       compute_precision(values(i,j), sig_figs, formats(j) == std::ios_base::scientific))
                  << std::setw(column_widths(j)) << values(i, j);
      }
      std::cout << std::endl;
    } else {
      std::vector<int> dims = dimensions(chains, i);
      std::vector<int> index(dims.size(), 1);
      int max = 1;
      for (int j = 0; j < dims.size(); j++)
        max *= dims[j];
      
      for (int k = 0; k < max; k++) {
        int param_index = i + matrix_index(index, dims);
        std::cout << std::setw(max_name_length + 1) << std::left 
                  << chains.param_name(param_index);
        std::cout << std::right;
        for (int j = 0; j < n; j++) {
          std::cout.setf(formats(j), std::ios::floatfield);
          std::cout 
            << std::setprecision(compute_precision(values(param_index,j), 
                                                   sig_figs, 
                                                   formats(j) == std::ios_base::scientific))
            << std::setw(column_widths(j)) << values(param_index, j);
        }
        std::cout << std::endl;
        next_index(index, dims);
      }
      i += max-1;
    }
  }

  /// Footer output
  std::cout << std::endl;
  std::cout << "Samples were drawn using " << stan_csv.metadata.algorithm
            << " with " << stan_csv.metadata.engine << "." << std::endl
            << "For each parameter, N_Eff is a crude measure of effective sample size," << std::endl
            << "and R_hat is the potential scale reduction factor on split chains (at " << std::endl
            << "convergence, R_hat=1)." << std::endl
            << std::endl;
  
  // Print autocorrelation, if desired
  for (int k = 1; k < argc; k++) {
    
    if (std::string(argv[k]).find("--autocorr=") != std::string::npos) {
      
      const int c = atoi(std::string(argv[k]).substr(11).c_str());
      
      if (c < 0 || c >= chains.num_chains()) {
        std::cout << "Bad chain index " << c << ", aborting autocorrelation display." << std::endl;
        break;
      }
      
      Eigen::MatrixXd autocorr(chains.num_params(), chains.num_samples(c));
      
      for (int i = 0; i < chains.num_params(); i++) {
        autocorr.row(i) = chains.autocorrelation(c, i);
      }
      
      // Format and print header
      std::cout << "Displaying the autocorrelations for chain " << c << ":" << std::endl;
      std::cout << std::endl;
      
      const int n_autocorr = autocorr.row(0).size();
      
      int lag_width = 1;
      int number = n_autocorr; 
      while ( number != 0) { number /= 10; lag_width++; }

      std::cout << std::setw(lag_width > 4 ? lag_width : 4) << "Lag";
      for (int i = 0; i < chains.num_params(); ++i) {
        std::cout << std::setw(max_name_length + 1) << std::right << chains.param_name(i);
      }
      std::cout << std::endl;

      // Print body  
      for (int n = 0; n < n_autocorr; ++n) {
        std::cout << std::setw(lag_width) << std::right << n;
        for (int i = 0; i < chains.num_params(); ++i) {
          std::cout << std::setw(max_name_length + 1) << std::right << autocorr(i, n);
        }
        std::cout << std::endl;
      }
  
    }
        
  }
      
  return 0;
        
}
vnl_vector_fixed<double,3> MLBSTrackingFilter< NumImageFeatures >::Classify(itk::Point<double, 3>& pos, int& candidates, vnl_vector_fixed<double,3>& olddir, double angularThreshold, double& prob, bool avoidStop)
{
    vnl_vector_fixed<double,3> direction; direction.fill(0);

    vigra::MultiArray<2, double> featureData;
    if(m_UseDirection)
        featureData = vigra::MultiArray<2, double>( vigra::Shape2(1,NumImageFeatures+3) );
    else
        featureData = vigra::MultiArray<2, double>( vigra::Shape2(1,NumImageFeatures) );

    typename FeatureImageType::PixelType featurePixel = GetImageValues(pos);

    // pixel values
    for (unsigned int f=0; f<NumImageFeatures; f++)
        featureData(0,f) = featurePixel[f];

    // direction features
    int c = 0;
    if (m_UseDirection)
    {
        vnl_vector_fixed<double,3> ref; ref.fill(0); ref[0]=1;
        for (unsigned int f=NumImageFeatures; f<NumImageFeatures+3; f++)
        {
            if (dot_product(ref, olddir)<0)
                featureData(0,f) = -olddir[c];
            else
                featureData(0,f) = olddir[c];
            c++;
        }
    }

    vigra::MultiArray<2, double> probs(vigra::Shape2(1, m_DecisionForest->class_count()));
    m_DecisionForest->predictProbabilities(featureData, probs);

    double outProb = 0;
    prob = 0;
    candidates = 0; // directions with probability > 0
    for (int i=0; i<m_DecisionForest->class_count(); i++)
    {
        if (probs(0,i)>0)
        {
            int classLabel = 0;
            m_DecisionForest->ext_param_.to_classlabel(i, classLabel);

            if (classLabel<m_DirectionIndices.size())
            {
                candidates++;

                vnl_vector_fixed<double,3> d = m_ODF.GetDirection(m_DirectionIndices.at(classLabel));
                double dot = dot_product(d, olddir);

                if (olddir.magnitude()>0)
                {
                    if (fabs(dot)>angularThreshold)
                    {
                        if (dot<0)
                            d *= -1;
                        dot = fabs(dot);
                        direction += probs(0,i)*dot*d;
                        prob += probs(0,i)*dot;
                    }
                }
                else
                {
                    direction += probs(0,i)*d;
                    prob += probs(0,i);
                }
            }
            else
                outProb += probs(0,i);
        }
    }

    ItkDoubleImgType::IndexType idx;
    m_NotWmImage->TransformPhysicalPointToIndex(pos, idx);
    if (m_NotWmImage->GetLargestPossibleRegion().IsInside(idx))
    {
        m_NotWmImage->SetPixel(idx, m_NotWmImage->GetPixel(idx)+outProb);
        m_WmImage->SetPixel(idx, m_WmImage->GetPixel(idx)+prob);
    }
    if (outProb>prob && prob>0)
    {
        candidates = 0;
        prob = 0;
        direction.fill(0.0);
    }
    if (avoidStop && m_AvoidStopImage->GetLargestPossibleRegion().IsInside(idx) && candidates>0 && direction.magnitude()>0.001)
        m_AvoidStopImage->SetPixel(idx, m_AvoidStopImage->GetPixel(idx)+0.1);

    return direction;
}