-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitching_gxm.ox
286 lines (261 loc) · 8.84 KB
/
switching_gxm.ox
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
#include <oxstd.oxh>
#include <oxfloat.oxh>
#import <switching>
///////////////////////////////////////////////////////////////////////
//
class GaussianMix
{
GaussianMix(const mData, const cS);
static Generate(const cN, const vProb, const avMu, const avSD);
static MapToArgs(const vP, const cS, const cP);
static MapToPar(const vProb, const avMu, const avSD);
GetStartPar();
GetLowerBound();
GetUpperBound();
GetLogGaussian(const vP);
virtual UpdatePar(vP);
virtual LogLikAt(const vP);
CheckPar(const vP);
decl m_mData;
decl m_cS, m_cN, m_cP;
};
GaussianMix::GaussianMix(const mData, const cS)
{
m_mData = mData;
m_cS = cS; // no of mixtures
m_cN = sizer(mData); // no of observations
m_cP = sizec(mData); // dimension of multivariate normal
}
/** Generates a random sample.
@param cN no of observations N
@param vProb[S] probabilities of each state
@param avMu[S] array[S] with means for each state (for p-dimensional normal: matrix[1][p] with means for each state)
@param avSd[S] array[S] with standard deviations for each state (for p-dimensional normal: matrix[1][p] with standard
deviations for each state, so assuming diagonal variance matrix)
@returns matrix[N][p]
*/
GaussianMix::Generate(const cN, const vProb, const avMu, const avSD)
{
decl cs = sizerc(vProb), cp = sizerc(avMu[0]);
// draw the states: this is a unit vector for each, e.g. 0100
decl ms = countr(ranu(cN, 1), cumulate(vec(vProb[ : cs - 2])));
decl mx = rann(cN, cp);
// loop over the states, drawing from the appropriate normal distribution
for (decl i = 0; i < cs; ++i)
{
decl state_i = vecindex(ms[][i]);
mx[state_i][] = avMu[i] + mx[state_i][] .* avSD[i];
}
return mx;
}
/** Split vectorized parameters.
@returns array[3]: {probs[1][S], {mu_0[1][p],...}[S], {sd_0[1][p],...}[S] }.
*/
GaussianMix::MapToArgs(const vP, const cS, const cP)
{
decl prob = vP[ : cS - 2]', amu, asd;
amu = asd = new array[cS];
for (decl i = 0, j = cS - 1; i < cS; ++i, j += 2 * cP)
{
amu[i] = vP[j : j + cP - 1]';
asd[i] = vP[j + cP : j + 2 * cP - 1]';
}
prob ~= 1 - sumr(prob);
return { prob, amu, asd };
}
/** Map separate parameters to vectorized version.
@param vProb[S] probabilities of each state
@param avMu[S] array[S] with means for each state (for p-dimensional normal: matrix[1][p] with means for each state)
@param avSd[S] array[S] with standard deviations for each state (for p-dimensional normal: matrix[1][p] with standard
deviations for each state, so assuming diagonal variance matrix)
@returns vectorized parameters: S-1 probabilities, Means 1, SDs 1, Means 2, SDs 2, ...
*/
GaussianMix::MapToPar(const vProb, const avMu, const avSD)
{
decl vp = vec(vProb[ : sizerc(vProb) - 2]);
for (decl i = 0; i < sizeof(avMu); ++i)
{
vp |= vec(avMu[i]) | vec(avSD[i]);
}
return vp;
}
/** Get the lower bound of the vectorized coefficients
*/
GaussianMix::GetLowerBound()
{
decl vp = zeros(m_cS - 1, 1);
for (decl i = 0; i < m_cS; ++i)
{
vp |= constant(-.Inf, m_cP, 1) | constant(1e-10, m_cP, 1);
}
return vp;
}
/** Get the lower bound of the vectorized coefficients
*/
GaussianMix::GetUpperBound()
{
decl vp = ones(m_cS - 1, 1);
for (decl i = 0; i < m_cS; ++i)
{
vp |= constant(+.Inf, m_cP, 1) | constant(+.Inf, m_cP, 1);
}
return vp;
}
/** Determines neutral starting values.
*/
GaussianMix::GetStartPar()
{
decl prob = constant(1 / m_cS, 1, m_cS), amu, asd;
amu = asd = new array[m_cS];
// get distance measure of standardized data on [0,1] scale
decl dist = sqrt(sumsqrr(standardize(m_mData)) / m_cP);
dist -= min(dist);
dist /= max(dist);
// use this for initial partition
for (decl i = 0, p_lo = 0, p_hi = 0; i < m_cS; ++i)
{
p_lo = p_hi; p_hi = p_lo + prob[i];
decl sel = vecindex(dist .>= p_lo .&& dist .<= p_hi);
// need protection against selection getting too small?
prob[i] = sizerc(sel) / sizer(m_mData);
amu[i] = meanc(m_mData[sel][]);
asd[i] = sqrt(varc(m_mData[sel][]));
}
return MapToPar(prob, amu, asd);
}
/** Selects parameters for convergence check.
*/
GaussianMix::CheckPar(const vP)
{
return vP;
}
/** Returns the log of the multivariate normal distribution at vP.
*/
GaussianMix::GetLogGaussian(const vP)
{
decl prob, amu, asd;
[prob, amu, asd] = MapToArgs(vP, m_cS, m_cP);
decl vlogdet = zeros(1, m_cS), mxtx = zeros(m_cN, m_cS);
for (decl i = 0; i < m_cS; ++i)
{
vlogdet[i] = 2 * sumr(log(asd[i]));
mxtx[][i] = sumsqrr((m_mData - amu[i]) ./ asd[i]);
}
return -0.5 * (mxtx + vlogdet + m_cP * log(M_2PI));
}
GaussianMix::UpdatePar(vP)
{
decl mxtx = GetLogGaussian(vP), prob = vP[ : m_cS - 2]', mu, sd, vp_prev = vP;
prob ~= 1 - sumr(prob);
decl mxr_xtx = maxr(mxtx);
decl posterior = prob .* exp(mxtx - mxr_xtx);
posterior ./= sumr(posterior);
prob = meanc(posterior); // new probabilities
vP[ : m_cS - 2] = prob';
posterior ./= prob;
for (decl i = 0, j = m_cS - 1; i < m_cS; ++i, j += 2 * m_cP)
{
mu = meanc(posterior[][i] .* m_mData);
sd = sqrt( meanc(posterior[][i] .* sqr(m_mData - mu)));
vP[j : ] = mu';
vP[j + m_cP : ] = sd';
}
//println(vp_prev ~ vP);
return vP;
}
/** Returns the loglikelihood.
*/
GaussianMix::LogLikAt(const vP)
{
decl mxtx = GetLogGaussian(vP), prob = vP[ : m_cS - 2]';
prob ~= 1 - sumr(prob);
decl mxr_xtx = maxr(mxtx);
decl mloglik = exp(mxtx - mxr_xtx) * prob';
return double(meanc(mxr_xtx + log(mloglik)));
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
class MySwitching : Switching
{
MySwitching(iLineSearchMode);
static Report(mResult, time);
}
MySwitching::MySwitching(iLineSearchMode)
{
Switching();
SetLineSearchMode(iLineSearchMode);
SetLineSearchCrit(0);
SetLineSearchWarmUp(0);
// SetExtraUpdate(1);
}
MySwitching::Report(mResult, time)
{
mResult[][0] -= max(mResult[][0]);
println("%cf", {"%10.0f","%10.0f","%10.0f","%10.0f","%10.0f","%10.2f"}, "%c", {"iters","updates","logliks","failures","below","CPU(s)"},
meanc(mResult[][2:]) ~ sumc(mResult[][1] .> MAX_WEAK_CONV) ~ .NaN ~ (timer() - time) / 100);
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
RunGXM(const iLineSearchMode, const cM, const cN, const cS, const cP)
{
decl vpi, amu, asd, mu;
decl model, switching, mresult = zeros(cM, 5), vp_dgp, vp_0, vp, dfunc, retval;
decl fn_random_args = [=]()
{
decl vprob = zeros(1, cS), amu, asd;
amu = asd = new array[cS];
for (decl i = 0; i < cS; ++i)
{
vprob[i] = ranu(1, 1) * (1 - sumr(vprob));
amu[i] = i + rann(1, cP) / 10;
asd[i] = 0.5 + ranu(1, cP);
}
vprob[cS - 1] = 1 - sumr(vprob[ : cS - 2]);
return {vprob, amu, asd};
};
decl time = timer();
// run all experiments on the same seed
ranseed(-1);
// parallel
for (decl i = 0; i < cM; ++i)
{
// draw a design
[vpi, amu, asd] = fn_random_args();
vp_dgp = GaussianMix::MapToPar(vpi, amu, asd);
// draw a sample and create model
model = new GaussianMix(GaussianMix::Generate(cN, vpi, amu, asd), cS);
switching = new MySwitching(iLineSearchMode);
vp = vp_0 = model.GetStartPar();
//println(vpi, amu, asd, "%25.15g", model.LogLikAt(vp), vp_0 ~ vp_dgp ~ vp);
//println("%25.15g", model.LogLikAt(vp), vp_0 ~ vp_dgp ~ vp ~ model.UpdatePar(vp));
//exit(0);
[vp, dfunc, retval] = switching.Estimate(model.LogLikAt, model.UpdatePar, model.CheckPar,
vp, model.GetLowerBound(), model.GetUpperBound(), 1e-12, 10000, -1);
//println("%25.15g", vp_0 ~ vp_dgp ~ vp);
mresult[i][] = dfunc ~ switching.GetIterInfo();
delete model;
delete switching;
}
print("Gaussian mixture model ", Switching::GetLineSearchName(iLineSearchMode), " M=", cM, " N=", cN, " S=", cS, " P=", cP);
MySwitching::Report(mresult, time);
return mresult;
}
///////////////////////////////////////////////////////////////////////
main()
{
format(1000);
decl als0 = {Switching::LS_NONE, Switching::LS_1STEP, Switching::LS_SQS3G, Switching::LS_PARABOLIC};
decl mlogdets = <>;
// 1. S=3,P=1: methods converge to different stationary points
// 2. S=3,P=1: why does parabolic have failures?
foreach (decl ls in als0)
mlogdets ~= RunGXM(ls, 100, 1000, 3, 1)[][0];
decl mgap = fabs(mlogdets - maxr(mlogdets));
// print the largest deviation from the maximum over all methods
println("\nMaximum of the likelihoods in deviation from the maximum of those achieved by each method:",
maxc(mgap));
println("Premature convergence, deviations:");
println((range(0, sizer(mgap) - 1)' ~ fabs(mgap))[vecindex(maxr(mgap) .> 1e-5)][] );
println("Premature convergence, likelihoods:");
println("%cf", {"%8.0f","%22.10g"}, (range(0, sizer(mgap) - 1)' ~ fabs(mlogdets))[vecindex(maxr(mgap) .> 1e-5)][] );
}