##// END OF EJS Templates
Fixed bugs in IPython.kernel....
Fixed bugs in IPython.kernel. * Removed :mod:`IPython.kernel.contexts` and :mod:`IPython.kernel.tests.test_contexts` and all code that used them. This was broken and needed to be removed. Also Enthought has a nice decorator that does this. * Fixed broken tests in :file:`test_multienginefc.py` and :file:`test_taskfc.py`. See https://bugs.launchpad.net/ipython/+bug/505358. * Changed ports number in :mod:`IPython.kernel.tests` to 10111 to avoid conflicts with users who are using the older 10105 ports. See https://bugs.launchpad.net/ipython/+bug/504515. * Fixed logic and bugs in :mod:`IPython.kernel.fcutils` and :mod:`IPython.kernel.clientconnector` related to how FURL files are validated and found. See https://bugs.launchpad.net/ipython/+bug/502790. * Fixed the unhandled :class:`DeadReferenceError`` exception that was happening in :mod:`IPython.kernel.multiengineclient' and :mod:`IPython.kernel.taskclient` when the controller died or restarted. Now we capture that error and raises a :class:`IPython.kernel.error.ConnectionError` that has a better error message. See https://bugs.launchpad.net/ipython/+bug/502796.

File last commit:

r2346:69191c91
r2517:8185e15c
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.
bgranger
Adding figures for options pricing example.
r2346 mec = client.MultiEngineClient(profile='mycluster')
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.
bgranger
Adding figures for options pricing example.
r2346 tc = client.TaskClient(profile='mycluster')
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
bgranger
Adding figures for options pricing example.
r2346 nK = 10
nsigma = 10
Brian Granger
Initial draft of Windows HPC documentation.
r2344 K_vals = np.linspace(90.0, 100.0, nK)
bgranger
Adding figures for options pricing example.
r2346 sigma_vals = np.linspace(0.1, 0.4, 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))
bgranger
Adding figures for options pricing example.
r2346 print "Submitted tasks: ", len(taskids)
Brian E Granger
Adding examples from ipython1-dev to docs/examples/kernel. These ...
r1337
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")