0.3.1 #514

Merged
altavir merged 36 commits from dev into master 2023-05-12 22:19:49 +03:00
2 changed files with 14 additions and 10 deletions
Showing only changes of commit e6da61c52a - Show all commits

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 * Zip buffer with itself, but shifted
* */ * */
public inline fun Buffer<T>.shiftOp( public inline fun Buffer<T>.zipWithShift(
shift: Int = 1, shift: Int = 1,
crossinline operation: A.(left: T, right: T) -> T crossinline operation: A.(left: T, right: T) -> T
): Buffer<T> { ): Buffer<T> {
val shifted = this.moveTo(this.startOffset+shift) val shifted = this.moveBy(shift)
return zip(shifted, operation) 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 } 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 public companion object
} }

View File

@ -12,10 +12,14 @@ import space.kscience.kmath.operations.bufferAlgebra
import space.kscience.kmath.operations.fold 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) 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) * 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 // calculating asymptotic variance
var phi: Double val phi = if (homoscedastic) { // under homoscedastic null hypothesis
if (homoscedastic) { // under homoscedastic null hypothesis 2 * (2 * shift - 1.0) * (shift - 1.0) / (3 * shift * series.size)
phi = 2 * (2 * shift - 1.0) * (shift - 1.0) / (3 * shift * series.size)
} else { // under homoscedastic null hypothesis } else { // under homoscedastic null hypothesis
phi = 0.0 var accumulator = 0.0
var shiftedProd = demeanedSquares var shiftedProd = demeanedSquares
for (j in 1..<shift) { for (j in 1..<shift) {
shiftedProd = shiftedProd.zip(demeanedSquares.moveTo(j)) { v1, v2 -> v1 * v2 } shiftedProd = shiftedProd.zip(demeanedSquares.moveTo(j)) { v1, v2 -> v1 * v2 }
val delta = series.size * shiftedProd.fold(0.0, sum) / variance.pow(2) 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) val zScore = (varianceRatio - 1) / phi.pow(0.5)