From e6da61c52a7c7b38cabe5621014993eef5bdf2d2 Mon Sep 17 00:00:00 2001 From: mrFendel Date: Tue, 18 Apr 2023 01:53:07 +0300 Subject: [PATCH] refactoring --- .../kscience/kmath/series/SeriesAlgebra.kt | 6 +++--- .../kscience/kmath/series/VarianceRatioTest.kt | 18 +++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt index 9efbd629c..72b9ca7a2 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/SeriesAlgebra.kt @@ -202,11 +202,11 @@ public open class SeriesAlgebra, out BA : BufferAlgebra /** * Zip buffer with itself, but shifted * */ - public inline fun Buffer.shiftOp( + public inline fun Buffer.zipWithShift( shift: Int = 1, crossinline operation: A.(left: T, right: T) -> T ): Buffer { - val shifted = this.moveTo(this.startOffset+shift) + val shifted = this.moveBy(shift) return zip(shifted, operation) } @@ -216,7 +216,7 @@ public open class SeriesAlgebra, out BA : BufferAlgebra override fun multiply(left: Buffer, right: Buffer): Buffer = left.zip(right) { l, r -> l * r } - public inline fun Buffer.diff(): Buffer = this.shiftOp {l, r -> r - l} + public fun Buffer.diff(shift: Int=1): Buffer = this.zipWithShift(shift) {l, r -> r - l} public companion object } 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 9a00b1be2..45bc836fe 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 @@ -12,10 +12,14 @@ import space.kscience.kmath.operations.bufferAlgebra import space.kscience.kmath.operations.fold -// TODO: add p-value +// TODO: add p-value with formula: 2*(1 - cdf(|zScore|)) public data class VarianceRatioTestResult(val varianceRatio: Double, val zScore: Double) + /** + * Container class for Variance Ratio Test result: + * ratio itself, corresponding Z-score, also it's p-value + * **/ -public fun varianceRatioTest(series: Series, shift: Int, homoscedastic: Boolean): VarianceRatioTestResult { +public fun varianceRatioTest(series: Series, shift: Int, homoscedastic: Boolean=true): VarianceRatioTestResult { /** * Calculate the Z statistic and the p-value for the Lo and MacKinlay's Variance Ratio test (1987) @@ -44,17 +48,17 @@ public fun varianceRatioTest(series: Series, shift: Int, homoscedastic: // calculating asymptotic variance - var phi: Double - if (homoscedastic) { // under homoscedastic null hypothesis - phi = 2 * (2 * shift - 1.0) * (shift - 1.0) / (3 * shift * series.size) + val phi = if (homoscedastic) { // under homoscedastic null hypothesis + 2 * (2 * shift - 1.0) * (shift - 1.0) / (3 * shift * series.size) } else { // under homoscedastic null hypothesis - phi = 0.0 + var accumulator = 0.0 var shiftedProd = demeanedSquares for (j in 1.. v1 * v2 } val delta = series.size * shiftedProd.fold(0.0, sum) / variance.pow(2) - phi += delta * 4 * (shift - j) * (shift - j) / shift / shift // TODO: refactor with square + accumulator += delta * 4 * (shift - j) * (shift - j) / shift / shift // TODO: refactor with square } + accumulator } val zScore = (varianceRatio - 1) / phi.pow(0.5)