1
0
Fork 0
mirror of https://github.com/Luzifer/webcheck.git synced 2024-11-08 22:50:01 +00:00
webcheck/vendor/github.com/montanaflynn/stats/correlation.go
Knut Ahlers 2fbabd6bd4
Vendor dependencies
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-07-23 14:54:12 +02:00

33 lines
742 B
Go

package stats
import "math"
// Correlation describes the degree of relationship between two sets of data
func Correlation(data1, data2 Float64Data) (float64, error) {
l1 := data1.Len()
l2 := data2.Len()
if l1 == 0 || l2 == 0 {
return math.NaN(), EmptyInput
}
if l1 != l2 {
return math.NaN(), SizeErr
}
sdev1, _ := StandardDeviationPopulation(data1)
sdev2, _ := StandardDeviationPopulation(data2)
if sdev1 == 0 || sdev2 == 0 {
return 0, nil
}
covp, _ := CovariancePopulation(data1, data2)
return covp / (sdev1 * sdev2), nil
}
// Pearson calculates the Pearson product-moment correlation coefficient between two variables.
func Pearson(data1, data2 Float64Data) (float64, error) {
return Correlation(data1, data2)
}