Skip to main content

StochasticModel — Market data download, model calibration, and option pricing (Black-Scholes / Heston / Merton / Bates)

StochasticModel is a small quantitative finance toolbox built around a single class, StochasticModel(), that helps you:

  • Fetch / load market data (option chains from Yahoo finance)
  • Choose the model type you want to work with
  • Calibrate model parameters to market quotes
  • Price vanilla options using Black–Scholes, Heston, or Merton (jump-diffusion)

This project is for research/education and internal tooling. It is not investment advice.

Install

pip install stochastic-model

Quick usage

Defining a Heston (stochastic volatility) model for Tesla options, fetching data and calibrating it to market prices:

from stochastic_model import StochasticModel

model = StochasticModel(ticker="TSLA", model="heston")

The ticker can be any valid Yahoo Finance ticker with available option data.

The model can take one of these three values:

  • "blach_scholes" to intiate a Black and Scholes model. It takes one parameter, sigma.
  • "heston" to intiate a Heston model with stochastic volatility, taking 5 parameters: kappa, theta, sigma, rho and v0.
  • "bates" to intiate a Bates model, combination of Heston stochastic volatility with Merton's jump diffusion model. It takes eight parameters: kappa, theta, sigma, rho, v0, lambda, mu and delta.
model.fetch_market_data(expiries=model.fetch_expiries()[:5],
                        save_to_class=True, save_to_csv="option_data.csv",
                        atm_threshold=0.10, min_open_interest=2000)

r = 0.0372

heston_calib = model.calibrate(model.data, r, x0=[0.3,0.13,3,-0.5,0.03],
                            print_report=True, print_step=100, error_type="mse",
                            bounds = [(0.1, 3), (0.001, 0.7), (0.001, 2), (-1, 1), (0.01, 0.3)])

model.batch_price_option(model.data, r=r, params=heston_calib, return_column="Heston_predictions")

model.data["Heston_predictions"]

Methods:

fetch_market_data(expiries=[], min_open_interest=100, atm_threshold=0.1, save_to_class=False, save_to_csv="")

Downloads option market data using yfinance. Input parameters are:

  • expiries=[] a list containing dates of expiries for which you want to download the data, e.g., "2026-03-20".
  • min_open_interest=100 filter illiquid options, by only download ing options with a minimum number of open trades.
  • atm_threshold=0.1
  • save_to_class=False whether to only return the data or also store it in the StochasticModel object under data property. If set to True, you can later access the download data through StochasticModel.data.
  • save_to_csv="" address of a local csv file to save the downloaded data.

fetch_expiries()

Returns a list containing expiry dates of the options available on yfinance. It takes no inputs.

load_csv_data(file)

Loads option data from a csv file using pandas.read_csv(). Input parameters are:

  • file="" path to the csv file containing the data, e.g., "option_data.csv". It must contain these columns: "strike", "lastPrice", "impliedVolatility","side", "expiry", "T".

def fetch_last_stock_price()

Loads the last price of the underlying stock or ETF. It prints out the price, returns it and also saves it under StochasticModel.last_stock_price for later being used for calibration and pricing purposes.

price_option(option_side="Call", S0=None, K=None, T=None, r=None, params=None)

Prices a European vanilla option using the class's .model method. Input parameters are:

  • option_side a string that can be "Call" or "Put".
  • S0 last price of the underlying.
  • K strike price.
  • r risk free rate.
  • params a list containing parameters of the chosen model type. Check the model's initialization function at the top of the page for more details.

batch_price_option(data, r=None, params=None, return_column="")

Prices a batch of European vanilla option using the class's .model method. Input parameters are:

  • data a Pandas DataFrame containing the data of the options to price. It must contain these columns: "strike", "lastPrice", "impliedVolatility","side", "expiry", "T".
  • r risk free rate.
  • params a list containing parameters of the chosen model type. Check the model's initialization function at the top of the page for more details.
  • return_column if not empty, the prices will be added to the data DataFrame in a column under this name.

quick_calibration(market_data, r, input_ranges, max_calls, error_type="mse", print_step=50, print_report=False)

Uses brute force to choose the combination of model parameters that minimize the error between model prediction and the provided data. It returns a list containing the optimum parameters. Optimum parameters will also be accessible through object.best_params. Input parameters are:

  • market_data a Pandas DataFrame containing the data of the options to price. It must contain these columns: "strike", "lastPrice", "impliedVolatility","side", "expiry", "T".
  • r risk free rate.
  • input_ranges a tuple or list containing ranges of inputs in the for of (min,max) tuples. Example for black_scholes: [(0.001,0.7)] for a range of values of sigma between 0.001 and 0.7.
  • max_calls maximum number of brute force iterations; number of steps between min and max ranges is decided based on this parameters.
  • error_type the error type to minimize; can be 'mea' (mean absolute error) or 'mse' (mean squared error).
  • print_step print the ongoing results every x iteration.
  • print_report a boolean to activate/deactivate printing the ongoing results.

calibrate(market_data, r, x0, error_type="mse", print_step=50, print_report=False, bounds=[])

Uses scipy.optimize.minimize function to calibrate model parameters and minimize the error between model prediction and the provided data. It returns a list containing the optimum parameters. The method used for minimization is "L-BFGS-B". Check more details on scipy documentations. Optimum parameters will also be accessible through object.best_params. Input parameters are:

  • market_data a Pandas DataFrame containing the data of the options to price. It must contain these columns: "strike", "lastPrice", "impliedVolatility","side", "expiry", "T".
  • r risk free rate.
  • x0 initial value for calibration.
  • error_type the error type to minimize; can be 'mea' (mean absolute error) or 'mse' (mean squared error).
  • print_step print the ongoing results every x iteration.
  • print_report a boolean to activate/deactivate printing the ongoing results.
  • bounds a list of tuples for specifying min and max bounds for each parameter. If empty, default bounds will be considered.

Metadata

Release files for stochastic-model 1.0.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 stochastic-model 1.0.0
File Size Uploaded Provenance
stochastic_model-1.0.0.tar.gz 6.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for stochastic-model 1.0.0
File Interpreter ABI Platform
stochastic_model-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / stochastic_model-1.0.0.tar.gz

Download URL stochastic_model-1.0.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c8248475774c5f91268269846a77681fe999b9eb978549daeccd5233097aa5f7
BLAKE2b-256 checksum
How to use checksums
b5b571f2137293874889e06d60808625e49b863fcaa04a2ab65700bed21754a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / stochastic_model-1.0.0-py3-none-any.whl

Download URL stochastic_model-1.0.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2effd491f84a92510a7b13ccf95fa491db827425178a60ff98f6a16a151a0be0
BLAKE2b-256 checksum
How to use checksums
67a5675c8d303bd0fd7a6e55bc7609e2a1cd7d4de48d25fa7e2a0fdb474a1b5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Security for stochastic-model 1.0.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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No 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