Skip to content

Simulation

simulate_graph(κ, μ, η, S, nv, ns, key=KEY)

Simulate a random graph. Return the vertex type indices and vertex types sampled from the type distribution, vertex thresholds from the threshold distribution, and adjacency matrix sampled from the kernel.

Parameters:

Name Type Description Default
κ Kernel

kernel function

required
μ TypeMeasure

type measure

required
η ThresholdMeasure

threshold distribution

required
nv VertexNum

number of vertices

required
ns TypeNum

number of types

required
S TypeDomain

type domain

required
key Key

random key

KEY

Returns:

Name Type Description
vsid VertexTypeIndex

vertex type indices

vs VertexType

vertex types

vk VertexThreshold

vertex thresholds

A AdjacencyMatrix

adjacency matrix

Source code in src/bprg/simulation.py
def simulate_graph(
    κ: Kernel,
    μ: TypeMeasure,
    η: ThresholdMeasure,
    S: TypeDomain,
    nv: VertexNum,
    ns: TypeNum,
    key: Key = KEY,
) -> tuple[VertexTypeIndex, VertexType, VertexThreshold, AdjacencyMatrix]:
    """
    Simulate a random graph. Return the vertex type indices and vertex types sampled from the type distribution,
    vertex thresholds from the threshold distribution, and adjacency matrix sampled from the kernel.

    Args:
        κ: kernel function
        μ: type measure
        η: threshold distribution
        nv: number of vertices
        ns: number of types
        S: type domain
        key: random key

    Returns:
        vsid: vertex type indices
        vs: vertex types
        vk: vertex thresholds
        A: adjacency matrix
    """
    s = jnp.linspace(S[0], S[1], ns)
    Δμ_s = μ(s)
    vsid, vs, vk, A = _simulate_graph(κ, η, s, Δμ_s, nv, key)
    return vsid, vs, vk, A

simulate_percolation_final(vk, A)

Simulate bootstrap percolation and return the final threshold vector.

Parameters:

Name Type Description Default
vk VertexThreshold

vertex thresholds

required
A AdjacencyMatrix

adjacency matrix

required

Returns: vk_final: threshold vector after the process stabilizes

Source code in src/bprg/simulation.py
def simulate_percolation_final(vk: VertexThreshold, A: AdjacencyMatrix) -> VertexThreshold:
    """Simulate bootstrap percolation and return the final threshold vector.

    Args:
        vk: vertex thresholds
        A: adjacency matrix
    Returns:
        vk_final: threshold vector after the process stabilizes
    """
    return _simulate_percolation_final(vk, A)

simulate_percolation_history(vk, A)

Simulate bootstrap percolation on a graph.

Parameters:

Name Type Description Default
vk VertexThreshold

vertex thresholds

required
A AdjacencyMatrix

adjacency matrix

required

Returns:

Name Type Description
vk_hist Array[VertexThreshold]

vertex thresholds at each step, shape (n_steps+1, nv)

ea_hist Array[bool]

boolean adjacency masks of the edges leaving that step's newly-infected vertices, shape (n_steps+1, nv, nv). ea_hist[t, i, j] is True iff vertex i became infected at step t and (i, j) is an edge of A — i.e. the edges that "caused" new infections downstream.

Source code in src/bprg/simulation.py
def simulate_percolation_history(vk: VertexThreshold, A: AdjacencyMatrix) -> tuple[Array[VertexThreshold], Array[bool]]:
    """Simulate bootstrap percolation on a graph.

    Args:
        vk: vertex thresholds
        A: adjacency matrix

    Returns:
        vk_hist: vertex thresholds at each step, shape (n_steps+1, nv)
        ea_hist: boolean adjacency masks of the edges leaving that step's
            newly-infected vertices, shape (n_steps+1, nv, nv).
            ea_hist[t, i, j] is True iff vertex i became infected at step t
            and (i, j) is an edge of A — i.e. the edges that "caused"
            new infections downstream.
    """
    vk_cur = vk
    i_all = vk == 0
    i_new = i_all

    vk_hist = [vk_cur]
    ea_hist = [_active_edges(i_new, A)]

    while jnp.any(i_new):
        vk_new, i_all, i_new = _percolate_step(vk_cur, i_new, i_all, A)
        vk_hist.append(vk_new)
        ea_hist.append(_active_edges(i_new, A))
        vk_cur = vk_new

    return jnp.stack(vk_hist, axis=0), jnp.stack(ea_hist, axis=0)

estimate_infection(κ, μ, η, S, nv, ns, nm, key=KEY)

Monte Carlo estimate of final proportion of infected vertices by type after the bootstrap percolation.

Parameters:

Name Type Description Default
κ Kernel

kernel function

