This commit is contained in:
Iaroslav 2020-06-08 17:38:48 +07:00
parent 5ff76209aa
commit d4226b7e7d
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
3 changed files with 5 additions and 11 deletions

View File

@ -15,8 +15,9 @@ class GaussianSampler private constructor(
private val standardDeviation: Double, private val standardDeviation: Double,
private val normalized: NormalizedGaussianSampler private val normalized: NormalizedGaussianSampler
) : Sampler<Double> { ) : Sampler<Double> {
override fun sample(generator: RandomGenerator): Chain<Double> = override fun sample(generator: RandomGenerator): Chain<Double> = normalized
normalized.sample(generator).map { standardDeviation * it + mean } .sample(generator)
.map { standardDeviation * it + mean }
override fun toString(): String = "Gaussian deviate [$normalized]" override fun toString(): String = "Gaussian deviate [$normalized]"

View File

@ -30,10 +30,8 @@ class MarsagliaNormalizedGaussianSampler private constructor(): NormalizedGaussi
if (r2 < 1 && r2 > 0) { if (r2 < 1 && r2 > 0) {
// Pair (x, y) is within unit circle. // Pair (x, y) is within unit circle.
alpha = sqrt(-2 * ln(r2) / r2) alpha = sqrt(-2 * ln(r2) / r2)
// Keep second element of the pair for next invocation. // Keep second element of the pair for next invocation.
nextGaussian = alpha * y nextGaussian = alpha * y
// Return the first element of the generated pair. // Return the first element of the generated pair.
break break
} }
@ -46,7 +44,6 @@ class MarsagliaNormalizedGaussianSampler private constructor(): NormalizedGaussi
// Use the second element of the pair (generated at the // Use the second element of the pair (generated at the
// previous invocation). // previous invocation).
val r = nextGaussian val r = nextGaussian
// Both elements of the pair have been used. // Both elements of the pair have been used.
nextGaussian = Double.NaN nextGaussian = Double.NaN
r r

View File

@ -73,8 +73,7 @@ class ZigguratNormalizedGaussianSampler private constructor() :
// Filling the tables. // Filling the tables.
var d = R var d = R
var t = d var t = d
var fd = var fd = gauss(d)
gauss(d)
val q = V / fd val q = V / fd
K[0] = (d / q * MAX).toLong() K[0] = (d / q * MAX).toLong()
K[1] = 0 K[1] = 0
@ -85,10 +84,7 @@ class ZigguratNormalizedGaussianSampler private constructor() :
(LAST - 1 downTo 1).forEach { i -> (LAST - 1 downTo 1).forEach { i ->
d = sqrt(-2 * ln(V / d + fd)) d = sqrt(-2 * ln(V / d + fd))
fd = fd = gauss(d)
gauss(
d
)
K[i + 1] = (d / t * MAX).toLong() K[i + 1] = (d / t * MAX).toLong()
t = d t = d
F[i] = fd F[i] = fd