From a91b43a52db60388a4947c1a277e6b26bd3e6997 Mon Sep 17 00:00:00 2001 From: mrFendel Date: Thu, 13 Apr 2023 17:52:14 +0300 Subject: [PATCH] tests for varianceRatio --- .../kmath/series/VarianceRatioTest.kt | 4 +- .../kmath/series/TestVarianceRatioTest.kt | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt index 77163f638..b769d78a3 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt @@ -24,10 +24,10 @@ public fun varianceRatioTest(series: Series, shift: Int, homoscedastic: * **/ val sum = { x: Double, y: Double -> x + y } - + //TODO: catch if shift is too large val mean = series.fold(0.0, sum) / series.size val demeanedSquares = series.map { power(it - mean, 2) } - val variance = demeanedSquares.fold(0.0, sum) + val variance = demeanedSquares.fold(0.0, sum) // TODO: catch if variance is zero with(Double.algebra.bufferAlgebra.seriesAlgebra()) { for (i in -1..-shift + 1) { series.shiftOp(i) { v1, v2 -> v1 + v2 } } diff --git a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt new file mode 100644 index 000000000..7c31663bc --- /dev/null +++ b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2018-2023 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.series + +import space.kscience.kmath.operations.algebra +import space.kscience.kmath.operations.bufferAlgebra +import kotlin.math.PI +import kotlin.test.Test +import kotlin.test.assertEquals + +class TestVarianceRatioTest { + + @Test + fun volatileData() { + with(Double.algebra.bufferAlgebra.seriesAlgebra()) { + val volatileData = series(10) { sin(PI * it + PI/2) + 1.0} + val resultHomo = varianceRatioTest(volatileData, 2, homoscedastic = true) + assertEquals(0.0, resultHomo.varianceRatio, 1e-6) + // homoscedastic zScore + assertEquals(-3.162277, resultHomo.zScore, 1e-6) + val resultHetero = varianceRatioTest(volatileData, 2, homoscedastic = false) + // heteroscedastic zScore + assertEquals(-3.535533, resultHetero.zScore, 1e-6) + } + } + + @Test + fun negativeData() { + with(Double.algebra.bufferAlgebra.seriesAlgebra()) { + val volatileData = series(10) { sin(PI * it)} + val resultHomo = varianceRatioTest(volatileData, 2, homoscedastic = true) + assertEquals(1.142857, resultHomo.varianceRatio, 1e-6) + // homoscedastic zScore + assertEquals(0.451753, resultHomo.zScore, 1e-6) + val resultHetero = varianceRatioTest(volatileData, 2, homoscedastic = false) + // heteroscedastic zScore + assertEquals(2.462591, resultHetero.zScore, 1e-6) + } + } + +// @Test +// fun zeroVolatility() { +// with(Double.algebra.bufferAlgebra.seriesAlgebra()) { +// val volatileData = series(10) { 1.0 } +// val result = varianceRatioTest(volatileData, 2, homoscedastic = true) +// } +// } +} \ No newline at end of file