-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkovChain.cpp
More file actions
56 lines (45 loc) · 1.33 KB
/
Copy pathMarkovChain.cpp
File metadata and controls
56 lines (45 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* MarkovChain.cpp
*
* Created on: Mar 4, 2010
* Author: mckinsel
*/
#include <assert.h>
#include "MarkovChain.h"
#include <iostream>
using namespace std;
MarkovChain::MarkovChain(unsigned int ord, int pseudo) {
order = ord;
pseudocount = pseudo;
total_count = 0;
}
MarkovChain::~MarkovChain() {
// TODO Auto-generated destructor stub
}
void MarkovChain::add_sequence(string seqtoadd) {
for(unsigned int i=0; i<seqtoadd.length() - order; i++){
string_counts[seqtoadd.substr(i, order)]++;
total_count++;
}
}
long double MarkovChain::ordermer_prob(string ordermer) {
if(ordermer.length() != order){
cout << "ordermer " << ordermer << endl;
}
assert(ordermer.length() == order);
return (string_counts[ordermer] + pseudocount)/((long double)total_count + pseudocount);
}
long double MarkovChain::sequence_probability(string evalstring) {
long double prob = 1;
if(evalstring.length() < 10){
// cout << "evalstring " << evalstring << " " << evalstring.length() << endl;
}
for(unsigned int i=0; i<evalstring.length() - order; i++){
if(evalstring.length() < 10){
cout << i << " " << evalstring.length() << evalstring.length() - order << endl;
}
prob *= ordermer_prob(evalstring.substr(i, order));
}
// cout << "Evaluating MC probability of sequence " << evalstring << " and it's " << prob << endl;
return prob;
}