Skip to main content

A Python library for simulating stochastic processes in finance.

Project description

FinStoch

PyPI Latest Release PyPI Downloads License - MIT Python Version CI Typing

What is it?

FinStoch is a Python library for simulating stochastic processes commonly used in quantitative finance. It provides clean, consistent interfaces for Monte Carlo path simulation using Euler-Maruyama discretization, with built-in analytics, seed control for reproducibility, and pandas integration.

Table of Contents

Main Features

  • Six stochastic process models covering equity prices, interest rates, and stochastic volatility
  • Reproducible simulations via seed control on all processes
  • Flexible time grids with configurable granularity (daily, hourly, minute-level) and business day support
  • Built-in analytics including VaR, CVaR, max drawdown, confidence bands, and summary statistics
  • pandas integration with to_dataframe() for seamless downstream analysis
  • Consistent API across all models: every process exposes simulate(), plot(), and the full analytics suite

Supported Processes

Model Class SDE
Geometric Brownian Motion GeometricBrownianMotion $dS = \mu S, dt + \sigma S, dW$
Merton Jump Diffusion MertonJumpDiffusion $dS = (\mu - \lambda k) S, dt + \sigma S, dW + JS, dN$
Ornstein-Uhlenbeck OrnsteinUhlenbeck $dS = \theta(\mu - S), dt + \sigma, dW$
Cox-Ingersoll-Ross CoxIngersollRoss $dS = \theta(\mu - S), dt + \sigma\sqrt{S}, dW$
Constant Elasticity of Variance ConstantElasticityOfVariance $dS = \mu S, dt + \sigma S^\gamma, dW$
Heston Stochastic Volatility HestonModel $dS = \mu S, dt + \sqrt{v} S, dW_S$, $dv = \kappa(\theta - v), dt + \sigma\sqrt{v}, dW_v$

All processes are discretized using the Euler-Maruyama scheme and return NumPy arrays of shape (num_paths, num_steps). The Heston model returns a tuple (S, v) of price and variance paths.

Where to Get It

# PyPI
pip install FinStoch

Dependencies

Package Minimum Version Purpose
NumPy 1.23 Array operations and random number generation
pandas 2.0 Time grid generation and DataFrame conversion
matplotlib 3.7 Path visualization
SciPy 1.9 Statistical functions for analytics
python-dateutil 2.9 Date range duration calculation

Quick Start

Simulate and plot

from FinStoch import GeometricBrownianMotion

gbm = GeometricBrownianMotion(
    S0=100, mu=0.05, sigma=0.2,
    num_paths=10,
    start_date='2023-09-01',
    end_date='2024-09-01',
    granularity='D',
)

# Reproducible simulation
paths = gbm.simulate(seed=42)
gbm.plot(paths=paths, title='GBM Simulation', ylabel='Price')

Convert to DataFrame

df = gbm.to_dataframe(paths)
# DataFrame with DatetimeIndex columns, one row per path

Heston model (stochastic volatility)

from FinStoch import HestonModel

heston = HestonModel(
    S0=100, v0=0.04, mu=0.05, sigma=0.3,
    theta=0.04, kappa=2.0, rho=-0.7,
    num_paths=10,
    start_date='2023-09-01',
    end_date='2024-09-01',
    granularity='D',
)

prices, variance = heston.simulate(seed=42)

# Convert either component to DataFrame
df_prices = heston.to_dataframe((prices, variance), variance=False)
df_var = heston.to_dataframe((prices, variance), variance=True)

Analytics

All processes inherit a suite of analytics methods from the base class:

paths = gbm.simulate(seed=42)

# Descriptive statistics at each time step
stats = gbm.summary_statistics(paths)  # dict: mean, std, skew, kurtosis, min, max

# Central tendency
mean_path = gbm.expected_path(paths)     # mean across paths
median = gbm.median_path(paths)          # median across paths

# Uncertainty
lower, upper = gbm.confidence_bands(paths, level=0.95)

# Risk measures (computed at terminal time step)
gbm.var(paths, alpha=0.05)    # Value at Risk
gbm.cvar(paths, alpha=0.05)   # Conditional VaR (Expected Shortfall)

# Drawdown analysis
drawdowns = gbm.max_drawdown(paths)  # max peak-to-trough per path

# Distribution visualization
gbm.terminal_distribution(paths, bins=50)  # histogram + fitted normal

Development

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Run tests
python -m unittest discover -s tests -p "*_test.py"

# Format
ruff format

# Lint
flake8 --max-line-length 127

# Type check
mypy . --exclude venv --exclude build --ignore-missing-imports

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

finstoch-0.6.2.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

finstoch-0.6.2-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file finstoch-0.6.2.tar.gz.

File metadata

  • Download URL: finstoch-0.6.2.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for finstoch-0.6.2.tar.gz
Algorithm Hash digest
SHA256 2b6deb9a9e418ae9dd38c49392c1b7be6ec0fe7fe135e8a885dabdd7834468ec
MD5 fa8dc273477e003cfc6a34bc53d61fb4
BLAKE2b-256 f0fecd670a43cf299a41e57ea4e936c46f11e228759e9b7c0031925e8560dcb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for finstoch-0.6.2.tar.gz:

Publisher: cd.yml on Yosri-Ben-Halima/FinStoch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file finstoch-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: finstoch-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for finstoch-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a3e0cb5db0e5afd3c0c3d72fc5b6bdd7d6652b9cdc32695134c740bd65feff6a
MD5 f3bd6e0e41fd61964e8693c974db7ebc
BLAKE2b-256 8557d8aa070ef6fada3ccf26b8ccf64a3b6c827487f87518bd0b14e3d8c35ef3

See more details on using hashes here.

Provenance

The following attestation bundles were made for finstoch-0.6.2-py3-none-any.whl:

Publisher: cd.yml on Yosri-Ben-Halima/FinStoch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page