-
Notifications
You must be signed in to change notification settings - Fork 0
/
atrapa-un-millon.js
366 lines (314 loc) · 9.23 KB
/
atrapa-un-millon.js
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
/* Stats simulator for "The Million Pound Drop Live"
* Jose Luis Blanco (C) 2012
*
* For http://www.ciencia-explicada.com/
* Contact: blog.ciencia.explicada@gmail.com
*
* Released under GNU GPL3
*/
document.write('<table border="0">');
var NUM_MAX_DESC = [4, 4, 4, 4, 3, 3, 3, 2];
for(var i=1;i<=8;i++)
{
var nam = "valor_pr_acert_desc"+i;
var nam_des = "valor_num_desc"+i;
var nams = "sp_slider_pr_acert_desc"+i;
var nams_des = "sp_slider_desc"+i;
var def_pr = (i<5 ? 75 : (i<8 ? 67 : 50));
var def_des = 1; //(i<5 ? 1 : (i<8 ? 67 : 50));
document.write("<tr><td>Prob. buen descarte P"+i+": <span id=\""+nam+"\">"+def_pr+"</span>% </td><td><div style=\"width:150px;\"><div id=\""+nams+"\" ></div></td> <td> Respuestas descartadas: <span id=\""+nam_des+"\">"+def_des+"</span> </td> <td> <div style=\"width:70px;\"> <div id=\""+nams_des+"\" ></div></div> </td> </tr>");
function make_slide_func(nam) { return ( function(event,ui) { $("#"+nam).text(ui.value.toFixed(0)); regenerate_all_plots(); } ); };
function make_slide_func_desc(nam_des) { return ( function(event,ui) { $("#"+nam_des).text(ui.value.toFixed(0)); regenerate_all_plots(); } ); };
$("#"+nams).slider({ min:0, max:100, value:def_pr, slide: make_slide_func(nam) });
$("#"+nams_des).slider({ min:1, max:(NUM_MAX_DESC[i-1]-1), value:def_des, slide: make_slide_func_desc(nam_des) });
}
document.write('</table>');
document.write('<table>'+
'<tr>'+
'<td> <div id="sp_placeholder_mean" style="width:300px;height:200px"></div> </td>'+
'<td> <div id="sp_placeholder_stats" style="width:300px;height:200px"></div> </td>'+
'</table>');
document.write('<div align="center"><p><h3>Histogramas del número de fajos en cada etapa [P0:inicial, P1-P8:tras esa pregunta]</h3></p></div>');
for(var i=0;i<=8;i++)
{
var nam = "sp_placeholder_t"+i;
document.write("<div id=\""+nam+"\" style=\"width:99%;height:70px\"></div>");
}
function factorial(n)
{
if ((n==0) || (n==1)) return 1;
else return n*factorial(n-1);
}
function binopdf(k, n,p)
{
if (k>n) return 0;
return (factorial(n)/(factorial(k)*factorial(n-k)))*Math.pow(p,k)*Math.pow(1-p,n-k);
}
function propagate_state(survive_prev,M, pAciertoAlDescartar, nDescartadas)
{
if (!(nDescartadas<=M)) alert("assert(nDescartadas<=M)!! nDescartadas=" + nDescartadas + " M="+M);
if (nDescartadas==0) pAciertoAlDescartar=1;
var pAcierto=1.0/(M-nDescartadas); // En cada una de las que no se han descartado
var maxFichas=survive_prev.length-1;
// Law of total probability:
var survive_next=[];
// Init at zeros:
survive_next.length = maxFichas+1;
for (var i=0;i<=maxFichas;i++) survive_next[i]=[i, 0];
// Evaluate:
for (var i=0;i<=maxFichas;i++)
{
var Pr = survive_prev[i][1];
for (var j=0;j<=maxFichas;j++)
{
var conditional_pr = (1-pAciertoAlDescartar)*(j==0 ? 1:0) + pAciertoAlDescartar* binopdf(j,i,pAcierto);
survive_next[j][1] += Pr * conditional_pr;
}
}
return survive_next;
}
function getObjInnerText(obj)
{
if (document.all) // IE;
return obj.innerText;
else
{
if (obj.textContent)
return obj.textContent;
else alert("Error: This application does not support your browser. Try again using IE or Firefox.");
}
}
function recompute_all_histograms()
{
var mydata = [];
for (var i = 0; i <=8; i ++) mydata[i] = [];
// Distribution for the initial state: we have 40 pieces, for sure.
for (var j = 0; j <=40; j ++)
mydata[0].push([j, (j==40) ? 1:0]);
var NUM_ANSWERS = [4, 4, 4, 4, 3, 3, 3, 2];
//var NUM_DESCARTADAS = [1, 1, 1, 1, 1, 1, 1, 1 ];
for (var i = 1; i <=8; i ++)
{
var nAnswers = NUM_ANSWERS[i-1];
var n = "valor_pr_acert_desc"+i;
var pAciertoAlDescartar = 0.01*Number( getObjInnerText( document.getElementById(n)) );
var nd = "valor_num_desc"+i;
var nDescartadas = Number( getObjInnerText(document.getElementById(nd)) );
mydata[i] = propagate_state(mydata[i-1],nAnswers, pAciertoAlDescartar, nDescartadas);
}
return mydata;
}
function compute_mean_num_of_pieces(all_data)
{
var N = all_data.length;
var MEAN = [];
for (var i=0;i<N;i++)
{
var M = all_data[i].length;
var SUM=0;
for (var j=0;j<M;j++)
SUM+=all_data[i][j][1]*j;
MEAN[i] = [i, SUM];
}
return MEAN;
}
function compute_prob_lose_all(all_data)
{
var N = all_data.length;
var PR_LOSE = [];
for (var i=0;i<N;i++)
PR_LOSE[i] = [i, all_data[i][0][1] ];
return PR_LOSE;
}
function inverse_cdf(H, delta)
{
var n = H.length;
var Xmin=0; //xs(1);
var Xmax=n-1; //xs(end);
// Expected population at each bin
var MEAN = 0;
for (var i=0;i<n;i++) MEAN+=i*H[i][1];
var Hc = [];
Hc.length = n;
Hc[0] = H[0][1];
for (var i=1;i<n;i++)
Hc[i] = Hc[i-1] + H[i][1];
// Normalize:
var Q=[];
Q.length = n;
for (var i=0;i<n;i++) {
Hc[i]/=Hc[n-1];
Q[i] = [i,Hc[i]];
}
// Find below/above limits:
var idx_bel = -1;
var idx_abo = -1;
for (var i=0;i<n;i++)
{
if ((idx_bel==-1) && (Hc[i]>=delta)) idx_bel=i;
if ((idx_abo==-1) && (Hc[i]>=(1-delta))) idx_abo=i;
}
var RET = new Object();
RET.idx_bel = idx_bel;
RET.idx_abo = idx_abo;
return RET;
}
function compute_std_num_of_pieces(all_data,means)
{
var N = all_data.length;
var STD = [];
for (var i=0;i<N;i++)
{
var M = all_data[i].length;
var MED = means[i][1];
var SUM=0;
for (var j=0;j<M;j++)
SUM+=all_data[i][j][1]*Math.pow( all_data[i][j][1]*j - MED, 2 );
STD[i] = [i, Math.sqrt(SUM)];
}
return STD;
}
function showTooltip(x, y, contents) {
$("<div id=\"tooltip\">" + contents + "</div>").css( {
position: 'absolute',
display: 'none',
top: y + 5,
left: x + 5,
border: '1px solid #fdd',
padding: '2px',
'background-color': '#fee',
opacity: 0.80
}).appendTo("body").fadeIn(400);
}
function myYAxisFormatter(v, axis) {
var p = v*100;
return p.toFixed(0) +"%";
}
function myXAxisQuestionFormatter(v, axis) {
return "P"+v.toFixed(0);
}
function regenerate_all_plots()
{
var mydata = recompute_all_histograms();
var MEANs = compute_mean_num_of_pieces(mydata);
//var STDs = compute_std_num_of_pieces(mydata,MEANs);
var PR_LOSE = compute_prob_lose_all(mydata);
var delta = 0.10;
var CIs = [];
CIs.length = 9;
for (var i = 0; i < 9; i ++)
{
var INV_CDF_DATA = inverse_cdf( mydata[i], delta);
CIs[i] =[i, INV_CDF_DATA.idx_abo, INV_CDF_DATA.idx_bel ];
}
// Draw mean # of pieces at each question:
{
var plot_mean = $.plot( sp_placeholder_mean,
[
{
data: MEANs,
label: "# esperado de fajos",
lines: { show: true },
points: { show: true }
},
{
data: CIs,
label: "CI 80%",
lines: { show: true, fill: true },
points: { show: true }
}
],
{
grid: { hoverable: true, clickable: true },
xaxis: { min:0, max: 8.5, tickFormatter: myXAxisQuestionFormatter },
yaxis: { min:0, max: 41 }
}
);
$(sp_placeholder_mean).bind("plothover", function (event, pos, item) {
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#tooltip").remove();
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(4);
showTooltip(item.pageX, item.pageY+25, Math.floor(x)+": "+y);
}
}
else {
$("#tooltip").remove();
previousPoint = null;
}
});
}
// Draw more stats:
{
var plot_stats = $.plot( sp_placeholder_stats,
[
{
data: PR_LOSE,
label: "Prob. perder todo",
bars: { show: true }
}
],
{
grid: { hoverable: true, clickable: true },
xaxis: { min:0, max: 9, tickFormatter: myXAxisQuestionFormatter },
yaxis: { min:0, max: 1, tickFormatter: myYAxisFormatter },
legend: { position:"nw" }
}
);
$(sp_placeholder_stats).bind("plothover", function (event, pos, item) {
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#tooltip").remove();
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(4);
showTooltip(item.pageX, item.pageY+45, "P"+Math.floor(x)+": "+(100*y).toFixed(2)+"%");
}
}
else {
$("#tooltip").remove();
previousPoint = null;
}
});
}
var previousPoint = null;
for (var i = 0; i < 9; i ++)
{
var plc_name = "#sp_placeholder_t" + i;
// Find maximum and convert to percentages:
var max_p=0;
for (var k=0;k<mydata[i].length;k++)
{
if (mydata[i][k][1]>max_p) max_p = mydata[i][k][1];
}
var plot = $.plot($( plc_name ),
[ { data: mydata[i], label: "P"+i} ], {
series: {
bars: { show: true }
},
grid: { hoverable: true, clickable: true },
xaxis: { min:0, max: 44 },
yaxis: { min:0, max: max_p, tickFormatter: myYAxisFormatter }
});
$(plc_name).bind("plothover", function (event, pos, item) {
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#tooltip").remove();
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(4);
showTooltip(item.pageX, item.pageY+25, Math.floor(x)+": "+(100*y).toFixed(2)+"%");
}
}
else {
$("#tooltip").remove();
previousPoint = null;
}
});
}
}
$(function () {
regenerate_all_plots();
});