forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistribution.js
More file actions
296 lines (249 loc) · 8.87 KB
/
distribution.js
File metadata and controls
296 lines (249 loc) · 8.87 KB
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
'use strict'
const ArgumentsError = require('../../error/ArgumentsError')
const isCollection = require('../../utils/collection/isCollection')
const isNumber = require('../../utils/number').isNumber
// TODO: rethink math.distribution
// TODO: rework to a typed function
function factory (type, config, load, typed, math) {
const matrix = load(require('../../type/matrix/function/matrix'))
const array = require('../../utils/array')
// seeded pseudo random number generator
const rng = load(require('./seededRNG'))
/**
* Create a distribution object with a set of random functions for given
* random distribution.
*
* Syntax:
*
* math.distribution(name)
*
* Examples:
*
* const normalDist = math.distribution('normal') // create a normal distribution
* normalDist.random(0, 10) // get a random value between 0 and 10
*
* See also:
*
* random, randomInt, pickRandom
*
* @param {string} name Name of a distribution. Choose from 'uniform', 'normal'.
* @return {Object} Returns a distribution object containing functions:
* `random([size] [, min] [, max])`,
* `randomInt([min] [, max])`,
* `pickRandom(array)`
*/
function distribution (name) {
if (!distributions.hasOwnProperty(name)) { throw new Error('Unknown distribution ' + name) }
const args = Array.prototype.slice.call(arguments, 1)
const distribution = distributions[name].apply(this, args)
return (function (distribution) {
// This is the public API for all distributions
const randFunctions = {
random: function (arg1, arg2, arg3) {
let size, min, max
if (arguments.length > 3) {
throw new ArgumentsError('random', arguments.length, 0, 3)
} else if (arguments.length === 1) {
// `random(max)` or `random(size)`
if (isCollection(arg1)) {
size = arg1
} else {
max = arg1
}
} else if (arguments.length === 2) {
// `random(min, max)` or `random(size, max)`
if (isCollection(arg1)) {
size = arg1
max = arg2
} else {
min = arg1
max = arg2
}
} else {
// `random(size, min, max)`
size = arg1
min = arg2
max = arg3
}
// TODO: validate type of size
if ((min !== undefined && !isNumber(min)) || (max !== undefined && !isNumber(max))) {
throw new TypeError('Invalid argument in function random')
}
if (max === undefined) max = 1
if (min === undefined) min = 0
if (size !== undefined) {
const res = _randomDataForMatrix(size.valueOf(), min, max, _random)
return type.isMatrix(size) ? matrix(res) : res
}
return _random(min, max)
},
randomInt: typed({
'number | Array': function (arg) {
const min = 0
if (isCollection(arg)) {
const size = arg
const max = 1
const res = _randomDataForMatrix(size.valueOf(), min, max, _randomInt)
return type.isMatrix(size) ? matrix(res) : res
} else {
const max = arg
return _randomInt(min, max)
}
},
'number | Array, number': function (arg1, arg2) {
if (isCollection(arg1)) {
const size = arg1
const max = arg2
const min = 0
const res = _randomDataForMatrix(size.valueOf(), min, max, _randomInt)
return type.isMatrix(size) ? matrix(res) : res
} else {
const min = arg1
const max = arg2
return _randomInt(min, max)
}
},
'Array, number, number': function (size, min, max) {
const res = _randomDataForMatrix(size.valueOf(), min, max, _randomInt)
return (size && size.isMatrix === true) ? matrix(res) : res
}
}),
pickRandom: typed({
'Array': function (possibles) {
return _pickRandom(possibles)
},
'Array, number | Array': function (possibles, arg2) {
let number, weights
if (Array.isArray(arg2)) {
weights = arg2
} else if (isNumber(arg2)) {
number = arg2
} else {
throw new TypeError('Invalid argument in function pickRandom')
}
return _pickRandom(possibles, number, weights)
},
'Array, number | Array, Array | number': function (possibles, arg2, arg3) {
let number, weights
if (Array.isArray(arg2)) {
weights = arg2
number = arg3
} else {
weights = arg3
number = arg2
}
if (!Array.isArray(weights) || !isNumber(number)) {
throw new TypeError('Invalid argument in function pickRandom')
}
return _pickRandom(possibles, number, weights)
}
})
}
function _pickRandom (possibles, number, weights) {
const single = (typeof number === 'undefined')
if (single) {
number = 1
}
if (type.isMatrix(possibles)) {
possibles = possibles.valueOf() // get Array
} else if (!Array.isArray(possibles)) {
throw new TypeError('Unsupported type of value in function pickRandom')
}
if (array.size(possibles).length > 1) {
throw new Error('Only one dimensional vectors supported')
}
let totalWeights = 0
if (typeof weights !== 'undefined') {
if (weights.length !== possibles.length) {
throw new Error('Weights must have the same length as possibles')
}
for (let i = 0, len = weights.length; i < len; i++) {
if (!isNumber(weights[i]) || weights[i] < 0) {
throw new Error('Weights must be an array of positive numbers')
}
totalWeights += weights[i]
}
}
const length = possibles.length
if (length === 0) {
return []
} else if (number >= length) {
return number > 1 ? possibles : possibles[0]
}
const result = []
let pick
while (result.length < number) {
if (typeof weights === 'undefined') {
pick = possibles[Math.floor(rng() * length)]
} else {
let randKey = rng() * totalWeights
for (let i = 0, len = possibles.length; i < len; i++) {
randKey -= weights[i]
if (randKey < 0) {
pick = possibles[i]
break
}
}
}
if (result.indexOf(pick) === -1) {
result.push(pick)
}
}
return single ? result[0] : result
// TODO: add support for multi dimensional matrices
}
function _random (min, max) {
return min + distribution() * (max - min)
}
function _randomInt (min, max) {
return Math.floor(min + distribution() * (max - min))
}
// This is a function for generating a random matrix recursively.
function _randomDataForMatrix (size, min, max, randFunc) {
const data = []
size = size.slice(0)
if (size.length > 1) {
for (let i = 0, length = size.shift(); i < length; i++) {
data.push(_randomDataForMatrix(size, min, max, randFunc))
}
} else {
for (let i = 0, length = size.shift(); i < length; i++) {
data.push(randFunc(min, max))
}
}
return data
}
return randFunctions
})(distribution)
}
// Each distribution is a function that takes no argument and when called returns
// a number between 0 and 1.
let distributions = {
uniform: function () {
return rng
},
// Implementation of normal distribution using Box-Muller transform
// ref : https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
// We take : mean = 0.5, standard deviation = 1/6
// so that 99.7% values are in [0, 1].
normal: function () {
return function () {
let u1
let u2
let picked = -1
// We reject values outside of the interval [0, 1]
// TODO: check if it is ok to do that?
while (picked < 0 || picked > 1) {
u1 = rng()
u2 = rng()
picked = 1 / 6 * Math.pow(-2 * Math.log(u1), 0.5) * Math.cos(2 * Math.PI * u2) + 0.5
}
return picked
}
}
}
distribution.toTex = undefined // use default template
return distribution
}
exports.name = 'distribution'
exports.factory = factory