bhylu.com

Advertisements

NumPy Exponential Distribution

An exponential distribution produces non-negative waiting times in a simple constant-rate model. NumPy’s scale is the mean waiting time, which is the reciprocal of rate.

How it works

If a toy process has rate two events per hour, its scale is half an hour. Confusing rate and scale reverses that interpretation. The memoryless model is a substantive assumption, not a general property of real equipment failures or rainfall. Use meaningful units and explain how the chosen process would be checked before treating simulated waiting times as a prediction.

Run the code

Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.

For local work, use a separate virtual environment and run python exponential.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.

Example

exponential.py

import numpy as np

rate_per_hour = 2.
scale_hours = 1. / rate_per_hour
waits = np.random.default_rng(41).exponential(scale=scale_hours, size=50)
print(scale_hours)
print(bool((waits >= 0).all()))
print(bool(np.isfinite(waits).all()))

Expected result

0.5
True
True

The parameter conversion is visible and the sampled waits satisfy the non-negative domain. The fixture does not estimate a real event rate or guarantee a maximum waiting time. Larger-than-average waits remain possible.

Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.