2022-08-29 14:25:30 +00:00
|
|
|
// stm: #unit
|
2020-06-27 12:39:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-06-30 00:11:54 +00:00
|
|
|
func TestMeanVar(t *testing.T) {
|
2020-06-27 12:39:42 +00:00
|
|
|
N := 16
|
2020-06-30 00:11:54 +00:00
|
|
|
ss := make([]*meanVar, N)
|
2020-06-29 22:31:05 +00:00
|
|
|
rng := rand.New(rand.NewSource(1))
|
2020-06-27 12:39:42 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2020-06-30 00:11:54 +00:00
|
|
|
ss[i] = &meanVar{}
|
2020-06-29 22:31:05 +00:00
|
|
|
maxJ := rng.Intn(1000)
|
2020-06-27 12:39:42 +00:00
|
|
|
for j := 0; j < maxJ; j++ {
|
2020-06-29 22:31:05 +00:00
|
|
|
ss[i].AddPoint(rng.NormFloat64()*5 + 500)
|
2020-06-27 12:39:42 +00:00
|
|
|
}
|
2020-06-30 00:11:54 +00:00
|
|
|
t.Logf("mean: %f, stddev: %f, count %f", ss[i].mean, ss[i].Stddev(), ss[i].n)
|
2020-06-27 12:39:42 +00:00
|
|
|
}
|
2020-06-30 00:11:54 +00:00
|
|
|
out := &meanVar{}
|
2020-06-27 12:39:42 +00:00
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
out.Combine(ss[i])
|
2020-06-30 00:11:54 +00:00
|
|
|
t.Logf("combine: mean: %f, stddev: %f", out.mean, out.Stddev())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCovar(t *testing.T) {
|
|
|
|
N := 16
|
|
|
|
ss := make([]*covar, N)
|
|
|
|
rng := rand.New(rand.NewSource(1))
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
ss[i] = &covar{}
|
|
|
|
maxJ := rng.Intn(1000) + 500
|
|
|
|
for j := 0; j < maxJ; j++ {
|
|
|
|
x := rng.NormFloat64()*5 + 500
|
2020-06-30 12:21:48 +00:00
|
|
|
ss[i].AddPoint(x, x*2-1000)
|
2020-06-30 00:11:54 +00:00
|
|
|
}
|
2020-06-30 12:21:48 +00:00
|
|
|
t.Logf("corell: %f, y = %f*x+%f @%.0f", ss[i].Correl(), ss[i].A(), ss[i].B(), ss[i].n)
|
|
|
|
t.Logf("\txVar: %f yVar: %f covar: %f", ss[i].StddevX(), ss[i].StddevY(), ss[i].Covariance())
|
2020-06-30 00:11:54 +00:00
|
|
|
}
|
|
|
|
out := &covar{}
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
out.Combine(ss[i])
|
2020-06-30 12:21:48 +00:00
|
|
|
t.Logf("combine: corell: %f, y = %f*x+%f", out.Correl(), out.A(), out.B())
|
|
|
|
t.Logf("\txVar: %f yVar: %f covar: %f", out.StddevX(), out.StddevY(), out.Covariance())
|
2020-06-27 12:39:42 +00:00
|
|
|
}
|
|
|
|
}
|