Skip to main content

samplepaths

Tests Documentation PyPI version Python versions Weekly downloads Total downloads License

Generate realizations (sample paths) of stochastic processes as NumPy arrays. Pure Python, and for large simulations as fast as compiled code.

Realizations of a Brownian motion with drift and their quantile bands

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
  • 68 processes in six families: continuous-time, diffusion, discrete-time, time series, several components and noise
  • One interface: every process is a class with sample(n) and sample_paths(n, paths) methods
  • Pure Python, and fast: vectorized with NumPy and multi-threaded; 100,000 Brownian paths of 64 steps take 6 ms on 14 cores, level with a Rust implementation (comparison)
  • 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 a process with several components, such as a Heston model, the components form a second axis. For most processes it is 2 to 13 times faster than a loop over sample(), and workers=8 spreads a large batch over 8 threads.

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

One example process of each of the six families

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, FractionalGeometricBrownianMotion, GammaProcess, GeometricBrownianMotion, CompoundPoissonProcess, HawkesProcess, InverseGaussianProcess, KouJumpDiffusion, MertonJumpDiffusion, MixedPoissonProcess, NormalInverseGaussianProcess, MultifractionalBrownianMotion, PoissonProcess, SquaredBesselProcess, VarianceGammaProcess, WienerProcess
Diffusion samplepaths.diffusion DiffusionProcess, ConstantElasticityVarianceProcess, CoxIngersollRossProcess, ExtendedVasicekProcess, OrnsteinUhlenbeckProcess, VasicekProcess, JacobiProcess, KimuraProcess, VerhulstProcess, GompertzProcess, FellerLogisticProcess, FractionalOrnsteinUhlenbeckProcess, FractionalVasicekProcess, FractionalCoxIngersollRossProcess, FractionalJacobiProcess, HoLeeProcess
Discrete-time samplepaths.discrete BernoulliProcess, ChineseRestaurantProcess, DirichletProcess, MarkovChain, MoranProcess, RandomWalk
Time series samplepaths.timeseries AutoregressiveProcess, MovingAverageProcess, ARIMAProcess, SARIMAProcess, ARCHProcess, GARCHProcess, TGARCHProcess, AGARCHProcess, EGARCHProcess
Several components samplepaths.multivariate CorrelatedBrownianMotions, HestonModel, BatesModel, SABRModel, HullWhiteTwoFactorModel, TwoFactorCoxIngersollRossModel
Noise samplepaths.noise GaussianNoise, FractionalGaussianNoise, ColoredNoise, VioletNoise, BlueNoise, WhiteNoise, PinkNoise, RedNoise, BrownianNoise

Diffusion processes are approximated with the Euler-Maruyama method; the Cox-Ingersoll-Ross process and the variance of the Heston model can also be sampled from their exact distributions.

Documentation

The documentation at https://samplepaths.readthedocs.io/ contains:

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.5.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 samplepaths 0.5.0
File Size Uploaded Provenance Actions
samplepaths-0.5.0.tar.gz 110.7 kB Without provenance Details for samplepaths-0.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 252.2 kB

Release files / samplepaths-0.5.0.tar.gz

Download URL samplepaths-0.5.0.tar.gz
Size 110.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8f3c5b1fef4b3c35b33b6a394f3c600e207607b1643d6621a465b9a8d232fcbb
BLAKE2b-256 checksum
How to use checksums
e30f7fd36209fb4273b151b441eb8f1fb849b81b721277f72242315068e65cb1
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.5.0-py3-none-any.whl

Download URL samplepaths-0.5.0-py3-none-any.whl
Size 141.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b8ba24b83e46db3c2d00c82226a5096913cf39eec2b3bbedea29435cb42a27eb
BLAKE2b-256 checksum
How to use checksums
1bf14b643540b9107d88a9e8ee311e4d77faf322cbfd2df44363b236c26e2511
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.5.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.

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