Calculate correlation

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-06-30 14:21:48 +02:00
parent 9ccd25b072
commit 4dd79ec6fa
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
2 changed files with 8 additions and 6 deletions

View File

@ -321,8 +321,8 @@ func (cov1 *covar) A() float64 {
func (cov1 *covar) B() float64 { func (cov1 *covar) B() float64 {
return cov1.meanY - cov1.meanX*cov1.A() return cov1.meanY - cov1.meanX*cov1.A()
} }
func (cov1 *covar) Coerrel() float64 { func (cov1 *covar) Correl() float64 {
return cov1.Covariance() / cov1.VarianceX() / cov1.VarianceY() return cov1.Covariance() / cov1.StddevX() / cov1.StddevY()
} }
type meanVar struct { type meanVar struct {
@ -586,7 +586,7 @@ var importAnalyzeCmd = &cli.Command{
fmt.Printf("%s: incr by %f~%f; tt %f~%f\n", k, s.gasRatio.Mean(), s.gasRatio.Stddev(), fmt.Printf("%s: incr by %f~%f; tt %f~%f\n", k, s.gasRatio.Mean(), s.gasRatio.Stddev(),
s.timeTaken.Mean(), s.timeTaken.Stddev()) s.timeTaken.Mean(), s.timeTaken.Stddev())
if s.extraCovar != nil { if s.extraCovar != nil {
fmt.Printf("\t correll: %f, tt = %f * extra + %f\n", s.extraCovar.Coerrel(), fmt.Printf("\t correll: %f, tt = %f * extra + %f\n", s.extraCovar.Correl(),
s.extraCovar.A(), s.extraCovar.B()) s.extraCovar.A(), s.extraCovar.B())
fmt.Printf("\t extra: %f~%f\n", s.extraCovar.meanX, s.extraCovar.StddevX()) fmt.Printf("\t extra: %f~%f\n", s.extraCovar.meanX, s.extraCovar.StddevX())
} }

View File

@ -33,13 +33,15 @@ func TestCovar(t *testing.T) {
maxJ := rng.Intn(1000) + 500 maxJ := rng.Intn(1000) + 500
for j := 0; j < maxJ; j++ { for j := 0; j < maxJ; j++ {
x := rng.NormFloat64()*5 + 500 x := rng.NormFloat64()*5 + 500
ss[i].AddPoint(x, x*(rng.NormFloat64()/10+2)-rng.NormFloat64()-1000) ss[i].AddPoint(x, x*2-1000)
} }
t.Logf("covar: %f, y = %f*x+%f @%.0f", ss[i].Covariance(), ss[i].A(), ss[i].B(), ss[i].n) 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())
} }
out := &covar{} out := &covar{}
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
out.Combine(ss[i]) out.Combine(ss[i])
t.Logf("combine: covar: %f, y = %f*x+%f", ss[i].Covariance(), ss[i].A(), ss[i].B()) 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())
} }
} }