-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeta_test.jule
More file actions
147 lines (134 loc) · 2.6 KB
/
beta_test.jule
File metadata and controls
147 lines (134 loc) · 2.6 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
// Copyright 2025 mertcandav.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.
use "std/math"
use "std/testing"
struct betaTest {
p: f64
q: f64
want: f64
}
let testsBeta: []betaTest = [
{
p: 1,
q: 2,
want: 0.5, // obtained from scipy.special.beta(1,2) (version=0.18.0)
},
{
p: 10,
q: 20,
want: 4.9925087406346778e-09, // obtained from scipy.special.beta(10,20) (version=0.18.0)
},
{
p: +0,
q: 10,
want: math::Inf(+1),
},
{
p: -0,
q: 10,
want: math::Inf(+1),
},
{
p: 0,
q: 0,
want: math::NaN(),
},
{
p: 0,
q: math::Inf(-1),
want: math::NaN(),
},
{
p: 10,
q: math::Inf(-1),
want: math::NaN(),
},
{
p: 0,
q: math::Inf(+1),
want: math::NaN(),
},
{
p: 10,
q: math::Inf(+1),
want: math::NaN(),
},
{
p: math::NaN(),
q: 10,
want: math::NaN(),
},
{
p: math::NaN(),
q: 0,
want: math::NaN(),
},
{
p: -1,
q: 0,
want: math::NaN(),
},
{
p: -1,
q: +1,
want: math::NaN(),
},
]
#test
fn testBeta(t: &testing::T) {
for i, test in testsBeta {
v := Beta(test.p, test.q)
if !(Tolerance(v, test.want, 1e-12) ||
(math::IsNaN(test.want) && math::IsNaN(v))) {
t.Errorf("test #{}: Beta({}, {})={}. want={}",
i, test.p, test.q, v, test.want)
}
u := Beta(test.q, test.p)
if !(Tolerance(u, test.want, 1e-12) ||
(math::IsNaN(test.want) && math::IsNaN(u))) {
t.Errorf("test #{}: Beta({}, {})={}. want={}",
i, test.q, test.p, u, test.want)
}
if math::IsInf(v, +1) || math::IsNaN(v) {
continue
}
vv := Beta(test.p, test.q+1)
uu := Beta(test.p+1, test.q)
if !Tolerance(v, vv+uu, 1e-12) {
t.Errorf("test #{}: Beta(p,q) != Beta(p,q+1)+Beta(p+1,q)")
}
vbeta2 := beta2(test.p, test.q)
if !Tolerance(v, vbeta2, 1e-12) {
t.Errorf("test #{}: Beta(p,q) != Beta2(p,q)")
}
}
}
fn beta2(x: f64, y: f64): f64 {
ret math::Gamma(x) * math::Gamma(y) / math::Gamma(x+y)
}
#test
fn testLbeta(t: &testing::T) {
for i, test in testsBeta {
want := math::Log(test.want)
v := Lbeta(test.p, test.q)
if !(Tolerance(v, want, 1e-15) ||
(math::IsNaN(want) && math::IsNaN(v))) {
t.Errorf("test #{}: Lbeta({}, {})={}. want={}",
i, test.p, test.q, v, want)
}
u := Lbeta(test.q, test.p)
if !(Tolerance(u, want, 1e-15) ||
(math::IsNaN(want) && math::IsNaN(u))) {
t.Errorf("test #{}: Lbeta({}, {})={}. want={}",
i, test.q, test.p, u, want)
}
if math::IsInf(v, +1) || math::IsNaN(v) {
continue
}
vbeta2 := math::Log(beta2(test.p, test.q))
if !Tolerance(v, vbeta2, 1e-15) {
t.Errorf("test #{}: Lbeta(p,q) != Log(Beta2(p,q))")
}
}
}