samplepaths
Generate realizations (sample paths) of stochastic processes as NumPy arrays.
from samplepaths import BrownianMotion
bm = BrownianMotion(drift=1, scale=2, t=10)
path = bm.sample(1000) # 1001 values on [0, 10]
paths = bm.sample_paths(1000, paths=5000) # 5000 realizations at once
- 37 processes in four families: continuous-time, diffusion, discrete-time and noise
- One interface: every process is a class with
sample(n)andsample_paths(n, paths)methods - Fast: vectorized with NumPy; 1000 Vasicek realizations of 256 steps take about 2 ms
- Reproducible: built on
numpy.random.Generator, per process or through a seedable default - Typed: type hints throughout, checked with mypy in strict mode
- Light: depends only on NumPy and SciPy
Full documentation: https://samplepaths.readthedocs.io/
Installation
pip install samplepaths
samplepaths requires Python 3.11 or newer, NumPy 2.2 or newer and SciPy 1.15 or newer.
Quickstart
Create a process with its parameters, then call sample(n) to generate a realization of n increments.
For a process on a time interval, the realization holds n + 1 values and times holds the matching time values.
import matplotlib.pyplot as plt
from samplepaths import FractionalBrownianMotion
fbm = FractionalBrownianMotion(hurst=0.7, t=5)
for _ in range(3):
path = fbm.sample(512)
plt.plot(fbm.times, path)
plt.show()
Reproducible results
Give a process its own generator, or seed the generator shared by all processes created without one.
import numpy as np
from samplepaths import BrownianMotion
from samplepaths import random
bm = BrownianMotion(rng=np.random.default_rng(42))
random.seed(value=42)
Sampling at chosen times
Most continuous-time processes can be sampled at times of your choice with sample_at().
from samplepaths import BrownianMotion
bm = BrownianMotion(drift=1, scale=1)
values = bm.sample_at([0, 3, 10, 11, 11.2, 20])
More than one way to sample
sample(n) has the same signature on every process. Variants have their own methods.
from samplepaths import FractionalGaussianNoise
from samplepaths import GeometricBrownianMotion
from samplepaths import PoissonProcess
prices = GeometricBrownianMotion(drift=0.05, volatility=0.2).sample_with_initial(252, initial=100.0)
arrivals = PoissonProcess(rate=2.0).sample_with_length(10.0)
noise = FractionalGaussianNoise(hurst=0.6).sample_hosking(32)
Many realizations at once
sample_paths(n, paths) returns a two-dimensional array with one realization per row.
For most processes it is 2 to 13 times faster than a loop over sample().
import numpy as np
from samplepaths import GeometricBrownianMotion
gbm = GeometricBrownianMotion(drift=0.05, volatility=0.2, rng=np.random.default_rng(1))
prices = gbm.sample_paths_with_initial(n=64, paths=100_000, initial=100.0)
payoffs = np.maximum(prices[:, -1] - 105.0, 0.0)
print(np.exp(-0.05) * payoffs.mean()) # a European call price, close to 8.02
Generic simulation code
Because the interface is shared, simulation code does not depend on the process it simulates.
import numpy as np
from samplepaths import CoxIngersollRossProcess
from samplepaths import GeometricBrownianMotion
from samplepaths.base import BaseTimeProcess
def final_values(process: BaseTimeProcess, n: int, paths: int) -> np.ndarray:
"""Simulate a number of paths and collect the last value of each."""
return process.sample_paths(n, paths)[:, -1]
final_values(GeometricBrownianMotion(drift=0.05, volatility=0.2), n=64, paths=1000)
final_values(CoxIngersollRossProcess(speed=1, mean=1, vol=0.5), n=64, paths=1000)
Processes
Every process can be imported from samplepaths or from the subpackage of its family.
| Family | Subpackage | Processes |
|---|---|---|
| Continuous-time | samplepaths.continuous |
BesselProcess, BrownianBridge, BrownianExcursion, BrownianMeander, BrownianMotion, CauchyProcess, FractionalBrownianMotion, GammaProcess, GeometricBrownianMotion, InverseGaussianProcess, MixedPoissonProcess, MultifractionalBrownianMotion, PoissonProcess, SquaredBesselProcess, VarianceGammaProcess, WienerProcess |
| Diffusion | samplepaths.diffusion |
DiffusionProcess, ConstantElasticityVarianceProcess, CoxIngersollRossProcess, ExtendedVasicekProcess, OrnsteinUhlenbeckProcess, VasicekProcess |
| Discrete-time | samplepaths.discrete |
BernoulliProcess, ChineseRestaurantProcess, DirichletProcess, MarkovChain, MoranProcess, RandomWalk |
| Noise | samplepaths.noise |
GaussianNoise, FractionalGaussianNoise, ColoredNoise, VioletNoise, BlueNoise, WhiteNoise, PinkNoise, RedNoise, BrownianNoise |
Diffusion processes are approximated with the Euler-Maruyama method.
Documentation
The documentation at https://samplepaths.readthedocs.io/ contains:
- a quickstart
- a user guide for sampling, simulating many realizations, random number generation and every family of processes
- a gallery of examples with their figures
- measured sampling times and a changelog
- an API reference for every class, method and function
Coming from stochastic
samplepaths is an independently maintained fork of stochastic 0.7.0 by Christopher Flynn.
It keeps the original processes and algorithms and modernizes everything around them: current Python, NumPy and SciPy, type hints, one sampling interface, and corrected drift handling.
Porting is mostly a change of import:
# stochastic
from stochastic.processes.continuous import BrownianMotion
# samplepaths
from samplepaths.continuous import BrownianMotion
See the migration guide for the full list of changes.
Development
The project is managed with uv and make.
git clone https://github.com/samplepaths/samplepaths.git
cd samplepaths
make install # create the environment
make check # lint, type check, test and build the docs
Run make to list all targets. Bug reports and pull requests are welcome on GitHub; see CONTRIBUTING.md.
License
samplepaths is released under the MIT license.
It is maintained by Bassem Girgis.
Metadata
Release files for samplepaths 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| samplepaths-0.4.0.tar.gz | 51.1 kB | Without provenance | Details for samplepaths-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| samplepaths-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for samplepaths-0.4.0-py3-none-any.whl |
Total release size: 116.2 kB
Release files / samplepaths-0.4.0.tar.gz
| Download URL | samplepaths-0.4.0.tar.gz |
|---|---|
| Size | 51.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1338419014f38bde841fd25bc52b9d1615e1258e6f95a46242f83c7f370d6e04
|
|
BLAKE2b-256 checksum How to use checksums |
f982b05db1885e4671404974a83c2104efe2b3dae8235b71ac2f195167f47e19
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / samplepaths-0.4.0-py3-none-any.whl
| Download URL | samplepaths-0.4.0-py3-none-any.whl |
|---|---|
| Size | 65.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f3790b9ddd621b0a6548bf973c8125f7f12402e6debd9594e69bc629a87ff3c0
|
|
BLAKE2b-256 checksum How to use checksums |
229c5dbefa7336ed0d1d681396bf69e385685b86a35cd9168128fb1343ae6499
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for samplepaths 0.4.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.