refactoring

This commit is contained in:
mrFendel 2023-04-18 01:53:07 +03:00
parent dababe3075
commit e6da61c52a
2 changed files with 14 additions and 10 deletions

View File

@ -202,11 +202,11 @@ public open class SeriesAlgebra<T, out A : Ring<T>, out BA : BufferAlgebra<T, A>
/**
* Zip buffer with itself, but shifted
* */
public inline fun Buffer<T>.shiftOp(
public inline fun Buffer<T>.zipWithShift(
shift: Int = 1,
crossinline operation: A.(left: T, right: T) -> T
): Buffer<T> {
val shifted = this.moveTo(this.startOffset+shift)
val shifted = this.moveBy(shift)
return zip(shifted, operation)
}
@ -216,7 +216,7 @@ public open class SeriesAlgebra<T, out A : Ring<T>, out BA : BufferAlgebra<T, A>
override fun multiply(left: Buffer<T>, right: Buffer<T>): Buffer<T> = left.zip(right) { l, r -> l * r }
public inline fun Buffer<T>.diff(): Buffer<T> = this.shiftOp {l, r -> r - l}
public fun Buffer<T>.diff(shift: Int=1): Buffer<T> = this.zipWithShift(shift) {l, r -> r - l}
public companion object
}

View File

@ -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<Double>, shift: Int, homoscedastic: Boolean): VarianceRatioTestResult {
public fun varianceRatioTest(series: Series<Double>, 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<Double>, 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..<shift) {
shiftedProd = shiftedProd.zip(demeanedSquares.moveTo(j)) { v1, v2 -> 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)