2023-02-24 23:50:13 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
2023-02-27 02:16:43 +01:00
|
|
|
"os/exec"
|
2023-02-24 23:50:13 +01:00
|
|
|
|
|
|
|
"git.dotya.ml/wanderer/ak9im/p2/stats"
|
|
|
|
)
|
|
|
|
|
2023-02-27 02:16:43 +01:00
|
|
|
var (
|
|
|
|
datafile = flag.String("datafile", "", "read data from this file")
|
|
|
|
vis = flag.Bool("vis", false, "run 'python visualise.py' to produce visualisations")
|
|
|
|
)
|
2023-02-24 23:50:13 +01:00
|
|
|
|
|
|
|
func run() error {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *datafile != "" {
|
2023-02-25 14:13:56 +01:00
|
|
|
data, err := readFile(datafile)
|
2023-02-24 23:50:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-25 14:08:38 +01:00
|
|
|
u := 0
|
|
|
|
y := 1
|
|
|
|
|
2023-02-24 23:50:13 +01:00
|
|
|
meanU := stats.Mean(data[u])
|
|
|
|
meanY := stats.Mean(data[y])
|
|
|
|
varianceU := stats.Variance(data[u])
|
|
|
|
varianceY := stats.Variance(data[y])
|
|
|
|
|
2023-02-25 13:23:47 +01:00
|
|
|
maxShift := 0.1
|
2023-02-25 13:50:28 +01:00
|
|
|
autocorrelationU := stats.Autocorrelate(data[u], maxShift)
|
|
|
|
autocorrelationY := stats.Autocorrelate(data[y], maxShift)
|
2023-02-25 13:23:47 +01:00
|
|
|
|
2023-02-25 14:08:38 +01:00
|
|
|
mutCorrelationUY, err := stats.MutCorrelate(data[u], data[y], maxShift)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-25 14:29:55 +01:00
|
|
|
mutCorrelationYU, err := stats.MutCorrelate(data[y], data[u], maxShift)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-26 17:39:39 +01:00
|
|
|
cov := stats.Covariance(data[u], data[y])
|
|
|
|
|
2023-02-27 04:09:01 +01:00
|
|
|
// impulseFunc, err := stats.ImpulseFunction(autocorrelationU, mutCorrelationUY)
|
|
|
|
impulseFunc, err := stats.ImpulseFunction(autocorrelationU, autocorrelationY)
|
2023-02-26 21:20:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-25 13:23:47 +01:00
|
|
|
log.Printf("len(data): %d", len(data[u]))
|
2023-02-24 23:50:13 +01:00
|
|
|
|
|
|
|
log.Printf("means - u: %v, y: %v", meanU, meanY)
|
|
|
|
|
|
|
|
log.Printf("variance - u: %v, y: %v", varianceU, varianceY)
|
2023-02-25 13:23:47 +01:00
|
|
|
|
|
|
|
log.Printf("len(autocorrelationU): %d", len(autocorrelationU))
|
|
|
|
log.Printf("autocorrelationU: %v", autocorrelationU)
|
|
|
|
log.Printf("autocorrelationY: %v", autocorrelationY)
|
2023-02-25 14:08:38 +01:00
|
|
|
log.Printf("mutual correlation U,Y: %v", mutCorrelationUY)
|
2023-02-25 14:29:55 +01:00
|
|
|
log.Printf("mutual correlation Y,U: %v", mutCorrelationYU)
|
2023-02-26 17:39:39 +01:00
|
|
|
log.Printf("covariance U,Y: %v", cov)
|
2023-02-27 04:08:01 +01:00
|
|
|
log.Printf("correlation U,Y: %v", stats.Correlation(data[u], data[y]))
|
2023-02-25 21:55:09 +01:00
|
|
|
|
2023-02-26 21:20:40 +01:00
|
|
|
log.Printf("len(impulseFunc): %d", len(impulseFunc))
|
|
|
|
log.Printf("impulseFunc: %v", impulseFunc)
|
|
|
|
|
2023-02-25 21:55:09 +01:00
|
|
|
err = saveStuff(
|
|
|
|
meanU, meanY,
|
|
|
|
varianceU, varianceY,
|
2023-02-26 17:39:39 +01:00
|
|
|
cov,
|
2023-02-25 21:55:09 +01:00
|
|
|
autocorrelationU, autocorrelationY,
|
|
|
|
mutCorrelationUY, mutCorrelationYU,
|
2023-02-26 21:20:40 +01:00
|
|
|
impulseFunc,
|
2023-02-25 21:55:09 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-24 23:50:13 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 02:16:43 +01:00
|
|
|
if *vis {
|
|
|
|
err := visualise()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func visualise() error {
|
|
|
|
red := "\033[31m"
|
|
|
|
cyan := "\033[36m"
|
|
|
|
reset := "\033[0m"
|
|
|
|
f := "visualise.py"
|
|
|
|
|
|
|
|
log.Printf("running %s`python %s`%s", cyan, f, reset)
|
|
|
|
|
|
|
|
out, err := exec.Command("python", f).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("visualise failed with:\n\n%s%s%s\n", red, out, reset)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:50:13 +01:00
|
|
|
return nil
|
|
|
|
}
|