Skip to main content

A Python library for simulating stochastic processes in finance.

Project description

FinStoch

A Python library for simulating stochastic processes in finance.

Installation

pip install FinStoch

Development

pip install -e ".[dev]"

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

# Format / lint / type check
ruff format
flake8 --max-line-length 127
mypy . --exclude venv --ignore-missing-imports

Processes

Geometric Brownian Motion

  • SDE

$$ dS_t = \mu S_t dt + \sigma S_t dW_t $$

  • A stochastic process where the logarithm of the variable follows a Brownian motion with drift, representing continuous growth with random fluctuations.

  • Euler-Maruyama Discretization

$$ S_{t+\Delta t} = S_t.e^{\left(\mu-\frac{\sigma^2}{2}\right)\Delta t+\sigma\sqrt{\Delta t}\epsilon_t} $$

Where $\epsilon_t \sim \mathcal{N}(0, 1)$.

import numpy as np
from FinStoch.processes import GeometricBrownianMotion

S0 = 100                    # Initial value 
mu = 0.05                   # Annualized Drift coefficient (expected return rate)
sigma = 0.2                 # Annualized Volatility (standard deviation of returns)
num_paths = 10              # Number of paths to simulate
start_date = '2023-09-01'   # Start date for the simulation
end_date = '2024-09-01'     # End date for the simulation
granularity = 'D'           # Granularity in daily intervals
    
# Create the GBM model
gbm = GeometricBrownianMotion(S0, mu, sigma, num_paths, start_date, end_date, granularity)

# Simulate the GBM process
simulated_paths = gbm.simulate()

# Plot the simulated paths
gbm.plot(paths=simulated_paths, 
         title='Stock Price under Geometric Brownian Motion Assumption', 
         ylabel='Stock Price',
         fig_size=(15,5)
        )

Plot

Merton's Jump Diffusion Model

  • SDE

$$ dS_t = \left(\mu - \lambda_j\left( e^{\mu_j+\frac{\sigma_j^2}{2}} - 1\right) \right) S_t dt + \sigma S_t dW_t + S_t \left( \prod_{i=1}^{dN_t} Y_i - 1\right) $$

  • An extension of the geometric Brownian motion that incorporates sudden, discrete jumps $ J_t $ in addition to continuous diffusion, capturing both regular volatility and occasional large shocks.

  • Euler-Maruyama Discretization

$$ S_{t+\Delta t} = S_t . e^{\left( \mu - \frac{1}{2} \sigma^2 -\lambda_j\left( e^{\mu_j+\frac{\sigma_j^2}{2}} - 1\right)\right) \Delta t + \sigma \sqrt{\Delta t} \epsilon_t + J_t } $$

Where $\epsilon_t \sim \mathcal{N}(0, 1)$ and $J_t$ is the jump component at time $t$.

import numpy as np
from FinStoch.processes import MertonJumpDiffusion

# Parameters
S0 = 100                    # Initial process value
mu = 0.05                   # Drift coefficient
sigma = 0.2                 # Volatility
lambda_j = 1                # Jump intensity
mu_j = 0.0                  # Mean of jump size
sigma_j = 0.15              # Standard deviation of jump size
num_paths = 10              # Number of simulated paths
start_date = '2023-09-01'   # Start date for the simulation
end_date = '2024-09-01'     # End date for the simulation
granularity = 'D'           # Granularity in daily intervals


# Create Merton model instance and plot
merton = MertonJumpDiffusion(S0, mu, sigma, lambda_j, mu_j, sigma_j, num_paths, start_date, end_date, granularity)

# Simulate the Merton process
simulated_paths = merton.simulate()

# Plot the simulated paths
merton.plot(paths=simulated_paths, 
         title='Stock Price under the Merton Model Assumption', 
         ylabel='Stock Price',
         fig_size=(15,5)
        )

Plot

Ornstein-Uhlenbeck model

  • SDE

