= Solution
Implement the <Bernoulli distribution>, taking special care that a zero-mass outcome is excluded from the <support of a probability distribution>:
``
class Flip(p: Double) extends FiniteDist[Boolean] {
require(!p.isNaN && 0.0 <= p && p <= 1.0)
def getDom: List[Boolean] =
if (p == 0.0) List(false)
else if (p == 1.0) List(true)
else List(true, false)
def getProb(x: Boolean): Double =
if (x) p else 1.0 - p
}
``
The <precondition> excludes NaN and all values outside $[0,1]$, including infinities. The two masses are nonnegative and sum mathematically to one. \b[Endpoint domains are singleton lists], while an interior parameter lists both outcomes exactly once. Treating `getDom` as always containing both <Booleans> would violate its positive-support contract at the endpoints.
Back to article page