##// END OF EJS Templates
Work in the documentation.
Work in the documentation.

File last commit:

r2345:cf784db2
r2345:cf784db2
Show More
mcdriver.py
71 lines | 2.2 KiB | text/x-python | PythonLexer
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337 #!/usr/bin/env python
"""Run a Monte-Carlo options pricer in parallel."""
Brian E Granger
Fixed most of the examples. A few still don't work, but this is a start.
r1338 from IPython.kernel import client
Brian Granger
Initial draft of Windows HPC documentation.
r2344 import numpy as np
from mcpricer import price_options
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337
Brian Granger
Work in the documentation.
r2345 # The MultiEngineClient is used to setup the calculation and works with all
# engine.
Brian Granger
Initial draft of Windows HPC documentation.
r2344 mec = client.MultiEngineClient(profile='default')
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337
Brian Granger
Work in the documentation.
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.
tc = client.TaskClient(profile='default')
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337
Brian Granger
Work in the documentation.
r2345 # Initialize the common code on the engines. This Python module has the
# price_options function that prices the options.
Brian Granger
Initial draft of Windows HPC documentation.
r2344 mec.run('mcpricer.py')
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337
Brian Granger
Work in the documentation.
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
Initial draft of Windows HPC documentation.
r2344 def my_prices(K, sigma):
S = 100.0
r = 0.05
days = 260
Brian Granger
Work in the documentation.
r2345 paths = 100000
Brian Granger
Initial draft of Windows HPC documentation.
r2344 return price_options(S, K, sigma, r, days, paths)
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337
# Create arrays of strike prices and volatilities
Brian Granger
Initial draft of Windows HPC documentation.
r2344 nK = 5
nsigma = 5
K_vals = np.linspace(90.0, 100.0, nK)
sigma_vals = np.linspace(0.0, 0.2, nsigma)
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337
Brian Granger
Work in the documentation.
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
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337 taskids = []
for K in K_vals:
for sigma in sigma_vals:
Brian Granger
Initial draft of Windows HPC documentation.
r2344 t = client.MapTask(my_prices, args=(K, sigma))
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337 taskids.append(tc.run(t))
print "Submitted tasks: ", taskids
Brian Granger
Work in the documentation.
r2345 # Block until all tasks are completed.
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337 tc.barrier(taskids)
Brian Granger
Work in the documentation.
r2345 # Get the results using TaskClient.get_task_result.
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337 results = [tc.get_task_result(tid) for tid in taskids]
Brian Granger
Work in the documentation.
r2345 # Assemble the result into a structured NumPy array.
Brian Granger
Initial draft of Windows HPC documentation.
r2344 prices = np.empty(nK*nsigma,
Brian Granger
Work in the documentation.
r2345 dtype=[('ecall',float),('eput',float),('acall',float),('aput',float)]
Brian Granger
Initial draft of Windows HPC documentation.
r2344 )
for i, price_tuple in enumerate(results):
prices[i] = price_tuple
prices.shape = (nK, nsigma)
Brian Granger
Work in the documentation.
r2345 K_vals, sigma_vals = np.meshgrid(K_vals, sigma_vals)
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337
Brian Granger
Work in the documentation.
r2345 def plot_options(sigma_vals, K_vals, prices):
Brian Granger
Initial draft of Windows HPC documentation.
r2344 """
Brian Granger
Work in the documentation.
r2345 Make a contour plot of the option price in (sigma, K) space.
Brian Granger
Initial draft of Windows HPC documentation.
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")