Skip to main content

avoidance-cascade-python

Python implementation of avoidance cascade detection for ternary agents.

Agents hold one of three states: +1 (engaged), 0 (neutral), −1 (avoiding). The library detects when avoidance begins to cascade through a population, models how it spreads, identifies tipping points, and simulates interventions.

Installation

pip install avoidance-cascade

For development:

git clone https://github.com/SuperInstance/avoidance-cascade-python.git
cd avoidance-cascade-python
pip install -e ".[dev]"

Quick Start

Detect a cascade

from avoidance_cascade import CascadeDetector

det = CascadeDetector(window_size=10, threshold=0.5)

# Each call is one time-step; pass a list of agent states
det.observe([1, 0, -1, -1, -1, 0, -1, 1, -1, -1])  # 6/10 avoiding → alert!

print(det.avoidance_ratio)  # 0.6
print(len(det.alerts))      # 1

Model contagion spread

from avoidance_cascade import SpreadModel

model = SpreadModel(population_size=200, contagion_rate=0.35, contact_degree=5, seed=42)
model.seed_avoiders([0, 1, 2])

result = model.simulate(steps=50)
print(f"Peak avoidance: {result.peak_avoidance_ratio:.2%} at step {result.peak_avoidance_step}")
print(f"Estimated R0:   {result.r0_estimate:.3f}")

Detect tipping points

from avoidance_cascade import TippingPointDetector

tp = TippingPointDetector(method="combined", threshold=0.5)
events = tp.analyze([0.05, 0.10, 0.20, 0.45, 0.55, 0.80])

for e in events:
    print(f"Step {e.step}: {e.method} — {e.metric_before:.2f} → {e.metric_after:.2f}")

Simulate interventions

from avoidance_cascade import SpreadModel, InterventionSimulator, Strategy

model = SpreadModel(population_size=100, contagion_rate=0.4, contact_degree=4, seed=42)
model.seed_avoiders([0, 1])

sim = InterventionSimulator(model, seed=42)

# Compare all strategies
results = sim.compare_all(apply_at_step=10, steps=50)
for r in results:
    print(f"{r.strategy.value:15s}  reduction={r.reduction:+.2%}  final={r.final_avoidance_ratio:.2%}")

API Reference

CascadeDetector

Parameter Type Default Description
window_size int 10 Rolling window length
threshold float 0.5 Avoidance ratio trigger
alert_handler Callable None Callback for alerts

Methods: observe(population), run(history), reset()

Properties: avoidance_ratio, engaged_ratio, neutral_ratio, alerts, step

SpreadModel

Parameter Type Default Description
population_size int 100 Number of agents
contagion_rate float 0.3 Per-contact transmission probability
contact_degree int 4 Neighbours per step
recovery_rate float 0.05 Spontaneous recovery probability
seed int | None None RNG seed

Methods: seed_avoiders(indices), seed_engaged(indices), step(), simulate(steps), reset()

TippingPointDetector

Parameter Type Default Description
method str "combined" Detection method
threshold float 0.5 Ratio threshold
velocity_threshold float 0.1 Min step-to-step change
accel_threshold float 0.05 Min second-derivative

Methods: observe(ratio), analyze(ratios), reset()

InterventionSimulator

Strategy Description
VACCINATE Lock neutral agents as engaged
QUARANTINE Reset all avoiders to neutral
CENSOR Halve the contact degree
BOOST_ENGAGED Convert neutrals to engaged

Methods: baseline(steps), simulate_intervention(strategy, ...), compare_all(...)

Running Tests

PYTHONPATH=src pytest tests/ -v

License

MIT © SuperInstance

Metadata

Release files for avoidance-cascade 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for avoidance-cascade 0.1.0
File Size Uploaded Provenance Actions
avoidance_cascade-0.1.0.tar.gz 12.9 kB Without provenance Details for avoidance_cascade-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for avoidance-cascade 0.1.0
File Interpreter ABI Platform Actions
avoidance_cascade-0.1.0-py3-none-any.whl Python 3 none any Details for avoidance_cascade-0.1.0-py3-none-any.whl

Total release size: 28.9 kB

Release files / avoidance_cascade-0.1.0.tar.gz

Download URL avoidance_cascade-0.1.0.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d7ba3c1f8eb532f6457ebaab6a315d98365dc9f8ccb3993bb1c8fde656f27984
BLAKE2b-256 checksum
How to use checksums
aae22ccc40bf8495d753c7b77603a61cb245c4a99a058605e98e56e1bc780085
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / avoidance_cascade-0.1.0-py3-none-any.whl

Download URL avoidance_cascade-0.1.0-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
922ea826b53d5873d6b60a2c200225686b2e9f90c37d256c74aae2fb31249fa7
BLAKE2b-256 checksum
How to use checksums
f883728c53d97f77a0a5483995c92d98e008725742f018e214255c959bb323fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for avoidance-cascade 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page