diff --git a/vlib/math/stats/stats.v b/vlib/math/stats/stats.v index ed071d6bacb640..6462a743ced615 100644 --- a/vlib/math/stats/stats.v +++ b/vlib/math/stats/stats.v @@ -434,7 +434,7 @@ pub fn covariance_mean[T](data1 []T, data2 []T, mean1 T, mean2 T) T { for i in 0 .. n { delta1 := data1[i] - mean1 delta2 := data2[i] - mean2 - covariance += T((delta1 * delta2 - covariance) / (i + T(1))) + covariance += T((delta1 * delta2 - covariance) / (T(i) + T(1))) } return covariance } @@ -459,8 +459,11 @@ pub fn lag1_autocorrelation_mean[T](data []T, mean T) T { for i := 1; i < data.len; i++ { delta0 := data[i - 1] - mean delta1 := data[i] - mean - q += T((delta0 * delta1 - q) / (i + T(1))) - v += T((delta1 * delta1 - v) / (T(i) + T(1))) + d01 := delta0 * delta1 + d11 := delta1 * delta1 + ti1 := T(i) + T(1) + q += T((d01 - q) / ti1) + v += T((d11 - v) / ti1) } return T(q / v) } @@ -486,7 +489,9 @@ pub fn kurtosis_mean_stddev[T](data []T, mean T, sd T) T { */ for i, v in data { x := (v - mean) / sd - avg += T((x * x * x * x - avg) / (i + T(1))) + x4 := x * x * x * x + ti1 := (T(i) + T(1)) + avg += T((x4 - avg) / ti1) } return avg - T(3) } @@ -511,7 +516,8 @@ pub fn skew_mean_stddev[T](data []T, mean T, sd T) T { */ for i, v in data { x := (v - mean) / sd - skew += T((x * x * x - skew) / (i + T(1))) + x3 := x * x * x + skew += T((x3 - skew) / (T(i) + T(1))) } return skew } diff --git a/vlib/math/stats/stats_test.v b/vlib/math/stats/stats_test.v index 7c9dcb30886457..dbdafc789449e9 100644 --- a/vlib/math/stats/stats_test.v +++ b/vlib/math/stats/stats_test.v @@ -428,13 +428,15 @@ fn test_covariance() { fn test_lag1_autocorrelation() { mut data := [10.0, 4.45, 5.9, 2.7] mut o := stats.lag1_autocorrelation(data) + mut e := 0.0 assert math.alike(o, -0.5542285481446095) data = [-3.0, 67.31, 4.4, 1.89] o = stats.lag1_autocorrelation(data) assert math.alike(o, -0.5102510654033415) data = [12.0, 7.88, 76.122, 54.83] o = stats.lag1_autocorrelation(data) - assert math.alike(o, 0.10484450460892072) + e = 0.10484450460892072 + assert math.veryclose(o, e), diff(o, e) // test for int, i64, f32 array assert stats.lag1_autocorrelation[int]([1, 2, 3, 1]) == 0 @@ -444,7 +446,7 @@ fn test_lag1_autocorrelation() { } fn diff(actual f64, expected f64) string { - return '\nactual:\n${actual}\nexpected:\n${expected}' + return '\n actual:${actual:40.35f}\nexpected:${expected:40.35f}\n diff:${actual - expected:40.35f}' } fn test_kurtosis() { @@ -464,27 +466,30 @@ fn test_kurtosis() { assert stats.kurtosis[int]([1, 2, 3, 1]) == 1 assert stats.kurtosis[i64]([i64(1), 2, 3, 1]) == 1 o = stats.kurtosis[f32]([f32(1.0), 3, 5, 7, 3]) - assert math.alike(o, -1.044378399848938) + e = -1.044378399848938 + assert math.alike(o, e), diff(o, e) } fn test_skew() { mut data := [10.0, 4.45, 5.9, 2.7] mut o := stats.skew(data) mut e := 0.5754021106320453 - assert math.veryclose(o, e), diff(o, e) + assert math.close(o, e), diff(o, e) data = [-3.0, 67.31, 4.4, 1.89] o = stats.skew(data) e = 1.1248733711136492 assert math.veryclose(o, e), diff(o, e) data = [12.0, 7.88, 76.122, 54.83] o = stats.skew(data) - assert math.alike(o, 0.19007911706827735) + e = 0.19007911706827735 + assert math.veryclose(o, e), diff(o, e) // test for int, i64, f32 array assert stats.skew[int]([1, 2, 3, 1]) == 2 assert stats.skew[i64]([i64(1), 2, 3, 1]) == 2 o = stats.skew[f32]([f32(1.0), 3, 5, 7, 3]) - assert math.alike(o, 0.27154541015625) + e = 0.27154541015625 + assert math.close(o, e), diff(o, e) } fn test_quantile() {