-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.cpp
134 lines (93 loc) · 2.96 KB
/
predict.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "predict.hpp"
#include <cassert>
CTNode::CTNode(void) :
m_log_prob_est(0.0),
m_log_prob_weighted(0.0)
{
m_count[0] = 0;
m_count[1] = 0;
m_child[0] = NULL;
m_child[1] = NULL;
}
CTNode::~CTNode(void) {
if (m_child[0]) delete m_child[0];
if (m_child[1]) delete m_child[1];
}
// number of descendants of a node in the context tree
size_t CTNode::size(void) const {
size_t rval = 1;
rval += child(false) ? child(false)->size() : 0;
rval += child(true) ? child(true)->size() : 0;
return rval;
}
// compute the logarithm of the KT-estimator update multiplier
double CTNode::logKTMul(symbol_t sym) const {
return NULL; // TODO: implement
}
// Calculate the logarithm of the weighted block probability.
void CTNode::updateLogProbability(void) {
// TODO : implement
}
// Update the node after having observed a new symbol.
void CTNode::update(const symbol_t symbol){
// TODO : implement
}
void CTNode::revert(const symbol_t symbol){
// TODO : implement
}
// create a context tree of specified maximum depth
ContextTree::ContextTree(size_t depth) :
m_root(new CTNode()),
m_depth(depth)
{ return; }
ContextTree::~ContextTree(void) {
if (m_root) delete m_root;
}
// clear the entire context tree
void ContextTree::clear(void) {
m_history.clear();
if (m_root) delete m_root;
m_root = new CTNode();
}
void ContextTree::update(const symbol_t sym) {
// TODO: implement
}
void ContextTree::update(const symbol_list_t &symbol_list) {
// TODO: implement
}
// updates the history statistics, without touching the context tree
void ContextTree::updateHistory(const symbol_list_t &symbol_list) {
for (size_t i=0; i < symbol_list.size(); i++) {
m_history.push_back(symbol_list[i]);
}
}
// removes the most recently observed symbol from the context tree
void ContextTree::revert(void) {
// TODO: implement
}
// shrinks the history down to a former size
void ContextTree::revertHistory(size_t newsize) {
assert(newsize <= m_history.size());
while (m_history.size() > newsize) m_history.pop_back();
}
// generate a specified number of random symbols
// distributed according to the context tree statistics
void ContextTree::genRandomSymbols(symbol_list_t &symbols, size_t bits) {
genRandomSymbolsAndUpdate(symbols, bits);
// restore the context tree to it's original state
for (size_t i=0; i < bits; i++) revert();
}
// generate a specified number of random symbols distributed according to
// the context tree statistics and update the context tree with the newly
// generated bits
void ContextTree::genRandomSymbolsAndUpdate(symbol_list_t &symbols, size_t bits) {
// TODO: implement
}
// the logarithm of the block probability of the whole sequence
double ContextTree::logBlockProbability(void) {
return m_root->logProbWeighted();
}
// get the n'th most recent history symbol, NULL if doesn't exist
const symbol_t *ContextTree::nthHistorySymbol(size_t n) const {
return n < m_history.size() ? &m_history[n] : NULL;
}