Python implementation of avoidance cascade detection for ternary agents
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file avoidance_cascade-0.1.0.tar.gz.
File metadata
- Download URL: avoidance_cascade-0.1.0.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7ba3c1f8eb532f6457ebaab6a315d98365dc9f8ccb3993bb1c8fde656f27984
|
|
| MD5 |
bd90b63edd86ccc278aefe490c006702
|
|
| BLAKE2b-256 |
aae22ccc40bf8495d753c7b77603a61cb245c4a99a058605e98e56e1bc780085
|
File details
Details for the file avoidance_cascade-0.1.0-py3-none-any.whl.
File metadata
- Download URL: avoidance_cascade-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
922ea826b53d5873d6b60a2c200225686b2e9f90c37d256c74aae2fb31249fa7
|
|
| MD5 |
1e45ee5e047f26227513ec64bd58599f
|
|
| BLAKE2b-256 |
f883728c53d97f77a0a5483995c92d98e008725742f018e214255c959bb323fb
|