Implement Commons RNG-like samplers in kmath-prob module for Multiplatform #164
@ -7,6 +7,11 @@ import scientifik.kmath.prob.chain
|
|||||||
import kotlin.math.ln
|
import kotlin.math.ln
|
||||||
import kotlin.math.pow
|
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> {
|
class AhrensDieterExponentialSampler private constructor(val mean: Double) : Sampler<Double> {
|
||||||
override fun sample(generator: RandomGenerator): Chain<Double> = generator.chain {
|
override fun sample(generator: RandomGenerator): Chain<Double> = generator.chain {
|
||||||
// Step 1:
|
// Step 1:
|
||||||
|
@ -6,6 +6,11 @@ import scientifik.kmath.prob.Sampler
|
|||||||
import scientifik.kmath.prob.chain
|
import scientifik.kmath.prob.chain
|
||||||
import kotlin.math.*
|
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> {
|
class BoxMullerNormalizedGaussianSampler private constructor() : NormalizedGaussianSampler, Sampler<Double> {
|
||||||
private var nextGaussian: Double = Double.NaN
|
private var nextGaussian: Double = Double.NaN
|
||||||
|
|
||||||
|
@ -5,6 +5,11 @@ import scientifik.kmath.chains.map
|
|||||||
import scientifik.kmath.prob.RandomGenerator
|
import scientifik.kmath.prob.RandomGenerator
|
||||||
import scientifik.kmath.prob.Sampler
|
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(
|
class GaussianSampler private constructor(
|
||||||
private val mean: Double,
|
private val mean: Double,
|
||||||
private val standardDeviation: Double,
|
private val standardDeviation: Double,
|
||||||
|
@ -6,6 +6,11 @@ import scientifik.kmath.prob.Sampler
|
|||||||
import scientifik.kmath.prob.chain
|
import scientifik.kmath.prob.chain
|
||||||
import kotlin.math.exp
|
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(
|
class KempSmallMeanPoissonSampler private constructor(
|
||||||
private val p0: Double,
|
private val p0: Double,
|
||||||
private val mean: Double
|
private val mean: Double
|
||||||
|
@ -8,9 +8,14 @@ import scientifik.kmath.prob.Sampler
|
|||||||
import scientifik.kmath.prob.next
|
import scientifik.kmath.prob.next
|
||||||
import kotlin.math.*
|
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> {
|
class LargeMeanPoissonSampler private constructor(val mean: Double) : Sampler<Int> {
|
||||||
private val exponential: Sampler<Double> = AhrensDieterExponentialSampler.of(1.0)
|
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 factorialLog: InternalUtils.FactorialLog = NO_CACHE_FACTORIAL_LOG
|
||||||
private val lambda: Double = floor(mean)
|
private val lambda: Double = floor(mean)
|
||||||
private val logLambda: Double = ln(lambda)
|
private val logLambda: Double = ln(lambda)
|
||||||
|
@ -7,6 +7,11 @@ import scientifik.kmath.prob.chain
|
|||||||
import kotlin.math.ln
|
import kotlin.math.ln
|
||||||
import kotlin.math.sqrt
|
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> {
|
class MarsagliaNormalizedGaussianSampler private constructor(): NormalizedGaussianSampler, Sampler<Double> {
|
||||||
private var nextGaussian = Double.NaN
|
private var nextGaussian = Double.NaN
|
||||||
|
|
||||||
|
@ -4,7 +4,11 @@ import scientifik.kmath.chains.Chain
|
|||||||
import scientifik.kmath.prob.RandomGenerator
|
import scientifik.kmath.prob.RandomGenerator
|
||||||
import scientifik.kmath.prob.Sampler
|
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(
|
class PoissonSampler private constructor(
|
||||||
mean: Double
|
mean: Double
|
||||||
) : Sampler<Int> {
|
) : Sampler<Int> {
|
||||||
|
@ -8,6 +8,11 @@ import scientifik.kmath.prob.chain
|
|||||||
import kotlin.math.ceil
|
import kotlin.math.ceil
|
||||||
import kotlin.math.exp
|
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> {
|
class SmallMeanPoissonSampler private constructor(mean: Double) : Sampler<Int> {
|
||||||
private val p0: Double = exp(-mean)
|
private val p0: Double = exp(-mean)
|
||||||
|
|
||||||
|
@ -7,6 +7,11 @@ import scientifik.kmath.prob.Sampler
|
|||||||
import scientifik.kmath.prob.chain
|
import scientifik.kmath.prob.chain
|
||||||
import kotlin.math.*
|
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() :
|
class ZigguratNormalizedGaussianSampler private constructor() :
|
||||||
NormalizedGaussianSampler, Sampler<Double> {
|
NormalizedGaussianSampler, Sampler<Double> {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user