Probability Flow: a cheat sheet¶
The short version of the formalism: what the pieces mean, and why they are shaped the way they are. Saving and loading has the file format, and the API reference has the calls.
Every code block here is executed by the test suite, and every number in it was measured rather than recalled, so a claim that drifts out of step with the library fails a test rather than misleading a reader.
What the library does¶
You write down an argument as a graph of propositions. Each proposition says how it depends on its parents. The library computes exact posteriors, so you can ask what a hypothesis is worth given everything, or given only part of it.
Nothing is approximate. Three engines, and you do not normally choose between
them: posterior() uses variable elimination and marginals() uses a junction
tree. The third is enumeration, which sums the whole joint. It is the reference the
other two are tested against and it is never selected for you, because it is both
the slowest and, measurably, the least accurate.
A graph is three things¶
Variables, edges, and a CPD on each variable. A CPD says how a node behaves given its parents. A node with no parents gets a prior instead.
Edges come in three kinds¶
Causal. The parent is a cause of the child.
Constitutive. The parent is part of what the child means. These point into a definitional node, and a definitional node's incoming edges are all constitutive.
Gating. The parent changes how much ANOTHER parent's edge carries. A gate is a multiplexer over an edge: it replaces that edge's weight rather than scaling it, so it can weaken the edge, strengthen it, or reverse its sign. Undercutting is the case where the gated weight is zero.
A gate does not point at the child the way the other two do. It points at an EDGE into the child, and that is how it is drawn: a dashed line landing on the edge it modulates, with a switch on it. The gate source does also become a parent of the child, because the arithmetic needs it to be, and declaring the gate is what draws that edge.
In the file it is not a kind of its own, and that is a fact about the schema
rather than about the formalism: a gated edge is written causal, and what
makes it gating is the gates entry in the child's CPD naming the source and
which parent's edge it modulates. The test for a gating edge is whether the
source appears in gates.
GATES WORK IN ONE CPD FAMILY. IndependentEvidence reads them, and Table
has no notion of one. That is also a fact about the code rather than the formalism,
since a table can express a multiplexer directly by making the gated parent's
rows depend on the gate source's state.
Issue 34
is where that would go, along with the rest of what Table is missing.
Four CPD families¶
Independent evidence. The additive one, and the common one. A baseline plus one weight per parent. Use it when parents each contribute their own support.
Noisy OR and noisy AND. Each input has an activation, the chance it does its job when satisfied. Noisy OR for "any of these could bring it about", noisy AND for "all of these are needed".
Logical. Deterministic. and, or, at_least, exactly, at_most over
constituents. This is what a definitional node gets.
Table. Explicit rows. Always available, rarely what you want.
Weights are in bits, and they are signed¶
A weight is a conditional log-odds ratio, in bits. It is the full swing in the child's log-odds between that parent holding and not holding, with the others fixed.
It is not a likelihood ratio. The familiar identity where log likelihood ratios add up to a posterior describes a credence assembled from a node's independent children, which is a different quantity at a different node.
Negative weights are allowed and they mean attack. A parent with a negative weight argues against its child. Only independent evidence can express this. Neither noisy family can, because every noisy factor is at most one, so adding a parent can only push one way.
Baseline¶
The baseline is the probability of the child when all its parents are false. It is a claim about the world, so you can write a sentence about it.
Virtual evidence¶
A virtual evidence node hangs off a variable and carries two numbers, the probability of the observation if the variable is true and if it is false.
These are the only directly observed nodes in our graphs. Observing one does not cut the path through its parent. It reweights the parent rather than fixing it, so evidence still flows upward past it. That property is what the whole construction rests on.
Definitional nodes¶
A definitional node is one its parents constitute rather than cause. "Someone took it" is constituted by Alice taking it, or Bob, or Carol.
A definition over one constituent is an identity, and a definitional node wants a
logical CPD rather than independent evidence, because its constituents do not
each add a separate increment of support. Both of those are advice. The library
allows a one-constituent definition and allows an additive CPD on a definitional
node, so neither is a guard you can lean on.
The hypothesis may well be definitional, and in the canonical example it is:
speeding-camera.json asks about "the car exceeded the speed limit", which is an
and over the speed and the limit.
The reveal game¶
Showing somebody part of a graph and asking what that part warrants is an
application built on the library rather than part of the formalism. It has its
own page: The reveal game, covering implicit priors, what
induce returns, and the composite baseline.
The file format¶
Saving and loading has the document in full, every CPD type, and what a reader does with a version it does not recognise. One thing about it is formalism rather than schema and so belongs here too:
A gated edge is written causal, and the test for a gating edge is whether
the source appears in the child's gates. Do not test the weight. A weight of
zero is what the library writes for a gate source carrying no evidence of its
own, but an ordinary cause can honestly carry zero too.
The API, briefly¶
import probability_flow as pf
g = pf.CausalGraph("example")
rain = g.add_variable("It rained last night")
wet = g.add_variable("The sidewalk is wet")
rain.set_prior(0.3)
g.add_edge(rain, wet)
wet.set_cpd(pf.IndependentEvidence(baseline=0.05, weights={rain: 3.0}))
forecast = rain.add_evidence("the forecast", p_if=0.8, p_if_not=0.3)
g.hypothesis = wet
float(wet.posterior()) # 0.123889, nothing observed
float(wet.posterior({forecast: True})) # 0.181358, the forecast said rain
text = pf.to_json(g)
back = pf.from_json(text) # round-trips exactly
assert pf.to_json(back) == text
print(pf.to_text(g)) # the graph in words, no dependencies
add_evidence hands the node back, which is how you hold on to it. A variable
carries at most one, so rain.evidence reaches it later. It is a single node and
not a collection, so there is nothing to subscript.
pf.to_text(g) and pf.render(g) are functions rather than methods on the graph.
g.render() happens to exist as well. g.to_text() does not.
Pass {variable: None} in an observation dict to un-observe one thing while
leaving the rest. That is the cheapest way to ask what one witness was worth.
Where to look next¶
Graphs and CPDs is the calls that build one. Asking questions is posteriors, observations and the engines. Drawing a graph covers the figures and the text renderer. Generating graphs covers making them at random with stated properties.
Run the tutorial in Colab builds a graph from nothing and asks it questions, in a notebook you can edit.
Two worked documents ship with the source distribution, and the site serves them: speeding-camera.json is the short one for a definition and a gate, and twelve-angry-men.json is a 51-variable version of Twelve Angry Men, with latent common causes for facts that travel together and gates running in both directions.