Simple Monte Carlo Options Pricer In Python

Pre-Requisites:

Required:

Recommended:

Understanding The Math

Assumptions

The Stock Price Evolution Model

Black-Scholes Option Pricing Model

Solving for Expectation

Final Expression

The Code

import math
import random

class SimpleMCPricer():
def __init__(self, expiry, strike, spot, vol, r, paths):
#The sigma value on the left side of the exponent
self.variance = vol**2 * expiry
#The sigma value on the right side of the e exponent
self.root_Variance = math.sqrt(self.variance)
#Corresponds to the (-1/2 * sigma^2)
self.itoCorr = -0.5*self.variance
##Corresponds to S0e^(rT - 1/2 sigma^2T)
self.movedSpot = spot*math.exp(r*expiry + self.itoCorr)
self.runningSum = 0
##Simulate for all paths
for i in range(0,paths):
thisGauss = random.randrange(0,1000,1)
thisGauss = thisGauss/1000
##Our rootVariance already has been multiplied by the expiry
thisSpot = self.movedSpot*math.exp(self.root_Variance*thisGauss)
#Determine payoff of this specific path
thisPayoff = thisSpot - strike
#Value of option is zero is our price is less than the strike
thisPayoff = thisPayoff if thisPayoff > 0 else 0
self.runningSum+=thisPayoff

self.mean = self.runningSum/paths
self.mean*= math.exp(-r * expiry)

def getMean(self):
return round(self.mean,2)

Run the Model!

model = SimpleMCPricer(2,32,30,.1,0.03,1000000)
model.getMean()
1.79

Sources and Further Reading

--

--

--

Computer Scientist Blog: www.pranav.ai Website: www.prandev.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

MMRotate: The most comprehensive and coherent algorithm framework for rotated object detection

Face Recognition using Computer Vision which integrates with AWS-CLI and WhatsApp

Artificial Intelligence

AI writes on ‘Tensorflow’

Google’s Best practices for Machine Learning Engineering

Want to Generate your own Music using Deep Learning? Here’s a Guide to do just that!

How to get started with machine learning on graphs

Classification of venomous snakes using fast.ai

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pranav Ahluwalia

Pranav Ahluwalia

Computer Scientist Blog: www.pranav.ai Website: www.prandev.com

More from Medium

Plot Volatility Surface in Python using

The Python Quants Certificate Program: OOP-based Ensemble Methods Backtester

Testing the accuracy of your models predictions

Using Python, Data Science and Classic Time Series models to create a trading algorithm for the US…