-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbeta-test.php
256 lines (232 loc) · 6.88 KB
/
rbeta-test.php
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
<?php
require_once 'rbeta.php';
class RBetaTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_00()
{
rbeta(0,0);
}
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_01()
{
rbeta(0,1);
}
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_10()
{
rbeta(1,0);
}
public function test_argment_I2()
{
print rbeta(INF,2);
}
public function test_argment_2I()
{
print rbeta(2,INF);
}
public function test_plot_and_accuracy()
{
$list = array(
array(2.7, 6.3),
array(1, 1),
array(1, 2),
array(2, 1));
$rep = 100000;
$div = 20;
foreach ($list as $ab) {
$alpha = $ab[0];
$beta = $ab[1];
$hist = array_fill(0,$div,0);
for ($i=0; $i<$rep; $i++) {
$x = rbeta($alpha,$beta);
$idx = (int) ($x*$div) ;
$hist[$idx]++;
}
$lower = 0;
for ($i=0; $i<$div; $i++) {
$upper = stats_dens_beta(($i+1)/$div,$alpha,$beta);
$mid = stats_dens_beta(($i+0.5)/$div,$alpha,$beta);
$expect = ($lower+2*$mid+$upper)/4;
$expect_max = max($lower, $mid, $upper);
$expect_min = min($lower, $mid, $upper);
print $i." = " . $hist[$i] . "(".($expect*$rep/$div).")\n";
$this->assertLessThan($hist[$i], $expect_min*0.95*$rep/$div-1);
$this->assertGreaterThan($hist[$i], $expect_max*1.05*$rep/$div+1);
$lower = $upper;
}
}
}
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_00_class()
{
$rb = new RBeta(0,0);
$rb->rand();
}
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_01_class()
{
$rb = new RBeta(0,1);
$rb->rand();
}
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_10_class()
{
$rb = new RBeta(1,0);
$rb->rand();
}
public function test_argment_I2_class()
{
$rb = new RBeta(INF,2);
print $rb->rand();
}
public function test_argment_2I_class()
{
$rb = new RBeta(2,INF);
print $rb->rand();
}
public function test_plot_and_accuracy_class()
{
$list = array(
array(2.7, 6.3),
array(1, 1),
array(1, 2),
array(2, 1));
$rep = 100000;
$div = 20;
foreach ($list as $ab) {
$alpha = $ab[0];
$beta = $ab[1];
$hist = array_fill(0,$div,0);
$rb = new RBeta($alpha,$beta);
for ($i=0; $i<$rep; $i++) {
$x = $rb->rand();
$idx = (int) ($x*$div) ;
$hist[$idx]++;
}
$lower = 0;
for ($i=0; $i<$div; $i++) {
$upper = stats_dens_beta(($i+1)/$div,$alpha,$beta);
$mid = stats_dens_beta(($i+0.5)/$div,$alpha,$beta);
$expect = ($lower+2*$mid+$upper)/4;
$expect_max = max($lower, $mid, $upper);
$expect_min = min($lower, $mid, $upper);
print $i." = " . $hist[$i] . "(".($expect*$rep/$div).")\n";
$this->assertLessThan($hist[$i], $expect_min*0.95*$rep/$div-1);
$this->assertGreaterThan($hist[$i], $expect_max*1.05*$rep/$div+1);
$lower = $upper;
}
}
}
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_00_classq()
{
$rb = new RBetaQ(0,0);
$rb->rand();
}
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_01_classq()
{
$rb = new RBetaQ(0,1);
$rb->rand();
}
/**
* @expectedException InvalidArgumentException
*/
public function test_argment_10_classq()
{
$rb = new RBetaQ(1,0);
$rb->rand();
}
public function test_argment_I2_classq()
{
$rb = new RBetaQ(INF,2);
print $rb->rand();
}
public function test_argment_2I_classq()
{
$rb = new RBetaQ(2,INF);
print $rb->rand();
}
public function test_plot_and_accuracy_classq()
{
$list = array(
array(2.7, 6.3),
array(1, 1),
array(1, 2),
array(2, 1));
$rep = 100000;
$div = 20;
foreach ($list as $ab) {
$alpha = $ab[0];
$beta = $ab[1];
$hist = array_fill(0,$div,0);
$rb = new RBetaQ($alpha,$beta);
for ($i=0; $i<$rep; $i++) {
$x = $rb->rand();
$idx = (int) ($x*$div) ;
$hist[$idx]++;
}
$lower = 0;
for ($i=0; $i<$div; $i++) {
$upper = stats_dens_beta(($i+1)/$div,$alpha,$beta);
$mid = stats_dens_beta(($i+0.5)/$div,$alpha,$beta);
$expect = ($lower+2*$mid+$upper)/4;
$expect_max = max($lower, $mid, $upper);
$expect_min = min($lower, $mid, $upper);
print $i." = " . $hist[$i] . "(".($expect*$rep/$div).")\n";
$this->assertLessThan($hist[$i], $expect_min*0.95*$rep/$div-1);
$this->assertGreaterThan($hist[$i], $expect_max*1.05*$rep/$div+1);
$lower = $upper;
}
}
}
public function test_plot_and_accuracy_class_func()
{
$list = array(
array(2.7, 6.3),
array(1, 1),
array(1, 2),
array(2, 1));
$rep = 100000;
$div = 20;
foreach ($list as $ab) {
$alpha = $ab[0];
$beta = $ab[1];
$hist = array_fill(0,$div,0);
for ($i=0; $i<$rep; $i++) {
$x = rbeta_class($alpha,$beta);
$idx = (int) ($x*$div) ;
$hist[$idx]++;
}
$lower = 0;
for ($i=0; $i<$div; $i++) {
$upper = stats_dens_beta(($i+1)/$div,$alpha,$beta);
$mid = stats_dens_beta(($i+0.5)/$div,$alpha,$beta);
$expect = ($lower+2*$mid+$upper)/4;
$expect_max = max($lower, $mid, $upper);
$expect_min = min($lower, $mid, $upper);
print $i." = " . $hist[$i] . "(".($expect*$rep/$div).")\n";
$this->assertLessThan($hist[$i], $expect_min*0.95*$rep/$div-1);
$this->assertGreaterThan($hist[$i], $expect_max*1.05*$rep/$div+1);
$lower = $upper;
}
}
}
}