##// END OF EJS Templates
Restore trailing spaces in certain doctests, which expect there to be trailing spaces.
Restore trailing spaces in certain doctests, which expect there to be trailing spaces.

File last commit:

r4637:d919e2ec
r4873:90f40a64
Show More
mcpricer.ipynb
242 lines | 111.4 KiB | text/plain | TextLexer

Parallel Monto-Carlo options pricingĀ¶

Problem setupĀ¶

InĀ [1]:
import sys
import time
from IPython.parallel import Client
import numpy as np
from mckernel import price_options
from matplotlib import pyplot as plt
InĀ [2]:
cluster_profile = "default"
price = 100.0  # Initial price
rate = 0.05  # Interest rate
days = 260  # Days to expiration
paths = 10000  # Number of MC paths
n_strikes = 5  # Number of strike values
min_strike = 90.0  # Min strike price
max_strike = 110.0  # Max strike price
n_sigmas = 5  # Number of volatility values
min_sigma = 0.1  # Min volatility
max_sigma = 0.4  # Max volatility
InĀ [3]:
strike_vals = np.linspace(min_strike, max_strike, n_strikes)
sigma_vals = np.linspace(min_sigma, max_sigma, n_sigmas)

Parallel computation across strike prices and volatilitiesĀ¶

The Client is used to setup the calculation and works with all engines.

InĀ [4]:
c = Client(profile=cluster_profile)

A LoadBalancedView is an interface to the engines that provides dynamic load balancing at the expense of not knowing which engine will execute the code.

InĀ [5]:
view = c.load_balanced_view()
InĀ [6]:
print "Strike prices: ", strike_vals
print "Volatilities: ", sigma_vals
Strike prices:  [  90.   95.  100.  105.  110.]
Volatilities:  [ 0.1    0.175  0.25   0.325  0.4  ]

Submit tasks for each (strike, sigma) pair.

InĀ [7]:
t1 = time.time()
async_results = []
for strike in strike_vals:
    for sigma in sigma_vals:
        ar = view.apply_async(price_options, price, strike, sigma, rate, days, paths)
        async_results.append(ar)
InĀ [8]:
print "Submitted tasks: ", len(async_results)
Submitted tasks:  25

Block until all tasks are completed.

InĀ [9]:
c.wait(async_results)
t2 = time.time()
t = t2-t1

print "Parallel calculation completed, time = %s s" % t
Parallel calculation completed, time = 2.57819604874 s

Process and visualize resultsĀ¶

Get the results using the get method:

InĀ [10]:
results = [ar.get() for ar in async_results]

Assemble the result into a structured NumPy array.

InĀ [11]:
prices = np.empty(n_strikes*n_sigmas,
    dtype=[('ecall',float),('eput',float),('acall',float),('aput',float)]
)

for i, price in enumerate(results):
    prices[i] = tuple(price)
    
prices.shape = (n_strikes, n_sigmas)
strike_mesh, sigma_mesh = np.meshgrid(strike_vals, sigma_vals)

Plot the value of the European call in (volatility, strike) space.

InĀ [12]:
plt.contourf(sigma_mesh, strike_mesh, prices['ecall'])
plt.axis('tight')
plt.colorbar()
plt.title('European Call')
plt.xlabel("Volatility")
plt.ylabel("Strike Price")
Out[12]:
<matplotlib.text.Text at 0x2c54750>
No description has been provided for this image

Plot the value of the Asian call in (volatility, strike) space.

InĀ [13]:
plt.contourf(sigma_mesh, strike_mesh, prices['acall'])
plt.axis('tight')
plt.colorbar()
plt.title("Asian Call")
plt.xlabel("Volatility")
plt.ylabel("Strike Price")
Out[13]:
<matplotlib.text.Text at 0x2e14610>
No description has been provided for this image

Plot the value of the European put in (volatility, strike) space.

InĀ [14]:
plt.contourf(sigma_mesh, strike_mesh, prices['eput'])
plt.axis('tight')
plt.colorbar()
plt.title("European Put")
plt.xlabel("Volatility")
plt.ylabel("Strike Price")
Out[14]:
<matplotlib.text.Text at 0x32ee190>
No description has been provided for this image

Plot the value of the Asian put in (volatility, strike) space.

InĀ [15]:
plt.contourf(sigma_mesh, strike_mesh, prices['aput'])
plt.axis('tight')
plt.colorbar()
plt.title("Asian Put")
plt.xlabel("Volatility")
plt.ylabel("Strike Price")
Out[15]:
<matplotlib.text.Text at 0x3487c50>
No description has been provided for this image