-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgammaFunc.c
More file actions
237 lines (196 loc) · 5.92 KB
/
gammaFunc.c
File metadata and controls
237 lines (196 loc) · 5.92 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* Copyright 2013 Jorge Merlino
This file is part of Context.
Context is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Context is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Context. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gammaTable.h"
#include "gammaFunc.h"
#include "debug.h"
#include "reset.h"
#include <math.h> /* for log */
#ifdef DEBUG
/** Number of gamma function evaluations resolved by table lookup. */
static int gammaHits=0;
/** Number of gamma function evaluations calculated explicitly. */
static int gammaMisses=0;
#endif
/** Ln of PI/2 */
#define LNPI_2 M_LNPI / 2
/** Log2 of the alphabet size. */
static double alphasizeLog = 0;
/** Binary entropy of 1/alphasize. */
static double alphaEntropy = 0;
/**
* Calculates LN(n!) If offset != 0 calculates LN((n + offset)!) defined as LN((n + offset) *
* (n - 1 + offset) * ... * (1 + offset))
* @param[in] n the argument
* @param[in] offset the offset to add to the argument in each step
*/
static double lnFactorial (int n, double offset) {
int i;
double fact = log(1 + offset);
for (i=n; i>1; i--) {
fact += log(i + offset);
}
return fact;
}
/**
* Calculates Ln (gamma(n)).
* @param[in] n the argument.
* @returns Ln (gamma(n)).
*/
static double evalLogGamma(int n)
{
int index = n-2;
if (index >= 0 && index < TBL_SIZE) {
DEBUGCODE(gammaHits++);
return logGammaTbl[index];
}
else if (n == 1) {
DEBUGCODE(gammaHits++);
return 0;
}
else {
DEBUGCODE(gammaMisses++);
return gsl_sf_lngamma(n);
}
}
/**
* Calculates Ln (gamma(n + 1/2)).
* @param[in] n the argument.
* @returns Ln (gamma(n + 1/2)).
*/
static double evalLogGamma2(int n)
{
int index = n-1;
if (index >= 0 && index < TBL_SIZE) {
DEBUGCODE(gammaHits++);
return logGammaTbl2[index];
}
else if (n == 0) {
DEBUGCODE(gammaHits++);
return LNPI_2; /* Log (gamma (1/2)) */
}
else {
DEBUGCODE(gammaMisses++);
return gsl_sf_lngamma(n + 0.5);
}
}
/**
* @param[in] stats The statistics needed to calculate the probability assignment.
* @returns KT probability assignment.
*/
double kt (statistics_t stats) {
Uint i, ns = 0, alpha_2 = alphasize / 2; /* integer division */
double sum = 0, gamma1, gamma2, gamma3;
for(i=0; i<alphasize; i++) {
sum += evalLogGamma2(stats->count[i]);
ns += stats->count[i];
}
if (alphasize % 2 == 0) {
gamma1 = evalLogGamma(ns + alpha_2);
gamma2 = alpha_2 * M_LNPI;
gamma3 = evalLogGamma(alpha_2);
}
else {
gamma1 = evalLogGamma2(ns + alpha_2);
gamma2 = (alpha_2 + 0.5) * M_LNPI;
gamma3 = evalLogGamma2(alpha_2);
}
return (gamma1 + gamma2 - gamma3 - sum) / M_LN2;
}
/**
* @param[in] stats The statistics needed to calculate the probability assignment.
* @returns KT probability assignment.
*/
double nodeCost (statistics_t stats) {
Uint ns = 0, i;
double sum = 0;
double loghalf = -0.6931471805599452862;
double x;
for (i=0; i<stats->symbolCount; i++) {
ns += stats->count[stats->symbols[i]];
if (stats->count[stats->symbols[i]] > 0) {
sum += evalLogGamma2(stats->count[stats->symbols[i]]-1);
}
}
x = (evalLogGamma(ns) - ((stats->symbolCount - 1) * loghalf) - evalLogGamma(stats->symbolCount) - sum + (stats->symbolCount * 0.5 * M_LNPI)) / M_LN2;
return x;
}
double deckard (statistics_t stats) {
Uint i;
double ns = 0;
double sum = 0;
double loghalf = -0.6931471805599452862;
double x, sumTmp;
double MAX_COUNT_D = MAX_COUNT;
for (i=0; i<stats->symbolCount; i++) {
ns += stats->count[stats->symbols[i]];
if (stats->count[stats->symbols[i]] > 0) {
sumTmp = evalLogGamma(MAX_COUNT+2) + (MAX_COUNT_D / 2) + (evalLogGamma(MAX_COUNT+1) / 2);
sum += stats->count[stats->symbols[i]] / (MAX_COUNT_D+1) * sumTmp;
}
}
x = (-(ns / (MAX_COUNT_D+1)) * (evalLogGamma((2*MAX_COUNT_D) + 1) - ((MAX_COUNT+1) * loghalf) - evalLogGamma(MAX_COUNT)) - evalLogGamma(MAX_COUNT) - ((MAX_COUNT-1) * loghalf) +
(ns / 2) * M_LNPI - ns * (evalLogGamma(MAX_COUNT + 1) / 2) + ((stats->symbolCount - 1) / ((MAX_COUNT_D / 2) - 1)) * (evalLogGamma(MAX_COUNT+1) - evalLogGamma(MAX_COUNT_D / 2)) -
evalLogGamma(MAX_COUNT_D / 2) - sum) / M_LN2;
return x;
}
/**
* @param[in] stats The statistics needed to calculate the probability assignment.
* @returns KT probability assignment.
*/
double escapeCost (statistics_t stats, Uint * distinct) {
Uint ns = 0, i;
double sum = 0;
double loghalf = -0.6931471805599452862;
for (i=0; i<stats->symbolCount; i++) {
ns += distinct[stats->symbols[i]];
if (distinct[stats->symbols[i]] > 0) {
sum += evalLogGamma2(distinct[stats->symbols[i]]-1);
}
}
return (evalLogGamma(ns) - ((stats->symbolCount - 1) * loghalf) - evalLogGamma(stats->symbolCount) - sum + (stats->symbolCount * 0.5 * M_LNPI)) / M_LN2;
}
/**
* @returns log2 of the alphabet size.
*/
double log2Alpha () {
if (alphasizeLog == 0) {
alphasizeLog = gsl_sf_log(alphasize) / M_LN2;
}
return alphasizeLog;
}
/**
* @returns binary entropy of 1/alphasize.
*/
double hAlpha () {
if (alphaEntropy == 0) {
double invAlpha = (double)1 / alphasize;
alphaEntropy = (invAlpha * log2Alpha()) - ((1 - invAlpha) * gsl_sf_log(1 - invAlpha) / M_LN2);
DEBUGCODE(printf(">>>> %e\n", alphaEntropy));
}
return alphaEntropy;
}
#ifdef DEBUG
/**
* @returns number of table lookup evaluations.
*/
int getHits() {
return gammaHits;
}
/**
* @returns number of explicit evaluations.
*/
int getMisses() {
return gammaMisses;
}
#endif