tests for varianceRatio

This commit is contained in:
mrFendel 2023-04-13 17:52:14 +03:00
parent 0ce1861ab4
commit a91b43a52d
2 changed files with 53 additions and 2 deletions

View File

@ -24,10 +24,10 @@ public fun varianceRatioTest(series: Series<Double>, shift: Int, homoscedastic:
* **/ * **/
val sum = { x: Double, y: Double -> x + y } 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 mean = series.fold(0.0, sum) / series.size
val demeanedSquares = series.map { power(it - mean, 2) } 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()) { with(Double.algebra.bufferAlgebra.seriesAlgebra()) {
for (i in -1..-shift + 1) { series.shiftOp(i) { v1, v2 -> v1 + v2 } } for (i in -1..-shift + 1) { series.shiftOp(i) { v1, v2 -> v1 + v2 } }

View File

@ -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)
// }
// }
}