diff --git a/horse/prob/pmf.kk b/horse/prob/pmf.kk index c596aea..7886a9f 100644 --- a/horse/prob/pmf.kk +++ b/horse/prob/pmf.kk @@ -34,3 +34,25 @@ pub tail fun foldl(p: pmf, init: a, f: (a, s, v) -> e a): e a // Convert the distribution to a list of entries. pub fun pmf/list(p: pmf): list<(s, v)> p.foldl(Nil) fn(l, s, v) Cons((s, v), l) + +// Distribution of cooccurrence of two events described by their distributions. +pub fun (*)(a: pmf, b: pmf, ?s/cmp: (a: s, b: s) -> order, ?v/(*): (a: v, b: v) -> e v): e pmf + match a + End -> End + Event(sa, va, nexta) -> match b + End -> End + Event(sb, vb, nextb) -> match sa.cmp(sb) + Lt -> nexta * b + Eq -> Event(sa, va * vb, nexta * nextb) + Gt -> a * nextb + +// Distribution of occurrence of at least one of two events described by their distributions. +pub fun (+)(a: pmf, b: pmf, ?s/cmp: (a: s, b: s) -> order, ?v/(+): (a: v, b: v) -> e v): e pmf + match a + End -> b + Event(sa, va, nexta) -> match b + End -> a + Event(sb, vb, nextb) -> match sa.cmp(sb) + Lt -> Event(sa, va, nexta + b) + Eq -> Event(sa, va + vb, nexta + nextb) + Gt -> Event(sb, vb, a + nextb)