APPLPy stands for A Probability Programming Language -- Python Edition. The primary goal of APPLPy is to provide an open-source conceptual probability package capable of manipulating random variables symbolically. Although the Python implementation is a recent development, a version based on the Maple computer algebra system has been used for over a decade. The Maple implementation, called APPL, has been successfully integrated into mathematical statistics and computing courses at the College of William and Mary, the United States Military Academy and Colorado College, while also facilitating research in areas ranging from order statistics to queuing theory. The hope of APPLPy is to make the computational capabilities of APPL available to researchers and educators on an open-source platform.
The current capabilities of APPLPy include:
- Conversion between PDF,CDF,SF,HF,CHF and IDF representations of random variables
- Computation of expected values, with both numeric and symbolic output
- Plotting distributions, including piece-wise distributions
- One-to-one and many-to-one transformations of piecewise distributions
- Random Variable Algebra (Sums/Differences/Products/Division)
- Random sampling from distributions
- Bootstrapping data sets
- Bayesian inference with ad-hoc prior distributions
- Computation of distributions for M/M/s queues
- Analysis of Discrete Time Markov Chains
Although APPLPy can be used for a variety of purposes, it is best suited to fill three special roles. First, it enables students to gain an intuitive understanding of mathematical statistics by automating tedious, calculus-intensive algorithms. As such, students can experiment with different models without having to perform difficult derivations or produce ad-hoc code. Second, it allows students to check hand derived results. This aids the learning process by providing a quick and reliable answer key. Finally, it allows researchers to explore systems whose properties would be intractable to derive by hand. As mentioned above, the Maple-based APPL software has already spawned a variety of insightful research. APPLPy has the potential to continue along this pathway. The simplicity of APPLPy's syntax allow users to explore stochastic models with ease.
APPLPy now targets Python 3 only.
Runtime dependencies are declared in the package metadata and will be installed automatically:
sympynumpyscipympmathmatplotlibseabornpandas
Clone the repository and install with uv:
git clone https://github.com/MthwRobinson/APPLPy.git
cd APPLPy
uv syncOr use the project Makefile:
make installIf you prefer pip-style uv commands, use:
uv pip install .To install development dependencies (ipython, jupyter, and ruff) as well:
uv sync --extra devOr with make:
make install-devInstall Rust extension tooling:
make install-rustBuild and install the Rust extension into the project environment:
make rust-developUse this for local development so Python imports the locally built Rust module from your active environment.
Validate import and call a dummy Rust function:
make rust-check-import--no-sync is used for these commands so uv run does not prune the maturin develop install.
Create all distribution artifacts in dist/:
make build-dist-rustThis produces:
- APPLPy Python source/wheel artifacts from
uv build - A platform-specific Rust extension wheel from
maturin build
Use the Makefile targets for linting and formatting:
make check # Run Ruff lint checks in applpy/
make tidy # Apply Ruff fixes and format code in applpy/Build the Docker image:
make docker-buildRun an interactive Python session in the container:
make docker-runRun Jupyter Lab in Docker and expose it on http://127.0.0.1:8888:
make docker-run-jupterInstall test dependencies and run all tests with coverage:
make install-test
make testRun just functional tests:
make test-functionalRun just unit tests:
make test-unitRun a specific test file (or test path) instead of the full suite:
make test TEST=test_applpy/unit/test_rv.py
make test-functional TEST=test_applpy/functional/test_notebook_examples.py
make test-unit TEST=test_applpy/unit/test_rv.pyRun lint checks locally:
make checkThe following are a few examples of how to use APPLPy.
from sympy import Rational
from applpy import CDF, HF, Mean, ExponentialRV
x_rv = ExponentialRV(Rational(1, 100))
print(Mean(x_rv)) # 100
print(CDF(x_rv, 0)) # 0
print(HF(x_rv, 0)) # 1/100from applpy import CDF, Mean, Variance, TriangularRV
inv_1 = TriangularRV(-2, 1, 3)
inv_2 = TriangularRV(-10, 3, 20)
portfolio = inv_1 + inv_2
print(CDF(inv_1, 0)) # 4/15
print(Variance(inv_1)) # 19/18
print(Mean(portfolio)) # 5from sympy import Rational
from applpy import CDF, convolution_iid, Mean, Mixture, TriangularRV, UniformRV
mix = Mixture(
[Rational(1, 4), Rational(1, 4), Rational(1, 2)],
[TriangularRV(2, 4, 6), TriangularRV(3, 5, 7), TriangularRV(1, 5, 9)],
)
print(Mean(mix)) # 19/4
print(float(CDF(mix, 4).evalf())) # 0.296875
print(float(Mean(convolution_iid(UniformRV(1, 2), 3)).evalf())) # 4.5See the following documentation for a list of distributions that are available in the package.
This repository includes a local agent skill at .agents/skills/applpy for translating natural-language probability questions into APPLPy models and runnable code.
Invoke it directly in your prompt with:
$applpy Model a 3-component reliability system where each component lifetime is Exponential(1/7), compute the system mean lifetime.
You can also call it with an explicit path:
Use $applpy at /home/matt/repos/applpy/.agents/skills/applpy to build a Markov chain model from this transition matrix and compute long-run probabilities.