-
Notifications
You must be signed in to change notification settings - Fork 21
/
sample_select.js
111 lines (65 loc) · 2.99 KB
/
sample_select.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
import {describe} from 'ava-spec';
const jssm = require('../../../build/jssm.es5.js');
describe('weighted_sample_select/1', async it => {
// wow is this hard to meaningfully test
it('(0) generates []', t => t.deepEqual(
[],
jssm.weighted_sample_select(0, [{item:'a',probability:2},{item:'a',probability:3}]) )
);
describe('has reasonable unweighted distribution', async uit => {
const unweighted = new jssm.Machine({
initial_state: 'a',
transitions: [
{ from: 'a', to: 'b' },
{ from: 'a', to: 'c' },
{ from: 'b', to: 'a' },
{ from: 'b', to: 'c' },
{ from: 'c', to: 'a' },
{ from: 'c', to: 'b' }
]
});
const res = unweighted.probabilistic_histo_walk(1500);
// statistically each should be around 500. raise alarms if they aren't 300.
uit('a expects 500 requires 300', t => t.is(true, res.get('a') >= 300));
uit('b expects 500 requires 300', t => t.is(true, res.get('b') >= 300));
uit('c expects 500 requires 300', t => t.is(true, res.get('c') >= 300));
});
describe('has reasonable weighted distribution', async uit => {
const weighted = new jssm.Machine({
initial_state: 'a',
transitions: [
{ from: 'a', to: 'b', probability: 0.5 },
{ from: 'a', to: 'c', probability: 4 },
{ from: 'a', to: 'd', probability: 0.5 },
{ from: 'a', to: 'e', probability: 0.5 },
{ from: 'b', to: 'a', probability: 0.5 },
{ from: 'b', to: 'c', probability: 4 },
{ from: 'b', to: 'd', probability: 0.5 },
{ from: 'b', to: 'e', probability: 0.5 },
{ from: 'c', to: 'a', probability: 0.5 },
{ from: 'c', to: 'b', probability: 0.5 },
{ from: 'c', to: 'd', probability: 0.5 },
{ from: 'c', to: 'e', probability: 0.5 },
{ from: 'd', to: 'a', probability: 0.5 },
{ from: 'd', to: 'b', probability: 0.5 },
{ from: 'd', to: 'c', probability: 4 },
{ from: 'd', to: 'e', probability: 0.5 },
{ from: 'e', to: 'a', probability: 0.5 },
{ from: 'e', to: 'b', probability: 0.5 },
{ from: 'e', to: 'c', probability: 4 },
{ from: 'e', to: 'd', probability: 0.5 }
]
});
const res = weighted.probabilistic_histo_walk(2500);
// statistically each should be around 375, or 1050 for c. raise alarms if they aren't 250, or 800 for c.
uit('a expects 375 requires 250', t => t.is(true, res.get('a') >= 250));
uit('b expects 375 requires 250', t => t.is(true, res.get('b') >= 250));
uit('c expects 1050 requires 800', t => t.is(true, res.get('c') >= 800));
uit('d expects 375 requires 250', t => t.is(true, res.get('c') >= 250));
uit('e expects 375 requires 250', t => t.is(true, res.get('c') >= 250));
});
// stochastics would help, eg "every returned item is a member" and "in a
// sufficient list any positive sample size is reasonable" and "always
// returns the right sample size" - whargarbl todo
});
// stochable