$$ dS_t = \theta (\mu - S_t) dt + \sigma dW_t $$

  • A mean-reverting stochastic process where the variable fluctuates around a long-term mean with a tendency to revert back, driven by continuous noise.

  • Euler-Maruyama Discretization

$$ S_{t+\Delta t} = S_t + \theta (\mu - S_t) \Delta t + \sigma \sqrt{\Delta t} \epsilon_t $$

Where $\epsilon_t \sim \mathcal{N}(0, 1)$.

import numpy as np
from FinStoch.processes import OrnsteinUhlenbeck 

# Parameters
S0 = 100                    # Initial value
mu = 100                    # Annualized drift coefficient
sigma = 0.2                 # Anualized volatility
theta = 0.5                 # Annualized mean reversion rate
num_paths = 10              # Number of paths to simulate
start_date = '2023-09-01'   # Start date for the simulation
end_date = '2024-09-01'     # End date for the simulation
granularity = 'D'           # Granularity in daily intervals

# Create Ornstein-Uhlenbeck model instance and plot
ou = OrnsteinUhlenbeck(S0, mu, sigma, theta, num_paths, start_date, end_date, granularity)

# Simulate the OU process
simulated_paths = ou.simulate()

# Plot the simulated paths
ou.plot(paths=simulated_paths, 
         title='Stock Price under the Ornstein Uhlenbeck model Assumption', 
         ylabel='Stock Price',
         fig_size=(15,5)
        )

Plot

Cox-Ingersoll-Ross Model

  • SDE

$$ dS_t = \kappa (\theta - S_t) dt + \sigma \sqrt{S_t} dW_t $$

  • A mean-reverting process with volatility that depends on the current level of the variable, ensuring the values are always non-negative.

  • Euler-Maruyama Discretization

$$ S_{t+\Delta t} = S_t + \kappa (\theta - S_t) \Delta t + \sigma \sqrt{S_t} \sqrt{\Delta t} \epsilon_t $$

Where $\epsilon_t \sim \mathcal{N}(0, 1)$.

import numpy as np
from FinStoch.processes import CoxIngersollRoss 

# Parameters 
S0 = 0.03                   # Initial value
mu = 0.03                   # Long-term mean
sigma = 0.1                 # Volatility
theta = 0.03                # Speed of reversion
num_paths = 10              # Number of simulation paths
start_date = '2023-09-01'   # Start date for the simulation
end_date = '2024-09-01'     # End date for the simulation
granularity = 'D'           # Granularity in daily intervals

# Create an instance of the CoxIngersollRoss class
cir = CoxIngersollRoss(S0, mu, sigma, theta, num_paths, start_date, end_date, granularity)

# Simulate the CIR process
simulated_paths = cir.simulate()

# Plot the simulated paths
cir.plot(paths=simulated_paths, 
         title='Rate under the CIR model Assumption', 
         ylabel='Rate',
         fig_size=(15,5)
        )

Plot

Constant Elasticity of Variance Model

  • SDE

$$ dS_t = \mu S_t dt + \sigma {S_t}^\gamma dW_t $$

  • A stochastic process that extends the Geometric Brownian Motion process.

  • Euler-Maruyama Discretization

$$ S_{t+\Delta t} = S_t + \mu S_t \Delta t + \sigma {S_t}^\gamma \sqrt{\Delta t} \epsilon_t $$

Where $\epsilon_t \sim \mathcal{N}(0, 1)$.

import numpy as np 
from FinStoch.processes import ConstantElasticityOfVariance

S0 = 100                    # Initial value
mu = 0.05                   # Annualized Drift coefficient (expected return rate)
sigma = 0.2                 # Annualized Volatility (standard deviation of returns)
gamma = 1.2                 # Elasticity coefficient
num_paths = 10              # Number of paths to simulate
start_date = '2023-09-01'   # Start date for the simulation
end_date = '2024-09-01'     # End date for the simulation
granularity = 'D'           # Granularity in daily intervals
    
