add MutCorrelate func, get mutual correlation

This commit is contained in:
leo 2023-02-25 14:08:38 +01:00
parent d7f4db9205
commit b81c38dae4
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 37 additions and 3 deletions

@ -21,14 +21,14 @@ func run() error {
defer f.Close()
u := 0
y := 1
data, err := readData(f)
if err != nil {
return err
}
u := 0
y := 1
meanU := stats.Mean(data[u])
meanY := stats.Mean(data[y])
varianceU := stats.Variance(data[u])
@ -38,6 +38,11 @@ func run() error {
autocorrelationU := stats.Autocorrelate(data[u], maxShift)
autocorrelationY := stats.Autocorrelate(data[y], maxShift)
mutCorrelationUY, err := stats.MutCorrelate(data[u], data[y], maxShift)
if err != nil {
return err
}
log.Printf("len(data): %d", len(data[u]))
log.Printf("means - u: %v, y: %v", meanU, meanY)
@ -47,6 +52,7 @@ func run() error {
log.Printf("len(autocorrelationU): %d", len(autocorrelationU))
log.Printf("autocorrelationU: %v", autocorrelationU)
log.Printf("autocorrelationY: %v", autocorrelationY)
log.Printf("mutual correlation U,Y: %v", mutCorrelationUY)
}
return nil

@ -1,6 +1,7 @@
package stats
import (
"errors"
"math"
"gonum.org/v1/gonum/stat"
@ -61,3 +62,30 @@ func AutocorrelateMP(f []float64, maxShift float64) []float64 {
return v
}
func MutCorrelate(f1, f2 []float64, maxShift float64) ([]float64, error) {
fLen := len(f1)
if fLen != len(f2) {
return []float64{}, errors.New("arrays passed are of different lengths")
}
m := float64(fLen) * maxShift
v := make([]float64, 0, int(m)*4)
currentShift := 0
for m >= float64(currentShift) {
var r float64
for i := 0; i < fLen-currentShift; i++ {
r += f1[i] * f2[i+currentShift]
}
r *= 1 / float64(fLen-currentShift)
v = append(v, r)
currentShift++
}
return v, nil
}