1414
1515class MonteCarloPricing :
1616
17- def __init__ (self , option_type : OptionType , S0 , strike_price , r , sigma , T , time_steps , sim , seed = None ):
17+ def __init__ (self , option_type : OptionType , S0 , strike_price , mu , sigma , T , time_steps , sim , seed = None ):
1818 """
1919 Initialize the Geometric Brownian Motion model with the given parameters.
2020
@@ -30,7 +30,7 @@ def __init__(self, option_type: OptionType, S0, strike_price, r, sigma, T, time_
3030 self .__option_type = option_type
3131 self .__S0 = S0
3232 self .__strike_price = strike_price
33- self .__r = r
33+ self .__mu = mu
3434 self .__sigma = sigma
3535 self .__T = T
3636 self .__time_steps = time_steps
@@ -60,7 +60,7 @@ def get_monte_carlo_option_price(self):
6060 else :
6161 raise ValueError (f'Unsupported Option Type: { self .__option_type } ' )
6262
63- option_price = np .exp (- self .__r * self .__T ) * np .mean (self .__payoff )
63+ option_price = np .exp (- self .__mu * self .__T ) * np .mean (self .__payoff )
6464
6565 logging .info (f"Option price calculated successfully with { self .__class__ .__name__ } ." )
6666
@@ -95,7 +95,7 @@ def simulate_gbm(self):
9595 # updates brownian motion
9696 B [i ,j ] = B [i ,j - 1 ] + np .sqrt (dt ) * Z [i ,j - 1 ]
9797 # calculates stock price based on the incremental difference
98- S [i ,j ] = S [i , j - 1 ] * np .exp ((self .__r - 0.5 * self .__sigma ** 2 )* dt + self .__sigma * (B [i , j ] - B [i , j - 1 ]))
98+ S [i ,j ] = S [i , j - 1 ] * np .exp ((self .__mu - 0.5 * self .__sigma ** 2 )* dt + self .__sigma * (B [i , j ] - B [i , j - 1 ]))
9999
100100 end = time .perf_counter ()
101101 self .__duration = end - start
@@ -150,7 +150,7 @@ def __generate_grid(self):
150150
151151 return np .linspace (0 , self .__T , self .__time_steps )
152152
153- def plot_price_paths (self , export = False ):
153+ def plot_price_paths (self , closing_prices = None , export = False ):
154154 """
155155 Plot the simulated stock prices for all simulations.
156156 """
@@ -171,6 +171,12 @@ def plot_price_paths(self, export=False):
171171 plt .xlabel ("Time (Years)" )
172172 plt .ylabel ("Stock Price" )
173173
174+ if closing_prices is not None :
175+ if len (closing_prices ) != len (t ):
176+ raise ValueError ("Length of closing prices does not match the number of time steps in the simulation." )
177+
178+ plt .plot (t , closing_prices , color = 'red' )
179+
174180 if export :
175181 plt .savefig ("monte_carlo_prices.pdf" , format = "pdf" , bbox_inches = "tight" )
176182
@@ -199,7 +205,6 @@ def plot_distribution_of_final_prices(self, export=False):
199205
200206 if export :
201207 plt .savefig ("monte_carlo_prices.pdf" , format = "pdf" , bbox_inches = "tight" , pad_inches = 0.2 )
202-
203208 plt .show ()
204209
205210 def plot_distribution_of_payoff (self , export = False ):
0 commit comments