diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/GaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/GaussianSampler.kt index 2a6bd6a09..755c73df3 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/GaussianSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/GaussianSampler.kt @@ -15,8 +15,9 @@ class GaussianSampler private constructor( private val standardDeviation: Double, private val normalized: NormalizedGaussianSampler ) : Sampler { - override fun sample(generator: RandomGenerator): Chain = - normalized.sample(generator).map { standardDeviation * it + mean } + override fun sample(generator: RandomGenerator): Chain = normalized + .sample(generator) + .map { standardDeviation * it + mean } override fun toString(): String = "Gaussian deviate [$normalized]" diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/MarsagliaNormalizedGaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/MarsagliaNormalizedGaussianSampler.kt index a0711ec24..80d93055d 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/MarsagliaNormalizedGaussianSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/MarsagliaNormalizedGaussianSampler.kt @@ -30,10 +30,8 @@ class MarsagliaNormalizedGaussianSampler private constructor(): NormalizedGaussi if (r2 < 1 && r2 > 0) { // Pair (x, y) is within unit circle. alpha = sqrt(-2 * ln(r2) / r2) - // Keep second element of the pair for next invocation. nextGaussian = alpha * y - // Return the first element of the generated pair. break } @@ -46,7 +44,6 @@ class MarsagliaNormalizedGaussianSampler private constructor(): NormalizedGaussi // Use the second element of the pair (generated at the // previous invocation). val r = nextGaussian - // Both elements of the pair have been used. nextGaussian = Double.NaN r diff --git a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/ZigguratNormalizedGaussianSampler.kt b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/ZigguratNormalizedGaussianSampler.kt index 979209e79..de593a811 100644 --- a/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/ZigguratNormalizedGaussianSampler.kt +++ b/kmath-prob/src/commonMain/kotlin/scientifik/kmath/prob/samplers/ZigguratNormalizedGaussianSampler.kt @@ -73,8 +73,7 @@ class ZigguratNormalizedGaussianSampler private constructor() : // Filling the tables. var d = R var t = d - var fd = - gauss(d) + var fd = gauss(d) val q = V / fd K[0] = (d / q * MAX).toLong() K[1] = 0 @@ -85,10 +84,7 @@ class ZigguratNormalizedGaussianSampler private constructor() : (LAST - 1 downTo 1).forEach { i -> d = sqrt(-2 * ln(V / d + fd)) - fd = - gauss( - d - ) + fd = gauss(d) K[i + 1] = (d / t * MAX).toLong() t = d F[i] = fd