-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmlemap.html
359 lines (316 loc) · 13.1 KB
/
mlemap.html
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="style/style.css"/>
<link href="http://cdn.jsdelivr.net/gh/joungkyun/font-d2coding/d2coding.css" rel="stylesheet" type="text/css">
<style>
/* https://stackoverflow.com/questions/7525977/how-to-write-fraction-value-using-html */
.frac {
display: inline-block;
position: relative;
vertical-align: middle;
letter-spacing: 0.001em;
text-align: center;
}
.frac > span {
display: block;
padding: 0.1em;
}
.frac span.bottom {
border-top: thin solid black;
}
.frac span.symbol {
display: none;
}
</style>
</head>
<body style="background-color:#F9F9F9;">
<div class="container" style="border:0px solid #FF0000;">
<!-- 가우시안 그래프 -->
<div class="row">
<div class="card" style="float:left;margin-top:5px;">
<div class="card-body">
<div id="graph1" style="float:left;width:1000px;height:400px;border:0px solid #FF0000;margin-top:10px;"></div>
</div>
</div>
</div>
<!-- 세타 슬리이더 -->
<div class="row">
<div class="card" style="margin-top:15px;margin-left:210px;">
<div class="card-body" style="padding-top: 5px;padding-bottom: 5px;">
<h5 class="card-title">μ=<span id="mu_C2_label"></span></h5>
<!--div min="0" max="100" value="50" class="slider" id="mu_C2"-->
<form class="customizing">
<input type="range" min="0" max="200" value="100" class="custom-range" id="mu_C2" style="width:410px;">
</form>
</div>
</div>
</div>
<!-- 가능도, 사후확률 그래프 -->
<div class="row">
<div class="col-6" style="padding-left:20px;border:0px solid #FF0000;">
<div class="card" style="margin-top:10px;">
<div class="card-header">Likelihood(μ;𝒟) = p(𝒟|μ): <span style="padding-top:15px;" id="Likelihood"></span></div>
<div class="card-body">
<div id="graph2" style="border:0px solid #FF0000;margin-top:10px;"></div>
</div>
</div>
</div>
<div class="col-6" style="padding-left:20px;border:0px solid #FF0000;">
<div class="card" style="margin-top:10px;">
<div class="card-header">p(𝒟|μ)p(μ): <span style="padding-top:15px;" id="posterior"></span></div>
<div class="card-body">
<div id="graph3" style="border:0px solid #FF0000;margin-top:10px;"></div>
</div>
</div>
</div>
</div>
</div><!--container-->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<!--script src="https://cdn.jsdelivr.net/gh/nicolaspanel/[email protected]/dist/numjs.min.js"></script-->
<script>
/*
* numpy.linspace() 함수의 구현
* https://gist.github.com/joates/6584908
*/
var linspace = function(a, b, n) {
if(typeof n === "undefined") n = Math.max(Math.round(b-a)+1,1);
if(n<2) { return n===1 ? [a] : []; }
var i,ret = Array(n);
n--;
for(i=n; i>=0; i--) { ret[i] = (i*b+(n-i)*a)/n; }
return ret;
}
function gaussian_(x, mu, variance) {
sigma = Math.sqrt(variance);
return (1. / sigma*Math.sqrt(2*Math.PI)) * Math.exp( (-1/2)*(Math.pow((x-mu)/sigma, 2)) );
}
/*
* https://stackoverflow.com/questions/25582882/javascript-math-random-normal-distribution-gaussian-bell-curve
*/
function randn_bm(mean, sigma) {
var u = 0, v = 0;
while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
while(v === 0) v = Math.random();
return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v ) * sigma + mean;
}
/*
* https://stackoverflow.com/questions/3730510/javascript-sort-array-and-return-an-array-of-indicies-that-indicates-the-positi
*/
var make_sort_index = function(test){
var test_with_index = [];
for (var i in test) {
test_with_index.push([test[i], i]);
}
test_with_index.sort(function(left, right) {
return left[0] < right[0] ? -1 : 1;
});
var indexes = [];
test = [];
for (var j in test_with_index) {
test.push(test_with_index[j][0]);
indexes.push(test_with_index[j][1]);
}
return indexes;
}
$( document ).ready(function() {
var GRAPH1 = document.getElementById('graph1');
var GRAPH2 = document.getElementById('graph2');
var GRAPH3 = document.getElementById('graph3');
var mu_C1 = 3.;
var variance_C1 = 0.5;
var mu_C2 = (3./100.)*parseFloat($('#mu_C2').val()) + 2.;
var variance_C2 = 1.;
var x = linspace(0, 12, 200);
// 두 가우시안의 prior를 1로 해서 별개의 확률분포로 만든다.
var p_C1 = 1.;
var p_C2 = 1.;
var gaussian = function(x, mu, variance, prior) {
return x.map(function(value, index) { return prior*gaussian_(value, mu, variance); });
}
var thetas = [4.0, 5.0, 6.0, 7.0];
var rand = Math.random();
//rand = 0.78;
rand *= thetas.length; //(5)
//rand = 3.9
rand = Math.floor(rand);
//rand = 3
console.log(thetas[rand]);
var sample_dist = {'theta':thetas[rand], 'var':1.0}
var X = new Array(10).fill(0).map(v => randn_bm(sample_dist['theta'], sample_dist['var']))
var prior = {
x: x,
y: gaussian(x, mu_C1, variance_C1, p_C1),
name: 'p(μ)',
type: 'scatter',
marker: {color:'#348ABD'},
};
var p_x_bar_theta = {
x: x,
y: gaussian(x, mu_C2, variance_C2, p_C2),
name: 'p(X|μ)',
type: 'scatter',
marker: {color:'rgb(204, 81, 101)'},
fill: 'tozeroy'
};
var samples = {
x: X,
y: new Array(10).fill(0),
type: 'scatter',
mode: 'markers',
marker: { color: 'rgb(204, 81, 101)', size: 10,
line: { color: 'rgb(255, 255, 255)', width: 2 }
},
name: 'samples',
};
var data = [ prior, p_x_bar_theta, samples ];
var mle_x = [];
var mle_y = [];
var mle_xy = {
x: mle_x,
y: mle_y,
// mode: 'markers',
// type: 'scatter',
marker: {
color: 'rgb(70, 181, 211)',
size: 4,
}
};
var mle_x_c = [];
var mle_y_c = [];
var mle_xy_c = {
x: mle_x_c,
y: mle_y_c,
mode: 'markers',
type: 'scatter',
marker: {
color: 'rgb(32, 114, 136)',
size: 15,
line: {
color: 'rgb(255, 255, 255)',
width: 3
}
}
};
var data_mle = [ mle_xy, mle_xy_c ];
var map_x = [];
var map_y = [];
var map_xy = {
x: map_x,
y: map_y,
// mode: 'markers',
// type: 'scatter',
marker: {
color: 'rgb(235, 164, 50)',
size: 4,
}
};
var map_x_c = [];
var map_y_c = [];
var map_xy_c = {
x: map_x_c,
y: map_y_c,
mode: 'markers',
type: 'scatter',
marker: {
color: 'rgb(219, 73, 76)',
size: 15,
line: {
color: 'rgb(255, 255, 255)',
width: 3
}
}
};
var data_map = [ map_xy, map_xy_c ];
// https://community.plot.ly/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/7
// https://codepen.io/fingerpori/pen/GGLWoz
// https://plot.ly/javascript/plotlyjs-events/
var layout = {
autosize: true,
title: 'Maximum Likelihood Estimation and Maximum a Posterior',
xaxis: {title:'X'},
yaxis: {title:'Prob. density'},
};
var layout_mle = {
autosize: false,
width: 400,
height: 300,
xaxis: {range:[2,9], title:'μ'},
yaxis: {title:'p(𝒟|μ)'},
margin: {
l: 40, r: 5, b: 30, t: 5, pad: 0
},
showlegend: false
};
var layout_map = {
autosize: false,
width: 400,
height: 300,
xaxis: {range:[2,9], title:'μ'},
yaxis: {title:'p(𝒟|μ)p(μ)'},
margin: {
l: 40, r: 5, b: 30, t: 5, pad: 0
},
showlegend: false
};
Plotly.newPlot(GRAPH1, data, layout, {editable: true});
Plotly.newPlot(GRAPH2, data_mle, layout_mle);
Plotly.newPlot(GRAPH3, data_map, layout_map);
$('#mu_C2').on("change mousemove", function() {
mu_C2 = (3./100.)*parseFloat($(this).val()) + 2.;
$('#mu_C2_label').html(mu_C2.toFixed(3));
update = { x: [ x, x ],
y: [ gaussian(x, mu_C1, variance_C1, p_C1), // prior
gaussian(x, mu_C2, variance_C2, p_C2), // p(x|theta)
]
}
Plotly.restyle(GRAPH1, update, [0,1]);
// calc. likelihood
// p(X|theta) = p(x_1|theta)*p(x_2|theta)*...*p(x_N|theta)
Ls = X.map(x => gaussian([x], mu_C2, variance_C2, p_C2));
const reducer = (accumulator, currentValue) => accumulator * currentValue;
L = Ls.reduce(reducer);
prior = gaussian([mu_C2], mu_C1, variance_C1, p_C1);
posterior = L*prior;
$('#Likelihood').html(L.toFixed(6));
$('#posterior').html(posterior.toFixed(6));
mle_x.push(mu_C2);
mle_y.push(L);
mle_x_c = [ mu_C2 ];
mle_y_c = [ L ];
sort_idx = make_sort_index(mle_x);
ordered_mle_x = [];
ordered_mle_y = [];
sort_idx.forEach(function(e){
ordered_mle_x.push(mle_x[e]);
ordered_mle_y.push(mle_y[e]);
})
update = {
x: [ ordered_mle_x, mle_x_c ],
y: [ ordered_mle_y, mle_y_c ]
}
Plotly.restyle(GRAPH2, update, [0,1]);
map_x.push(mu_C2);
map_y.push(posterior);
map_x_c = [ mu_C2 ];
map_y_c = [ posterior ];
sort_idx = make_sort_index(map_x);
ordered_map_x = [];
ordered_map_y = [];
sort_idx.forEach(function(e){
ordered_map_x.push(map_x[e]);
ordered_map_y.push(map_y[e]);
})
update = {
x: [ ordered_map_x, map_x_c ],
y: [ ordered_map_y, map_y_c ]
}
Plotly.restyle(GRAPH3, update, [0, 1]);
});
});
</script>
</body>
</html>