Skip to content

Commit f340895

Browse files
committed
FEATURE: refactoring monte carlo
1 parent ee8512d commit f340895

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

pdesolvers/optionspricing/market_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
class HistoricalStockData:
55
def __init__(self, ticker):
66
self.__stock_data = None
7-
self.__options = None
87
self.__ticker = ticker
98

109
def fetch_stock_data(self, start_date, end_date):

pdesolvers/optionspricing/monte_carlo.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ def __init__(self, option_type: OptionType, S0, strike_price, r, sigma, T, time_
2828
self.__time_steps = time_steps
2929
self.__sim = sim
3030
self.__S = None
31+
self.__run = False
3132
self.__duration = 0.0
3233

3334
def get_monte_carlo_option_price(self):
3435

3536
S = self.simulate_gbm()
37+
self.__run = True
3638

3739
if self.__option_type == OptionType.EUROPEAN_CALL:
3840
payoff = np.maximum(S[:, -1] - self.__strike_price, 0)
@@ -72,7 +74,7 @@ def simulate_gbm(self):
7274
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]))
7375

7476
end = time.perf_counter()
75-
self.duration = end - start
77+
self.__duration = end - start
7678

7779
self.__S = S
7880
return self.__S
@@ -104,4 +106,7 @@ def plot(self):
104106
plt.show()
105107

106108
def get_execution_time(self):
109+
if not self.__run:
110+
raise RuntimeError("Execution time is not available because the simulation has not been run yet.")
111+
107112
return self.__duration

pdesolvers/tests/test_options_pricing.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ def test_check_simulate_gbm_initial_values(self, mc_pricing_params):
8181

8282
results = test_mc.simulate_gbm()
8383

84-
assert (results[:,0] == mc_pricing_params['S0']).all()
84+
assert (results[:,0] == mc_pricing_params['S0']).all()
85+
86+
def test_check_get_execution_time_raises_exception_when_no_simulation_is_run(self, mc_pricing_params):
87+
88+
test_mc = MonteCarloPricing(
89+
OptionType.EUROPEAN_CALL, **mc_pricing_params
90+
)
91+
92+
with pytest.raises(RuntimeError, match="Execution time is not available because the simulation has not been run yet."):
93+
test_mc.get_execution_time()

0 commit comments

Comments
 (0)