Modelcore
Random Boolean network
Also called Kauffman network, NK Boolean network.
Kauffman's model of gene regulation: N genes, each on or off, updated at once by a random Boolean function of K random inputs.
A random Boolean network, or Kauffman network, is a random gene regulatory network in which each of genes is on or off. Each gene reads other genes, chosen at random, and computes its next value with a random Boolean function of them, and all genes update at the same time. Kauffman proposed the model in 1969 to ask what a typical network of gene control does, without the details of any one cell. A run of a network always ends in a cycle of states, an attractor, and Kauffman read the attractors as the cell types of an organism. The model shows a transition: for unbiased functions, networks with are ordered, with they are chaotic, and is critical.
The machine
The network. There are genes, numbered 1 to . Gene has an input list of distinct genes , which may include , and a Boolean function stored as a truth table of bits.
The random ensemble. For each gene independently, the input list is a uniform random list of distinct genes, and each of the entries of the truth table is 1 with probability , independently. The parameter is the bias; makes every Boolean function of inputs equally likely. Some authors draw the inputs with repetition, which changes the small numbers but not the large limits. A network drawn once and then kept fixed is a quenched network; redrawing it at every step gives the annealed model, which is easier to analyze.
The dynamics. The state is a vector , and all genes update at once:
So the network defines one map , and its state graph, with an edge from to , is a functional graph on states. Every state has one successor, so every run enters a cycle after a transient. The cycles are the attractors, a cycle of length 1 is a fixed point, and the set of states that reach an attractor is its basin. The basins partition the state space.
Finding all attractors. For a small network, one scan over the state space finds every attractor and every basin:
ATTRACTORS(F, N)- 1
label[s] ← none for each of the 2^N states s - 2
for each state s do - 3
path ← empty list; t ← s - 4
while label[t] = none and t is not on path do append t to path; t ← F(t) - 5
if label[t] = none then the states of path from t onward form a new attractor a else a ← label[t] - 6
label[u] ← a for each u on path - 7
return the attractors and, for each one, the number of states with its label
Each state joins one path and gets its label once, so the scan makes steps after the table of is built. Building the table costs time. This is the full state space search of Boolean network attractor detection; for large it is out of reach, and that page describes the methods that avoid it.
An example. Take three genes A, B and C with , and , written as states . Then is a fixed point, reached also from . The state is a fixed point, reached also from . The states and form a cycle of length 2, since , and and lead into it. So there are three attractors, with basins of 2, 2 and 4 states. This network is not random, but it is a member of the ensemble with and .
What it counts
- The number of attractors of a network, and how it grows with .
- The length of each attractor and of the transients that lead to it.
- The size of each basin, as a fraction of the states.
- The spread: the number of genes that differ at the next step when one gene of a state is flipped. Its mean decides whether small changes die out (ordered) or grow (chaotic).
- The frozen genes: those that end with the same value on every attractor, whatever the start.
Relation to other models
- Gene regulatory network. A Boolean model of a real network has the inputs and functions of known regulators. A random Boolean network replaces them by random draws, so its results are about typical networks, not about one cell.
- Cellular automaton. A cellular automaton also has cells with finite states and a synchronous update, but its cells sit on a regular lattice, read their geometric neighbors, and all use the same rule. A random Boolean network has random wiring and a different random rule at each gene.
- Random mappings. With , every entry of the map is an independent uniform state, so is a uniform random map of a set of size to itself, and its attractors are the cycles of that map.
- Asynchronous networks. If one gene updates at a time, in a random or arbitrary order, a state can have several successors, and the attractors become the terminal strongly connected components of the state graph. The attractors of the two schemes differ in general. In the example above, the cycle of and is not an attractor under one at a time updates: from , an update of A alone gives , and an update of C then gives the fixed point .
- Mean field and spin models. The annealed approximation of Derrida and Pomeau treats the network like a mean field model of statistical physics, as for the Ising model, and the order to chaos transition is a percolation like transition of the spread of a change.
Known results in the model
The expected number of fixed points is 1 (theorem). For every , and , the expected number of fixed points of a random network is 1.
Proof. Fix a state . The value is one entry of the truth table of gene , which is 1 with probability whatever the inputs of are. So is if and if . The tables of different genes are independent, so , which is the probability of under independent bits with bias . The sum over all is 1.
The one step spread is (theorem). Draw a random network, a state and a gene , all uniform and independent, and let be the number of genes whose next value changes when is flipped. Then .
Proof. Gene has among its inputs with probability . Then the flip changes the entry of the truth table that reads, from one entry to another one, and two different entries are independent bits with bias , which differ with probability . If is not an input of , does not change. So .
The critical line (annealed approximation). Derrida and Pomeau (1986) followed the distance between two runs in the annealed model and found that a small distance shrinks when and grows when . For the critical value is . This matches the spread above: on average, one flipped gene changes less than one gene at the next step in the ordered phase and more than one in the chaotic phase. The approximation ignores the fixed wiring of a quenched network, so it is a guide for quenched networks, not a proof about them.
Fully random networks (theorem). For the network is a uniform random map on states. The number of states on cycles has , and given , the cycles are those of a uniform permutation of points, so their number has mean and variance . The expected number of cyclic states grows like and the expected number of attractors like (Flajolet and Odlyzko). So attractors in the chaotic limit are few but very long, with lengths of the order of . For the exact mean number of attractors is with variance ; the test of the implementation draws 2000 networks with and measures a mean of , within one standard error.
Critical networks have many attractors. Kauffman's simulations of networks with suggested that the number of attractors grows like , and he compared this with the number of cell types of organisms. That estimate came from sampling a few starting states, which misses small basins. Samuelsson and Troein (2003) showed that the mean number of attractors of critical networks grows faster than any power of .
Networks with one input (theorem). For , each gene is a constant, a copy or a negation of its one input, so the wiring is a set of loops with trees hanging off them. A loop of genes with no constant returns to its state after at most steps, since after steps every gene is flipped by the parity of the negations on the loop. A gene off the loops repeats, with a delay, the gene it reads. So every attractor length divides the least common multiple of the numbers over the loops, and genes that read a constant freeze.
Proof: The attractor scan finds every attractor and basin, and two exact means of the random ensemblefull
Setup. Let be a map of a finite set of states to itself, here and . A state is cyclic if for some . The cycles of are the attractors, and the basin of an attractor is the set of states with for some .
Lemma 1. Every state reaches exactly one cycle, so the basins partition .
Proof. The sequence takes values in a finite set, so for some , and is cyclic. Once the sequence is on a cycle it stays on that cycle, because maps a cycle to itself. Two different cycles are disjoint, since each state has one successor. So the cycle that the sequence enters is unique.
Theorem 1 (the scan). ATTRACTORS returns every cycle of once, and the label of each state is the cycle that it reaches. It makes calls of .
Proof. We show by induction over the scan the invariant: every labeled state carries the cycle that it reaches, and every cycle that contains a labeled state has been reported once. It holds at the start, when no state is labeled. Take the scan of (lines L3 to L6), with unlabeled. Line L4 follows and stops at the first state that is labeled or already on the path; it stops because the states on the path are distinct and is finite.
If is labeled, with cycle , then by the invariant reaches , so every state on the path, which reaches , reaches , and line L6 labels it . No state on the path is on a new cycle: a cyclic state on the path would make the walk return to it before reaching , which line L4 would have caught as a repeat. So no cycle is missed.
If is not labeled, it is on the path, so the walk from returns to , and the part of the path from onward is a cycle . No state of was labeled before, so by the invariant had not been reported, and line L5 reports it now. The states of the path before reach , and line L6 labels all of them .
Each state is appended to a path once, since it is labeled at the end of that scan and later scans stop at labeled states. So the total length of all paths is , and there are calls of . After the scan of all every state is labeled; every cycle contains a labeled state and so has been reported once.
Lemma 2 (the second method of the test). Let and . The sequence is decreasing, it becomes constant, and its limit is the set of cyclic states.
Proof. , and implies . A decreasing sequence of finite sets becomes constant, say , with . Then is onto , so it is a permutation of the finite set , and every state of is cyclic. Conversely, a cyclic state with is for all , so it is in .
Theorem 2 (fixed points). For the random ensemble with any , and bias , the expected number of fixed points is 1.
Proof. For a fixed state , is one entry of the table of gene , a bit that is 1 with probability , independent of the input list. So . The tables of different genes are independent, so . Summing over all gives , and by linearity of expectation this sum is the expected number of fixed points.
Theorem 3 (spread). Draw a network from the ensemble, a state and a gene , independently and uniformly. The expected number of genes with is .
Proof. Fix . If is not in the input list of , the two arguments of are equal and so are the values. If is in the list, which has probability for a uniform list of distinct genes, the two arguments differ in one coordinate, so they are two different entries of the table of . These are independent bits with bias , given the list, and , and they differ with probability . So , and summing over the genes gives .
Theorem 4 (fully random networks). For and , is uniform among all maps of to itself.
Proof. For , the input list of gene is a permutation of all genes, so the entries of its table are in one to one correspondence with the states, and is a separate bit of the table for each . All bits are independent and uniform, so the values are independent and uniform on .
The exact law of the number of cycles of a uniform random map, which the test uses, is checked there against an enumeration of all maps on 3 points.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/random-boolean-network/. The authors are listed in prov/JT779F.jsonl.
"""Random Boolean networks (Kauffman's NK model) and their attractors.
A network on n genes is a list of (inputs, table) pairs, one per gene. inputs
is a tuple of K distinct genes, and table is a list of 2^K bits: the next
value of the gene is table[sum(x[inputs[r]] << r)]. A state is an integer
whose bit i is the value of gene i, so there are 2^n states.
Random draws. random_network(rng, n, k, p) draws, gene by gene in index
order, first rng.sample(range(n), k) for the inputs, then 2^k calls of
rng.random(), one per table entry in index order; an entry is 1 when the
draw is below p.
"""
from __future__ import annotations
from typing import Dict, List, Sequence, Tuple
Network = List[Tuple[Tuple[int, ...], List[int]]]
def random_network(rng, n: int, k: int, p: float = 0.5) -> Network:
"""Kauffman's random network: K distinct random inputs and a random table with bias p per gene."""
if not 0 <= k <= n:
raise ValueError("need 0 <= k <= n")
net: Network = []
for _ in range(n):
inputs = tuple(rng.sample(range(n), k))
table = [1 if rng.random() < p else 0 for _ in range(1 << k)]
net.append((inputs, table))
return net
def step(net: Network, state: int) -> int:
"""Synchronous update: every gene reads its inputs in the old state."""
out = 0
for i, (inputs, table) in enumerate(net):
idx = 0
for r, g in enumerate(inputs):
idx |= ((state >> g) & 1) << r
out |= table[idx] << i
return out
def successor_table(net: Network) -> List[int]:
"""The map F on all 2^n states, as a list."""
return [step(net, s) for s in range(1 << len(net))]
def attractors(succ: Sequence[int]) -> Tuple[List[List[int]], List[int], List[int]]:
"""Cycles of the functional graph s -> succ[s], with basin sizes.
Returns (cycles, basin, label): cycles[a] lists the states of attractor a
in the order of the dynamics, starting from its smallest state; basin[a]
is the number of states that reach it (the cycle included); label[s] is
the attractor that state s reaches. Attractors are numbered in the order
in which the scan over s = 0, 1, ... finds them. Time O(2^n).
"""
size = len(succ)
label = [-1] * size # @line L1
mark = [-1] * size # the scan start that put a state on the current path
cycles: List[List[int]] = []
for s in range(size): # @line L2
if label[s] != -1:
continue
path = [] # @line L3
t = s
while label[t] == -1 and mark[t] != s: # @line L4
mark[t] = s
path.append(t)
t = succ[t]
# if t has no label, it is on the current path, and the path closes a new cycle
if label[t] == -1: # @line L5
start = path.index(t)
cyc = path[start:]
r = cyc.index(min(cyc))
cycles.append(cyc[r:] + cyc[:r])
a = len(cycles) - 1
else:
a = label[t]
for u in path: # @line L6
label[u] = a
basin = [0] * len(cycles)
for s in range(size):
basin[label[s]] += 1
return cycles, basin, label # @line L7
def attractors_by_image(succ: Sequence[int]) -> Tuple[List[List[int]], Dict[int, int]]:
"""A second method: the cyclic states are the limit of X, F(X), F(F(X)), ... from X = all states.
Returns the cycles (each from its smallest state, sorted by that state) and
a map from the smallest state of each cycle to its basin size, where the
basin is found by iterating each state until it reaches a cyclic state.
"""
x = set(range(len(succ)))
while True:
y = {succ[s] for s in x}
if y == x:
break
x = y
cycles = []
seen = set()
for s in sorted(x):
if s in seen:
continue
cyc = [s]
t = succ[s]
while t != s:
cyc.append(t)
t = succ[t]
seen.update(cyc)
cycles.append(cyc)
rep = {}
for cyc in cycles:
for u in cyc:
rep[u] = cyc[0]
basin: Dict[int, int] = {}
for s in range(len(succ)):
t = s
while t not in rep:
t = succ[t]
basin[rep[t]] = basin.get(rep[t], 0) + 1
return cycles, basin
def hamming_spread(net: Network, state: int, gene: int) -> int:
"""Number of genes whose next value changes when one gene of the state is flipped."""
return bin(step(net, state) ^ step(net, state ^ (1 << gene))).count("1")
test_random_boolean_network.py 136 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/random-boolean-network/. The authors are listed in prov/JT779F.jsonl.
import math
import random
from fractions import Fraction
from random_boolean_network import (
attractors,
attractors_by_image,
hamming_spread,
random_network,
step,
successor_table,
)
def bits(state, n):
return [(state >> i) & 1 for i in range(n)]
def harmonic(k, power=1):
return sum((Fraction(1, i ** power) for i in range(1, k + 1)), Fraction(0))
def random_map_cycle_moments(size):
"""Exact mean and variance of the number of cycles of a uniform random map on size points.
P(k cyclic points) = (k / size) prod_{i < k} (1 - i / size); given k cyclic points, the
cycles are those of a uniform permutation of k points, with mean H_k and variance H_k - H_k^(2).
"""
mean = Fraction(0)
second = Fraction(0)
prod = Fraction(1)
for k in range(1, size + 1):
pk = Fraction(k, size) * prod
prod *= 1 - Fraction(k, size)
h1, h2 = harmonic(k), harmonic(k, 2)
mean += pk * h1
second += pk * (h1 - h2 + h1 * h1)
return mean, second - mean * mean
def test_step_reads_the_old_state():
rng = random.Random(1)
for _ in range(50):
n = rng.randint(1, 7)
net = random_network(rng, n, rng.randint(0, n), rng.random())
s = rng.randrange(1 << n)
x = bits(s, n)
want = [table[sum(x[g] << r for r, g in enumerate(inputs))] for inputs, table in net]
assert bits(step(net, s), n) == want
def test_worked_example_of_the_page():
# A' = B, B' = A, C' = A and B; gene i is bit i of the state
net = [((1,), [0, 1]), ((0,), [0, 1]), ((0, 1), [0, 0, 0, 1])]
cycles, basin, label = attractors(successor_table(net))
assert cycles == [[0], [1, 2], [7]]
assert label[4] == 0 and label[5] == label[6] == 1 and label[3] == 2
got = sorted((tuple(c), b) for c, b in zip(cycles, basin))
assert got == [((0,), 2), ((1, 2), 4), ((7,), 2)]
def test_attractors_match_a_second_method_up_to_n_12():
rng = random.Random(2)
cases = [(n, k) for n in range(1, 9) for k in range(0, min(n, 4) + 1)] + [(10, 2), (11, 3), (12, 2), (12, 1)]
for n, k in cases:
for p in (0.5, 0.2):
net = random_network(rng, n, k, p)
succ = successor_table(net)
cycles, basin, label = attractors(succ)
cycles2, basin2 = attractors_by_image(succ)
assert sorted(cycles) == cycles2
assert {c[0]: b for c, b in zip(cycles, basin)} == basin2
assert sum(basin) == 1 << n
cyclic = {u: a for a, c in enumerate(cycles) for u in c}
for s in range(1 << n):
# label[s] is the attractor that s reaches
t = s
while t not in cyclic:
t = succ[t]
assert cyclic[t] == label[s]
def test_exact_random_map_moments_small_cases():
# by enumeration of all maps on 3 points: 27 maps
import itertools
size = 3
counts = []
for f in itertools.product(range(size), repeat=size):
cycles, _, _ = attractors(list(f))
counts.append(len(cycles))
mean = Fraction(sum(counts), len(counts))
var = Fraction(sum(c * c for c in counts), len(counts)) - mean * mean
assert (mean, var) == random_map_cycle_moments(size)
def test_k_equal_n_is_a_uniform_random_map():
# measured mean number of attractors against the exact mean, 2000 networks with n = K = 4
rng = random.Random(3)
n, reps = 4, 2000
mean, var = random_map_cycle_moments(1 << n)
total = 0
for _ in range(reps):
cycles, _, _ = attractors(successor_table(random_network(rng, n, n, 0.5)))
total += len(cycles)
z = (total / reps - float(mean)) / math.sqrt(float(var) / reps)
assert abs(z) < 4
def test_expected_number_of_fixed_points_is_one():
rng = random.Random(4)
reps = 1000
for n, k, p in ((5, 2, 0.3), (4, 4, 0.5)):
xs = []
for _ in range(reps):
succ = successor_table(random_network(rng, n, k, p))
xs.append(sum(1 for s, t in enumerate(succ) if s == t))
m = sum(xs) / reps
sd = math.sqrt(sum((x - m) ** 2 for x in xs) / (reps - 1))
assert abs(m - 1.0) < 4 * sd / math.sqrt(reps)
def test_one_step_spread_is_2p_1_minus_p_k():
rng = random.Random(5)
reps = 3000
for n, k, p in ((10, 2, 0.5), (8, 3, 0.2)):
xs = []
for _ in range(reps):
net = random_network(rng, n, k, p)
xs.append(hamming_spread(net, rng.randrange(1 << n), rng.randrange(n)))
m = sum(xs) / reps
sd = math.sqrt(sum((x - m) ** 2 for x in xs) / (reps - 1))
assert abs(m - 2 * p * (1 - p) * k) < 4 * sd / math.sqrt(reps)
References
- Stuart A. Kauffman, Metabolic stability and epigenesis in randomly constructed genetic nets, Journal of Theoretical Biology 22(3), 437-467, 1969, the definition of the random nets with N genes and K inputs, and the simulations of their cycles
- Bernard Derrida and Yves Pomeau, Random networks of automata, a simple annealed approximation, Europhysics Letters 1(2), 45-49, 1986, the annealed approximation for the distance between two runs and the transition between order and chaos
- Björn Samuelsson and Carl Troein, Superpolynomial growth in the number of attractors in Kauffman networks, Physical Review Letters 90, 098701, 2003, the number of attractors of critical networks with K = 2
- Philippe Flajolet and Andrew M. Odlyzko, Random mapping statistics, Advances in Cryptology, EUROCRYPT 89, LNCS 434, 329-354, 1990, the number of cyclic points and of components of a random mapping
- Stuart A. Kauffman, The Origins of Order, Self-Organization and Selection in Evolution, Oxford University Press, 1993, the chapters on random Boolean networks as models of genomic regulation
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag JT779F Edit this page History Report an error Markdown