Permalink
Browse files

remove kernel examples already ported to newparallel

  • Loading branch information...
1 parent 8c88c90 commit 1a971fc3ace97a1e0a40d848838c3bbe83d99637 @minrk minrk committed Apr 8, 2011
Binary file not shown.
View

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,90 +0,0 @@
-"""
-An exceptionally lousy site spider
-Ken Kinder <ken@kenkinder.com>
-
-This module gives an example of how the TaskClient interface to the
-IPython controller works. Before running this script start the IPython controller
-and some engines using something like::
-
- ipcluster -n 4
-"""
-from twisted.python.failure import Failure
-from IPython.kernel import client
-import time
-
-fetchParse = """
-from twisted.web import microdom
-import urllib2
-import urlparse
-
-def fetchAndParse(url, data=None):
- links = []
- try:
- page = urllib2.urlopen(url, data=data)
- except Exception:
- return links
- else:
- if page.headers.type == 'text/html':
- doc = microdom.parseString(page.read(), beExtremelyLenient=True)
- for node in doc.getElementsByTagName('a'):
- if node.getAttribute('href'):
- links.append(urlparse.urljoin(url, node.getAttribute('href')))
- return links
-"""
-
-class DistributedSpider(object):
-
- # Time to wait between polling for task results.
- pollingDelay = 0.5
-
- def __init__(self, site):
- self.tc = client.TaskClient()
- self.rc = client.MultiEngineClient()
- self.rc.execute(fetchParse)
-
- self.allLinks = []
- self.linksWorking = {}
- self.linksDone = {}
-
- self.site = site
-
- def visitLink(self, url):
- if url not in self.allLinks:
- self.allLinks.append(url)
- if url.startswith(self.site):
- print ' ', url
- self.linksWorking[url] = self.tc.run(client.StringTask('links = fetchAndParse(url)', pull=['links'], push={'url': url}))
-
- def onVisitDone(self, result, url):
- print url, ':'
- self.linksDone[url] = None
- del self.linksWorking[url]
- if isinstance(result.failure, Failure):
- txt = result.failure.getTraceback()
- for line in txt.split('\n'):
- print ' ', line
- else:
- for link in result.ns.links:
- self.visitLink(link)
-
- def run(self):
- self.visitLink(self.site)
- while self.linksWorking:
- print len(self.linksWorking), 'pending...'
- self.synchronize()
- time.sleep(self.pollingDelay)
-
- def synchronize(self):
- for url, taskId in self.linksWorking.items():
- # Calling get_task_result with block=False will return None if the
- # task is not done yet. This provides a simple way of polling.
- result = self.tc.get_task_result(taskId, block=False)
- if result is not None:
- self.onVisitDone(result, url)
-
-def main():
- distributedSpider = DistributedSpider(raw_input('Enter site to crawl: '))
- distributedSpider.run()
-
-if __name__ == '__main__':
- main()
@@ -1,14 +0,0 @@
-"""
-A Distributed Hello world
-Ken Kinder <ken@kenkinder.com>
-"""
-from IPython.kernel import client
-
-tc = client.TaskClient()
-mec = client.MultiEngineClient()
-
-mec.execute('import time')
-hello_taskid = tc.run(client.StringTask('time.sleep(3) ; word = "Hello,"', pull=('word')))
-world_taskid = tc.run(client.StringTask('time.sleep(3) ; word = "World!"', pull=('word')))
-print "Submitted tasks:", hello_taskid, world_taskid
-print tc.get_task_result(hello_taskid,block=True).ns.word, tc.get_task_result(world_taskid,block=True).ns.word
@@ -1,148 +0,0 @@
-#!/usr/bin/env python
-"""Run a Monte-Carlo options pricer in parallel."""
-
-#-----------------------------------------------------------------------------
-# Imports
-#-----------------------------------------------------------------------------
-
-import sys
-import time
-from IPython.kernel import client
-import numpy as np
-from mcpricer import price_options
-from matplotlib import pyplot as plt
-
-#-----------------------------------------------------------------------------
-# Setup parameters for the run
-#-----------------------------------------------------------------------------
-
-def ask_question(text, the_type, default):
- s = '%s [%r]: ' % (text, the_type(default))
- result = raw_input(s)
- if result:
- return the_type(result)
- else:
- return the_type(default)
-
-cluster_profile = ask_question("Cluster profile", str, "default")
-price = ask_question("Initial price", float, 100.0)
-rate = ask_question("Interest rate", float, 0.05)
-days = ask_question("Days to expiration", int, 260)
-paths = ask_question("Number of MC paths", int, 10000)
-n_strikes = ask_question("Number of strike values", int, 5)
-min_strike = ask_question("Min strike price", float, 90.0)
-max_strike = ask_question("Max strike price", float, 110.0)
-n_sigmas = ask_question("Number of volatility values", int, 5)
-min_sigma = ask_question("Min volatility", float, 0.1)
-max_sigma = ask_question("Max volatility", float, 0.4)
-
-strike_vals = np.linspace(min_strike, max_strike, n_strikes)
-sigma_vals = np.linspace(min_sigma, max_sigma, n_sigmas)
-
-#-----------------------------------------------------------------------------
-# Setup for parallel calculation
-#-----------------------------------------------------------------------------
-
-# The MultiEngineClient is used to setup the calculation and works with all
-# engine.
-mec = client.MultiEngineClient(profile=cluster_profile)
-
-# 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=cluster_profile)
-
-# Initialize the common code on the engines. This Python module has the
-# price_options function that prices the options.
-mec.run('mcpricer.py')
-
-#-----------------------------------------------------------------------------
-# Perform parallel calculation
-#-----------------------------------------------------------------------------
-
-print "Running parallel calculation over strike prices and volatilities..."
-print "Strike prices: ", strike_vals
-print "Volatilities: ", sigma_vals
-sys.stdout.flush()
-
-# Submit tasks to the TaskClient for each (strike, sigma) pair as a MapTask.
-t1 = time.time()
-taskids = []
-for strike in strike_vals:
- for sigma in sigma_vals:
- t = client.MapTask(
- price_options,
- args=(price, strike, sigma, rate, days, paths)
- )
- taskids.append(tc.run(t))
-
-print "Submitted tasks: ", len(taskids)
-sys.stdout.flush()
-
-# Block until all tasks are completed.
-tc.barrier(taskids)
-t2 = time.time()
-t = t2-t1
-
-print "Parallel calculation completed, time = %s s" % t
-print "Collecting results..."
-
-# Get the results using TaskClient.get_task_result.
-results = [tc.get_task_result(tid) for tid in taskids]
-
-# Assemble the result into a structured NumPy array.
-prices = np.empty(n_strikes*n_sigmas,
- dtype=[('ecall',float),('eput',float),('acall',float),('aput',float)]
-)
-
-for i, price_tuple in enumerate(results):
- prices[i] = price_tuple
-
-prices.shape = (n_strikes, n_sigmas)
-strike_mesh, sigma_mesh = np.meshgrid(strike_vals, sigma_vals)
-
-print "Results are available: strike_mesh, sigma_mesh, prices"
-print "To plot results type 'plot_options(sigma_mesh, strike_mesh, prices)'"
-
-#-----------------------------------------------------------------------------
-# Utilities
-#-----------------------------------------------------------------------------
-
-def plot_options(sigma_mesh, strike_mesh, prices):
- """
- Make a contour plot of the option price in (sigma, strike) space.
- """
- plt.figure(1)
-
- plt.subplot(221)
- plt.contourf(sigma_mesh, strike_mesh, prices['ecall'])
- plt.axis('tight')
- plt.colorbar()
- plt.title('European Call')
- plt.ylabel("Strike Price")
-
- plt.subplot(222)
- plt.contourf(sigma_mesh, strike_mesh, prices['acall'])
- plt.axis('tight')
- plt.colorbar()
- plt.title("Asian Call")
-
- plt.subplot(223)
- plt.contourf(sigma_mesh, strike_mesh, prices['eput'])
- plt.axis('tight')
- plt.colorbar()
- plt.title("European Put")
- plt.xlabel("Volatility")
- plt.ylabel("Strike Price")
-
- plt.subplot(224)
- plt.contourf(sigma_mesh, strike_mesh, prices['aput'])
- plt.axis('tight')
- plt.colorbar()
- plt.title("Asian Put")
- plt.xlabel("Volatility")
-
-
-
-
-
-
@@ -1,45 +0,0 @@
-import numpy as np
-from math import *
-
-
-def price_options(S=100.0, K=100.0, sigma=0.25, r=0.05, days=260, paths=10000):
- """
- 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.
- """
- 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)
- euro_put = r_factor*np.mean(np.maximum(zeros, K-stock_price))
- asian_put = r_factor*np.mean(np.maximum(zeros, K-stock_price_avg))
- euro_call = r_factor*np.mean(np.maximum(zeros, stock_price-K))
- asian_call = r_factor*np.mean(np.maximum(zeros, stock_price_avg-K))
- return (euro_call, euro_put, asian_call, asian_put)
-
@@ -1,18 +0,0 @@
-from IPython.kernel import client
-
-mec = client.MultiEngineClient()
-
-result = mec.map(lambda x: 2*x, range(10))
-print "Simple, default map: ", result
-
-m = mec.mapper(block=False)
-pr = m.map(lambda x: 2*x, range(10))
-print "Submitted map, got PendingResult: ", pr
-result = pr.r
-print "Using a mapper: ", result
-
-@mec.parallel()
-def f(x): return 2*x
-
-result = f(range(10))
-print "Using a parallel function: ", result
Oops, something went wrong.

0 comments on commit 1a971fc

Please sign in to comment.