Skip to content

sigalg

Measure-Theoretic Probability in Python

SigAlg is a Python library for measure-theoretic probability: build probability spaces from sample spaces, \(\sigma\)-algebras, and probability measures, define random variables and stochastic processes, and compute derived objects (e.g., conditional expectations, checks for martingales).

Unlike most probabilistic computing libraries that treat probabilities primarily as arrays of numbers, SigAlg exposes the richer structures of measure-theoretic probability as manipulable, inspectable objects. The goal is to reduce friction when translating from mathematics to working code.

Key Features:

  • Core probabilistic objects — Sample spaces, \(\sigma\)-algebras, and probability measures modeled close to their definitions.
  • Filtrations of \(\sigma\)-algebras — Support for time-evolving information structures used in stochastic processes.
  • Random variables and vectors — Algebraic operations and transformations, including conditional expectation and variance.
  • \(L^2\) spaces of random variables — Inner products, norms, orthogonal projections, Fourier expansions, measure-theoretic regression.
  • Stochastic processes — Adapted and predictable processes, stopping times and stopped processes, discrete Itô integrals, and a growing library of built-in processes.
  • Exact and Monte Carlo — Support for Monte Carlo simulation, discrete approximations to continuous-time objects, and exhaustive exact enumeration.
  • Integration with scientific Python — NumPy/Pandas interoperability; visualization via Matplotlib/Plotly; probability distributions via SciPy.

All the above is implemented according to SigAlg's core design philosophy of a focus on mathematical fidelity and accuracy, not just black-box simulation and speed. SigAlg is meant to be a different kind of library—an interface between abstract mathematics and concrete code that complements the rest of the Python ecosystem. Get Started →

"""Demo SigAlg API by creating a random walk from scratch."""

import pandas as pd
from scipy.stats import bernoulli

from sigalg.core import Time
from sigalg.processes import IIDProcess

# Create an IID Bernoulli process B with p=0.7
T = Time.discrete(start=1, stop=4)
B = IIDProcess(
    distribution=bernoulli(p=0.7),
    support=[0, 1],
    time=T,
).from_enumeration()

# Create a process Y with Y_t = -1 or Y_t = 1
Y = 2 * B - 1

# Create a random walk process X
X = Y.cumsum(name="X")

# Get the natural filtration F_t = σ(X_1,X_2,...,X_t)
F = X.natural_filtration

# Compute the conditional expectation E(X_4 | F_3)
expectation = X[4].expectation(sigma_algebra=F[3])

# Print the trajectories of X and the expectation together
print(pd.concat([X.data, expectation.data], axis=1))

# A random walk with positive drift is a submartingale
print("\nIs X a submartingale?", X.is_submartingale())
            1  2  3  4  E(X_4|3)
trajectory                      
0          -1 -2 -3 -4      -2.6
1          -1 -2 -3 -2      -2.6
2          -1 -2 -1 -2      -0.6
3          -1 -2 -1  0      -0.6
4          -1  0 -1 -2      -0.6
5          -1  0 -1  0      -0.6
6          -1  0  1  0       1.4
7          -1  0  1  2       1.4
8           1  0 -1 -2      -0.6
9           1  0 -1  0      -0.6
10          1  0  1  0       1.4
11          1  0  1  2       1.4
12          1  2  1  0       1.4
13          1  2  1  2       1.4
14          1  2  3  2       3.4
15          1  2  3  4       3.4

Is X a submartingale? True