Permalink
Browse files

Reworking parallel examples as notebooks.

  • Loading branch information...
1 parent f6078d0 commit 3ec3b2431e5c1ed436bd6d6e843946ab3fd854e6 @ellisonbg ellisonbg committed Aug 16, 2011
View
@@ -3530,7 +3530,6 @@ def magic_notebook(self, s):
formats include (xml/ipynb, json, py).
"""
args = magic_arguments.parse_argstring(self.magic_notebook, s)
- print args
from IPython.nbformat import current
if args.export:
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="utf-8"?>
+<notebook>
+ <name>helloworld</name>
+ <nbformat>2</nbformat>
+ <worksheets>
+ <worksheet>
+ <cells>
+ <markdowncell>
+ <source># Distributed hello world
+
+Originally by Ken Kinder (ken at kenkinder dom com)</source>
+ </markdowncell>
+ <codecell>
+ <input>from IPython.parallel import Client</input>
+ <language>python</language>
+ <prompt_number>3</prompt_number>
+ <collapsed>1</collapsed>
+ <outputs />
+ </codecell>
+ <codecell>
+ <input>rc = Client()
+view = rc.load_balanced_view()</input>
+ <language>python</language>
+ <prompt_number>4</prompt_number>
+ <collapsed>1</collapsed>
+ <outputs />
+ </codecell>
+ <codecell>
+ <input>def sleep_and_echo(t, msg):
+ import time
+ time.sleep(t)
+ return msg</input>
+ <language>python</language>
+ <prompt_number>5</prompt_number>
+ <collapsed>1</collapsed>
+ <outputs />
+ </codecell>
+ <codecell>
+ <input>world = view.apply_async(sleep_and_echo, 3, 'World!')
+hello = view.apply_async(sleep_and_echo, 2, 'Hello')
+</input>
+ <language>python</language>
+ <prompt_number>6</prompt_number>
+ <collapsed>1</collapsed>
+ <outputs />
+ </codecell>
+ <codecell>
+ <input>print "Submitted tasks:", hello.msg_ids, world.msg_ids
+print hello.get(), world.get()</input>
+ <language>python</language>
+ <prompt_number>7</prompt_number>
+ <collapsed>0</collapsed>
+ <outputs>
+ <output>
+ <output_type>stream</output_type>
+ <text>Submitted tasks: [&amp;apos;9e533683-d54e-4588-929e-984dd3eb6dc4&amp;apos;] [&amp;apos;90395f15-723f-44df-a743-a5d88cdeb6a0&amp;apos;]
+Hello</text>
+ </output>
+ <output>
+ <output_type>stream</output_type>
+ <text>World!</text>
+ </output>
+ </outputs>
+ </codecell>
+ </cells>
+ </worksheet>
+ </worksheets>
+</notebook>
@@ -1,19 +1,34 @@
-"""
-A Distributed Hello world
-Ken Kinder <ken@kenkinder.com>
-"""
+# <nbformat>2</nbformat>
+
+# <markdowncell>
+
+# # Distributed hello world
+#
+# Originally by Ken Kinder (ken at kenkinder dom com)
+
+# <codecell>
+
from IPython.parallel import Client
+# <codecell>
+
rc = Client()
+view = rc.load_balanced_view()
+
+# <codecell>
def sleep_and_echo(t, msg):
import time
time.sleep(t)
return msg
-
-view = rc.load_balanced_view()
+
+# <codecell>
world = view.apply_async(sleep_and_echo, 3, 'World!')
hello = view.apply_async(sleep_and_echo, 2, 'Hello')
+
+# <codecell>
+
print "Submitted tasks:", hello.msg_ids, world.msg_ids
print hello.get(), world.get()
+
@@ -1,45 +0,0 @@
-
-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.
- """
- import numpy as np
- from math import exp,sqrt
-
- 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)
-
Oops, something went wrong.

0 comments on commit 3ec3b24

Please sign in to comment.