|
1 | 1 | {
|
2 |
| - "cells": [], |
3 |
| - "metadata": {}, |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 159, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import numpy as np\n", |
| 10 | + "import math\n", |
| 11 | + "import time" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 176, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "name": "stdout", |
| 21 | + "output_type": "stream", |
| 22 | + "text": [ |
| 23 | + "Call option price with Monte carlo approach: 10.45805672244926\n", |
| 24 | + "Put option price wiht Monte carlo approach: 5.574003411737752\n" |
| 25 | + ] |
| 26 | + } |
| 27 | + ], |
| 28 | + "source": [ |
| 29 | + "class OptionPricing:\n", |
| 30 | + " def __init__(self, s0, E, T, rf,sigma, iterations):\n", |
| 31 | + " self.s0 = s0\n", |
| 32 | + " self.E = E\n", |
| 33 | + " self.T = T\n", |
| 34 | + " self.rf = rf\n", |
| 35 | + " self.iterations = iterations\n", |
| 36 | + " self.sigma = sigma\n", |
| 37 | + " \n", |
| 38 | + " def call_option_simulation(self):\n", |
| 39 | + " #2 columns. column one for zeors, and column 2 for payoff max(0, S-E) for call option\n", |
| 40 | + " option_data = np.zeros([self.iterations,2])\n", |
| 41 | + " \n", |
| 42 | + " #1d array with items as many as Iterations\n", |
| 43 | + " rand = np.random.normal(0,1,[1, self.iterations])\n", |
| 44 | + " \n", |
| 45 | + " #stock price equation\n", |
| 46 | + " stock_price = self.s0*np.exp(self.T*(self.rf- 0.5*self.sigma**2)+self.sigma*np.sqrt(self.T)*rand)\n", |
| 47 | + " \n", |
| 48 | + " #calculate S-E\n", |
| 49 | + " option_data[:,1] = stock_price - self.E\n", |
| 50 | + " \n", |
| 51 | + " #np.amax() to return the max(0,S-E)\n", |
| 52 | + " average = np.sum(np.amax(option_data, axis=1))/(float(self.iterations))\n", |
| 53 | + " \n", |
| 54 | + " #using exp(-rT) to discount the price\n", |
| 55 | + " return np.exp(-1.0*self.rf*self.T)*average\n", |
| 56 | + " \n", |
| 57 | + " \n", |
| 58 | + " def put_option_simulation(self):\n", |
| 59 | + " \n", |
| 60 | + " #2 columns. column one for zeors, and column 2 for payoff max(0, S-E) for call option\n", |
| 61 | + " option_data = np.zeros([self.iterations,2])\n", |
| 62 | + " \n", |
| 63 | + " #1d array with items as many as Iterations\n", |
| 64 | + " rand = np.random.normal(0,1,[1, self.iterations])\n", |
| 65 | + " \n", |
| 66 | + " #stock price equation\n", |
| 67 | + " stock_price = self.s0*np.exp(self.T*(self.rf-0.5*self.sigma**2)+self.sigma*np.sqrt(self.T)*rand)\n", |
| 68 | + " \n", |
| 69 | + " #calculate E-S - put options\n", |
| 70 | + " option_data[:,1] = self.E - stock_price\n", |
| 71 | + " \n", |
| 72 | + " #np.amax() to return the max(0,S-E)\n", |
| 73 | + " average = np.sum(np.amax(option_data,axis=1))/float(self.iterations)\n", |
| 74 | + " \n", |
| 75 | + " #using exp(-rT) to discount the price\n", |
| 76 | + " return np.exp(-1.0*self.rf*self.T)*average\n", |
| 77 | + " \n", |
| 78 | + "if __name__ == \"__main__\":\n", |
| 79 | + " s0 = 100 #stock price at t=0\n", |
| 80 | + " E = 100 #strike price\n", |
| 81 | + " T = 1 #time to maturity\n", |
| 82 | + " rf = 0.05 #risk free rate\n", |
| 83 | + " sigma = 0.2 #volatility of underlying stock\n", |
| 84 | + " iterations = 1000000 #number of iterations in the Monte carlo simulation\n", |
| 85 | + " \n", |
| 86 | + " model = OptionPricing(s0,E,T,rf,sigma,iterations)\n", |
| 87 | + " print ('Call option price with Monte carlo approach: ', model.call_option_simulation())\n", |
| 88 | + " print('Put option price wiht Monte carlo approach: ', model.put_option_simulation())" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": null, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [] |
| 97 | + } |
| 98 | + ], |
| 99 | + "metadata": { |
| 100 | + "kernelspec": { |
| 101 | + "display_name": "Python 3", |
| 102 | + "language": "python", |
| 103 | + "name": "python3" |
| 104 | + }, |
| 105 | + "language_info": { |
| 106 | + "codemirror_mode": { |
| 107 | + "name": "ipython", |
| 108 | + "version": 3 |
| 109 | + }, |
| 110 | + "file_extension": ".py", |
| 111 | + "mimetype": "text/x-python", |
| 112 | + "name": "python", |
| 113 | + "nbconvert_exporter": "python", |
| 114 | + "pygments_lexer": "ipython3", |
| 115 | + "version": "3.6.5" |
| 116 | + } |
| 117 | + }, |
4 | 118 | "nbformat": 4,
|
5 | 119 | "nbformat_minor": 2
|
6 | 120 | }
|
0 commit comments