{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Binomial Pricing Model\n",
"### [(Go to Quant Lab)](https://israeldi.github.io/quantlab/)\n",
"\n",
"#### Source: Numerical Methods in Finance with C++\n",
"\n",
"© Maciej J. Capinski, Tomasz Zastawniak\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Table of Contents\n",
"\n",
"1. [Random Numbers](#1.-Random-Numbers)\n",
"2. [Plotting Random Samples](#2.-Plotting-Random-Samples)\n",
"3. [Simulation](#3.-Simulation)\n",
" - 3.1 [Random Variables](#3.1-Random-Variables)\n",
" - 3.2 [Stochastic Processes](#3.2-Stochastic-Processes)\n",
" - 3.2.1 [Geometric Brownian Motion](#3.2.1-Geometric-Brownian-Motion)\n",
" - 3.2.2 [Square-Root Diffusion](#3.2.2-Square-Root-Diffusion)\n",
" - 3.2.3 [Stochastic Processes](#3.2-Stochastic-Processes)\n",
" - 3.2.4 [Stochastic Processes](#3.2-Stochastic-Processes)\n",
" - 3.3 [Variance Reduction](#3.3-Variance-Reduction)\n",
"4. [Valuation](#4.-Valuation)\n",
" - 4.1 [European Options](#4.1-European-Options)\n",
" - 4.2 [American Options](#4.2-American-Options)\n",
"5. [Risk Measures](#5.-Risk-Measures)\n",
" - 5.1 [Value-at-Risk](#5.1-Value-at-Risk)\n",
" - 5.2 [Credit Value Adjustments](#5.2-Credit-Value-Adjustments)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initially import all the modules we will be using for our notebook"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import numpy as np\n",
"import numpy.random as npr \n",
"import sys\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.1 Creating Main Program"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hi there\n",
"Provide a character: 1\n"
]
}
],
"source": [
"if __name__== \"__main__\":\n",
" print(\"Hi there\")\n",
" input(\"Provide a character: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.2 Entering Data\n",
"#### ([Back to Top](#Table-of-Contents))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter S_0: 1\n",
"Enter U: 2\n",
"Enter D: -0.5\n",
"Enter R: 0\n",
"Input data checked\n",
"There is no arbitrage\n",
"\n",
"q = 0.2\n",
"n = 3\n",
"i = 2\n",
"S(n,i) = 4.5\n"
]
}
],
"source": [
"if __name__== \"__main__\":\n",
" # Get input parameters S0, U, D, R\n",
" # YOUR CODE....\n",
"\n",
" # make sure that 0< S0, -1 < D < U, -1 < R\n",
" # YOUR CODE....\n",
" \n",
" # checking arbitrage condition\n",
" # YOUR CODE....\n",
"\n",
" print(\"Input data checked\")\n",
" print(\"There is no arbitrage\\n\")\n",
" \n",
" # compute risk-neutral probability\n",
" # YOUR CODE....\n",
" \n",
" # compute stock price at node n=3,i=2\n",
" n = 3; i = 2\n",
" print(\"n = \", n)\n",
" print(\"i = \", i)\n",
" print(\"S(n,i) = \", \"YOUR CODE\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.3 Functions\n",
"#### ([Back to Top](#Table-of-Contents))"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"uuid": "4618b170-6bd3-4500-905a-0fe402f198c1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"q = 0.13043478260869565\n",
"Enter S0: 3\n",
"Enter U: 2\n",
"Enter D: -0.2\n",
"Enter R: 0\n",
"Input data checked\n",
"There is no arbitrage\n",
"\n",
"n = 3\n",
"i = 2\n",
"S(n,i) = 21.6\n"
]
}
],
"source": [
"# computing risk-neutral probability\n",
"def RiskNeutProb(U, D, R):\n",
" # YOUR CODE....\n",
"\n",
"# computing the stock price at node n,i\n",
"def S(S0, U, D, n, i):\n",
" '''\n",
" Returns the stock price at S(n, i)\n",
" '''\n",
" \n",
" # YOUR CODE....\n",
"\n",
"def isValidInput(S0, U, D, R):\n",
" '''\n",
" Returns: This function should return zero if any condition is violated, returns 1 otherwise\n",
" '''\n",
" \n",
" # making sure that 0