forked from kscience/kmath
Variance test post-merge cleanup
This commit is contained in:
parent
4dbcaca87c
commit
8eb25596a0
@ -1,3 +1,3 @@
|
|||||||
job("Build") {
|
job("Build and run tests") {
|
||||||
gradlew("openjdk:11", "build")
|
gradlew("amazoncorretto:17-alpine", "build")
|
||||||
}
|
}
|
@ -15,7 +15,7 @@ allprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "space.kscience"
|
group = "space.kscience"
|
||||||
version = "0.3.1-dev-RC"
|
version = "0.3.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
|
@ -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.DecompositionFactory_FSCC
|
||||||
import org.ejml.sparse.csc.factory.LinearSolverFactory_DSCC
|
import org.ejml.sparse.csc.factory.LinearSolverFactory_DSCC
|
||||||
import org.ejml.sparse.csc.factory.LinearSolverFactory_FSCC
|
import org.ejml.sparse.csc.factory.LinearSolverFactory_FSCC
|
||||||
|
import space.kscience.kmath.UnstableKMathAPI
|
||||||
import space.kscience.kmath.linear.*
|
import space.kscience.kmath.linear.*
|
||||||
import space.kscience.kmath.linear.Matrix
|
import space.kscience.kmath.linear.Matrix
|
||||||
import space.kscience.kmath.UnstableKMathAPI
|
|
||||||
import space.kscience.kmath.nd.StructureFeature
|
import space.kscience.kmath.nd.StructureFeature
|
||||||
import space.kscience.kmath.operations.DoubleField
|
import space.kscience.kmath.operations.DoubleField
|
||||||
import space.kscience.kmath.operations.FloatField
|
import space.kscience.kmath.operations.FloatField
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
package space.kscience.kmath.distributions
|
package space.kscience.kmath.distributions
|
||||||
|
|
||||||
import space.kscience.kmath.chains.Chain
|
import space.kscience.kmath.chains.Chain
|
||||||
|
import space.kscience.kmath.operations.DoubleField.pow
|
||||||
import space.kscience.kmath.random.RandomGenerator
|
import space.kscience.kmath.random.RandomGenerator
|
||||||
import space.kscience.kmath.samplers.GaussianSampler
|
import space.kscience.kmath.samplers.GaussianSampler
|
||||||
import space.kscience.kmath.samplers.InternalErf
|
import space.kscience.kmath.samplers.InternalErf
|
||||||
@ -34,8 +35,23 @@ public class NormalDistribution(public val sampler: GaussianSampler) : Distribut
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private companion object {
|
public companion object {
|
||||||
private val SQRT2 = sqrt(2.0)
|
private val SQRT2 = sqrt(2.0)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zelen & Severo approximation for the standard normal CDF.
|
||||||
|
* The error upper boundary by 7.5 * 10e-8.
|
||||||
|
*/
|
||||||
|
public fun zSNormalCDF(x: Double): Double {
|
||||||
|
val t = 1 / (1 + 0.2316419 * abs(x))
|
||||||
|
val sum = 0.319381530 * t -
|
||||||
|
0.356563782 * t.pow(2) +
|
||||||
|
1.781477937 * t.pow(3) -
|
||||||
|
1.821255978 * t.pow(4) +
|
||||||
|
1.330274429 * t.pow(5)
|
||||||
|
val temp = sum * exp(-abs(x).pow(2) / 2) / (2 * PI).pow(0.5)
|
||||||
|
return if (x >= 0) 1 - temp else temp
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.distributions
|
|
||||||
|
|
||||||
import space.kscience.kmath.operations.DoubleField.pow
|
|
||||||
import kotlin.math.PI
|
|
||||||
import kotlin.math.absoluteValue
|
|
||||||
import kotlin.math.exp
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Zelen & Severo approximation for the standard normal CDF.
|
|
||||||
* The error is bounded by 7.5 * 10e-8.
|
|
||||||
* */
|
|
||||||
public fun zSNormalCDF(x: Double): Double {
|
|
||||||
|
|
||||||
val t = 1 / (1 + 0.2316419 * x.absoluteValue)
|
|
||||||
val summ = 0.319381530*t - 0.356563782*t.pow(2) + 1.781477937*t.pow(3) - 1.821255978*t.pow(4) + 1.330274429*t.pow(5)
|
|
||||||
val temp = summ * exp(-x.absoluteValue.pow(2) / 2) / (2 * PI).pow(0.5)
|
|
||||||
return if (x >= 0) 1 - temp else temp
|
|
||||||
}
|
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
package space.kscience.kmath.series
|
package space.kscience.kmath.series
|
||||||
|
|
||||||
import space.kscience.kmath.distributions.zSNormalCDF
|
import space.kscience.kmath.distributions.NormalDistribution
|
||||||
import space.kscience.kmath.operations.DoubleField.pow
|
import space.kscience.kmath.operations.DoubleField.pow
|
||||||
import space.kscience.kmath.operations.fold
|
import space.kscience.kmath.operations.fold
|
||||||
import kotlin.math.absoluteValue
|
import kotlin.math.absoluteValue
|
||||||
@ -14,8 +14,12 @@ import kotlin.math.absoluteValue
|
|||||||
/**
|
/**
|
||||||
* Container class for Variance Ratio Test result:
|
* Container class for Variance Ratio Test result:
|
||||||
* ratio itself, corresponding Z-score, also it's p-value
|
* ratio itself, corresponding Z-score, also it's p-value
|
||||||
* **/
|
*/
|
||||||
public data class VarianceRatioTestResult(val varianceRatio: Double=1.0, val zScore: Double=0.0, val pValue: Double=0.5)
|
public data class VarianceRatioTestResult(
|
||||||
|
val varianceRatio: Double = 1.0,
|
||||||
|
val zScore: Double = 0.0,
|
||||||
|
val pValue: Double = 0.5,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,11 +27,17 @@ public data class VarianceRatioTestResult(val varianceRatio: Double=1.0, val zSc
|
|||||||
* under Homoscedastic or Heteroscedstic assumptions
|
* under Homoscedastic or Heteroscedstic assumptions
|
||||||
* with two-sided p-value test
|
* with two-sided p-value test
|
||||||
* https://ssrn.com/abstract=346975
|
* https://ssrn.com/abstract=346975
|
||||||
* **/
|
*
|
||||||
public fun SeriesAlgebra<Double, *, *, *>.varianceRatioTest(series: Series<Double>, shift: Int, homoscedastic: Boolean=true): VarianceRatioTestResult {
|
* @author https://github.com/mrFendel
|
||||||
|
*/
|
||||||
|
public fun SeriesAlgebra<Double, *, *, *>.varianceRatioTest(
|
||||||
|
series: Series<Double>,
|
||||||
|
shift: Int,
|
||||||
|
homoscedastic: Boolean = true,
|
||||||
|
): VarianceRatioTestResult {
|
||||||
|
|
||||||
require(shift > 1) {"Shift must be greater than one"}
|
require(shift > 1) { "Shift must be greater than one" }
|
||||||
require(shift < series.size) {"Shift must be smaller than sample size"}
|
require(shift < series.size) { "Shift must be smaller than sample size" }
|
||||||
val sum = { x: Double, y: Double -> x + y }
|
val sum = { x: Double, y: Double -> x + y }
|
||||||
|
|
||||||
|
|
||||||
@ -46,7 +56,7 @@ public fun SeriesAlgebra<Double, *, *, *>.varianceRatioTest(series: Series<Doubl
|
|||||||
val varianceAgg = demeanedSquaresAgg.fold(0.0, sum)
|
val varianceAgg = demeanedSquaresAgg.fold(0.0, sum)
|
||||||
|
|
||||||
val varianceRatio =
|
val varianceRatio =
|
||||||
varianceAgg * (series.size.toDouble() - 1) / variance / (series.size.toDouble() - shift.toDouble() + 1) / (1 - shift.toDouble()/series.size.toDouble()) / shift.toDouble()
|
varianceAgg * (series.size.toDouble() - 1) / variance / (series.size.toDouble() - shift.toDouble() + 1) / (1 - shift.toDouble() / series.size.toDouble()) / shift.toDouble()
|
||||||
|
|
||||||
|
|
||||||
// calculating asymptotic variance
|
// calculating asymptotic variance
|
||||||
@ -63,7 +73,7 @@ public fun SeriesAlgebra<Double, *, *, *>.varianceRatioTest(series: Series<Doubl
|
|||||||
}
|
}
|
||||||
|
|
||||||
val zScore = (varianceRatio - 1) / phi.pow(0.5)
|
val zScore = (varianceRatio - 1) / phi.pow(0.5)
|
||||||
val pValue = 2*(1 - zSNormalCDF(zScore.absoluteValue))
|
val pValue = 2 * (1 - NormalDistribution.zSNormalCDF(zScore.absoluteValue))
|
||||||
return VarianceRatioTestResult(varianceRatio, zScore, pValue)
|
return VarianceRatioTestResult(varianceRatio, zScore, pValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user