feature: add skew, covariance and variance

This commit is contained in:
zenix 2022-06-21 15:38:46 +09:00
parent 1e31c4fb04
commit d8d77cec1e
2 changed files with 75 additions and 0 deletions

View File

@ -733,4 +733,51 @@ func Correlation(a Series, b Series, length int, method ...CorrFunc) float64 {
return runner(a, b, length)
}
// similar to pandas.Series.cov() function with ddof=0
//
// Compute covariance with Series
func Covariance(a Series, b Series, length int) float64 {
if a.Length() < length {
length = a.Length()
}
if b.Length() < length {
length = b.Length()
}
meana := Mean(a, length)
meanb := Mean(b, length)
sum := 0.0
for i := 0; i < length; i++ {
sum += (a.Index(i) - meana) * (b.Index(i) - meanb)
}
sum /= float64(length)
return sum
}
func Variance(a Series, length int) float64 {
return Covariance(a, a, length)
}
// similar to pandas.Series.skew() function.
//
// Return unbiased skew over input series
func Skew(a Series, length int) float64 {
if length > a.Length() {
length = a.Length()
}
mean := Mean(a, length)
sum2 := 0.0
sum3 := 0.0
for i := 0; i < length; i++ {
diff := a.Index(i) - mean
sum2 += diff * diff
sum3 += diff * diff * diff
}
if length <= 2 || sum2 == 0 {
return math.NaN()
}
l := float64(length)
return l * math.Sqrt(l-1) / (l - 2) * sum3 / math.Pow(sum2, 1.5)
}
// TODO: ta.linreg

View File

@ -56,3 +56,31 @@ func TestCorr(t *testing.T) {
corr = Correlation(&a, &b, 4, Spearman)
assert.InDelta(t, corr, -0.94868, 0.001)
}
/*
python
import pandas as pd
s1 = pd.Series([.2, 0., .6, .2, .2])
s2 = pd.Series([.3, .6, .0, .1])
print(s1.cov(s2, ddof=0))
*/
func TestCov(t *testing.T) {
var a = Float64Slice{.2, .0, .6, .2}
var b = Float64Slice{.3, .6, .0, .1}
cov := Covariance(&a, &b, 4)
assert.InDelta(t, cov, -0.042499, 0.001)
}
/*
python
import pandas as pd
s1 = pd.Series([.2, 0., .6, .2, .2])
print(s1.skew())
*/
func TestSkew(t *testing.T) {
var a = Float64Slice{.2, .0, .6, .2}
sk := Skew(&a, 4)
assert.InDelta(t, sk, 1.129338, 0.001)
}