← Back to all products
$8
Loss Simulator
Simulate loss scenarios — hacks, depegs, liquidation cascades — to stress-test coverage pools.
TOMLPythonMarkdown
📁 File Structure 12 files
loss-simulator/
├── LICENSE
├── README.md
├── examples/
│ └── basic_usage.py
├── pyproject.toml
├── security-notes.md
├── src/
│ ├── __init__.py
│ ├── distributions.py
│ ├── formatters.py
│ ├── models.py
│ ├── scenarios.py
│ └── simulator.py
└── tests/
└── test_simulator.py
📖 Documentation Preview README excerpt
Loss Simulator
Monte Carlo simulation for coverage pool solvency analysis.
Price: $7.99 | License: MIT | Python: 3.10+ | Dependencies: None (stdlib only)
Part of [Coverage Vault](../../) — CryptoForge Store #7.
What It Does
Models the financial dynamics of a DeFi coverage/insurance pool over time using Monte Carlo simulation. Runs thousands of independent paths, each simulating daily premium collection, stochastic claim arrivals (Poisson), and claim severity (Beta distribution). Computes:
- Solvency rate — fraction of paths where the pool remains solvent
- Value at Risk (VaR) — loss threshold at confidence levels (95%, 99%, 99.5%)
- Conditional VaR (CVaR) — expected loss beyond VaR (tail risk)
- Capital percentiles — P1 through P99 of final capital
- Insolvency distribution — when failures occur (30-day buckets)
No external dependencies — uses only the Python standard library (random, math, dataclasses).
Quick Start
from src.models import PoolParams, SimulationConfig
from src.simulator import MonteCarloSimulator
from src.formatters import format_aggregate
# Define pool parameters
pool = PoolParams(
initial_capital=1_000_000,
premium_rate_annual_bps=500, # 5% annual premium
total_coverage=5_000_000,
average_claim_frequency=0.03, # 3% claim rate
average_claim_severity=0.3, # 30% average severity
)
# Configure simulation
config = SimulationConfig(
num_simulations=10_000,
time_horizon_days=365,
seed=42,
)
# Run simulation
sim = MonteCarloSimulator(pool, config)
result = sim.run()
# Display results
print(format_aggregate(result))
print(f"Solvency rate: {result.solvency_rate:.1%}")
print(f"99% VaR: ${result.var_values[0.99]:,.0f}")
Features
Pool Configuration
*... continues with setup instructions, usage examples, and more.*
📄 Code Sample .py preview
src/distributions.py
"""
Loss Simulator — Statistical Distributions
════════════════════════════════════════════
Claim arrival, severity, premium income, and policy lifecycle
distributions using only the Python standard library (no numpy).
"""
from __future__ import annotations
import math
import random
from dataclasses import dataclass
@dataclass
class ClaimArrival:
"""
Poisson-distributed claim arrivals.
Models the number of claims per time step as a Poisson process
with a given average rate (lambda).
"""
avg_claims_per_step: float
def sample(self, rng: random.Random) -> int:
"""
Sample number of claims using Knuth's Poisson algorithm.
For lambda > 30, uses normal approximation.
"""
lam = self.avg_claims_per_step
if lam <= 0:
return 0
if lam > 30:
# Normal approximation for large lambda
val = rng.gauss(lam, math.sqrt(lam))
return max(0, round(val))
# Knuth's algorithm for small lambda
L = math.exp(-lam)
k = 0
p = 1.0
while True:
k += 1
p *= rng.random()
if p <= L:
return k - 1
# ... 94 more lines ...