-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.js
More file actions
22777 lines (19546 loc) · 618 KB
/
bundle.js
File metadata and controls
22777 lines (19546 loc) · 618 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/* ================================================================
* probability-distributions by Matt Asher (me[at]mattasher.com)
* Originally created for StatisticsBlog.com
*
* first created at : Sat Oct 10 2015
*
* ================================================================
* Copyright 2015 Matt Asher
*
* Licensed under the MIT License
* You may not use this file except in compliance with the License.
*
* ================================================================ */
var crypto = require('crypto');
// Shortcuts
var exp = Math.exp;
var ln = Math.log;
var PI = Math.PI;
var pow = Math.pow;
module.exports = {
/**
* This is the core function for generating entropy
*
* @param len number of bytes of entropy to create
* @returns {number} A pseduo random number between 0 and 1
*
*/
prng: function(len) {
if(len === undefined) len=16;
var entropy = crypto.randomBytes(len);
var result = 0;
for(var i=0; i<len; i++) {
result = result + Number(entropy[i])/Math.pow(256,(i+1))
}
return result
},
/**
*
* @param n The number of random variates to create. Must be a positive integer.
* @param alpha First shape parameter
* @param beta Second shape parameter
* @param loc Location or Non-centrality parameter
*/
rbeta: function(n, alpha, beta, loc) {
// Uses relationship with gamma to calculate
// Validations
n = this._v(n, "n");
alpha = this._v(alpha, "nn", 1);
beta = this._v(beta, "nn", 1);
loc = this._v(loc, "r", 0);
var toReturn = [];
for(var i=0; i<n; i++) {
var g1 = this.rgamma(1, alpha, 1)[0];
var g2 = this.rgamma(1, beta, 1)[0];
toReturn[i] = loc + g1/(g1+g2);
}
return toReturn
},
/**
*
* @param n Number of variates to return.
* @param size Number of Bernoulli trials to be summed up. Defaults to 1
* @param p Probability of a "success". Defaults to 0.5
* @returns {Array} Random variates array
*/
rbinom: function(n, size, p) {
n = this._v(n, "n");
size = this._v(size, "nni", 1);
p = this._v(p, "p", 0.5);
var toReturn = [];
for(var i=0; i<n; i++) {
var result = 0;
for(var j=0; j<size; j++) {
if(this.prng() < p) {
result++
}
}
toReturn[i] = result;
}
return toReturn
},
/**
*
* @param n The number of variates to create
* @param loc Location parameter
* @param scale Scale parameter
* @returns {Array} Random variates array
*/
rcauchy: function(n, loc, scale) {
n = this._v(n, "n");
loc = this._v(loc, "r", 0);
scale = this._v(scale, "nn", 1);
var toReturn = [];
for(var i=0; i<n; i++) {
var x = scale * Math.tan(PI * (this.prng()-0.5))+loc;
toReturn[i] = x;
}
return toReturn
},
/**
*
* @param n The number of variates to create
* @param df Degrees of freedom for the distribution
* @param ncp Non-centrality parameter
* @returns {Array} Random variates array
*/
rchisq: function(n, df, ncp) {
n = this._v(n, "n");
df = this._v(df, "nn");
ncp = this._v(ncp, "r", 0);
var toReturn = [];
for(var i=0; i<n; i++) {
// Start at ncp
var x = ncp;
for(var j=0; j<df; j++) {
x = x + Math.pow(this.rnorm(1)[0],2);
}
toReturn[i] = x;
}
return toReturn
},
/**
*
* @param x Where to sample the density
* @param rate The rate parameter. Must be a positive number
* @returns {Number} The density given the parameter values
*/
dexp: function(x, rate) {
x = this._v(x, "r");
rate = this._v(rate, "pos", 1);
if(x < 0) return 0
return rate * exp(-rate * x)
},
/**
*
* @param n The number of random variates to create. Must be a positive integer.
* @param rate The rate parameter. Must be a positive number
*/
rexp: function(n, rate) {
n = this._v(n, "n");
rate = this._v(rate, "pos", 1);
var toReturn = [];
for(var i=0; i<n; i++) {
toReturn[i] = -ln(this.prng())/rate;
}
return toReturn
},
/**
*
* @param n The number of variates to create
* @param df1 Degrees of freedom for the first parameter
* @param df2 Degrees of freedom for the first parameter
* @returns {Array} Random variates array
*/
rf: function(n, df1, df2) {
n = this._v(n, "n");
df1 = this._v(df1, "nn");
df2 = this._v(df2, "nn");
var toReturn = [];
for(var i=0; i<n; i++) {
var num = this.rchisq(1, df1)[0]/df1;
var denom = this.rchisq(1, df2)[0]/df2;
toReturn[i] = num/denom;
}
return toReturn
},
/**
*
* @param n The number of random variates to create. Must be a positive integer
* @param alpha
* @param rate
* @returns {Array} Random variates array
*/
rgamma: function(n, alpha, rate) {
// Adapted from https://github.com/mvarshney/simjs-source/ & scipy
n = this._v(n, "n");
alpha = this._v(alpha, "nn");
rate = this._v(rate, "pos", 1);
var LOG4 = ln(4.0);
var SG_MAGICCONST = 1.0 + ln(4.5);
var beta = 1/rate;
var toReturn = [];
for(var i = 0; i<n; i++) {
/* Based on Python 2.6 source code of random.py.
*/
if (alpha > 1.0) {
var ainv = Math.sqrt(2.0 * alpha - 1.0);
var bbb = alpha - LOG4;
var ccc = alpha + ainv;
while (true) {
var u1 = this.prng();
if ((u1 < 1e-7) || (u > 0.9999999)) {
continue;
}
var u2 = 1.0 - this.prng();
var v = ln(u1 / (1.0 - u1)) / ainv;
var x = alpha * exp(v);
var z = u1 * u1 * u2;
var r = bbb + ccc * v - x;
if ((r + SG_MAGICCONST - 4.5 * z >= 0.0) || (r >= ln(z))) {
var result = x * beta;
break;
}
}
} else if (alpha == 1.0) {
var u = this.prng();
while (u <= 1e-7) {
u = this.prng();
}
var result = - ln(u) * beta;
} else {
while (true) {
var u = this.prng();
var b = (Math.E + alpha) / Math.E;
var p = b * u;
if (p <= 1.0) {
var x = Math.pow(p, 1.0 / alpha);
} else {
var x = - ln((b - p) / alpha);
}
var u1 = this.prng();
if (p > 1.0) {
if (u1 <= Math.pow(x, (alpha - 1.0))) {
break;
}
} else if (u1 <= exp(-x)) {
break;
}
}
var result = x * beta;
}
toReturn[i] = result;
}
return toReturn;
},
/**
*
* @param n The number of random variates to create. Must be a positive integer
* @param min Minimum value
* @param max Maximum value
* @param inclusive By default the minimum and maximum are inclusive. To make exclusive, set to false
* @returns {Array}
*/
rint: function(n, min, max, inclusive) {
n = this._v(n, "n");
min = this._v(min, "int");
max = this._v(max, "int");
if(inclusive === false) {
min++;
if(min === max) throw new Error("Minimum value cannot be greater than maximum value. For non-inclusive, minimum and maximum must be separated by at least 2.");
} else {
max++
}
if(min > max) throw new Error("Minimum value cannot be greater than maximum value. For non-inclusive, minimum and maximum must be separated by at least 2.");
var toReturn = [];
var raw = this.runif(n, min, max);
for(var i=0; i<n; i++) {
toReturn[i] = Math.floor(raw[i]);
}
return toReturn
},
// Syntax as in R library VGAM
/**
*
* @param n The number of random variates to create. Must be a positive integer
* @param loc Mean
* @param scale Scale parameter
* @returns {Array} Random variates array
*/
rlaplace: function(n, loc, scale) {
n = this._v(n, "n");
loc = this._v(loc, "r", 0);
scale = this._v(scale, "nn", 1);
var toReturn = [];
for(var i=0; i<n; i++) {
var core = this.sample([-1,1])[0] * ln(this.prng());
var x = loc - scale * core;
toReturn[i] = x;
}
return toReturn
},
/**
*
* @param n The number of random variates to create. Must be a positive integer.
* @param meanlog The mean log.
* @param sdlog Log SD. Must be greater than 0.
* @returns {Array} Random variates array
*/
rlnorm: function(n, meanlog, sdlog) {
n = this._v(n, "n");
meanlog = this._v(meanlog, "r", 0);
sdlog = this._v(sdlog, "nn", 1);
var toReturn = [];
for(var i=0; i<n; i++) {
var x = this.rnorm(1, meanlog, sdlog)[0];
toReturn[i] = exp(x);
}
return toReturn
},
/**
*
* @param n The number of random variates to create. Must be a positive integer.
* @param size Number of hits required
* @param p Hit probability
* @param mu Optional way to specify hit probability
* @returns {Array} Random variates array
*/
rnbinom: function(n, size, p, mu) {
n = this._v(n, "n");
if(size === undefined) size=1;
if(Math.round(size) != size) throw new Error("Size must be a whole number");
if(size < 1) throw new Error("Size must be one or greater");
if(p !== undefined && mu !== undefined) throw new Error("You must specify probability or mean, not both");
if(mu !== undefined) p = size/(size+mu);
p = this._v(p, "p");
var toReturn = [];
for(var i=0; i<n; i++) {
// Core distribution
var result = 0;
var leftToFind = size;
while(leftToFind > 0) {
result++;
if(this.prng() < p) leftToFind--;
}
toReturn[i] = result - 1;
}
return toReturn
},
/**
*
* @param x Where to sample the density
* @param mean Mean of the distribution
* @param sd Standard deviation for the distribution
* @returns {Number} The density given the parameter values
*/
dnorm: function(x, mean, sd) {
x = this._v(x, "r");
mean = this._v(mean, "r", 0);
sd = this._v(sd, "nn", 1);
// Check for degeneracy
if(sd === 0) {
if(x === mean) return Infinity;
return 0
}
var a = sd*(Math.sqrt(2*PI));
var b = -(x-mean)*(x-mean);
var c = 2*sd*sd;
return (1/a)*exp(b/c)
},
/**
*
* @param n The number of random variates to create. Must be a positive integer.
* @param mean Mean of the distribution
* @param sd Standard Deviation of the distribution
* @returns {Array} Random variates array
*/
rnorm: function(n, mean, sd) {
// Adapted from http://blog.yjl.im/2010/09/simulating-normal-random-variable-using.html
n = this._v(n, "n");
mean = this._v(mean, "r", 0);
sd = this._v(sd, "nn", 1);
var toReturn = [];
for(var i=0; i<n; i++) {
var V1, V2, S, X;
do {
var U1 = this.prng();
var U2 = this.prng();
V1 = (2 * U1) - 1;
V2 = (2 * U2) - 1;
S = (V1 * V1) + (V2 * V2);
} while (S > 1);
X = Math.sqrt(-2 * ln(S) / S) * V1;
X = mean + sd * X;
toReturn.push(X);
}
return toReturn
},
/**
*
* @param x Where to sample the density
* @param lambda Mean/variance
* @returns {Number} The density given the parameter values
*/
dpois: function(x, lambda) {
x = this._v(x, "nni");
lambda = this._v(lambda, "nn");
// Check for degeneracy
if(lambda === 0) {
if(x === 0) return 1;
return 0
}
var a = pow(lambda, x);
var b = exp(-lambda);
var c = this._factorial(x);
return a*b/c
},
/**
*
* @param n The number of random variates to create. Must be a positive integer.
* @param lambda Mean/Variance of the distribution
* @returns {Array} Random variates array
*/
rpois: function(n, lambda) {
n = this._v(n, "n");
lambda = this._v(lambda, "pos");
var toReturn = [];
for(var i=0; i<n; i++) {
// Adapted from http://wiki.q-researchsoftware.com/wiki/How_to_Generate_Random_Numbers:_Poisson_Distribution
if (lambda < 30) {
var L = exp(-lambda);
var p = 1;
var k = 0;
do {
k++;
p *= this.prng();
} while (p > L);
toReturn.push(k - 1);
} else {
// Roll our own
// Fix total number of samples
var samples = 10000;
var p = lambda/samples;
var k = 0;
for(var j=0; j<samples; j++) {
if(this.prng() < p) {
k++
}
}
toReturn[i] = k;
}
}
return toReturn
},
/**
* Density function for uniform distribution
*
* @param x Location to get density for
* @param min {number} Minimum value
* @param max {number} Maximum value
* @returns {number} Density of the function given the location and parameters
*/
dunif: function(x, min, max) {
x = this._v(x, "r");
min = this._v(min, "r", 0);
max = this._v(max, "r", 1);
if(min > max) throw new Error("Minimum value cannot be greater than maximum value");
if(x < min || x > max) return 0;
if(min === max) return Infinity;
return 1/(max-min);
},
/**
*
* @param n Number of variates to return
* @param min Lower bound
* @param max Upper bound
* @returns {Array} Random variates array
*/
runif: function(n, min, max) {
n = this._v(n, "n");
min = this._v(min, "r", 0);
max = this._v(max, "r", 1);
if(min > max) throw new Error("Minimum value cannot be greater than maximum value");
var toReturn = [];
for(var i=0; i<n; i++) {
var raw = this.prng();
var scaled = min + raw*(max-min);
toReturn.push(scaled)
}
return toReturn
},
/**
* Generate a random word of specified length using library of characters. Uses English alphabet if no library is specified
* @param len Number of letters in this word
* @param {string} alphabet to use
* @returns {string} String of randomly selected characters from the alphabet
*/
rword: function(len, alphabet) {
len = this._v(len, "n");
alphabet = this._v(alphabet, "str", "abcdefghijklmnopqrstuvwxyz");
var lib = alphabet.split("");
var arr = this.sample(lib, len, true);
return arr.join("");
},
/**
*
* @param collection Array of items to sample from
* @param n Number of items to sample. If missing, n will be set to the length of the collection and it will shuffle
* @param replace Sample with replacement? False by default
* @param ratios Ratios to weight items. Can be any non-negative number. By default all items are given equal weight
* @returns {Array} Array of sampled items
*/
sample: function(collection, n, replace, ratios) {
// Validations
collection = this._v(collection, "a");
n = this._v(n, "n", collection.length); // If n is undefined, sample the full array
if(replace === undefined) replace = false;
if(!replace && collection.length < n)
throw new Error("You cannot select " + n + " items from an array of length " + collection.length + " without replacement");
if(ratios === undefined) {
ratios = [];
for(var m=0; m<collection.length; m++) { ratios[m] = 1 }
}
var cumulativeProbs = this._getCumulativeProbs(ratios, collection.length);
// Main loop
var toReturn = [];
for(var i=0; i<n; i++) {
var chosen = this._sampleOneIndex(cumulativeProbs);
if(replace) {
toReturn[i] = collection[chosen];
} else {
// Remove from collection and ratios
toReturn[i] = collection.splice(chosen, 1)[0];
ratios.splice(chosen, 1);
// Make sure we aren't at the end
if(ratios.length) {
cumulativeProbs = this._getCumulativeProbs(ratios, collection.length);
}
}
}
return toReturn;
},
/**
*
* @param data Array of data to display
* @param domID String ID of the DOM element to use for display. This must ALREADY exist on your page
* @param options Object see below for details
*
* IMPORTANT: This is not a "pure" function, it takes a DOM element ID as an argument and modifies (completely overwrites)
* that element.
*
* IMPORTANT 2: options.conditions are eval'd so don't send untrusted code sent to this function!
*
*/
visualize: function(data, domID, options) {
var defaultOptions = {
lag: 1000, // Timing multiplier in milliseconds
blank: "", // What to show when no number is shown
inf: "∞", // Symbol to indicate infinity
loop: true, // When done, start again at beginning at end of array
conditions: "", // This is eval'd and checked against true. Example, "x > 3 && x < 10". Use "x" as the variable.
arrivalTimes: false,
arrivalSymbol: '<span class="pd-arrival">•</span>',
arrivalFlashTime: 0.25,
significantDigits: 0 // 0 will show as many as JS provides, or choose a number to limit
}
if(typeof options === "undefined") options = {}
// Merge defaultOptions with user options favoring user options
for (var property in defaultOptions) {
if (defaultOptions.hasOwnProperty(property)) {
if(typeof options[property] === "undefined") {
options[property] = defaultOptions[property];
}
}
}
// Elem is a DOM element to output to
var elem = document.getElementById(domID);
if(!elem) throw new Error("Unable to find DOM element " + domID);
var len = data.length, i = 0;
var format = function(x) {
var x = data[i];
// Are we showing only certain things
if(options.conditions) {
// Check for only allowed characters, this is NOT complete security
if(!/^[x\&\|=0-9\<\>\s\-\.]+$/.test(options.conditions)) throw new Error("Bad input sent to options.conditions");
if (eval(options.conditions) !== true) {
x = options.blank;
return x
}
}
if(options.significantDigits) {
x = x.toPrecision(options.significantDigits)
}
if(x === Infinity) {
x = options.inf;
}
return x;
}
if(options.arrivalTimes) {
var blankOut = function() {
setTimeout(function() {
elem.innerHTML = options.blank;
}, options.arrivalFlashTime*options.lag);
}
var cycle = function() {
elem.innerHTML = options.arrivalSymbol;
blankOut();
if(++i === len) i = 0;
if(options.loop || i !== 0) {
setTimeout(cycle, data[i]*options.lag)
} else {
// End on a blank
elem.innerHTML = options.blank;
}
}
cycle();
} else {
var update = function() {
// Main vis for non-arrival numbers
elem.innerHTML = format(data[i]);
if(++i === len) i = 0;
if(options.loop || i !== 0) {
setTimeout(update, options.lag)
} else {
// End on a blank
setTimeout(function() { elem.innerHTML = options.blank }, options.lag)
}
}
update();
}
},
// HELPERS
/**
*
* @param ratios Array of non-negative numbers to be turned into CDF
* @param len length of the collection
* @returns {Array}
* @private
*/
_getCumulativeProbs: function(ratios, len) {
if(len === undefined) throw new Error("An error occurred: len was not sent to _getCumulativeProbs");
if(ratios.length !== len) throw new Error("Probabilities for sample must be same length as the array to sample from");
var toReturn = [];
if(ratios !== undefined) {
ratios = this._v(ratios, "a");
if(ratios.length !== len) throw new Error("Probabilities array must be the same length as the array you are sampling from");
var sum = 0;
ratios.map(function(ratio) {
ratio = this._v(ratio, "nn"); // Note validating as ANY non-negative number
sum+= ratio;
toReturn.push(sum);
}.bind(this));
// Divide by total to normalize
for(var k=0; k<toReturn.length; k++) { toReturn[k] = toReturn[k]/sum }
return toReturn
}
},
_sampleOneIndex: function(cumulativeProbs) {
var toTake = this.prng();
// Find out where this lands in weights
var cur = 0;
while(toTake > cumulativeProbs[cur]) cur++;
return cur;
},
_factorial: function(n) {
var toReturn=1;
for (var i = 2; i <= n; i++)
toReturn = toReturn * i;
return toReturn;
},
// Return default if undefined, otherwise validate
// Return a COPY of the validated parameter
_v: function(param, type, defaultParam) {
if(param == null && defaultParam != null)
return defaultParam;
switch(type) {
// Array of 1 item or more
case "a":
if(!Array.isArray(param) || !param.length) throw new Error("Expected an array of length 1 or greater");
return param.slice(0);
// Integer
case "int":
if(param !== Number(param)) throw new Error("A required parameter is missing or not a number");
if(param !== Math.round(param)) throw new Error("Parameter must be a whole number");
if(param === Infinity) throw new Error("Sent 'infinity' as a parameter");
return param;
// Natural number
case "n":
if(param === undefined) throw new Error("You must specify how many values you want");
if(param !== Number(param)) throw new Error("The number of values must be numeric");
if(param !== Math.round(param)) throw new Error("The number of values must be a whole number");
if(param < 1) throw new Error("The number of values must be a whole number of 1 or greater");
if(param === Infinity) throw new Error("The number of values cannot be infinite ;-)");
return param;
// Valid probability
case "p":
if(Number(param) !== param) throw new Error("Probability value is missing or not a number");
if(param > 1) throw new Error("Probability values cannot be greater than 1");
if(param < 0) throw new Error("Probability values cannot be less than 0");
return param;
// Positive numbers
case "pos":
if(Number(param) !== param) throw new Error("A required parameter is missing or not a number");
if(param <= 0) throw new Error("Parameter must be greater than 0");
if(param === Infinity) throw new Error("Sent 'infinity' as a parameter");
return param;
// Look for numbers (reals)
case "r":
if(Number(param) !== param) throw new Error("A required parameter is missing or not a number");
if(param === Infinity) throw new Error("Sent 'infinity' as a parameter");
return param;
// Non negative real number
case "nn":
if(param !== Number(param)) throw new Error("A required parameter is missing or not a number");
if(param < 0) throw new Error("Parameter cannot be less than 0");
if(param === Infinity) throw new Error("Sent 'infinity' as a parameter");
return param;
// Non negative whole number (integer)
case "nni":
if(param !== Number(param)) throw new Error("A required parameter is missing or not a number");
if(param !== Math.round(param)) throw new Error("Parameter must be a whole number");
if(param < 0) throw new Error("Parameter cannot be less than zero");
if(param === Infinity) throw new Error("Sent 'infinity' as a parameter");
return param;
// Non-empty string
case "str":
if(param !== String(param)) throw new Error("A required parameter is missing or not a string");
if(param.length === 0) throw new Error("Parameter must be at least one character long");
return param;
}
},
// ________ _______ ______ _____ _____ __ __ ______ _ _ _______ _
// | ____\ \ / / __ \| ____| __ \|_ _| \/ | ____| \ | |__ __|/\ | |
// | |__ \ V /| |__) | |__ | |__) | | | | \ / | |__ | \| | | | / \ | |
// | __| > < | ___/| __| | _ / | | | |\/| | __| | . ` | | | / /\ \ | |
// | |____ / . \| | | |____| | \ \ _| |_| | | | |____| |\ | | |/ ____ \| |____
// |______/_/ \_\_| |______|_| \_\_____|_| |_|______|_| \_| |_/_/ \_\______|
/**
*
* @param n Number of variates to return
* @param loc Starting point. Must be a non-negative integer. 0 for degenerate distribution of 0.
* @param p Probability of moving towards finish
* @param cap Maximum steps before giving up
* @param trace Variable to track progress
* @returns {Array} Random variates array
*
* The FML distribution is a is based on the number of steps taken to return to the origin
* from a given position, with transition probabilities set at the beginning by picking a
* random variate from U(0,1).
*/
rfml: function (n, loc, p, cap, trace) {
n = this._v(n, "n");
loc = this._v(loc, "nni", 1);
if(p === undefined) p=this.prng;
cap = this._v(cap, "n", 10000);
if(trace === undefined) trace={};
var toReturn = [];
for(var i=0; i<n; i++) {
var x = 0;
var s = loc;
var currP = p();
if(loc === 0) {
toReturn[i] = 0;
} else {
do {
var trial = this.prng();
if(trial < currP) {
s++;
trace[String(i) + "_" + String(x)] = { problems: s, p: currP, result: 1 }
} else {
s--;
trace[String(i) + "_" + String(x)] = { problems: s, p: currP, result: -1 }
}
x++
} while(s > 0 && x < cap);
if(x === cap) x = -1; // Indicate we failed to do it in time.
toReturn[i] = x;
}
}
return toReturn
},
// http://www.statisticsblog.com/2013/05/uncovering-the-unreliable-friend-distribution-a-case-study-in-the-limits-of-mc-methods/
/**
*
* The Unrelaible Friend distribution
* @param n
* @returns {Array} Random variates array
*/
ruf: function(n) {
n = this._v(n, "n");
var toReturn = [];
for(var i=0; i<n; i++) {
toReturn[i] = this.rexp(1, this.prng())[0];
}
return toReturn
}
};
// TODO: Add "perfect fake" functions: http://www.statisticsblog.com/2010/06/the-perfect-fake/
// NOTES
// Potential config options:
// default entropy amount
// Need pathway to make ready for secure applications (NIST/diehard?)
// Always return a vector unless number is 1? This could be config option or put "1" at end of fcn to get 1 only
// Separate out core random variate creation from number to create loop
// TODO: To test out quality of randomness, stub in specific values for this.prng and make sure correct stuff is returned.
},{"crypto":61}],2:[function(require,module,exports){
// Peter Helstrup Jensen https://github.com/peheje
const ui = require('./ui');
const rd = require('./readDOM'), ge = rd.getElement, gi = rd.getInput, gin = rd.getInputNumber, gij = rd.getInputJson, si = rd.setInput;
const gen = require('./genetics_poly');
const random = require('./random');
const PR_MUTATE = 0.5; // Chance one coefficient-set genetics_poly.mutates 1 of its coefficients
const MUTATE_INTENSITY = 0.4; // How much mutation allowed in either direction, but it is decreased with best error
const MAX_CROSSOVER = 0.3; // In crossing over, how much potential mix is a son of also his mother.
const POOL_REDUCTION = 0.001;
const MAX_POOL_REDUCTION = 0.5;
const DELAY_MS = 0;
const ORIG_POOL_SIZE = gin("poolsize");
console.log("ORIG_POOL_SIZE", ORIG_POOL_SIZE);
ui.activate(initLoop, mainLoop);
function initLoop({ order: N, poolsize: PS, minguess: min, maxguess: max }) {
console.time("run");
let pool = [];
for (let i = 0; i < PS; i++) {
pool.push(gen.seed(N + 1, min, max));
}
return pool;