forked from linupa/GMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMM.cpp
212 lines (169 loc) · 3.87 KB
/
GMM.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
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
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
#include "GMM.h"
using namespace std;
using Eigen::MatrixXf;
using Eigen::MatrixXd;
using Eigen::VectorXd;
using Eigen::VectorXcd;
using Eigen::MatrixXcd;
using Eigen::EigenSolver;
double p(VectorXd x, VectorXd mean, MatrixXd cov);
GMM::GMM(int n_clust, int dim)
{
n_cluster = n_clust;
dimension = dim;
for ( int i = 0 ; i < n_clust ; i++ )
{
clusters.push_back(Cluster(dim));
}
points = NULL;
}
GMM::~GMM(void)
{
default_clusters.clear();
clusters.clear();
if ( points )
{
points->clear();
delete points;
}
}
void GMM::setSamples(vector<VectorXd> *samples)
{
if ( points )
{
points->clear();
delete points;
}
points = samples;
reset();
}
void GMM::reset(void)
{
clusters.clear();
clusters = default_clusters;
n_cluster = clusters.size();
vector<Cluster>::iterator it;
for ( it = clusters.begin() ; it != clusters.end() ; it++ )
{
it->update();
}
return;
}
void GMM::setDefaultClusters()
{
default_clusters.clear();
default_clusters = clusters;
return;
}
void GMM::iterate(void)
{
int i, c;
int numSamples;
int numClusters;
MatrixXd ric;
if ( !points || points->size() == 0 )
return;
numSamples = points->size();
numClusters = clusters.size();
ric = MatrixXd::Zero(numSamples,numClusters);
for ( i = 0 ; i < numSamples ; i++ )
{
double sum = 0.;
vector<Cluster>::iterator cit;
for ( cit = clusters.begin(), c = 0 ; cit != clusters.end() ; cit++, c++ )
{
ric(i,c) = cit->pi * p((*points)[i], cit->mean, cit->cov);
sum += ric(i,c);
}
if ( sum < 0.01 )
{
fprintf(stderr, "Too small ric(%d) %f %f\n", i, ric(i,0), ric(i,1));
}
for ( c = 0 ; c < numClusters ; c++ )
{
ric(i,c) = ric(i,c) / sum;
}
}
VectorXd mc;
mc = VectorXd::Zero(numClusters);
for ( i = 0 ; i < numSamples ; i++ )
{
for ( c = 0 ; c < numClusters ; c++ )
{
mc[c] += ric(i,c);
}
}
fprintf(stderr, "MC %f, %f\n", mc[0], mc[1]);
clusters[0].pi = mc[0] / (mc[0] + mc[1]);
clusters[1].pi = mc[1] / (mc[0] + mc[1]);
fprintf(stderr, "PI %f, %f\n", clusters[0].pi, clusters[1].pi);
for ( c = 0 ; c < numClusters ; c++ )
{
clusters[c].mean = VectorXd::Zero(dimension);
for ( i = 0 ; i < numSamples ; i++ )
{
clusters[c].mean += ric(i,c) * (*points)[i];
}
clusters[c].mean = clusters[c].mean / mc[c];
cerr << "Mean (" << c << ")"<< endl << clusters[c].mean << endl;
clusters[c].cov = MatrixXd::Zero(dimension,dimension);
for ( i = 0 ; i < numSamples ; i++ )
{
VectorXd dx;
dx = (*points)[i] - clusters[c].mean;
clusters[c].cov += ric(i,c) * dx * dx.transpose();
}
clusters[c].cov = clusters[c].cov / mc[c];
cerr << "Cov (" << c << ")"<< endl << clusters[c].cov << endl;
VectorXcd eig = clusters[c].cov.eigenvalues();
cerr << "Eigen " << endl << eig << endl;
clusters[c].update();
}
}
Cluster::Cluster(int dim)
{
dimension = dim;
mean = VectorXd::Zero(dim);
cov = MatrixXd::Zero(dim,dim);
eigval = VectorXcd::Zero(dim);
eigvec = MatrixXcd::Zero(dim,dim);
}
void Cluster::update(void)
{
EigenSolver<MatrixXd> es(cov);
cerr << "EigenVector0 " << endl << es.eigenvectors().col(0) << endl;
cerr << "EigenVector1 " << endl << es.eigenvectors().col(1) << endl;
eigval = es.eigenvalues();
eigvec = es.eigenvectors();
}
double p(VectorXd x, VectorXd mean, MatrixXd Cov)
{
double ret;
VectorXd X;
X = x - mean;
double exp_value = -0.5 * (X.transpose() * Cov.inverse() * X)(0);
ret = 1. / (2.*M_PI*sqrt(Cov.determinant()) ) * exp( exp_value );
// assert( ret >= 0. );
return ret;
}
double getRandomNormal(void)
{
double z0, z1;
double u1, u2;
double two_pi = 2*M_PI;
do
{
u1 = rand() * (1.0 / RAND_MAX);
u2 = rand() * (1.0 / RAND_MAX);
}
while ( u1 <= 0.0001 );
z0 = sqrt(-2.0 * log(u1)) * cos(two_pi * u2);
z1 = sqrt(-2.0 * log(u1)) * sin(two_pi * u2);
return z0;
}