##// END OF EJS Templates
Merging Brian's work on branch: lp:~ipython-dev/ipython/kernel-config...
Merging Brian's work on branch: lp:~ipython-dev/ipython/kernel-config This makes trunk significantly more usable again.

File last commit:

r2345:cf784db2
r2352:48bf2e26 merge
Show More
mcpricer.py
45 lines | 1.5 KiB | text/x-python | PythonLexer
Brian Granger
Initial draft of Windows HPC documentation.
r2344 import numpy as np
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337 from math import *
Brian Granger
Initial draft of Windows HPC documentation.
r2344 def price_options(S=100.0, K=100.0, sigma=0.25, r=0.05, days=260, paths=10000):
"""
Brian Granger
Work in the documentation.
r2345 Price European and Asian options using a Monte Carlo method.
Parameters
----------
S : float
The initial price of the stock.
K : float
The strike price of the option.
sigma : float
The volatility of the stock.
r : float
The risk free interest rate.
days : int
The number of days until the option expires.
paths : int
The number of Monte Carlo paths used to price the option.
Returns
-------
A tuple of (E. call, E. put, A. call, A. put) option prices.
Brian Granger
Initial draft of Windows HPC documentation.
r2344 """
h = 1.0/days
const1 = exp((r-0.5*sigma**2)*h)
const2 = sigma*sqrt(h)
stock_price = S*np.ones(paths, dtype='float64')
stock_price_sum = np.zeros(paths, dtype='float64')
for j in range(days):
growth_factor = const1*np.exp(const2*np.random.standard_normal(paths))
stock_price = stock_price*growth_factor
stock_price_sum = stock_price_sum + stock_price
stock_price_avg = stock_price_sum/days
zeros = np.zeros(paths, dtype='float64')
r_factor = exp(-r*h*days)
Brian Granger
Work in the documentation.
r2345 euro_put = r_factor*np.mean(np.maximum(zeros, K-stock_price))
Brian Granger
Initial draft of Windows HPC documentation.
r2344 asian_put = r_factor*np.mean(np.maximum(zeros, K-stock_price_avg))
Brian Granger
Work in the documentation.
r2345 euro_call = r_factor*np.mean(np.maximum(zeros, stock_price-K))
Brian Granger
Initial draft of Windows HPC documentation.
r2344 asian_call = r_factor*np.mean(np.maximum(zeros, stock_price_avg-K))
Brian Granger
Work in the documentation.
r2345 return (euro_call, euro_put, asian_call, asian_put)
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337