Skip to content

Commit b7998bd

Browse files
committed
FEATURE: adding execution time for monte carlo
1 parent 06a0c4a commit b7998bd

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

pdesolvers/optionspricing/market_data.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ def estimate_metrics(self):
2525
sigma = self.__stock_data["Log Returns"].std()
2626
mu = self.__stock_data["Log Returns"].mean()
2727

28-
# annualized_sigma = self.__stock_data["Log Returns"].std() * np.sqrt(252)
29-
# annualized_mu = self.__stock_data["Log Returns"].mean() * 252
30-
3128
return sigma, mu
3229

3330
def get_latest_stock_price(self):

pdesolvers/optionspricing/monte_carlo.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
import matplotlib.pyplot as plt
3+
import time
34

45
from pdesolvers.enums.enums import OptionType
56

@@ -27,6 +28,7 @@ def __init__(self, option_type: OptionType, S0, strike_price, r, sigma, T, time_
2728
self.__time_steps = time_steps
2829
self.__sim = sim
2930
self.__S = None
31+
self.duration = 0.0
3032

3133
def get_monte_carlo_option_price(self):
3234

@@ -50,6 +52,8 @@ def simulate_gbm(self):
5052
This method calculates the stock prices at each time step for each simulation.
5153
"""
5254

55+
start = time.perf_counter()
56+
5357
t = self.__generate_grid()
5458
dt = t[1] - t[0]
5559

@@ -67,8 +71,10 @@ def simulate_gbm(self):
6771
# calculates stock price based on the incremental difference
6872
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]))
6973

70-
self.__S = S
74+
end = time.perf_counter()
75+
self.duration = end - start
7176

77+
self.__S = S
7278
return self.__S
7379

7480
def __generate_grid(self):
@@ -95,4 +101,7 @@ def plot(self):
95101
plt.title("Simulated Geometric Brownian Motion")
96102
plt.xlabel("Time (Years)")
97103
plt.ylabel("Stock Price")
98-
plt.show()
104+
plt.show()
105+
106+
def get_execution_time(self):
107+
return self.duration

0 commit comments

Comments
 (0)