haifengl / smile

Statistical Machine Intelligence & Learning Engine
https://haifengl.github.io
Other
5.97k stars 1.13k forks source link

Gamma random number generator support only integer shape parameter. #746

Closed phrmoy closed 10 months ago

phrmoy commented 11 months ago

Describe the bug Trying to call rand() from a ExponentialFamilyMixture object results in the following exception:

[E] Exception in thread "main" java.lang.IllegalArgumentException: Gamma random number generator support only integer shape parameter. [E] at smile.stat.distribution.GammaDistribution.rand(GammaDistribution.java:155) [E] at smile.stat.distribution.Mixture.rand(Mixture.java:194)

Expected behavior Expected random variate from Mixture model.

Actual behavior

The algorithm throws an exception. Here is the stack trace:

[E] Exception in thread "main" java.lang.IllegalArgumentException: Gamma random number generator support only integer shape parameter. [E] at smile.stat.distribution.GammaDistribution.rand(GammaDistribution.java:155) [E] at smile.stat.distribution.Mixture.rand(Mixture.java:194)

Code snippet

val days = List(6, 14, 197, 483, 502, 193, 221, 122, 252, 145, 266, 312, 284, 172, 744, 150, 664, 289, 451, 180)
val a = new Mixture.Component(0.3, new GaussianDistribution(0.0, 1.0))
val b = new Mixture.Component(0.3, new ExponentialDistribution(1.0))
val c = new Mixture.Component(0.4, new GammaDistribution(1.0, 2.0))
val kernel = ExponentialFamilyMixture.fit(days.map(_.toDouble).toArray, a, b, c)
println(kernel.rand())

Input data List(6, 14, 197, 483, 502, 193, 221, 122, 252, 145, 266, 312, 284, 172, 744, 150, 664, 289, 451, 180)

Additional context

haifengl commented 11 months ago

The fitted shape parameter of gamma distribution is about 2.2. Our implementation supports only integer value of shape parameter by design. It is not a bug.