-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbeta-test.rb
93 lines (85 loc) · 2.28 KB
/
rbeta-test.rb
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
# -*- coding: utf-8 -*-
require 'test/unit'
require 'rbeta'
require 'logger'
class TestRBeta < Test::Unit::TestCase
UseGSL = false
def test_argment_exception()
[[0,0], [0,1], [1,0]].each do |arg|
assert_raise(RuntimeError) do
RBetaQ.new(arg[0],arg[1])
end
end
end
def test_argment_one()
[[1,1], [1,2], [2,1]].each do |arg|
assert_nothing_raised() do
rb = RBetaQ.new(arg[0],arg[1])
puts [arg, rb.rand].join(",")
end
end
end
def test_argment_normal()
[[2,2], [2,3], [3,2]].each do |arg|
assert_nothing_raised() do
rb = RBetaQ.new(arg[0],arg[1])
puts [arg, rb.rand].join(",")
end
end
end
def test_argment_inf()
[[RBetaQ::INF,RBetaQ::INF], [2,RBetaQ::INF], [RBetaQ::INF,2]].each do |arg|
assert_nothing_raised() do
rb = RBetaQ.new(arg[0],arg[1])
puts [arg, rb.rand].join(",") # 要確認
end
end
end
def test_plot_and_accuracy()
list = [
[2.7, 6.3],
[1, 1],
[1, 2],
[2, 1]
]
rep = 100000
div = 20
list.each do |ab|
alpha = ab[0]
beta = ab[1]
hist = Array.new(div,0)
rb = RBetaQ.new(alpha,beta)
for i in 0...rep do
x = rb.rand()
idx = (x*div).to_i
hist[idx]+=1
end
lower = 0
for i in 0...div do
if UseGSL
# https://gnu.org/software/gsl/manual/gsl-ref.html#The-Beta-Distribution
upper = GSL::Ran::beta_pdf((i+1.0)/div,alpha,beta)
mid = GSL::Ran::beta_pdf((i+0.5)/div,alpha,beta)
else
# https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Beta.html
upper = @@r.dbeta((i+1.0)/div,alpha,beta)
mid = @@r.dbeta((i+0.5)/div,alpha,beta)
end
expect = (lower+2*mid+upper)/4
expect_max = [lower, mid, upper].max
expect_min = [lower, mid, upper].min
STDERR.puts i.to_s + " = " + hist[i].to_s + "(" + (expect*rep/div).to_s + ")\n"
assert(hist[i] > expect_min*0.95*rep/div-1)
assert(hist[i] < expect_max*1.05*rep/div+1)
lower = upper
end
assert(hist.reduce(:+) == rep)
end
end
end
if TestRBeta::UseGSL
require 'gsl'
else
require 'rsruby'
@@r = RSRuby.instance
end