-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.html
232 lines (206 loc) · 6.55 KB
/
index.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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Gaussian Process Regression</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<style>
html, body, .container, .row, div.col-md-12, #container {height: 100%; }
</style>
</head>
<body>
<div class="container" role="main">
<div class="row">
<div class="col-md-12">
<h3>Gaussian Process Regression</h3>
<p>Click to add points</p>
<p>Log likelihood: <span id="logLikelihood"></span></p>
<div id="container">
<div id="plot" style="width: 100%; height: 70%; min-height: 450px;"></div>
<div class="row">
</div>
</div>
</div>
</div>
</div>
<script src="extern/dat.gui.min.js"></script>
<script src="js/linalg.core.js"></script>
<script src="js/gp.js"></script>
<script src="js/bfgs.js"></script>
<script src="extern/jquery.flot.min.js"></script>
<script src="extern/jquery.flot.fillbetween.min.js"></script>
<script>
var regressor;
// linspaced with regressor.x added and neighborhood points removed
var getXNew = function() {
var x_new = [ ]
for (var i = 0; i < regressor.x.length; ++i)
x_new.push(regressor.x[i]);
var linspaced = linspace(0, 1, 100);
for (var i = 0; i < linspaced.length; ++i) {
var mindist = 1;
var include = true;
for (var j = 0; j < regressor.x.length; ++j) {
if (Math.abs(regressor.x[j] - linspaced[i]) < 0.005) {
include = false;
break;
}
}
if (include) x_new.push(linspaced[i]);
}
x_new.sort();
return x_new;
}
var app = {
getSamples: function() {
var x_new = getXNew();
var samples = regressor.getRealizations(x_new);
updatePlot(samples);
}
}
function updatePlot(samples) {
if (regressor.N == 0) {
$.plot('#plot', [
{
data: [[0,0]],
lines: { show: true, lineWidth: 2, shadowSize: 0 },
color: '#33c'
},
],
{
xaxis: { min: 0, max: 1 },
yaxis: { min: -1, max: 1 },
grid: { clickable: true }
}
);
} else {
var x_new = getXNew();
var moments = regressor.getMoments(x_new);
var lower = moments.mean.subtract(moments.variance.cwiseSqrt());
var upper = moments.mean.add(moments.variance.cwiseSqrt());
var lower95 = moments.mean.subtract(moments.variance.cwiseSqrt().scale(2));
var upper95 = moments.mean.add(moments.variance.cwiseSqrt().scale(2));
var zip = function(x, y) {
var data = [ ];
for (var i = 0; i < x.length; i++)
data.push([x[i], y[i]]);
return data;
}
trends = [
{
label: " Data",
data: zip(regressor.x, regressor.y),
points: { show: true, fillColor: '#000' },
color: '#000',
},
{
id: 'lower',
data: zip(x_new, lower),
lines: { show: true, lineWidth: 0, fill: false },
},
{
label:' ±σ',
id: 'upper',
data: zip(x_new, upper),
lines: { show: true, lineWidth: 0, fill: 0.15, shadowSize: 0 },
color: 'rgba(50,50,50,0.4)',
fillBetween: 'lower'
},
{
id: 'lower95',
data: zip(x_new, lower95),
lines: { show: true, lineWidth: 0, fill: false },
},
{
label:' ±2σ',
id: 'upper95',
data: zip(x_new, upper95),
lines: { show: true, lineWidth: 0, fill: 0.15, shadowSize: 0 },
color: 'rgba(50,50,50,0.15)',
fillBetween: 'lower95'
},
{
label: " Mean",
data: zip(x_new, moments.mean),
lines: { show: true, lineWidth: 3, shadowSize: 0 },
color: '#dd66cc'
}
];
if (samples) {
for (var i = 0; i < samples.length; ++i) {
var trend = {
data: zip(x_new, samples[i]),
lines: { show: true, lineWidth: 1, shadowSize: 0 },
color: '#'+Math.floor(Math.random()*16777215).toString(16)
};
trends.push(trend);
}
}
$.plot('#plot', trends,
{
xaxis: { min: 0, max: 1 },
yaxis: { min: -1, max: 1 },
grid: { clickable: true },
legend: { position: 'nw' }
}
);
var logLikelihood = regressor.logLikelihood(regressor.getHyperparams()) * 1000 | 0;
document.getElementById('logLikelihood').innerHTML = logLikelihood / 1000;
}
};
window.onload = function() {
regressor = new Regressor();
regressor.setKernel(GP.kernels[0].name);
$("#plot").bind("plotclick", function (event, pos, item) {
var x = [ ], y = [ ];
for (var i = 0; i < regressor.N; ++i) {
x.push(regressor.x[i]);
y.push(regressor.y[i]);
}
x.push(pos.x);
y.push(pos.y);
regressor.setData(x, y);
requestAnimationFrame(function() { updatePlot(); } );
});
gui = new dat.GUI();
function addHyperparamFolder() {
gui.removeFolder('Hyperparameters');
var folder = gui.addFolder('Hyperparameters');
for (var i = 0; i < regressor.kernel.hyperparams.length; ++i) {
var param = regressor.kernel.hyperparams[i];
folder.add(regressor.hyperparams, param.name, param.min, param.max).step(param.step).name(param.name).onChange(function(value) {
requestAnimationFrame(function() { updatePlot(); } );
});
}
folder.add(regressor, 'optimize').name('Optimize').onChange(function(value) {
for (var i in folder.__controllers)
folder.__controllers[i].updateDisplay();
requestAnimationFrame(function() { updatePlot(); } );
});
folder.add(app, 'getSamples').name('Draw samples').onChange(function(value) {
});
folder.add(regressor, 'reset').name('Reset').onChange(function(value) {
for (var i in folder.__controllers)
folder.__controllers[i].updateDisplay();
requestAnimationFrame(function() { updatePlot(); } );
});
folder.open();
};
var f0 = gui.addFolder('Options');
var kernelNames = [ ];
for (var i = 0; i < GP.kernels.length; ++i) {
kernelNames.push(GP.kernels[i].name);
}
f0.add(regressor, 'kernelName', kernelNames).name('Kernel').onChange(function(value) {
regressor.setKernel(value);
addHyperparamFolder();
requestAnimationFrame(function() { updatePlot(); } );
});
f0.open();
addHyperparamFolder();
updatePlot();
};
</script>
</body>