CIS 166: Introductory Programming

Lehman College, City University of New York

Fall 2014

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

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 d_{1} and d_{2}.
(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 mathwhich tells the python intepreter to include all the functions in the math library. (You can also use:

Looking at the equations for d_{1} and d_{2}, 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 # Your name here 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) # Run your program: optionPricing()Remember when writing mathematics in python to use * for multiplication and to use parenthesis to group terms together. Test your program with a stock price of 100, maturity time of 1, strike price of 200, risk free rate of 2.5, and volatility of .1 (10%) (your program should display 18.1185281944).

Now, extend your program to calculate and display d_{2}. For the test case above, you should
print out 18.0185281944 for d_{2}.

With the remaining time, work on the programming programs.