forked from conveyal/freqBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
454 lines (364 loc) · 14.7 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<!DOCTYPE html>
<!-- python -m SimpleHTTPServer 8888 & -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Test</title>
<script src="http://d3js.org/d3.v3.js" charset="utf-8"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
</head>
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
svg text,
body {
font-size: 10pt;
font-family: sans;
}
</style>
<body>
<div>
<label for="headwayA">Headway of bus A</label>
<input type="range" id="headwayA" min="1" max="60" value="30" class="control" />
<label for="ttimeA">Travel time of bus A</label>
<input type="range" id="ttimeA" min="1" max="60" value="30" class="control" />
</div>
<div>
<label for="headwayB">Headway of bus B</label>
<input type="range" id="headwayB" min="1" max="60" value="40" class="control" />
<label for="ttimeB">Travel time of bus B</label>
<input type="range" id="ttimeB" min="1" max="60" value="20" class="control" />
</div>
<div>
<label for="headwayC">Headway of bus C</label>
<input type="range" id="headwayC" min="1" max="60" value="50" class="control" />
<label for="ttimeC">Travel time of bus C</label>
<input type="range" id="ttimeC" min="1" max="60" value="25" class="control" />
</div>
<script type="text/javascript">
// create a context for the purpose of namespacing and applying strict syntax
(function() {
'use strict';
var N = 60; // number of bins in each histogram
function emptyDistribution() {
var ret = new Array(N);
for (var i = 0; i < N; i++) {
ret[i] = 0;
}
return ret;
}
function constantDistribution(width) {
var ret = emptyDistribution();
for (var i = 0; i < width; i++) {
ret[i] = 1 / width;
}
return ret;
}
function cumulative(densities) {
var ret = new Array(densities.length);
var accumulated = 0;
for (var i = 0; i < densities.length; i++) {
// definition of discrete cumulative probability is P(X<=x)
// so add before setting. But this will either over or
// underestimate (combining discrete & continuous problem)
accumulated += densities[i];
ret[i] = accumulated;
}
return ret;
}
function complement(distribution) {
var ret = new Array(N);
for (var i = 0; i < N; i++) {
ret[i] = 1 - distribution[i];
}
return ret;
}
function or(p1, p2) {
return p1 + p2 - p1 * p2;
}
function either(p1, p2) {
var ret = new Array(N);
for (var i = 0; i < N; i++) {
ret[i] = or(p1[i], p2[i]);
}
return ret;
}
function product(p1, p2) {
var ret = new Array(N);
for (var i = 0; i < N; i++) {
ret[i] = p1[i] * p2[i];
}
return ret;
}
function add(p1, p2) {
var ret = new Array(N);
for (var i = 0; i < N; i++) ret[i] = p1[i] + p2[i];
return ret;
}
function subtract(p1, p2) {
var ret = new Array(N);
for (var i = 0; i < N; i++) ret[i] = p1[i] - p2[i];
return ret;
}
function trimRight (array) {
var i = array.length - 1
for (; i > 0; i--) {
if (Math.abs(array[i]) > 1e-10)
break;
}
return array.slice(0, i + 1);
}
/** offset an array by a fixed amount (e.g. a travel time) */
function offset (array, distance) {
var left = [];
for (var i = 0; i < distance; i++) {
left[i] = 0;
}
return left.concat(array);
}
/** make an elpased time distribution */
function elapsed(boardedA, ttimeA, boardedB, ttimeB, boardedC, ttimeC) {
var elapsedTime = [];
for (var i = 0; i < N + Math.max(ttimeA, ttimeB, ttimeC); i++) {
elapsedTime[i] = 0;
}
for (var i = 0; i < N; i++) {
elapsedTime[i + ttimeA] += boardedA[i];
elapsedTime[i + ttimeB] += boardedB[i];
elapsedTime[i + ttimeC] += boardedC[i];
}
return trimRight(elapsedTime);
}
/** P(A arrives | A has not been boarded) = P(A arrives | A has not arrived) * P(A has not arrived | A has not been boarded) */
function pArr(headway, minutes, cBoard) {
if (minutes >= headway)
return 0;
var pArr = 1 / (headway - minutes);
var pHasCome = minutes / headway;
var discount = (1 - pHasCome) / (1 - cBoard);
return pArr * discount;
}
/* Add a new SVG graph to the DOM displaying the given data. */
function makeGraph(data, label, max) {
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = 600 - margin.left - margin.right,
height = 150 - margin.top - margin.bottom;
// Map graph values to SVG (x,y) coordinates and colors
var x = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeRoundBands([0, width]);
var y = d3.scale.linear()
.domain([0, max !== undefined ? max : Math.max.apply(null, data)])
.range([height, 0]);
//var c = d3.scale.category10.domain(d3.range(3));
// Create a new SVG element in the DOM, which will hold the graph
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g") // group element
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Create X axis and add it to a group within the SVG
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)");
// Create Y axis and add it to a group within the SVG
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(2);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end");
// Popup "tooltip" to show the value of any given bar
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d, i) {
return 't=' + i + ' p=' + d.toFixed(3);
});
// What does this do?
svg.call(tip);
// Bind the data, creating or updating one bar for each data point in the array
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d, i) {
return x(i);
})
.attr("width", x.rangeBand())
.attr("y", function(d, i) {
return y(d);
})
.attr("height", function(d) {
return height - y(d);
})
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
// add label
svg.append('text').text(label).attr('transform', 'translate(30, 10)');
}
function drawGraphs() {
d3.selectAll('svg').remove();
var headwayA = parseInt(document.getElementById('headwayA').value);
var ttimeA = parseInt(document.getElementById('ttimeA').value);
var headwayB = parseInt(document.getElementById('headwayB').value);
var ttimeB = parseInt(document.getElementById('ttimeB').value);
var headwayC = parseInt(document.getElementById('headwayC').value);
var ttimeC = parseInt(document.getElementById('ttimeC').value);
var busA = constantDistribution(headwayA);
var busB = constantDistribution(headwayB);
var busC = constantDistribution(headwayC);
var cBusA = cumulative(busA);
var cBusB = cumulative(busB);
var cBusC = cumulative(busC);
// P(A or B or C) = P((A or B) or C)
// P(A or B) = P(A) + P(B) - (P(A) * P(B))
var anyBusArrived = either(either(cBusA, cBusB), cBusC);
var noBusArrived = complement(anyBusArrived);
// Leave out the bus being examined from the cumulative part
// cumulative versus density, any way to re-express this?
var boardedA = product(busA, complement(either(cBusB, cBusC)));
var boardedB = product(busB, complement(either(cBusA, cBusC)));
var boardedC = product(busC, complement(either(cBusA, cBusB)));
var cBoardedA = cumulative(boardedA);
var cBoardedB = cumulative(boardedB);
var cBoardedC = cumulative(boardedC);
var cBoardedAny = add(add(cBoardedA, cBoardedB), cBoardedC);
// Warning, cumulative(either()) != either(cumulative())
// we want prob that (A already happened) OR (B already happened)
// not prob that (A OR B) already happened... but what does this mean?
makeGraph(anyBusArrived, 'Any bus arrived', 1);
makeGraph(noBusArrived, 'No bus arrived', 1);
makeGraph(cBusA, 'Bus A arrived', 1);
makeGraph(cBoardedA, 'Boarded bus A', 1);
makeGraph(cBusB, 'Bus B arrived', 1);
makeGraph(cBoardedB, 'Boarded bus B', 1);
makeGraph(cBusC, 'Bus C arrived', 1);
makeGraph(cBoardedC, 'Boarded bus C', 1);
makeGraph(cBoardedAny, 'Boarded any bus', 1);
// compute the elapsed time distribution
var elapsedTime = elapsed(boardedA, ttimeA, boardedB, ttimeB, boardedC, ttimeC);
makeGraph(elapsedTime, 'Elapsed time distribution');
// the simple case of the clever traveler
var destA = offset(busA, ttimeA);
var destB = offset(busB, ttimeB);
var destC = offset(busC, ttimeC);
var cDestA = cumulative(destA);
var cDestB = cumulative(destB);
var cDestC = cumulative(destC);
var tookA = product(destA, complement(either(cDestB, cDestC)));
var tookB = product(destB, complement(either(cDestC, cDestA)));
var tookC = product(destC, complement(either(cDestB, cDestA)));
var bDestA = tookA.slice(ttimeA);
var bDestB = tookB.slice(ttimeB);
var bDestC = tookC.slice(ttimeC);
var cbDestA = cumulative(bDestA);
var cbDestB = cumulative(bDestB);
var cbDestC = cumulative(bDestC);
makeGraph(cbDestA, "Clever passenger with perfect information boarded bus A, simple algorithm", 1);
makeGraph(cbDestB, "Clever passenger with perfect information boarded bus B, simple algorithm", 1);
makeGraph(cbDestC, "Clever passenger with perfect information boarded bus C, simple algorithm", 1);
var cbAny = add(add(cbDestA, cbDestB), cbDestC);
makeGraph(cbAny, "Clever passenger with perfect information boarded any bus, simple algorithm", 1);
var destElapsed = elapsed(bDestA, ttimeA, bDestB, ttimeB, bDestC, ttimeC);
makeGraph(destElapsed, "Elapsed time distribution for the clever traveler with perfect information, simple algorithm");
// now the case of the minimum upper bound
var acBoardedA = 0;
var acBoardedB = 0;
var acBoardedC = 0;
var mBoardedA = [];
var mBoardedB = [];
var mBoardedC = [];
for (var x = 0; x < N; x++) {
var BBetterA = (Math.max(headwayB - x, 0) + ttimeB < ttimeA) * Math.max(1 - x / headwayB, 0) / (1 - acBoardedB);
var CBetterA = (Math.max(headwayC - x, 0) + ttimeC < ttimeA) * Math.max(1 - x / headwayC, 0) / (1 - acBoardedC);
var anyBetterA = BBetterA + CBetterA - BBetterA * CBetterA;
mBoardedA[x] = pArr(headwayA, x, acBoardedA) * (1 - anyBetterA) * (1 - (acBoardedA + acBoardedB + acBoardedC));
var ABetterB = (Math.max(headwayA - x, 0) + ttimeA < ttimeB) * Math.max(1 - x / headwayA, 0) / (1 - acBoardedA);
var CBetterB = (Math.max(headwayC - x, 0) + ttimeC < ttimeB) * Math.max(1 - x / headwayC, 0) / (1 - acBoardedC);
var anyBetterB = ABetterB + CBetterB - ABetterB * CBetterB;
mBoardedB[x] = pArr(headwayB, x, acBoardedB) * (1 - anyBetterB) * (1 - (acBoardedA + acBoardedB + acBoardedC));
var ABetterC = (Math.max(headwayA - x, 0) + ttimeA < ttimeC) * Math.max(1 - x / headwayA, 0) / (1 - acBoardedA);
var BBetterC = (Math.max(headwayB - x, 0) + ttimeB < ttimeC) * Math.max(1 - x / headwayB, 0) / (1 - acBoardedB);
var anyBetterC = ABetterC + BBetterC - ABetterC * BBetterC;
mBoardedC[x] = pArr(headwayC, x, acBoardedC) * (1 - anyBetterC) * (1 - (acBoardedA + acBoardedB + acBoardedC));
acBoardedA += mBoardedA[x];
acBoardedB += mBoardedB[x];
acBoardedC += mBoardedC[x];
console.log(x, anyBetterA, anyBetterB, anyBetterC);
console.log(acBoardedA + acBoardedB + acBoardedC)
if (anyBetterA > 1 || anyBetterB > 1 || anyBetterC > 1 ||
anyBetterA < 0 || anyBetterB < 0 || anyBetterC < 0 ||
(acBoardedA + acBoardedB + acBoardedC) > 1)
console.log("invalid probability");
}
var mBoardedAny = add(mBoardedA, add(mBoardedB, mBoardedC));
makeGraph(cumulative(mBoardedA), 'Probability that risk-averse traveler boarded A', 1);
makeGraph(cumulative(mBoardedB), 'Probability that risk-averse traveler boarded B', 1);
makeGraph(cumulative(mBoardedC), 'Probability that risk-averse traveler boarded C', 1);
makeGraph(cumulative(mBoardedAny), 'Probability that risk-averse traveler boarded any', 1);
var mElapsed = elapsed(mBoardedA, ttimeA, mBoardedB, ttimeB, mBoardedC, ttimeC);
makeGraph(mElapsed, 'Elapsed time distribution for risk averse traveler');
}
d3.selectAll('.control').on('change', drawGraphs);
drawGraphs();
}()); // close namespace and call to execute.
</script>
</body>
</html>