diff --git a/p2/run.go b/p2/run.go index 28a68f0..19faa25 100644 --- a/p2/run.go +++ b/p2/run.go @@ -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 diff --git a/p2/stats/stats.go b/p2/stats/stats.go index 184a66e..1911764 100644 --- a/p2/stats/stats.go +++ b/p2/stats/stats.go @@ -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 +}