Python: Installing and Using a Module (example: NumPy)

Technical articles
Welcome page
This article describes how to import and use a module in Python. The example is installing NumPy and then using it for valuation of options, specifically a European style call. Elementary Python scripting knowledge is assumed. No knowledge of options is assumed.​
DNS bind name server

DNS bind name server

Importing Modules
Additional functionality can be added to python by using modules. This article shows how to import the module NumPy and then use it for calculating option values.

Further down is some background information on the NumPy (the module) and how it is being used in this example.

What is a Module?
A module is a file of Python code.

The simplest ways to import a module into your script are:
  • import
  • from import *

Installing the Module

numpy can be downloaded from sourceforge .
Also see the numpy website

pip can be used to install the module.

First Install pip:
python -m pip install --upgrade pip

Use pip to install NumPy and associated modules
pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose

Using the Module
Note that this code uses the Python 2.7 print statement, scroll down for 3.x the print statement. The first line indicates that the Macintosh OSX 2.7 Python binary is being used.

An example of one method to import the module is on the second line of the script.


from numpy import *

S0 = 100.          # initial index level
K = 105.           # strike price
T = 1.0            # time-to-maturity
r = 0.05           # riskless short rate
sigma = 0.2        # volatility
I = 100000         # number of iterations/simulations

​# Valuation algorithm
z = random.standard_normal(I)

ST = S0 * exp((r - 0.5 * sigma ** 2) * T + sigma * sqrt(T) * z)

# index values at maturity
hT = maximum(ST - K, 0)            # inner values at maturity
C0 = exp(-r * T) * sum(hT) / I     # Monte Carlo estimator

print "Value of the European Cal Option %5.3f" % C0

Python 3 Print Statement
The following print statement will work with Python 3.
print ("Value of the European Cal Option %5.3f" % C0)

Monte Carlo Simulation, Options and NumPy
Monte Carlo simulation is used for option pricing and risk management problems. However, the Monte Carlo method is computationally demanding. The implementation in NumPy is is more compact and faster than using the standard Python libraries. 

A European style option can only be exercised on the expiration date. In contrast, an American style option can be exercised at any time before the expiration date. European style options are used in examples as their values are easier to calculate.

A call option is the right to buy a specific financial instrument (specific amounts of currency, stocks, commodities, et cetera) at a specific price. The practical use of options is that they provide a type of insurance. If BMW needs a certain amount of Euros on a certain date and is expecting to have US Dollars on hand from car sales, they buy an option to make certain that even if the currency markets go against them, they know what to budget for the transaction. The premium is the amount they pay for an option. The more volatile the underlying security is, the higher the premium. The premium will indicate a certain volatility. This is called implied volatility.

The analogy of car insurance may help make this clear. For example, the higher the likelihood a person will get into an accident, the higher the premium for car insurance.

The model for calculating option prices is the Black-Scholes-Merton model, which is usually referred to as Black-Scholes.

Suggestions for Future Learning

The official NumPy web site is:

Related Articles