Made the Go implementation a library. Test is pathetic but it's a start.

master
Zed A. Shaw 1 month ago
parent cb49aae8e7
commit 185c64686f
  1. 5
      go/magic_stats/Makefile
  2. 34
      go/magic_stats/welford.go
  3. 40
      go/magic_stats/welford_test.go

@ -1,10 +1,7 @@
R_SCRIPT=C:/Users/lcthw/AppData/Local/Programs/R/R-4.6.0/bin/Rscript.exe R_SCRIPT=C:/Users/lcthw/AppData/Local/Programs/R/R-4.6.0/bin/Rscript.exe
all:
go build .
test: test:
./magic_stats go test
r_test: test r_test: test
$(R_SCRIPT) ../../cpp/tests/confirm.r $(R_SCRIPT) ../../cpp/tests/confirm.r

@ -3,9 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"math" "math"
"math/rand/v2"
"os"
"log"
) )
type Welford struct { type Welford struct {
@ -97,34 +94,3 @@ func (stats *Welford) TTest(second *Welford) TTest {
return TTest{t_stat, dof, p_val} return TTest{t_stat, dof, p_val}
} }
func main() {
var welf Welford
var welf2 Welford
skew_factor := rand.Float64() * 10
samples_file, err := os.Create("samples.txt")
if err != nil { log.Fatal(err) }
defer samples_file.Close()
for i := 0; i < 100; i++ {
sample := rand.Float64() * 100
welf.Sample(sample)
welf2.Sample(sample + skew_factor)
sample_line := fmt.Sprintf("%f %f\n", sample, sample + skew_factor)
samples_file.Write([]byte(sample_line))
}
welf.Dump()
welf2.Dump()
result := welf.TTest(&welf2)
fmt.Println(
"----\nTTest Results:\nMean:", welf.Mean,
"\nVariance: ", welf.Variance(),
"\nStddev: ", welf.StdDev(),
"\nTStat: ", result.TStat,
"\nDOF: ", result.DOF,
"\nPVal: ", result.PVal);
}

@ -0,0 +1,40 @@
package main
import (
"fmt"
"math/rand/v2"
"os"
"log"
"testing"
)
func TestBasicWelford(t *testing.T) {
var welf Welford
var welf2 Welford
skew_factor := rand.Float64() * 10
samples_file, err := os.Create("samples.txt")
if err != nil { log.Fatal(err) }
defer samples_file.Close()
for i := 0; i < 100; i++ {
sample := rand.Float64() * 100
welf.Sample(sample)
welf2.Sample(sample + skew_factor)
sample_line := fmt.Sprintf("%f %f\n", sample, sample + skew_factor)
samples_file.Write([]byte(sample_line))
}
welf.Dump()
welf2.Dump()
result := welf.TTest(&welf2)
fmt.Println(
"----\nTTest Results:\nMean:", welf.Mean,
"\nVariance: ", welf.SampleVariance(),
"\nStddev: ", welf.StdDev(),
"\nTStat: ", result.TStat,
"\nDOF: ", result.DOF,
"\nPVal: ", result.PVal);
}
Loading…
Cancel
Save