diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt index c56583fa8..8ad7f7293 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt @@ -19,9 +19,9 @@ import org.ejml.sparse.csc.factory.DecompositionFactory_DSCC import org.ejml.sparse.csc.factory.DecompositionFactory_FSCC import org.ejml.sparse.csc.factory.LinearSolverFactory_DSCC import org.ejml.sparse.csc.factory.LinearSolverFactory_FSCC -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.* import space.kscience.kmath.linear.Matrix +import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.StructureFeature import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.FloatField 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 8afc01c81..d81a6c317 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 @@ -9,11 +9,10 @@ import space.kscience.kmath.operations.DoubleField.pow import space.kscience.kmath.operations.algebra import space.kscience.kmath.operations.bufferAlgebra import space.kscience.kmath.operations.fold -import space.kscience.kmath.structures.slice // TODO: add p-value with formula: 2*(1 - cdf(|zScore|)) -public data class VarianceRatioTestResult(val varianceRatio: Double, val zScore: Double) +public data class VarianceRatioTestResult(val varianceRatio: Double=1.0, val zScore: Double=0.0) /** * Container class for Variance Ratio Test result: * ratio itself, corresponding Z-score, also it's p-value @@ -27,12 +26,15 @@ public fun varianceRatioTest(series: Series, shift: Int, homoscedastic: * https://ssrn.com/abstract=346975 * **/ + require(shift > 1) {"Shift must be greater than one"} + require(shift < series.size) {"Shift must be smaller than sample size"} val sum = { x: Double, y: Double -> x + y } - //TODO: catch if shift is too large + with(Double.algebra.bufferAlgebra.seriesAlgebra()) { val mean = series.fold(0.0, sum) / series.size val demeanedSquares = series.map { power(it - mean, 2) } - val variance = demeanedSquares.fold(0.0, sum) // TODO: catch if variance is zero + val variance = demeanedSquares.fold(0.0, sum) + if (variance == 0.0) return VarianceRatioTestResult() var seriesAgg = series @@ -53,7 +55,7 @@ public fun varianceRatioTest(series: Series, shift: Int, homoscedastic: } else { // under heteroscedastic null hypothesis var accumulator = 0.0 for (j in 1.. v1 * v2 }.fold(0.0, sum) / variance.pow(2) accumulator += delta * 4 * (shift - j).toDouble().pow(2) / shift.toDouble().pow(2) } 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 index 0ece143eb..8e559276c 100644 --- a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt +++ b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/series/TestVarianceRatioTest.kt @@ -31,7 +31,7 @@ class TestVarianceRatioTest { 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) + val resultHomo = varianceRatioTest(volatileData, 2) assertEquals(0.0, resultHomo.varianceRatio, 1e-6) // homoscedastic zScore assertEquals(-3.162277, resultHomo.zScore, 1e-6) @@ -45,7 +45,7 @@ class TestVarianceRatioTest { fun negativeData() { with(Double.algebra.bufferAlgebra.seriesAlgebra()) { val negativeData = series(10) { sin(it * 1.2)} - val resultHomo = varianceRatioTest(negativeData, 3, homoscedastic = true) + val resultHomo = varianceRatioTest(negativeData, 3) assertEquals(1.240031, resultHomo.varianceRatio, 1e-6) // homoscedastic zScore assertEquals(0.509183, resultHomo.zScore, 1e-6) @@ -55,12 +55,13 @@ class TestVarianceRatioTest { } } - //TODO: add zero volatility Test, logReturns test, big shift Test -// @Test -// fun zeroVolatility() { -// with(Double.algebra.bufferAlgebra.seriesAlgebra()) { -// val zeroVolData = series(10) { 1.0 } -// val result = varianceRatioTest(zeroVolData, 2, homoscedastic = true) -// } -// } + @Test + fun zeroVolatility() { + with(Double.algebra.bufferAlgebra.seriesAlgebra()) { + val zeroVolData = series(10) { 0.0 } + val result = varianceRatioTest(zeroVolData, 4) + assertEquals(1.0, result.varianceRatio, 1e-6) + assertEquals(0.0, result.zScore, 1e-6) + } + } } \ No newline at end of file