required
μ TypeMeasure

type measure

required
η ThresholdMeasure

threshold distribution

required
S TypeDomain

type domain

required
nv VertexNum

number of vertices

required
ns TypeNum

number of types

required
nm SimulationNum

number of simulations

required
key Key

random key

KEY

Returns:

Name Type Description
pi_s Array[float]

final proportion of infected vertices by type, shape (ns,)

Source code in src/bprg/simulation.py
def estimate_infection(
    κ: Kernel,
    μ: TypeMeasure,
    η: ThresholdMeasure,
    S: TypeDomain,
    nv: VertexNum,
    ns: TypeNum,
    nm: SimulationNum,
    key: Key = KEY,
) -> Array[float]:
    """
    Monte Carlo estimate of final proportion of infected vertices by type after the bootstrap percolation.

    Args:
        κ: kernel function
        μ: type measure
        η: threshold distribution
        S: type domain
        nv: number of vertices
        ns: number of types
        nm: number of simulations
        key: random key

    Returns:
        pi_s: final proportion of infected vertices by type, shape (ns,)
    """
    return _estimate_infection(κ, μ, η, S, nv, ns, nm, key)

estimate_infection_apportioned_type(κ, μ, η, S, nv, ns, nm, key=KEY)

Monte Carlo estimate of final proportion of infected vertices by type after the bootstrap percolation, using apportioned types to reduce variance.

Parameters:

Name Type Description Default
κ Kernel

kernel function

required
μ TypeMeasure

type measure

required
η ThresholdMeasure

threshold distribution

required
S TypeDomain

type domain

required
nv VertexNum

number of vertices

required
ns TypeNum

number of types

required
nm SimulationNum

number of simulations

required
key Key

random key

KEY

Returns:

Name Type Description
pi_s Array

final proportion of infected vertices by type, shape (ns,)

Source code in src/bprg/simulation.py
def estimate_infection_apportioned_type(
    κ: Kernel,
    μ: TypeMeasure,
    η: ThresholdMeasure,
    S: TypeDomain,
    nv: VertexNum,
    ns: TypeNum,
    nm: SimulationNum,
    key: Key = KEY,
) -> Array:
    """
    Monte Carlo estimate of final proportion of infected vertices by type after the bootstrap percolation,
    using apportioned types to reduce variance.

    Args:
        κ: kernel function
        μ: type measure
        η: threshold distribution
        S: type domain
        nv: number of vertices
        ns: number of types
        nm: number of simulations
        key: random key

    Returns:
        pi_s: final proportion of infected vertices by type, shape (ns,)
    """
    return _estimate_infection_apportioned_type(κ, μ, η, S, nv, ns, nm, key)

count_infected_by_type(vs, vk, S, ns)

Count infected vertices (vk == 0) by vertex type.

Parameters:

Name Type Description Default
vs VertexType

vertex type vector

required
vk VertexThreshold

vertex threshold vector

required
S TypeDomain

type domain

required
ns TypeNum

number of types

required

Returns:

Name Type Description
n_si Array

infected count per type, shape (ns,)

Source code in src/bprg/simulation.py
def count_infected_by_type(
    vs: VertexType,
    vk: VertexThreshold,
    S: TypeDomain,
    ns: TypeNum,
) -> Array:
    """
    Count infected vertices (vk == 0) by vertex type.

    Args:
        vs: vertex type vector
        vk: vertex threshold vector
        S: type domain
        ns: number of types

    Returns:
        n_si: infected count per type, shape (ns,)
    """
    s = jnp.linspace(S[0], S[1], ns)
    type_idx = jnp.searchsorted(s, vs)
    infected = (vk == 0).astype(jnp.int32)
    n_si = jnp.bincount(type_idx, weights=infected, length=ns)
    return n_si

count_infected_by_typeindex(vsid, vk, ns)

Count infected vertices (vk == 0) by vertex type index.

Parameters:

Name Type Description Default
vsid VertexTypeIndex

vertex type indices

required
vk VertexThreshold

vertex threshold vector

required
ns TypeNum

number of types

required

Returns:

Name Type Description
n_si Array

infected count per type, shape (ns,)

Source code in src/bprg/simulation.py
def count_infected_by_typeindex(
    vsid: VertexTypeIndex,
    vk: VertexThreshold,
    ns: TypeNum,
) -> Array:
    """
    Count infected vertices (vk == 0) by vertex type index.

    Args:
        vsid: vertex type indices
        vk: vertex threshold vector
        ns: number of types

    Returns:
        n_si: infected count per type, shape (ns,)
    """
    infected = (vk == 0).astype(jnp.int32)
    n_si = jnp.bincount(vsid, weights=infected, length=ns)
    return n_si