Show More
@@ -1,42 +1,43 b'' | |||||
1 | import numpy as N |
|
1 | import numpy as N | |
2 | from math import * |
|
2 | from math import * | |
3 |
|
3 | |||
4 | class MCOptionPricer(object): |
|
4 | class MCOptionPricer(object): | |
5 | def __init__(self, S=100.0, K=100.0, sigma=0.25, r=0.05, days=260, paths=10000): |
|
5 | def __init__(self, S=100.0, K=100.0, sigma=0.25, r=0.05, days=260, paths=10000): | |
6 | self.S = S |
|
6 | self.S = S | |
7 | self.K = K |
|
7 | self.K = K | |
8 | self.sigma = sigma |
|
8 | self.sigma = sigma | |
9 | self.r = r |
|
9 | self.r = r | |
10 | self.days = days |
|
10 | self.days = days | |
11 | self.paths = paths |
|
11 | self.paths = paths | |
12 | self.h = 1.0/self.days |
|
12 | self.h = 1.0/self.days | |
13 | self.const1 = exp((self.r-0.5*self.sigma**2)*self.h) |
|
13 | self.const1 = exp((self.r-0.5*self.sigma**2)*self.h) | |
14 | self.const2 = self.sigma*sqrt(self.h) |
|
14 | self.const2 = self.sigma*sqrt(self.h) | |
15 |
|
15 | |||
16 | def run(self): |
|
16 | def run(self): | |
17 | stock_price = self.S*N.ones(self.paths, dtype='float64') |
|
17 | stock_price = self.S*N.ones(self.paths, dtype='float64') | |
18 | stock_price_sum = N.zeros(self.paths, dtype='float64') |
|
18 | stock_price_sum = N.zeros(self.paths, dtype='float64') | |
19 | for j in range(self.days): |
|
19 | for j in range(self.days): | |
20 | growth_factor = self.const1*N.exp(self.const2*N.random.standard_normal(self.paths)) |
|
20 | growth_factor = self.const1*N.exp(self.const2*N.random.standard_normal(self.paths)) | |
21 | stock_price = stock_price*growth_factor |
|
21 | stock_price = stock_price*growth_factor | |
22 | stock_price_sum = stock_price_sum + stock_price |
|
22 | stock_price_sum = stock_price_sum + stock_price | |
23 | stock_price_avg = stock_price_sum/self.days |
|
23 | stock_price_avg = stock_price_sum/self.days | |
24 | zeros = N.zeros(self.paths, dtype='float64') |
|
24 | zeros = N.zeros(self.paths, dtype='float64') | |
25 | r_factor = exp(-self.r*self.h*self.days) |
|
25 | r_factor = exp(-self.r*self.h*self.days) | |
26 | self.vanilla_put = r_factor*N.mean(N.maximum(zeros,self.K-stock_price)) |
|
26 | self.vanilla_put = r_factor*N.mean(N.maximum(zeros,self.K-stock_price)) | |
27 | self.asian_put = r_factor*N.mean(N.maximum(zeros,self.K-stock_price_avg)) |
|
27 | self.asian_put = r_factor*N.mean(N.maximum(zeros,self.K-stock_price_avg)) | |
28 | self.vanilla_call = r_factor*N.mean(N.maximum(zeros,stock_price-self.K)) |
|
28 | self.vanilla_call = r_factor*N.mean(N.maximum(zeros,stock_price-self.K)) | |
29 | self.asian_call = r_factor*N.mean(N.maximum(zeros,stock_price_avg-self.K)) |
|
29 | self.asian_call = r_factor*N.mean(N.maximum(zeros,stock_price_avg-self.K)) | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | def main(): |
|
32 | def main(): | |
33 | op = MCOptionPricer() |
|
33 | op = MCOptionPricer() | |
34 | op.run() |
|
34 | op.run() | |
35 | print "Vanilla Put Price = ", op.vanilla_put |
|
35 | print "Vanilla Put Price = ", op.vanilla_put | |
36 | print "Asian Put Price = ", op.asian_put |
|
36 | print "Asian Put Price = ", op.asian_put | |
37 | print "Vanilla Call Price = ", op.vanilla_call |
|
37 | print "Vanilla Call Price = ", op.vanilla_call | |
38 | print "Asian Call Price = ", op.asian_call |
|
38 | print "Asian Call Price = ", op.asian_call | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | if __name__ == '__main__': |
|
41 | if __name__ == '__main__': | |
42 | main() |
|
42 | main() | |
|
43 |
@@ -1,57 +1,57 b'' | |||||
1 | #------------------------------------------------------------------------------- |
|
1 | #------------------------------------------------------------------------------- | |
2 | # Driver code that the client runs. |
|
2 | # Driver code that the client runs. | |
3 | #------------------------------------------------------------------------------- |
|
3 | #------------------------------------------------------------------------------- | |
4 | # To run this code start a controller and engines using: |
|
4 | # To run this code start a controller and engines using: | |
5 | # ipcluster -n 2 |
|
5 | # ipcluster -n 2 | |
6 | # Then run the scripts by doing irunner rmt.ipy or by starting ipython and |
|
6 | # Then run the scripts by doing irunner rmt.ipy or by starting ipython and | |
7 | # doing run rmt.ipy. |
|
7 | # doing run rmt.ipy. | |
8 |
|
8 | |||
9 | from rmtkernel import * |
|
9 | from rmtkernel import * | |
10 | from IPython.kernel import client |
|
10 | from IPython.kernel import client | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | def wignerDistribution(s): |
|
13 | def wignerDistribution(s): | |
14 | """Returns (s, rho(s)) for the Wigner GOE distribution.""" |
|
14 | """Returns (s, rho(s)) for the Wigner GOE distribution.""" | |
15 | return (numpy.pi*s/2.0) * numpy.exp(-numpy.pi*s**2/4.) |
|
15 | return (numpy.pi*s/2.0) * numpy.exp(-numpy.pi*s**2/4.) | |
16 |
|
16 | |||
17 |
|
17 | |||
18 | def generateWignerData(): |
|
18 | def generateWignerData(): | |
19 | s = numpy.linspace(0.0,4.0,400) |
|
19 | s = numpy.linspace(0.0,4.0,400) | |
20 | rhos = wignerDistribution(s) |
|
20 | rhos = wignerDistribution(s) | |
21 | return s, rhos |
|
21 | return s, rhos | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | def serialDiffs(num, N): |
|
24 | def serialDiffs(num, N): | |
25 | diffs = ensembleDiffs(num, N) |
|
25 | diffs = ensembleDiffs(num, N) | |
26 | normalizedDiffs = normalizeDiffs(diffs) |
|
26 | normalizedDiffs = normalizeDiffs(diffs) | |
27 | return normalizedDiffs |
|
27 | return normalizedDiffs | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | def parallelDiffs(rc, num, N): |
|
30 | def parallelDiffs(rc, num, N): | |
31 | nengines = len(rc.get_ids()) |
|
31 | nengines = len(rc.get_ids()) | |
32 | num_per_engine = num/nengines |
|
32 | num_per_engine = num/nengines | |
33 | print "Running with", num_per_engine, "per engine." |
|
33 | print "Running with", num_per_engine, "per engine." | |
34 | rc.push(dict(num_per_engine=num_per_engine, N=N)) |
|
34 | rc.push(dict(num_per_engine=num_per_engine, N=N)) | |
35 | rc.execute('diffs = ensembleDiffs(num_per_engine, N)') |
|
35 | rc.execute('diffs = ensembleDiffs(num_per_engine, N)') | |
36 | # gather blocks always for now |
|
36 | # gather blocks always for now | |
37 | pr = rc.gather('diffs') |
|
37 | pr = rc.gather('diffs') | |
38 | return pr.r |
|
38 | return pr.r | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | # Main code |
|
41 | # Main code | |
42 | if __name__ == '__main__': |
|
42 | if __name__ == '__main__': | |
43 | rc = client.MultiEngineClient() |
|
43 | rc = client.MultiEngineClient() | |
44 | print "Distributing code to engines..." |
|
44 | print "Distributing code to engines..." | |
45 | r = rc.run('rmtkernel.py') |
|
45 | r = rc.run('rmtkernel.py') | |
46 | rc.block = False |
|
46 | rc.block = False | |
47 |
|
47 | |||
48 | # Simulation parameters |
|
48 | # Simulation parameters | |
49 | nmats = 100 |
|
49 | nmats = 100 | |
50 | matsize = 30 |
|
50 | matsize = 30 | |
51 |
|
51 | |||
52 | %timeit -n1 -r1 serialDiffs(nmats,matsize) |
|
52 | %timeit -n1 -r1 serialDiffs(nmats,matsize) | |
53 | %timeit -n1 -r1 parallelDiffs(rc, nmats, matsize) |
|
53 | %timeit -n1 -r1 parallelDiffs(rc, nmats, matsize) | |
54 |
|
54 | |||
55 | # Uncomment these to plot the histogram |
|
55 | # Uncomment these to plot the histogram | |
56 | import pylab |
|
56 | # import pylab | |
57 | pylab.hist(parallelDiffs(rc,matsize,matsize)) |
|
57 | # pylab.hist(parallelDiffs(rc,matsize,matsize)) |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now