-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
128 lines (104 loc) · 2.87 KB
/
index.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
/* global module */
/**
* Library for sampling of random values from a discrete probability distribution,
* using the Walker-Vose alias method.
*
* Creates a new Sample instance for the given probabilities and outcomes.
*
* @param {Array} the probabilities.
* @param {Array} the outcomes. Index is assumed as outcome if not provided.
*/
function Sample(probabilities, outcomes) {
'use strict';
this.alias = [];
this.prob = [];
this.outcomes = outcomes || this.indexedOutcomes(probabilities.length);
this.precomputeAlias(probabilities);
}
/**
* Samples outcomes from the underlying probability distribution.
*
* @param {int} the number of samples. Optional parameter, defaults to 1.
* @return {Object} a random outcome according to the underlying probability distribution
* and the requested number of samples. If the requested number of samples
* is greater than 1 this method returns an array.
*/
Sample.prototype.next = function (numOfSamples) {
'use strict';
var n = numOfSamples || 1,
out = [],
i = 0;
do {
var c = this.randomInt(0, this.prob.length);
out[i] = this.outcomes[(Math.random() < this.prob[c]) ? c : this.alias[c]];
} while (++i < n);
return (n > 1) ? out : out[0];
};
/**
* Ported from ransampl.c
* Scientific Computing Group of JCNS at MLZ Garching.
* http://apps.jcns.fz-juelich.de/doku/sc/ransampl
*/
Sample.prototype.precomputeAlias = function (p) {
'use strict';
var n = p.length,
sum = 0,
nS = 0,
nL = 0,
P = [],
S = [],
L = [],
g, i, a;
// Normalize probabilities
for (i = 0; i < n; ++i) {
if (p[i] < 0) {
throw 'Probability must be a positive: p[' + i + ']=' + p[i];
}
sum += p[i];
}
if (sum === 0) {
throw 'Probability cannot be zero.';
}
for (i = 0; i < n; ++i) {
P[i] = p[i] * n / sum;
}
// Set separate index lists for small and large probabilities:
for (i = n - 1; i >= 0; --i) {
// at variance from Schwarz, we revert the index order
if (P[i] < 1)
S[nS++] = i;
else
L[nL++] = i;
}
// Work through index lists
while (nS && nL) {
a = S[--nS]; // Schwarz's l
g = L[--nL]; // Schwarz's g
this.prob[a] = P[a];
this.alias[a] = g;
P[g] = P[g] + P[a] - 1;
if (P[g] < 1)
S[nS++] = g;
else
L[nL++] = g;
}
while (nL)
this.prob[L[--nL]] = 1;
while (nS)
// can only happen through numeric instability
this.prob[S[--nS]] = 1;
};
Sample.prototype.indexedOutcomes = function (n) {
'use strict';
var o = [];
for (var i = 0; i < n; i++) o[i] = i;
return o;
};
Sample.prototype.randomInt = function (min, max) {
'use strict';
return Math.floor(Math.random() * (max - min)) + min;
};
module.exports = function (probabilities, outcomes) {
'use strict';
return new Sample(probabilities, outcomes);
};