Implement Commons RNG-like samplers in kmath-prob module for Multiplatform #164

Merged
CommanderTvis merged 44 commits from feature/mp-samplers into dev 2021-03-31 09:25:44 +03:00
9 changed files with 46 additions and 2 deletions
Showing only changes of commit 2b24bd979e - Show all commits

View File

@ -7,6 +7,11 @@ import scientifik.kmath.prob.chain
import kotlin.math.ln
import kotlin.math.pow
/**
* Based on commons-rng implementation.
*
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.html
*/
class AhrensDieterExponentialSampler private constructor(val mean: Double) : Sampler<Double> {
override fun sample(generator: RandomGenerator): Chain<Double> = generator.chain {
// Step 1:

View File

@ -6,6 +6,11 @@ import scientifik.kmath.prob.Sampler
import scientifik.kmath.prob.chain
import kotlin.math.*
/**
* Based on commons-rng implementation.
*
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.html
*/
class BoxMullerNormalizedGaussianSampler private constructor() : NormalizedGaussianSampler, Sampler<Double> {
private var nextGaussian: Double = Double.NaN

View File

@ -5,6 +5,11 @@ import scientifik.kmath.chains.map
import scientifik.kmath.prob.RandomGenerator
import scientifik.kmath.prob.Sampler
/**
* Based on commons-rng implementation.
*
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/GaussianSampler.html
*/
class GaussianSampler private constructor(
private val mean: Double,
private val standardDeviation: Double,

View File

@ -6,6 +6,11 @@ import scientifik.kmath.prob.Sampler
import scientifik.kmath.prob.chain
import kotlin.math.exp
/**
* Based on commons-rng implementation.
*
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.html
*/
class KempSmallMeanPoissonSampler private constructor(
private val p0: Double,
private val mean: Double

View File

@ -8,9 +8,14 @@ import scientifik.kmath.prob.Sampler
import scientifik.kmath.prob.next
import kotlin.math.*
/**
* Based on commons-rng implementation.
*
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSampler.html
*/
class LargeMeanPoissonSampler private constructor(val mean: Double) : Sampler<Int> {
private val exponential: Sampler<Double> = AhrensDieterExponentialSampler.of(1.0)
private val gaussian: Sampler<Double> = ZigguratNormalizedGaussianSampler()
private val gaussian: Sampler<Double> = ZigguratNormalizedGaussianSampler.of()
private val factorialLog: InternalUtils.FactorialLog = NO_CACHE_FACTORIAL_LOG
private val lambda: Double = floor(mean)
private val logLambda: Double = ln(lambda)

View File

@ -7,6 +7,11 @@ import scientifik.kmath.prob.chain
import kotlin.math.ln
import kotlin.math.sqrt
/**
* Based on commons-rng implementation.
*
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.html
*/
class MarsagliaNormalizedGaussianSampler private constructor(): NormalizedGaussianSampler, Sampler<Double> {
private var nextGaussian = Double.NaN

View File

@ -4,7 +4,11 @@ import scientifik.kmath.chains.Chain
import scientifik.kmath.prob.RandomGenerator
import scientifik.kmath.prob.Sampler
/**
* Based on commons-rng implementation.
*
* https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/PoissonSampler.html
*/
class PoissonSampler private constructor(
mean: Double
) : Sampler<Int> {

View File

@ -8,6 +8,11 @@ import scientifik.kmath.prob.chain
import kotlin.math.ceil
import kotlin.math.exp
/**
* Based on commons-rng implementation.
*
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/SmallMeanPoissonSampler.html
*/
class SmallMeanPoissonSampler private constructor(mean: Double) : Sampler<Int> {
private val p0: Double = exp(-mean)

View File

@ -7,6 +7,11 @@ import scientifik.kmath.prob.Sampler
import scientifik.kmath.prob.chain
import kotlin.math.*
/**
* Based on commons-rng implementation.
*
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.html
*/
class ZigguratNormalizedGaussianSampler private constructor() :
NormalizedGaussianSampler, Sampler<Double> {