Implement Commons RNG-like samplers in kmath-prob module for Multiplatform #164
@ -1,11 +1,8 @@
|
|||||||
package kscience.kmath.commons.prob
|
package kscience.kmath.stat
|
||||||
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kscience.kmath.stat.RandomGenerator
|
|
||||||
import kscience.kmath.stat.blocking
|
|
||||||
import kscience.kmath.stat.fromSource
|
|
||||||
import kscience.kmath.stat.samplers.GaussianSampler
|
import kscience.kmath.stat.samplers.GaussianSampler
|
||||||
import org.apache.commons.rng.simple.RandomSource
|
import org.apache.commons.rng.simple.RandomSource
|
||||||
import java.time.Duration
|
import java.time.Duration
|
@ -1,4 +1,4 @@
|
|||||||
package kscience.kmath.commons.prob
|
package kscience.kmath.stat
|
||||||
|
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kscience.kmath.chains.Chain
|
import kscience.kmath.chains.Chain
|
@ -9,6 +9,9 @@ import kscience.kmath.stat.samplers.NormalizedGaussianSampler
|
|||||||
import kscience.kmath.stat.samplers.ZigguratNormalizedGaussianSampler
|
import kscience.kmath.stat.samplers.ZigguratNormalizedGaussianSampler
|
||||||
import kotlin.math.*
|
import kotlin.math.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements [UnivariateDistribution] for the normal (gaussian) distribution.
|
||||||
|
*/
|
||||||
public inline class NormalDistribution(public val sampler: GaussianSampler) : UnivariateDistribution<Double> {
|
public inline class NormalDistribution(public val sampler: GaussianSampler) : UnivariateDistribution<Double> {
|
||||||
public constructor(
|
public constructor(
|
||||||
mean: Double,
|
mean: Double,
|
||||||
|
@ -2,6 +2,10 @@ package kscience.kmath.stat.internal
|
|||||||
|
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Based on Commons Math implementation.
|
||||||
|
* See [https://commons.apache.org/proper/commons-math/javadocs/api-3.3/org/apache/commons/math3/special/Erf.html].
|
||||||
|
*/
|
||||||
internal object InternalErf {
|
internal object InternalErf {
|
||||||
fun erfc(x: Double): Double {
|
fun erfc(x: Double): Double {
|
||||||
if (abs(x) > 40) return if (x > 0) 0.0 else 2.0
|
if (abs(x) > 40) return if (x > 0) 0.0 else 2.0
|
||||||
|
@ -12,7 +12,7 @@ import kotlin.math.pow
|
|||||||
* Sampling from an [exponential distribution](http://mathworld.wolfram.com/ExponentialDistribution.html).
|
* Sampling from an [exponential distribution](http://mathworld.wolfram.com/ExponentialDistribution.html).
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* 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
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSampler.html].
|
||||||
*/
|
*/
|
||||||
public class AhrensDieterExponentialSampler private constructor(public val mean: Double) : Sampler<Double> {
|
public class AhrensDieterExponentialSampler private constructor(public val mean: Double) : Sampler<Double> {
|
||||||
public override fun sample(generator: RandomGenerator): Chain<Double> = generator.chain {
|
public override fun sample(generator: RandomGenerator): Chain<Double> = generator.chain {
|
||||||
|
@ -15,7 +15,8 @@ import kotlin.math.*
|
|||||||
* Marsaglia and Tsang, A Simple Method for Generating Gamma Variables. ACM Transactions on Mathematical Software, Volume 26 Issue 3, September, 2000.
|
* Marsaglia and Tsang, A Simple Method for Generating Gamma Variables. ACM Transactions on Mathematical Software, Volume 26 Issue 3, September, 2000.
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* Based on Commons RNG implementation.
|
||||||
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.html
|
*
|
||||||
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.html].
|
||||||
*/
|
*/
|
||||||
public class AhrensDieterMarsagliaTsangGammaSampler private constructor(
|
public class AhrensDieterMarsagliaTsangGammaSampler private constructor(
|
||||||
alpha: Double,
|
alpha: Double,
|
||||||
|
@ -34,7 +34,7 @@ import kotlin.math.min
|
|||||||
* that exploit the power of 2.
|
* that exploit the power of 2.
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* Based on Commons RNG implementation.
|
||||||
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/AliasMethodDiscreteSampler.html
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/AliasMethodDiscreteSampler.html].
|
||||||
*/
|
*/
|
||||||
public open class AliasMethodDiscreteSampler private constructor(
|
public open class AliasMethodDiscreteSampler private constructor(
|
||||||
// Deliberate direct storage of input arrays
|
// Deliberate direct storage of input arrays
|
||||||
|
@ -11,7 +11,7 @@ import kotlin.math.*
|
|||||||
* distribution.
|
* distribution.
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* 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
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/BoxMullerNormalizedGaussianSampler.html].
|
||||||
*/
|
*/
|
||||||
public class BoxMullerNormalizedGaussianSampler private constructor() : NormalizedGaussianSampler, Sampler<Double> {
|
public class BoxMullerNormalizedGaussianSampler private constructor() : NormalizedGaussianSampler, Sampler<Double> {
|
||||||
private var nextGaussian: Double = Double.NaN
|
private var nextGaussian: Double = Double.NaN
|
||||||
|
@ -9,7 +9,7 @@ import kscience.kmath.stat.Sampler
|
|||||||
* Sampling from a Gaussian distribution with given mean and standard deviation.
|
* Sampling from a Gaussian distribution with given mean and standard deviation.
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* 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
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/GaussianSampler.html].
|
||||||
*
|
*
|
||||||
* @property mean the mean of the distribution.
|
* @property mean the mean of the distribution.
|
||||||
* @property standardDeviation the variance of the distribution.
|
* @property standardDeviation the variance of the distribution.
|
||||||
|
@ -16,7 +16,7 @@ import kotlin.math.exp
|
|||||||
* Sampling uses 1 call to UniformRandomProvider.nextDouble(). This method provides an alternative to the SmallMeanPoissonSampler for slow generators of double.
|
* Sampling uses 1 call to UniformRandomProvider.nextDouble(). This method provides an alternative to the SmallMeanPoissonSampler for slow generators of double.
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* 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
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/KempSmallMeanPoissonSampler.html].
|
||||||
*/
|
*/
|
||||||
public class KempSmallMeanPoissonSampler private constructor(
|
public class KempSmallMeanPoissonSampler private constructor(
|
||||||
private val p0: Double,
|
private val p0: Double,
|
||||||
|
@ -18,7 +18,7 @@ import kotlin.math.*
|
|||||||
* This sampler is suitable for mean >= 40.
|
* This sampler is suitable for mean >= 40.
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* 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
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/LargeMeanPoissonSampler.html].
|
||||||
*/
|
*/
|
||||||
public class LargeMeanPoissonSampler private constructor(public val mean: Double) : Sampler<Int> {
|
public class LargeMeanPoissonSampler private constructor(public val mean: Double) : Sampler<Int> {
|
||||||
private val exponential: Sampler<Double> = AhrensDieterExponentialSampler.of(1.0)
|
private val exponential: Sampler<Double> = AhrensDieterExponentialSampler.of(1.0)
|
||||||
|
@ -13,7 +13,7 @@ import kotlin.math.sqrt
|
|||||||
* [BoxMullerNormalizedGaussianSampler].
|
* [BoxMullerNormalizedGaussianSampler].
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* 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
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.html]
|
||||||
*/
|
*/
|
||||||
public class MarsagliaNormalizedGaussianSampler private constructor() : NormalizedGaussianSampler, Sampler<Double> {
|
public class MarsagliaNormalizedGaussianSampler private constructor() : NormalizedGaussianSampler, Sampler<Double> {
|
||||||
private var nextGaussian = Double.NaN
|
private var nextGaussian = Double.NaN
|
||||||
|
@ -14,7 +14,7 @@ import kscience.kmath.stat.Sampler
|
|||||||
* Devroye, Luc. (1981). The Computer Generation of Poisson Random Variables Computing vol. 26 pp. 197-207.
|
* Devroye, Luc. (1981). The Computer Generation of Poisson Random Variables Computing vol. 26 pp. 197-207.
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* Based on Commons RNG implementation.
|
||||||
* See https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/PoissonSampler.html
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/PoissonSampler.html].
|
||||||
*/
|
*/
|
||||||
public class PoissonSampler private constructor(mean: Double) : Sampler<Int> {
|
public class PoissonSampler private constructor(mean: Double) : Sampler<Int> {
|
||||||
private val poissonSamplerDelegate: Sampler<Int> = of(mean)
|
private val poissonSamplerDelegate: Sampler<Int> = of(mean)
|
||||||
|
@ -17,7 +17,7 @@ import kotlin.math.exp
|
|||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* 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
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/SmallMeanPoissonSampler.html].
|
||||||
*/
|
*/
|
||||||
public class SmallMeanPoissonSampler private constructor(mean: Double) : Sampler<Int> {
|
public class SmallMeanPoissonSampler private constructor(mean: Double) : Sampler<Int> {
|
||||||
private val p0: Double = exp(-mean)
|
private val p0: Double = exp(-mean)
|
||||||
|
@ -12,7 +12,7 @@ import kotlin.math.*
|
|||||||
* implementation has been adapted from the C code provided therein.
|
* implementation has been adapted from the C code provided therein.
|
||||||
*
|
*
|
||||||
* Based on Commons RNG implementation.
|
* 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
|
* See [https://commons.apache.org/proper/commons-rng/commons-rng-sampling/apidocs/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.html].
|
||||||
*/
|
*/
|
||||||
public class ZigguratNormalizedGaussianSampler private constructor() :
|
public class ZigguratNormalizedGaussianSampler private constructor() :
|
||||||
NormalizedGaussianSampler, Sampler<Double> {
|
NormalizedGaussianSampler, Sampler<Double> {
|
||||||
|
Loading…
Reference in New Issue
Block a user