-
Notifications
You must be signed in to change notification settings - Fork 12
/
chisq.go
53 lines (45 loc) · 1.16 KB
/
chisq.go
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
// Chi-Squared distribution
package stat
import (
. "github.com/ematvey/go-fn/fn"
)
func Xsquare_PDF(n int64) func(x float64) float64 {
k := float64(n) / 2
normalization := pow(0.5, k) / Γ(k)
return func(x float64) float64 {
return normalization * pow(x, k-1) * NextExp(-x/2)
}
}
func Xsquare_LnPDF(n int64) func(x float64) float64 {
k := float64(n) / 2
normalization := log(0.5)*k - LnΓ(k)
return func(x float64) float64 {
return normalization + log(x)*(k-1) - x/2
}
}
//Xsquare(n) => sum of n N(0,1)^2
func NextXsquare(n int64) (x float64) {
for i := iZero; i < n; i++ {
n := NextNormal(0, 1)
x += n * n
}
return
}
func Xsquare(n int64) func() float64 {
return func() float64 {
return NextXsquare(n)
}
}
//Cumulative density function of the Chi-Squared distribution
func Xsquare_CDF(n int64) func(p float64) float64 {
return func(p float64) float64 {
return Γr(float64(n)/2, p/2)
}
}
//Inverse CDF (Quantile) function of the Chi-Squared distribution
func Xsquare_InvCDF(n int64) func(p float64) float64 {
return func(p float64) float64 {
//return Gamma_InvCDF_At(n/2, 2, p) to be implemented
return Gamma_InvCDF_For(float64(n)/2, 2, p)
}
}