{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f755836",
   "metadata": {},
   "source": [
    "<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdfa54ce",
   "metadata": {},
   "source": [
    "This notebook simulates an asset price path with jumps using a Jump-Diffusion model. The model incorporates both continuous Brownian motion and discrete jumps to capture sudden price changes. The asset price path is generated through a loop, updating the price at each time step based on random components. Parameters such as initial stock price, drift, volatility, jump intensity, and jump size are used to define the simulation. This is useful in practice for modeling asset prices that may experience sudden changes, such as stocks or commodities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7978242b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bc38516",
   "metadata": {},
   "source": [
    "Define initial stock price, drift, volatility, and jump parameters for the simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dc2ffcff",
   "metadata": {},
   "outputs": [],
   "source": [
    "S0 = 100  # Initial stock price\n",
    "mu = 0.05  # Drift\n",
    "sigma = 0.2  # Volatility\n",
    "lambda_ = 0.1  # Jump intensity (average number of jumps per year)\n",
    "mu_j = -0.2  # Mean of the jump size\n",
    "sigma_j = 0.1  # Standard deviation of the jump size\n",
    "T = 1  # Time horizon (1 year)\n",
    "dt = 1/252  # Time step (1 trading day)\n",
    "N = int(T / dt)  # Number of steps"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29e4d6d2",
   "metadata": {},
   "source": [
    "Seed the random number generator for reproducibility"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "220b24ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(42)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8577dd29",
   "metadata": {},
   "source": [
    "Initialize an array to store simulated prices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "12b8ba86",
   "metadata": {},
   "outputs": [],
   "source": [
    "prices = np.zeros(N)\n",
    "prices[0] = S0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8efd0278",
   "metadata": {},
   "source": [
    "Simulate the asset price path using a loop to update the price at each time step. Each update incorporates both Brownian motion and potential jumps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "079bf838",
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in range(1, N):\n",
    "    # Generate normally distributed random variable for Brownian motion\n",
    "    Z = np.random.normal(0, 1)\n",
    "    \n",
    "    # Determine if a jump occurs and generate the jump size\n",
    "    J = np.random.normal(mu_j, sigma_j) if np.random.poisson(lambda_ * dt) > 0 else 0\n",
    "    \n",
    "    # Update the price using the Jump-Diffusion model formula\n",
    "    prices[t] = prices[t-1] * np.exp((mu - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * Z + J)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64df25f3",
   "metadata": {},
   "source": [
    "Plot the simulated asset price path to visualize the outcome"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6fcd9cb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 72240 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(prices, label='Simulated Asset Price with Jumps')\n",
    "plt.xlabel('Time (days)')\n",
    "plt.ylabel('Price')\n",
    "plt.title('Simulated Asset Price Path with Poisson Jumps')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30458478",
   "metadata": {},
   "source": [
    "<a href=\"https://pyquantnews.com/\">PyQuant News</a> is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to <a href=\"https://gettingstartedwithpythonforquantfinance.com/\">get started with Python for quant finance</a>. For educational purposes. Not investment advise. Use at your own risk."
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
