Skip to content

probability-flow

Discrete causal graphs and exact inference, in pure Python with no dependencies.

You write an argument down as a graph of propositions. Each proposition says how it depends on its parents. The library answers what any of them is worth, given everything or given only part of it.

pip install probability-flow

Thirty seconds

from probability_flow import CausalGraph, CPT

g = CausalGraph("Why is the sidewalk wet?")

rain = g.add_variable("It rained last night")
sprinkler = g.add_variable("The sprinkler ran")
wet = g.add_variable("The sidewalk is wet")

rain.set_prior(0.2)
sprinkler.set_prior(0.4)

rain.causes(wet)
sprinkler.causes(wet)
wet.set_cpd(CPT(causes=[rain, sprinkler], table=[[0.01, 0.90],      # no rain
                                                 [0.90, 0.99]]))    # rain

float(rain.posterior())                                 # 0.2
float(rain.posterior({wet: True}))                      # 0.39
float(sprinkler.posterior({wet: True}))                 # 0.765
float(sprinkler.posterior({wet: True, rain: True}))     # 0.4231

That is the graph:

Three variables and two causal edges, before anything is observed

And this is the same graph once the sidewalk is observed wet, with the amber outline marking what was observed rather than inferred:

The same graph with the sidewalk observed wet

Read the last two numbers together. A wet sidewalk makes the sprinkler likely, and then learning that it rained makes the sprinkler much less likely again, because the rain already accounts for the water. Nothing connects the two causes directly. That is explaining away, and getting it for free is the reason to keep a graph rather than a list of numbers.

And with the rain known too, which pulls the sprinkler back down

Nothing here is approximate. Three engines, all exact, and you do not normally choose between them.

Where to go next

The formalism is the short version of what the pieces mean: the three kinds of edge, the four CPD families, weights in bits, definitional nodes and virtual evidence. Start here if you are going to read or write a graph that somebody else built.

The tutorial, in Colab builds one from nothing and asks it questions, in a notebook you can run and edit rather than only read.

The API reference is everything a consumer calls, grouped by what you are doing rather than by which module it lives in: graphs and CPDs, asking questions, saving and loading, drawing, generating, and the reveal game.

Agents

The library is designed to be used with a coding agent, and the thing that makes that work is that it refuses rather than guesses. An ambiguous CPD, a prior on a caused variable, a definition with nothing constituting it, a question whose cost exceeds a stated ceiling: each one raises, with a message that says what was wrong and what to write instead.

So the useful move is to point an agent at this site and at the installed package, and let the errors teach it. probability_flow has no dependencies, the error messages carry the reasoning, and to_text(graph) prints the whole structure with every CPD row and the current beliefs, which is usually the fastest way to show an agent what state a graph is actually in.