horse/prob: add basic distribution operations
This commit is contained in:
@@ -34,3 +34,25 @@ pub tail fun foldl(p: pmf<s, v>, init: a, f: (a, s, v) -> e a): e a
|
||||
// Convert the distribution to a list of entries.
|
||||
pub fun pmf/list(p: pmf<s, v>): 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<s, v>, b: pmf<s, v>, ?s/cmp: (a: s, b: s) -> order, ?v/(*): (a: v, b: v) -> e v): e pmf<s, v>
|
||||
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<s, v>, b: pmf<s, v>, ?s/cmp: (a: s, b: s) -> order, ?v/(+): (a: v, b: v) -> e v): e pmf<s, v>
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user