-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlotto.html
357 lines (330 loc) · 10.7 KB
/
lotto.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
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="JPS lotto" />
<title>Lotto</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css"
/>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jstat@latest/dist/jstat.min.js"></script>
<script src="./minified-web.js"></script>
<script src="./applyTo.js"></script>
<script src="./sw-test.js"></script>
</head>
<body>
<main>
<article>
<h4>Sums</h4>
<details>
<summary role="button">
Box
<span style="float: right"
><code><small class="sum-box"></small></code
></span>
</summary>
<div id="box-sum"></div>
</details>
<details>
<summary role="button">
Histogram
<span style="float: right"
><code
><small
id="sum-his"
title="Sums distribution is normal if p-value > 0.05"
></small></code
></span>
</summary>
<div id="his-sum"></div>
</details>
</article>
<article>
<h4>Pairs</h4>
<details>
<summary role="button">Counts & Skips</summary>
<div id="plo-pai"></div>
</details>
</article>
<article>
<h4>Numbers</h4>
<details>
<summary role="button">Counts & Skips</summary>
<div id="plo-cou-ski"></div>
</details>
<table class="striped">
<thead>
<tr>
<th scope="col">Numbers</th>
<th scope="col">Sum</th>
<th scope="col">Counts</th>
<th scope="col">Counts Sum</th>
<th scope="col">Skips</th>
<th scope="col">Skips Sum</th>
</tr>
</thead>
<tbody id="nums"></tbody>
</table>
</article>
</main>
<script defer>
const kCombinations = (t, n) => {
if (n > t.length || n <= 0) return [];
if (n === t.length) return [t];
const e = [];
if (1 === n) {
for (let n = 0; n < t.length; n++) e.push([t[n]]);
return e;
}
for (let o = 0; o < t.length - n + 1; o++) {
const r = t.slice(o, o + 1),
l = kCombinations(t.slice(o + 1), n - 1);
for (let t = 0; t < l.length; t++) e.push(r.concat(l[t]));
}
return e;
};
const formatNumber = (num) => (num < 10 ? `0${num}` : `${num}`);
(async () => {
const response = await fetch("./lotto.json");
const json = await response.json();
const lotto = {
counts: {},
sums: {},
groups: {},
pairs: {},
skips: {},
};
for (let num = 1; num <= 40; ++num) {
const fir = formatNumber(num - 1);
const sec = formatNumber(num);
const key = `${fir},${sec}`;
lotto.pairs[key] = {};
lotto.pairs[key].count = 0;
}
for (const key in json) {
let { n, r } = json[key];
n = n.sort((a, b) => a - b);
r = r.sort((a, b) => a - b);
Object.keys(lotto.pairs).forEach((pair) => {
const [fir, sec] = pair.split(",");
if (n.includes(Number(fir)) && r.includes(Number(sec))) {
lotto.pairs[pair].count++;
}
});
const nums = n.concat(r);
nums.forEach((num) => {
lotto.counts[num] = lotto.counts[num] ?? 0;
lotto.counts[num]++;
});
const keyN = n.map(formatNumber).join(",");
const keyR = r.map(formatNumber).join(",");
lotto.groups[keyN] = lotto.groups[keyN] ?? 0;
lotto.groups[keyN]++;
lotto.groups[keyR] = lotto.groups[keyR] ?? 0;
lotto.groups[keyR]++;
const sumN = n.reduce((a, b) => a + b, 0);
const sumR = r.reduce((a, b) => a + b, 0);
lotto.sums[sumN] = lotto.sums[sumN] ?? 0;
lotto.sums[sumN]++;
lotto.sums[sumR] = lotto.sums[sumR] ?? 0;
lotto.sums[sumR]++;
}
const sums = [];
Object.keys(lotto.sums).forEach((sum) => {
const count = lotto.sums[sum];
sums.push(...Array(count).fill(Number(sum)));
});
Plotly.newPlot(document.getElementById("box-sum"), [
{ x: sums, type: "box", boxmean: true, name: "Sum" },
]);
Plotly.newPlot(document.getElementById("his-sum"), [
{ x: sums, type: "histogram", histnorm: "probability" },
]);
const [q1, median, q3] = jStat.quartiles(sums);
const sw = window.swTest(sums, 0.05);
const min = jStat.min(sums);
const max = jStat.max(sums);
window.applyTo(
"#sum-his",
"fill",
`p-value ~ ${Math.round(sw.pvalue * 100000) / 100000}`
);
window.applyTo(".sum-box", "fill", `${q1} <= sum <= ${q3}`);
const orderPairs = Object.entries(lotto.pairs).sort(
(a, b) => b[1].count - a[1].count
);
const xPairs = orderPairs.map((num) => num[0]);
const pairsCount = orderPairs.map((num) => num[1].count);
const orderNums = Object.entries(lotto.counts).sort(
(a, b) => b[1] - a[1]
);
const xCounts = orderNums.map((num) => formatNumber(Number(num[0])));
const countVals = orderNums.map((num) => num[1]);
const numsKeys = Object.keys(lotto.counts);
const numsValues = Object.values(lotto.counts);
const [, medianNums, numQ3] = jStat.quartiles(numsValues);
const numsMedian = jStat.median(numsKeys.map(Number));
let combs = kCombinations(numsKeys, 5).map((arr) =>
arr.map((num) => Number(num))
);
combs = combs.filter((comb) => {
const sum = comb.reduce((a, b) => a + b, 0);
const key = comb.map(formatNumber);
const all = comb.every((num) => lotto.counts[num] > medianNums);
const med = comb.filter((num) => num < numsMedian).length;
const div = comb.filter((num) => num % 2 === 0).length;
return (
all &&
!lotto.groups[key] &&
sum >= q1 &&
sum <= q3 &&
med > 1 &&
div > 1 &&
div < 4
);
});
const plays = Object.keys(json)
.map(Number)
.sort((a, b) => b - a);
xCounts.map(Number).forEach((num) => {
for (
let i = 0;
i < plays.length && lotto.skips[num] === undefined;
i++
) {
if (json[plays[i]].n.includes(num)) {
lotto.skips[num] = i * 2;
}
if (json[plays[i]].r.includes(num)) {
lotto.skips[num] = i * 2 + 1;
}
}
});
orderPairs.forEach(([key, _]) => {
const [fir, sec] = key.split(",").map(Number);
for (
let i = 0;
i < plays.length && lotto.pairs[key].skip === undefined;
i++
) {
const { n, r } = json[plays[i]];
if (n.includes(fir) && n.includes(sec)) {
lotto.pairs[key].skip = i * 2;
}
if (r.includes(fir) && r.includes(sec)) {
lotto.pairs[key].skip = i * 2 + 1;
}
}
});
const pairsSkips = xPairs.map((key) => lotto.pairs[key].skip);
Plotly.newPlot(
document.getElementById("plo-pai"),
[
{
name: "Count",
x: xPairs,
y: pairsCount,
mode: "markers",
type: "scatter",
},
{
name: "Skip",
x: xPairs,
y: pairsSkips,
mode: "markers",
type: "scatter",
},
],
{
xaxis: {
title: "Pairs",
type: "category",
tickmode: "array",
tickvals: xPairs,
},
yaxis: {
title: "Counts / Skips",
},
}
);
const [q1Pairs, medianPairs, q3Pairs] = jStat.quartiles(pairsCount);
console.info([q1Pairs, medianPairs, q3Pairs]);
const skipVals = xCounts.map((x) => lotto.skips[Number(x)]);
Plotly.newPlot(
document.getElementById("plo-cou-ski"),
[
{
name: "Count",
x: xCounts,
y: countVals,
mode: "markers",
type: "scatter",
},
{
name: "Skip",
x: xCounts,
y: skipVals,
mode: "markers",
type: "scatter",
},
],
{
xaxis: {
title: "Numbers",
type: "category",
tickmode: "array",
tickvals: xCounts,
},
yaxis: {
title: "Counts / Skips",
},
}
);
const validSkips = {};
combs.forEach((comb) => {
comb.forEach((num) => {
validSkips[num] = lotto.skips[num];
});
});
const valids = Object.keys(validSkips)
.filter((num) => validSkips[num] > 8)
.map(Number);
combs = combs.filter((comb) =>
comb.every((num) => valids.includes(num))
);
lotto.combs = combs;
const combsSums = {};
combs.forEach((comb) => {
const key = comb.map(formatNumber).join(",");
combsSums[key] = comb.reduce((a, b) => a + b, 0);
});
const combsOrder = Object.entries(combsSums).sort(
(a, b) => b[1] - a[1]
);
combsOrder.forEach(([key, sum]) => {
const numbers = key.split(",").map(Number);
const arrCounts = numbers.map((n) => lotto.counts[n]);
const countsSum = arrCounts.reduce((a, b) => a + b, 0);
const arrSkips = numbers.map((n) => validSkips[n]);
const skipsSum = arrSkips.reduce((a, b) => a + b, 0);
window.applyTo(
"#nums",
"add",
EE("tr", [
EE("td", EE("mark", key)),
EE("td", sum),
EE("td", arrCounts.join(",")),
EE("td", countsSum),
EE("td", arrSkips.join(",")),
EE("td", skipsSum),
])
);
});
console.info(lotto);
})();
</script>
</body>
</html>