-
Notifications
You must be signed in to change notification settings - Fork 0
/
EigenBingham.h
291 lines (214 loc) · 6.74 KB
/
EigenBingham.h
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#ifndef EIGEN_BINGHAM
#define EIGEN_BINGHAM
#include <Eigen/Dense>
#include <functional>
/*
Methods: Generate a Bingham Distribution,
Fitting, Mixture Models, Hypersphere Methods,
No Meshing Functions or whatever...
Lookup Table Construction as Raw Binary!!
Probably slow as hell...
Potentially Faster Computation of Partition Function
Gradient Computation of F
A lot of stuff in libbingham is extra stuff that isn't needed.
*/
namespace Bingham {
using namespace Eigen;
using namespace std;
using T = double; //Floating Point Precision
const size_t DIM = 3; //Dimensionality of a Point
const T SPI = 2.0 / sqrt(M_PI);
const T EPSILON = 1E-8;
const size_t MAXFACT = 10000;
const size_t ITERATION_MULT = 10;
const size_t MIN_ITERATIONS = 10;
typedef Matrix<T, DIM, DIM> matD; //DIM x DIM Matrix
typedef Matrix<T, Dynamic, DIM> matX; //DIM x X Matrix
typedef Matrix<T, DIM, 1> vecD; //DIM x 1 Vector
/*
================================================================================
Computation of the Hypergeometric Function of Matrix Argument
================================================================================
*/
// Log Factorials and Factorials
T lfact(size_t k){
static T logf[MAXFACT]; //lfact lookuptable
static bool first = true;
if(first){
first = false;
logf[0] = 0;
for (size_t i = 1; i < MAXFACT; i++)
logf[i] = log(i) + logf[i-1];
}
return logf[k];
}
T fact(size_t k){ //Factorial of k
return exp(lfact(k));
}
// Surface Area of N-Sphere
T NSphereArea(size_t D){
if(D == 0) return 2;
if(D == 1) return 2*M_PI;
if(D == 2) return 4*M_PI;
if(D == 3) return 2*M_PI*M_PI;
return (2*M_PI/((T)D-1))*NSphereArea(D-2);
}
// Hypergeometric Functions of Matrix Argument
template<size_t D>
T C1F1(T a, T b, vecD Z, size_t iter);
template<>
T C1F1<1>(T a, T b, vecD Z, size_t iter){
T g, F = 0;
vecD logZ = Z.array().log();
for(size_t i = 0; i < iter; i++){
g = 0;
g += lgamma( i + a );
g -= lgamma( i + b );
g += i * logZ(DIM-1);
g -= lfact(i);
//if ((i > Z(0)) && exp(g) < EPSILON * F)
// break;
F += exp(g);
}
F /= 2;
if(DIM % 2 == 0) return F * SPI;
return F;
}
template<>
T C1F1<2>(T a, T b, vecD Z, size_t iter){
T g, F = 0;
vecD logZ = Z.array().log();
for(size_t i = 0; i < iter; i++){
for(size_t j = 0; j < iter; j++){
g = 0;
g += lgamma( i + a ) + lgamma( j + a );
g -= lgamma( i + j + b );
g += i * logZ(DIM-2) + j * logZ(DIM-1);
g -= lfact(i) + lfact(j);
// if ((i > Z(0) || j > Z(1)) /*&& exp(g) < EPSILON * F*/)
// break;
F += exp(g);
}
}
F /= 2 * sqrt(M_PI);
if(DIM % 2 == 0) return F * SPI;
return F;
}
template<>
T C1F1<3>(T a, T b, vecD Z, size_t iter){
T g, F = 0;
vecD logZ = Z.array().log();
for(size_t i = 0; i < iter; i++){
for(size_t j = 0; j < iter; j++){
for(size_t k = 0; k < iter; k++){
g = 0;
g += lgamma( i + a ) + lgamma( j + a ) + lgamma( k + a );
g -= lgamma( i + j + k + b);
g += i * logZ(DIM-3) + j * logZ(DIM-2) + k * logZ(DIM-1);
g -= lfact(i) + lfact(j) + lfact(k);
// if ((i > Z(0) || j > Z(1) || k > Z(2)) && exp(g) < EPSILON * F)
// break;
F += exp(g);
}
}
}
F /= 2 * M_PI;
if(DIM % 2 == 0) return F * SPI;
return F;
}
template<>
T C1F1<4>(T a, T b, vecD Z, size_t iter){
T g, F = 0;
vecD logZ = Z.array().log();
for(size_t i = 0; i < iter; i++){
for(size_t j = 0; j < iter; j++){
for(size_t k = 0; k < iter; k++){
for(size_t l = 0; l < iter; l++){
g = 0;
g += lgamma( i + a ) + lgamma( j + a ) + lgamma( k + a ) + lgamma( l + a );
g -= lgamma( i + j + k + l + b);
g += i * logZ(DIM-4) + j * logZ(DIM-3) + k * logZ(DIM-2) + l * logZ(DIM-1);
g -= lfact(i) + lfact(j) + lfact(k) + lfact(l);
// if ((i > Z(0) || j > Z(1) || k > Z(2)) && exp(g) < EPSILON * F)
// break;
F += exp(g);
}
}
}
}
F /= 2 * M_PI * sqrt(M_PI);
if(DIM % 2 == 0) return F * SPI;
return F;
}
T MAX(T a, T b){
return (a > b)?a:b;
}
/*
================================================================================
Distribution Wrapper
================================================================================
*/
struct Bingham {
size_t size = 0; //Number of Samples
matD C = matD::Identity(); //Underlying Gaussian Covariance
matD M = matD::Identity(); //Orientation Matrix (Orthogonal)
vecD Z = vecD::Zero(); //Concentration Matrix (Diagonal)
T F = 1.0f; //Normalization Constant
T d = 0.0f;
//Constructor from Data
Bingham(){}
Bingham(matX& X){
compute(X);
}
const void compute(matX& X){
size = X.rows();
if(size >= DIM){
C = X.adjoint()*X; //Covariance Matrix
C = C/(size-1); //Normalize
SelfAdjointEigenSolver<matD> EIG(C); //Eigensolver
M = EIG.eigenvectors(); //Eigenvectors
Z = EIG.eigenvalues(); //Eigenvalues
}
F = normalize();
}
//Normalization Constant
T normalize(){
/*
Note: We subtract the smallest eigenvalue from all eigenvalues and shift everything!
Then: The sphere dimensionality is actually only 2.
*/
/*
Ok: The relation appears to hold true, therefore I assume the formula is valid.
*/
/*
Basically: For a quaternion, they are placed on the unit hypersphere in 3 space.
This means I set DIM to 4 and I have to shift everything s.t. the smallest eigenvalue becomes zero.
Since they are ordered increasing, everything else will be positive automatically.
I just need to make sure that the scaling factors are correct in that case.
*/
//Re-Shift the Set!
const T shift = 0.0f;//-Z(0);
vecD NZ = Z.array() + shift;
//Compute the Hypergeometric Function of Matrix Argument
size_t iter = 20;
T FZ;
//Truncated Computation
if(DIM >= 1 && fabs(NZ(DIM-1)) < EPSILON)
FZ = 1;
else if(DIM >= 2 && fabs(NZ(DIM-2)) < EPSILON)
FZ = C1F1<1>(0.5, DIM*0.5, NZ, iter);
else if(DIM >= 3 && fabs(NZ(DIM-3)) < EPSILON)
FZ = C1F1<2>(0.5, DIM*0.5, NZ, iter);
else if(DIM >= 4 && fabs(NZ(DIM-4)) < EPSILON)
FZ = C1F1<3>(0.5, DIM*0.5, NZ, iter);
//Otherwise: Direct Computation
else FZ = C1F1<DIM>(0.5, DIM*0.5, NZ, iter);
return NSphereArea(DIM-1)*exp(-shift)*FZ;
}
//Probability Density Function
T P(vecD x){
return exp(x.transpose()*(M*Z.asDiagonal()*M.transpose())*x)/F;
}
};
};
#endif