-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodelutils.go
78 lines (72 loc) · 1.6 KB
/
modelutils.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
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
package gophy
import (
"gonum.org/v1/gonum/mat"
"gonum.org/v1/gonum/mathext"
"gonum.org/v1/gonum/stat/distuv"
)
func sumMatrix(m *mat.Dense) (s float64) {
r, _ := m.Dims() // assumes square
s = 0
for i := 0; i < r; i++ {
for j := 0; j < r; j++ {
s += m.At(i, j)
}
}
return
}
func sumRow(m *mat.Dense, i int) (s float64) {
r, _ := m.Dims()
s = 0
for j := 0; j < r; j++ {
s += m.At(i, j)
}
return
}
//LikeResult likelihood value and site
type LikeResult struct {
value float64
site int
}
type LikeSupResult struct {
value *SupFlo
site int
}
func pointGamma(p float64, a float64, b float64) float64 {
c := distuv.ChiSquared{K: 2 * (a)}
return ((c.Quantile(p)) / (2 * b))
}
// GetGammaCats for likelihood calculators
func GetGammaCats(alpha float64, cats int, median bool) []float64 {
K := float64(cats)
a, b := alpha, alpha
factor := a / b * K
rK := make([]float64, cats)
if median == true {
gap05 := 1.0 / (2.0 * K)
for i := 0; i < cats; i++ {
rK[i] = pointGamma((float64(i)*2.0+1.)*gap05, a, b)
}
t := 0.
for i := 0; i < cats; i++ {
t += rK[i]
}
for i := 0; i < cats; i++ {
rK[i] *= factor / t
}
return rK
} else {
freqK := make([]float64, cats)
for i := 0; i < cats-1; i++ {
freqK[i] = pointGamma((float64(i)+1.0)/K, a, b)
}
for i := 0; i < cats-1; i++ {
freqK[i] = mathext.GammaIncReg(a+1, freqK[i]*b)
}
rK[0] = freqK[0] * factor
rK[cats-1] = (1 - freqK[cats-2]) * factor
for i := 1; i < cats-1; i++ {
rK[i] = (freqK[i] - freqK[i-1]) * factor
}
return rK
}
}