# Create the CEV model
cev = ConstantElasticityOfVariance(S0, mu, sigma, gamma, num_paths, start_date, end_date, granularity)

# Simulate the CEV process
simulated_paths = cev.simulate()

# Plot the simulated paths
cev.plot(paths=simulated_paths, 
         title='Stock Price under the Constant Elasricity of Variance Model Assumption', 
         ylabel='Stock Price',
         fig_size=(15,5)
        )

Plot

Heston Stochastic Volatility Model

  • SDEs

$$ dS_t = \mu S_t dt + \sqrt{v_t} S_t dW_{S,t} $$

$$ dv_t = \kappa (\theta - v_t) dt + \sigma_v \sqrt{v_t} dW_{v,t} $$

$$ dW_{S,t}\times dW_{v,t}=\rho dt $$

  • A stochastic volatility model where the volatility of a variable follows its own mean-reverting process, allowing for time-varying volatility that evolves over time.

  • Euler-Maruyama Discretization

$$ S_{t+\Delta t} = S_t.e^{\left(\mu-\frac{v_t}{2}\right)\Delta t+\sqrt{v_t}\sqrt{\Delta t}\epsilon_{S,t}} $$

$$ v_{t+\Delta t} = v_t + \kappa (\theta - v_t) \Delta t + \sigma_v \sqrt{v_t} \sqrt{\Delta t} \epsilon_{v,t} $$

$$ Corr(\epsilon_{S,t},\epsilon_{v,t})=\rho $$

Where $\epsilon_S$ and $\epsilon_v$ are correlated standard normal variables.

import numpy as np
from FinStoch.processes import HestonModel

# Parameters
S0 = 100                    # Initial value
v0 = 0.02                   # Initial volatility
mu = 0.05                   # Long-term mean of the value
theta = 0.04                # Long-term mean of the volatility
sigma = 0.3                 # Volatility of the volatility
kappa = 1.5                 # Speed of reversion
rho = -0.7                  # Correlation between shocks
num_paths = 10              # Number of simulation paths
start_date = '2023-09-01'   # Start date for the simulation
end_date = '2024-09-01'     # End date for the simulation
granularity = 'D'           # Granularity in daily intervals

# Initialize Heston model
heston = HestonModel(S0, v0, mu, sigma, theta, kappa, rho, num_paths, start_date, end_date, granularity)

# Simulate the Heston model
simulated_paths = heston.simulate()

# Plot the simulated paths
heston.plot(paths=simulated_paths,
            title='Asset Price under the Heston model Assumption', 
            ylabel='Asset Price',
            fig_size=(15,5)
        )

heston.plot(paths=simulated_paths,
            title='Asset Volatility under the Heston model Assumption', 
            ylabel='Asset Volatility',
            variance=True,
            fig_size=(15,5)
            )

Plot Plot

License

This project is licensed under the MIT license found in the LICENSE file.

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.5.0.tar.gz (11.7 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.5.0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for finstoch-0.5.0.tar.gz
Algorithm Hash digest
SHA256 ff5d2e474f89fa9d5c0e40e9a5122a165c8826afd20441b9c4358221252cf5f8
MD5 31dbd45338ebe85689e0b0940201d0ad
BLAKE2b-256 648785a13fe10c694c2c5a114cd7b9aaf9adc915dec52f58b180a2b8493763f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for finstoch-0.5.0.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.5.0-py3-none-any.whl.

File metadata

  • Download URL: finstoch-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 18.3 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fed38a47af951ded5ea122e7f2dcc10b64274b1b7a7c5b3d93d9b55698177dd0
MD5 4e2a7a3d2b183fb7741b16204d91972a
BLAKE2b-256 9ac5251681dd5f2e8bc7652752ab703010919b7987c6a5b9b8f1dcaae1b5c8e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for finstoch-0.5.0-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