Show More
@@ -1,70 +1,71 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """Run a Monte-Carlo options pricer in parallel.""" |
|
3 | """Run a Monte-Carlo options pricer in parallel.""" | |
4 |
|
4 | |||
5 | from IPython.kernel import client |
|
5 | from IPython.kernel import client | |
6 | import numpy as N |
|
6 | import numpy as N | |
7 | from mcpricer import MCOptionPricer |
|
7 | from mcpricer import MCOptionPricer | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | tc = client.TaskClient() |
|
10 | tc = client.TaskClient() | |
11 | rc = client.MultiEngineClient() |
|
11 | rc = client.MultiEngineClient() | |
12 |
|
12 | |||
13 | # Initialize the common code on the engines |
|
13 | # Initialize the common code on the engines | |
14 | rc.run('mcpricer.py') |
|
14 | rc.run('mcpricer.py') | |
15 |
|
15 | |||
16 |
# Push the variables that won't change |
|
16 | # Push the variables that won't change | |
|
17 | #(stock print, interest rate, days and MC paths) | |||
17 | rc.push(dict(S=100.0, r=0.05, days=260, paths=10000)) |
|
18 | rc.push(dict(S=100.0, r=0.05, days=260, paths=10000)) | |
18 |
|
19 | |||
19 | task_string = """\ |
|
20 | task_string = """\ | |
20 | op = MCOptionPricer(S,K,sigma,r,days,paths) |
|
21 | op = MCOptionPricer(S,K,sigma,r,days,paths) | |
21 | op.run() |
|
22 | op.run() | |
22 | vp, ap, vc, ac = op.vanilla_put, op.asian_put, op.vanilla_call, op.asian_call |
|
23 | vp, ap, vc, ac = op.vanilla_put, op.asian_put, op.vanilla_call, op.asian_call | |
23 | """ |
|
24 | """ | |
24 |
|
25 | |||
25 | # Create arrays of strike prices and volatilities |
|
26 | # Create arrays of strike prices and volatilities | |
26 |
K_vals = N. |
|
27 | K_vals = N.linspace(90.0,100.0,5) | |
27 |
sigma_vals = N. |
|
28 | sigma_vals = N.linspace(0.0, 0.2,5) | |
28 |
|
29 | |||
29 | # Submit tasks |
|
30 | # Submit tasks | |
30 | taskids = [] |
|
31 | taskids = [] | |
31 | for K in K_vals: |
|
32 | for K in K_vals: | |
32 | for sigma in sigma_vals: |
|
33 | for sigma in sigma_vals: | |
33 | t = client.Task(task_string, |
|
34 | t = client.Task(task_string, | |
34 | push=dict(sigma=sigma,K=K), |
|
35 | push=dict(sigma=sigma,K=K), | |
35 | pull=('vp','ap','vc','ac','sigma','K')) |
|
36 | pull=('vp','ap','vc','ac','sigma','K')) | |
36 | taskids.append(tc.run(t)) |
|
37 | taskids.append(tc.run(t)) | |
37 |
|
38 | |||
38 | print "Submitted tasks: ", taskids |
|
39 | print "Submitted tasks: ", taskids | |
39 |
|
40 | |||
40 | # Block until tasks are completed |
|
41 | # Block until tasks are completed | |
41 | tc.barrier(taskids) |
|
42 | tc.barrier(taskids) | |
42 |
|
43 | |||
43 | # Get the results |
|
44 | # Get the results | |
44 | results = [tc.get_task_result(tid) for tid in taskids] |
|
45 | results = [tc.get_task_result(tid) for tid in taskids] | |
45 |
|
46 | |||
46 | # Assemble the result |
|
47 | # Assemble the result | |
47 | vc = N.empty(K_vals.shape[0]*sigma_vals.shape[0],dtype='float64') |
|
48 | vc = N.empty(K_vals.shape[0]*sigma_vals.shape[0],dtype='float64') | |
48 | vp = N.empty(K_vals.shape[0]*sigma_vals.shape[0],dtype='float64') |
|
49 | vp = N.empty(K_vals.shape[0]*sigma_vals.shape[0],dtype='float64') | |
49 | ac = N.empty(K_vals.shape[0]*sigma_vals.shape[0],dtype='float64') |
|
50 | ac = N.empty(K_vals.shape[0]*sigma_vals.shape[0],dtype='float64') | |
50 | ap = N.empty(K_vals.shape[0]*sigma_vals.shape[0],dtype='float64') |
|
51 | ap = N.empty(K_vals.shape[0]*sigma_vals.shape[0],dtype='float64') | |
51 | for i, tr in enumerate(results): |
|
52 | for i, tr in enumerate(results): | |
52 | ns = tr.ns |
|
53 | ns = tr.ns | |
53 | vc[i] = ns.vc |
|
54 | vc[i] = ns.vc | |
54 | vp[i] = ns.vp |
|
55 | vp[i] = ns.vp | |
55 | ac[i] = ns.ac |
|
56 | ac[i] = ns.ac | |
56 | ap[i] = ns.ap |
|
57 | ap[i] = ns.ap | |
57 | vc.shape = (K_vals.shape[0],sigma_vals.shape[0]) |
|
58 | vc.shape = (K_vals.shape[0],sigma_vals.shape[0]) | |
58 | vp.shape = (K_vals.shape[0],sigma_vals.shape[0]) |
|
59 | vp.shape = (K_vals.shape[0],sigma_vals.shape[0]) | |
59 | ac.shape = (K_vals.shape[0],sigma_vals.shape[0]) |
|
60 | ac.shape = (K_vals.shape[0],sigma_vals.shape[0]) | |
60 | ap.shape = (K_vals.shape[0],sigma_vals.shape[0]) |
|
61 | ap.shape = (K_vals.shape[0],sigma_vals.shape[0]) | |
61 |
|
62 | |||
62 |
|
63 | |||
63 | def plot_options(K_vals, sigma_vals, prices): |
|
64 | def plot_options(K_vals, sigma_vals, prices): | |
64 | """Make a contour plot of the option prices.""" |
|
65 | """Make a contour plot of the option prices.""" | |
65 | import pylab |
|
66 | import pylab | |
66 | pylab.contourf(sigma_vals, K_vals, prices) |
|
67 | pylab.contourf(sigma_vals, K_vals, prices) | |
67 | pylab.colorbar() |
|
68 | pylab.colorbar() | |
68 | pylab.title("Option Price") |
|
69 | pylab.title("Option Price") | |
69 | pylab.xlabel("Volatility") |
|
70 | pylab.xlabel("Volatility") | |
70 | pylab.ylabel("Strike Price") |
|
71 | pylab.ylabel("Strike Price") |
General Comments 0
You need to be logged in to leave comments.
Login now