Created
June 4, 2018 20:53
-
-
Save cqfd/dda9a78564f05f9ea9aa56f9a5c61eb9 to your computer and use it in GitHub Desktop.
Rainier Beta distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.stripe.rainier.compute._ | |
import com.stripe.rainier.core._ | |
object Beta { | |
def apply(a: Real, b: Real): Continuous = new Continuous { | |
def realLogDensity(p: Real): Real = | |
If(p <= 0, | |
Real.zero.log, | |
If(p >= 1, | |
Real.zero.log, | |
(a - 1) * p.log + (b - 1) * (1 - p).log - Combinatrics.beta(a, b))) | |
def param: RandomVariable[Real] = { | |
val x = new Variable | |
val logistic = Real.one / (Real.one + (x * -1).exp) | |
val logisticJacobian = logistic * (1 - logistic) | |
val density = realLogDensity(logistic) + logisticJacobian.log | |
RandomVariable(logistic, density) | |
} | |
def generator: Generator[Double] = Generator.require(Set(a, b)) { (r, n) => | |
val z1 = Gamma(a, 1).generator.get(r, n) | |
val z2 = Gamma(b, 1).generator.get(r, n) | |
z1 / (z1 + z2) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment