Today's lab will focus on the python mathematics library.

Pricing Options

An option in financial markets is a contract that allows you to buy (but does not require you to buy) at a set price on a set date in the future. For example, if you have an option to buy Apple stock at $700 per share on December 1, then if Apple stock is at$720, then you can use the option and make $20 per share. If the Apple stock is$680, then since the contract only gives you the option of buying the shares, you can ignore it.

Companies work hard to price options to maximize their profits. The program for today is to write a program that calculates some of what's needed to price options. A common way to price options is to use the Black Scholes model (we won't go into how the model works, but if you are interested there are many sites that explain more about options and pricing models such as this wiki page and beginner's guide) The relevant formulas (from beginner's guide) are:  where C is the cost of the option that we are calculating. S, T, and K are inputs that represent the current cost of the stock, the time until maturity, and the strike (the fixed price in the future) of the stock, respectively. N() is the cumulative distribution function of the standard normal distribution, r is the risk free rate, and σ (sigma) is volatility of the underlying asset.

That's a lot of math! Python has a built-in library that contains many commonly used math functions already defined for you. We will use them to define d1 and d2. (the cost C can also be calculated using only the math library but requires some understanding of statistics, so, we won't do it here).

To use the math library, you need to include a line in your python file:

import math
which tells the python intepreter to include all the functions in the math library. (You can also use from math import * .)

Looking at the equations for d1 and d2, we will need to calculate logarithm and square roots. For example to find the logarithm of S/K, we would write:

math.log(S/K)
and to find the square root of T, we would write:
math.sqrt(T)
Let's put this altogether in a program to compute d_1:
# Lab 3: calculating option pricing

def optionPricing():
S,T,K,r,sigma = eval(input("Enter values for the stock price, maturity time, strike price, risk free rate, and volatility (separated by commas):"))
d1 = (math.log(S/K) + (r + sigma*sigma/2 )*T ) / (sigma*math.sqrt(T))
print("d1 is", d1)