mcdriver.py
71 lines
| 2.2 KiB
| text/x-python
|
PythonLexer
Brian E Granger
|
r1337 | #!/usr/bin/env python | ||
"""Run a Monte-Carlo options pricer in parallel.""" | ||||
Brian E Granger
|
r1338 | from IPython.kernel import client | ||
Brian Granger
|
r2344 | import numpy as np | ||
from mcpricer import price_options | ||||
Brian E Granger
|
r1337 | |||
Brian Granger
|
r2345 | # The MultiEngineClient is used to setup the calculation and works with all | ||
# engine. | ||||
bgranger
|
r2346 | mec = client.MultiEngineClient(profile='mycluster') | ||
Brian E Granger
|
r1337 | |||
Brian Granger
|
r2345 | # The TaskClient is an interface to the engines that provides dynamic load | ||
# balancing at the expense of not knowing which engine will execute the code. | ||||
bgranger
|
r2346 | tc = client.TaskClient(profile='mycluster') | ||
Brian E Granger
|
r1337 | |||
Brian Granger
|
r2345 | # Initialize the common code on the engines. This Python module has the | ||
# price_options function that prices the options. | ||||
Brian Granger
|
r2344 | mec.run('mcpricer.py') | ||
Brian E Granger
|
r1337 | |||
Brian Granger
|
r2345 | # Define the function that will make up our tasks. We basically want to | ||
# call the price_options function with all but two arguments (K, sigma) | ||||
# fixed. | ||||
Brian Granger
|
r2344 | def my_prices(K, sigma): | ||
S = 100.0 | ||||
r = 0.05 | ||||
days = 260 | ||||
Brian Granger
|
r2345 | paths = 100000 | ||
Brian Granger
|
r2344 | return price_options(S, K, sigma, r, days, paths) | ||
Brian E Granger
|
r1337 | |||
# Create arrays of strike prices and volatilities | ||||
bgranger
|
r2346 | nK = 10 | ||
nsigma = 10 | ||||
Brian Granger
|
r2344 | K_vals = np.linspace(90.0, 100.0, nK) | ||
bgranger
|
r2346 | sigma_vals = np.linspace(0.1, 0.4, nsigma) | ||
Brian E Granger
|
r1337 | |||
Brian Granger
|
r2345 | # Submit tasks to the TaskClient for each (K, sigma) pair as a MapTask. | ||
# The MapTask simply applies a function (my_prices) to the arguments: | ||||
# my_prices(K, sigma) and returns the result. | ||||
Brian E Granger
|
r1337 | taskids = [] | ||
for K in K_vals: | ||||
for sigma in sigma_vals: | ||||
Brian Granger
|
r2344 | t = client.MapTask(my_prices, args=(K, sigma)) | ||
Brian E Granger
|
r1337 | taskids.append(tc.run(t)) | ||
bgranger
|
r2346 | print "Submitted tasks: ", len(taskids) | ||
Brian E Granger
|
r1337 | |||
Brian Granger
|
r2345 | # Block until all tasks are completed. | ||
Brian E Granger
|
r1337 | tc.barrier(taskids) | ||
Brian Granger
|
r2345 | # Get the results using TaskClient.get_task_result. | ||
Brian E Granger
|
r1337 | results = [tc.get_task_result(tid) for tid in taskids] | ||
Brian Granger
|
r2345 | # Assemble the result into a structured NumPy array. | ||
Brian Granger
|
r2344 | prices = np.empty(nK*nsigma, | ||
Brian Granger
|
r2345 | dtype=[('ecall',float),('eput',float),('acall',float),('aput',float)] | ||
Brian Granger
|
r2344 | ) | ||
for i, price_tuple in enumerate(results): | ||||
prices[i] = price_tuple | ||||
prices.shape = (nK, nsigma) | ||||
Brian Granger
|
r2345 | K_vals, sigma_vals = np.meshgrid(K_vals, sigma_vals) | ||
Brian E Granger
|
r1337 | |||
Brian Granger
|
r2345 | def plot_options(sigma_vals, K_vals, prices): | ||
Brian Granger
|
r2344 | """ | ||
Brian Granger
|
r2345 | Make a contour plot of the option price in (sigma, K) space. | ||
Brian Granger
|
r2344 | """ | ||
from matplotlib import pyplot as plt | ||||
plt.contourf(sigma_vals, K_vals, prices) | ||||
plt.colorbar() | ||||
plt.title("Option Price") | ||||
plt.xlabel("Volatility") | ||||
plt.ylabel("Strike Price") | ||||