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