-
Notifications
You must be signed in to change notification settings - Fork 1
/
SIMTP3.js
99 lines (71 loc) · 1.9 KB
/
SIMTP3.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
function getRNG() {
return Math.random();
}
function generarUniforme(limiteInferior, limiteSuperior, n) {
var r = [];
for (var i = 0; i < n; i++) {
var random = limiteInferior + (limiteSuperior - limiteInferior) * getRNG();
r.push(random);
}
return r;
}
function generarNormalBoxMuller(media, estDesv, n) {
var r = [];
for (var i = 0; i < n; i += 2) {
var randomA = getRNG();
var randomB = getRNG();
var v = Math.sqrt(-2 * Math.log(randomA)) * Math.cos(2 * Math.PI * randomB);
v *= estDesv;
v += media;
r.push(v);
v = Math.sqrt(-2 * Math.log(randomA)) * Math.sin(2 * Math.PI * randomB);
v *= estDesv;
v += media;
r.push(v);
}
return r;
}
function generarNormalConvolucion(media, estDesv, n) {
var r = [];
for (var i = 0; i < n; i++) {
var randoms = [];
var s = 0;
for (var j = 0; j < 12; j++) {
randoms.push(getRNG());
s += randoms[j];
}
s -= 6;
var re = media + (s * estDesv);
r.push(re);
}
return r;
}
function generarPoisson(lambda, n) {
var r = [];
for (var i = 0; i < n; i++) {
var p = 1;
var x = -1;
var a = Math.exp(-lambda);
do {
var u = getRNG();
p = p * u;
x++;
} while (p >= a);
r.push(x);
}
return r;
}
function generarExponencialMedia(media, n) {
var r = [];
for (var i = 0; i < n; i++) {
var random = getRNG();
var x = -media;
x *= Math.log(1 - random);
r.push(x);
}
return r;
}
function generarExponencialLambda(lambda, n) {
return generarExponencialMedia(1 / lambda, n);
}
export { generarUniforme, generarNormalBoxMuller, generarNormalConvolucion, generarPoisson, generarExponencialMedia, generarExponencialLambda }