A first functional version of obiclean

This commit is contained in:
2022-08-20 18:01:07 +02:00
parent a07d348aea
commit 5dd835d3e7
16 changed files with 1091 additions and 8 deletions

27
pkg/obistats/minmax.go Normal file
View File

@ -0,0 +1,27 @@
package obistats
func Max[T int64 | int32 | int16 | int8 | int | float32 | float64] (data []T) T {
m := data[0]
for _,v := range data {
if v > m {
m = v
}
}
return m
}
func Min[T int64 | int32 | int16 | int8 | int | float32 | float64] (data []T) T {
m := data[0]
for _,v := range data {
if v < m {
m = v
}
}
return m
}

49
pkg/obistats/utils.go Normal file
View File

@ -0,0 +1,49 @@
package obistats
import (
log "github.com/sirupsen/logrus"
"math"
)
// Lchoose returns logarithms of binomial coefficients
func Lchoose(n,x int) float64 {
fn := float64(n)
fx := float64(x)
ln1, _ := math.Lgamma(fn + 1.0)
lx1, _ := math.Lgamma(fx + 1.0)
lnx1, _ := math.Lgamma(fn - fx + 1.0)
return ln1 - lx1 - lnx1
}
func Choose(n,x int) float64 {
return math.Exp(Lchoose(x,n))
}
func LogAddExp(x, y float64) float64 {
tmp := x - y
if tmp > 0 {
return x + math.Log1p(math.Exp(-tmp))
} else if tmp <= 0 {
return y + math.Log1p(math.Exp(tmp))
} else {
// Nans, or infinities of the same sign involved
log.Errorf("logaddexp %f %f", x, y)
return x + y
}
}