Skip to content

Generating graphs

probability_flow.generation builds causal graphs at random with stated properties. It exists because a benchmark needs many graphs that differ in ways somebody chose, rather than a handful somebody wrote.

from probability_flow.generation import GraphSpec, StructureSpec, generate

spec = GraphSpec(
    structures=(StructureSpec(n=2, m=1, size=5),),
    conjunction_prob=0.2,
    gate_prob=0.3,
    definitional_prob=0.3,
)
g = generate(spec, seed=7)

len(g.variables)                    # 11
len(g.edges())                      # 12
g.hypothesis.name                   # 'H'
float(g.hypothesis.posterior())     # 0.056391

The same seed gives the same document

Byte for byte, including the order the variables appear in. That is what makes a generated graph usable as a test fixture and what makes a result reproducible from a number in a log.

generate(spec, seed=7).to_json() == generate(spec, seed=7).to_json()   # True

Each graph records how it was made, so a document found later can be traced back to the call that produced it:

g.metadata
# {'seed': 7, 'generator': 'probability_flow.generate',
#  'generator_version': '1.5.0', 'generator_commit': '...'}

The version and the commit are whichever produced the document, so a graph found later can be regenerated with the library that made it rather than with whatever is installed now.

The shape knobs

A graph is grown as one or more structures, each attached to the hypothesis. StructureSpec(n, m, size) says how many edges run into the hypothesis, how many run out of it, and how many nodes the structure is grown to before attaching. So n=1, m=0 is a cause cone, n=0, m=1 is an effect cone, and both non-zero puts the hypothesis inside a loop.

Everything else is on GraphSpec. The ones worth knowing first:

Knob What it does
p_cause whether a new node attaches as a cause of its anchor or an effect
share_prob how often a new edge lands on an existing node instead of a new one, which is what creates the loops
anchor_rule uniform, recent or degree, which is the main dial on overall shape
conjunction_prob, disjunction_prob how often a node gets a noisy conjunction or disjunction. The remainder is independent evidence
definitional_prob how often an undetermined node with two or more parents becomes a definition
gate_prob rolled per EDGE, not per node. A gate is a new node that targets one edge
weight_bits_range the magnitude of a weight, in bits
p_attack how often a weight is negative, meaning the parent argues against its child

Weights are in bits throughout, and a negative one is an attack. gate_prob is rolled per edge because a gate belongs to an edge rather than to a node: it replaces that edge's weight rather than scaling it, so it can weaken an edge, strengthen it, or reverse its sign.

What a generated graph guarantees

from probability_flow.generation import check_reveal_legal
check_reveal_legal(g)      # raises if the graph could not be used in a reveal game

Two properties are worth stating because they are easy to assume and expensive to discover missing.

The graph is connected, and every node is reachable from the hypothesis by undirected edges. A node that is not reachable can never be shown to anybody, so it would contribute to the true posterior while being impossible to put in front of a reader.

Nothing in the document records the sampled world. The generator draws a world to decide what an observation says, and that world is not written into the file. generate checks its own output before returning it, so a document that carried the answer would raise rather than ship.

from probability_flow.generation import leaks_ground_truth
leaks_ground_truth(json.loads(g.to_json()))      # []

Many at once

from probability_flow.generation import generate_batch
graphs = generate_batch(spec, n=100)

Some specifications cannot be satisfied for some seeds, and on_failure decides what happens then. The default skips, so a batch of 100 may come back shorter, and the alternative raises so you find out which seed and why.

What the defaults are for

They produce a small, legible graph. Every knob above moves one property of the result without moving the others, so a specification can be changed one dial at a time and the effect read off.