-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.js
More file actions
85 lines (73 loc) · 2.04 KB
/
1.js
File metadata and controls
85 lines (73 loc) · 2.04 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
function distribution_N(mean, s, min, max){
var res = min-1
while (res < min || res > max){
u = 1 - Math.random(); // Subtraction to flip [0, 1) to (0, 1].
v = 1 - Math.random();
res = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
res = s*res + mean
}
return res
}
function distribution_U(a,b){
if (a > b)
return "a > b";
var res = Math.random() * (b - a) + a;
return res
}
function distribution_exp(a){
var u = Math.random();
var res = -1/a * Math.log(1 - u);
return res
}
function distribution_point(a){
return a;
}
function sample(my_dis){ // build one element of sample with distribution my_dis
if (typeof(my_dis[0]) == 'string'){
if (my_dis[0] == 'N')
return distribution_N(my_dis[1], my_dis[2], my_dis[3], my_dis[4])
if (my_dis[0] == 'p')
return distribution_point(my_dis[1])
if (my_dis[0] == 'e'){
return distribution_exp(my_dis[1])
}
if (my_dis[0] == 'U')
return distribution_U(my_dis[1], my_dis[2])
}
else
var rand = Math.random() * 100;
t = 0
for (i = 0; i < my_dis.length; i++ ){
t += my_dis[i][0];
if (t > rand){ //define part of distribution
return sample(my_dis[i][1]);
}
}
}
function test1(){ // check how many value in [mean-delta,mean+delta]
var delta = 0.2;
tail = 0
for (i = 0; i < 100; i++){ //size of sample = 100
t = distribution_N(0,1, -1, 1);
if (Math.abs(t) > delta)
tail++;
}
console.assert(tail<20, "tails is too mach in Natural distribution")
}
function test2(){
var a = Math.random();
var b = Math.random();
if (a > b)
t = distribution_U(b,a);
else
t = distribution_U(a,b);
console.assert(a >= b, "out of bound in Uniform distribution")
}
function example(){
// The first argument is percents, the second is type of distribution
// and then parametrs [%, ['type of distribution', parametrs]]
distribution0 = [[70, ['p', 0]], [25, ['N',1, 0.3, 0.5, 1.5]], [5, ['U', 2, 5]]] //example from problem 1
console.log(sample(distribution0));
distribution1 = [[50, distribution0], [50, ['U', 1, 2]]]
console.log(sample(distribution1));
}