Skip to main content

PyStochastic

PyStochastic is a Python library for probability, stochastic calculus and stochastic modelling, Monte Carlo methods and numerical methods for stochastic differential equations.

The project aims to provide a simple and consistent interface for simulating, analysing and visualising stochastic models.


✨ Features

PyStochastic currently provides tools for:

  • Probability distributions
    • Continuous distributions
    • Discrete distributions
  • Random number generation
  • Stochastic processes
  • Numerical SDE solvers
    • Euler-Maruyama
    • Milstein
  • Monte Carlo analysis
    • Estimation
    • Moments
    • Variance and standard error
    • Confidence intervals
    • Quantiles
    • Histograms
    • Empirical cumulative distribution functions
    • Confidence curves
  • Vectorised simulations
  • Plotting and visualisation

📦 Installation

Clone the repository:

git clone https://github.com/BastienVelcin/PyStochastic.git
cd PyStochastic

Then install the package:

pip install .

🚀 Quick start

Probability distributions

PyStochastic provides a common interface for probability distributions.

For example:

from pystochastic.dist import Normal

distribution = Normal(mu=0, sigma=1)

samples = distribution.sample(10000)

mean = distribution.mean()
variance = distribution.variance()

Probability distributions also provide functions such as pdf and cdf when appropriate.


Discrete distributions

Discrete probability distributions use the DiscreteDistribution interface and provide a pmf method.

For example:

from pystochastic.dist import Bernoulli

distribution = Bernoulli(p=0.3)

samples = distribution.sample(10000)

probability = distribution.pmf(1)

🎲 Random number generation

PyStochastic provides random number generators for both continuous and discrete distributions through the pyrandom module.

from pystochastic.pyrandom import crandom

samples = crandom.normal(
    mu=0,
    sigma=1,
    size=10000,
)

A global seed can also be configured for reproducible simulations.

from pystochastic.pyrandom.setseed import seed

seed(42)

📈 Stochastic processes

PyStochastic provides several classical stochastic processes, including:

  • Brownian motion
  • Poisson process
  • Geometric Brownian motion
  • Ornstein-Uhlenbeck process
  • Vasicek model
  • Cox-Ingersoll-Ross model

For example:

from pystochastic.processes import Brownian

process = Brownian(
    t_0=0,
    t_n=1,
    n_steps=1000,
    n_simulations=10,
)

process.simulate()

The simulated paths can then be analysed and visualised.


🧮 Stochastic differential equations

PyStochastic provides numerical solvers for stochastic differential equations.

Currently implemented methods include:

  • Euler-Maruyama
  • Milstein

These solvers support vectorised simulations and can be applied to multidimensional stochastic systems.

For example:

from pystochastic.sde import EulerMaruyama

solver = EulerMaruyama(
    drift=drift,
    diffusion=diffusion,
    x_0=x_0,
    t_0=0,
    t_n=1,
    n_steps=1000,
)

solution = solver.solve()

🎯 Monte Carlo

The MonteCarlo class provides statistical tools for analysing collections of simulated samples.

For example:

from pystochastic.montecarlo import MonteCarlo

mc = MonteCarlo(samples)

estimate = mc.estimate()
variance = mc.variance()
standard_error = mc.standard_error()

Confidence intervals can also be computed:

lower, upper = mc.confidence_interval(
    confidence=0.95,
    type="student",
)

PyStochastic also provides tools for statistical visualisation, including histograms, empirical CDFs and confidence curves.

mc.histogram()

mc.ecdf()

mc.confidence_curve()

📊 Vectorisation

A major focus of PyStochastic is efficient simulation.

Whenever possible, simulations are vectorised using NumPy rather than performing independent Python-level loops.

This is particularly useful when a large number of Monte Carlo simulations is required.


🧪 Testing

PyStochastic uses pytest for its test suite.

Run all tests with:

pytest

The project currently contains tests covering:

  • Probability distributions
  • Discrete distributions
  • Random number generators
  • Stochastic processes
  • SDE solvers
  • Monte Carlo methods
  • Public APIs

The test suite also includes mathematical consistency and statistical tests.


⚡ Benchmarks

Performance benchmarks are included to compare different simulation approaches and evaluate the benefit of vectorisation.

For example, vectorised simulations can provide substantial speedups compared with sequential matrix-based implementations for large numbers of simulations.


📚 Project structure

pystochastic/
├── dist/
│   ├── dist.py
│   └── ...
│
├── montecarlo/
│   ├── montecarlo.py
│   └── ...
│
├── processes/
│   ├── brownian.py
│   ├── poisson.py
│   ├── GeometricBrownianMotion.py
│   ├── OrnsteinUhlenbeck.py
│   ├── vasicek.py
│   └── CIR.py
│
├── pyrandom/
│   ├── crandom.py
│   ├── drandom.py
│   └── setseed.py
│
└── sde/
    ├── eulermaruyama.py
    └── milstein.py

🛠️ Development

Clone the repository and install the project in editable mode:

git clone https://github.com/BastienVelcin/PyStochastic.git
cd PyStochastic

pip install -e .

Install the development dependencies and run the tests:

pytest

🗺️ Roadmap

Possible future developments include:

  • Additional probability distributions
  • Additional stochastic processes
  • Additional SDE numerical schemes
  • Improved documentation
  • Additional performance optimisations
  • More statistical analysis tools
  • Expanded benchmarking
  • Improved visualisation capabilities

🤝 Contributing

Contributions, suggestions and bug reports are welcome.

If you find a bug or have an idea for a new feature, please open an issue on GitHub.

Pull requests are also welcome.


📄 License

PyStochastic is released under the MIT License.

See the LICENSE file for more information.

Metadata

Release files for pystochastic 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pystochastic 0.2.0
File Size Uploaded Provenance Actions
pystochastic-0.2.0.tar.gz 57.3 kB With provenance Details for pystochastic-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pystochastic 0.2.0
File Interpreter ABI Platform Actions
pystochastic-0.2.0-py3-none-any.whl Python 3 none any Details for pystochastic-0.2.0-py3-none-any.whl

Total release size: 132.7 kB

Release files / pystochastic-0.2.0.tar.gz

Download URL pystochastic-0.2.0.tar.gz
Size 57.3 kB
Tags Source
SHA-256 checksum
How to use checksums
189ac790eff4ba83a2310e4943aa3efae0fa39b971de27782ccc3b0ad03c38f5
BLAKE2b-256 checksum
How to use checksums
0253e389f1e51afb316f56a6744a58e4bc8b4b78944ed96d99ed50b36fe55775
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 19, 2026.

Transparency log

Release files / pystochastic-0.2.0-py3-none-any.whl

Download URL pystochastic-0.2.0-py3-none-any.whl
Size 75.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8565c26591b808643e57ac7ca6a72858ba9b91cb2c73cefa0bcebe98acd74695
BLAKE2b-256 checksum
How to use checksums
1476480723730832c2e8be2b59c3c420f2dddcecf3d78aebadd8df5e5878929c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 19, 2026.

Transparency log

Security for pystochastic 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pystochastic-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page