This repository contains Python scripts and a Jupyter notebook for simulating stock prices using the Heston model and calculating implied volatilities from the simulated data. The Heston model is a stochastic volatility model used primarily to price options. It assumes that an asset's volatility is not constant but follows a stochastic process, specifically a mean-reverting square-root process. This allows the model to capture the observed market phenomena where volatility changes over time and is correlated with the asset's price.
The Heston model specifies the following dynamics for the asset price
-
Price Dynamics:
$$dS(t) = \mu S(t) , dt + \sqrt{v(t)} S(t) , dW_s(t)$$ Here, $\mu is the risk-free rate,
$S(t)$ is the spot price of the asset,$v(t)$ is the variance, and$dW_s(t)$ is the increment of a Wiener process (Brownian motion) driving the asset price. -
Variance Dynamics:
$$dv(t) = \kappa (\theta - v(t)) , dt + \sigma \sqrt{v(t)} , dW_v(t)$$ $\kappa$ represents the rate of reversion,$\theta$ is the long-term variance mean,$\sigma$ is the volatility of volatility, and$dW_v(t)$ is the increment of a second, correlated Wiener process.
The model allows for a correlation between the two Wiener processes,
The project is structured into three main components:
Heston_Model.py
: Implements the Heston model for simulating stock price paths and volatility.Implied_Vol.ipynb
: Jupyter notebook for calculating the implied volatilities from the simulated stock prices using the Heston model.utils.py
: Contains utility functions to fetch and prepare data required for the simulations.
- Script:
Heston_Model.py
- Functionality: Simulates multiple paths of stock prices and volatility using the Heston model. Users can specify the stock ticker, simulation period, number of simulations, and the discretization method (Euler or Milstein).
- Output: The script outputs the simulated price paths, which are then visualized or further analyzed for financial metrics.
- Functionality: Uses the results from the Heston model to calculate European put and call option prices at various strike prices. Then, it computes the implied volatilities for these options and plots the volatility smile.
- Visualization: The notebook includes plots of the implied volatility curves (volatility smile) and histograms showing the probability density function of the stock prices at expiration.
- Functionality: Provides essential data fetching and preprocessing functions used by the Heston model simulation. This includes fetching stock and volatility data from Yahoo Finance, processing the data to compute initial values and correlations needed for the model.
- Python 3.x
- NumPy
- Matplotlib
- pandas
- yfinance
- py_vollib_vectorized