66
77class Solution :
88
9- def __init__ (self , result , x_grid , y_grid , dx , dy ):
9+ def __init__ (self , result , x_grid , y_grid , dx , dy , duration ):
1010 self .result = result
1111 self .x_grid = x_grid
1212 self .y_grid = y_grid
1313 self .dx = dx
1414 self .dy = dy
15+ self .duration = duration
1516
1617 def plot (self ):
1718 """
@@ -46,6 +47,9 @@ def _set_plot_labels(self, ax):
4647 ax .set_zlabel ('Value' )
4748 ax .set_title ('3D Surface Plot' )
4849
50+ def get_execution_time (self ):
51+ return self .duration
52+
4953 def __sub__ (self , other ):
5054 """
5155 Compares two solutions by interpolating the sparse grid to the dense grid and computing the difference
@@ -83,8 +87,8 @@ def __sub__(self, other):
8387
8488class Solution1D (Solution ):
8589
86- def __init__ (self , result , x_grid , y_grid , dx , dy ):
87- super ().__init__ (result , x_grid , y_grid , dx , dy )
90+ def __init__ (self , result , x_grid , y_grid , dx , dy , duration ):
91+ super ().__init__ (result , x_grid , y_grid , dx , dy , duration )
8892
8993 def _set_plot_labels (self , ax ):
9094 ax .set_xlabel ('Space' )
@@ -94,8 +98,8 @@ def _set_plot_labels(self, ax):
9498
9599
96100class SolutionBlackScholes (Solution ):
97- def __init__ (self , result , x_grid , y_grid , dx , dy , delta , gamma , theta , option_type ):
98- super ().__init__ (result , x_grid , y_grid , dx , dy )
101+ def __init__ (self , result , x_grid , y_grid , dx , dy , duration , delta , gamma , theta , option_type ):
102+ super ().__init__ (result , x_grid , y_grid , dx , dy , duration )
99103 self .option_type = option_type
100104 self .delta = delta
101105 self .gamma = gamma
@@ -105,8 +109,8 @@ def plot_greek(self, greek_type=enum.Greeks.DELTA, time_step=0):
105109
106110 greek_types = {
107111 enum .Greeks .DELTA : {'data' : self .delta , 'title' : enum .Greeks .DELTA .value },
108- enum .Greeks .GAMMA : {'data' : self .gamma , 'title' : enum .Greeks .DELTA .value },
109- enum .Greeks .THETA : {'data' : self .theta , 'title' : enum .Greeks .DELTA .value }
112+ enum .Greeks .GAMMA : {'data' : self .gamma , 'title' : enum .Greeks .GAMMA .value },
113+ enum .Greeks .THETA : {'data' : self .theta , 'title' : enum .Greeks .THETA .value }
110114 }
111115
112116 # if greek_type.lower() not in greek_types:
@@ -115,14 +119,15 @@ def plot_greek(self, greek_type=enum.Greeks.DELTA, time_step=0):
115119 chosen_greek = greek_types [greek_type ]
116120 greek_data = chosen_greek ['data' ][:, time_step ]
117121 plt .figure (figsize = (8 , 6 ))
118- plt .plot (self .y_grid , greek_data , label = f"Delta at t={ self .x_grid [time_step ]:.4f} " , color = "blue" )
122+ plt .plot (self .y_grid , greek_data , label = f"{ chosen_greek [ 'title' ] } at t={ self .x_grid [time_step ]:.4f} " , color = "grey" , alpha = 0.8 )
119123
120124 plt .title (f"{ chosen_greek ['title' ]} vs. Stock Price at t={ self .x_grid [time_step ]:.4f} " )
121125 plt .xlabel ("Stock Price (S)" )
122126 plt .ylabel (chosen_greek ['title' ])
123127 plt .grid ()
124128 plt .legend ()
125129
130+ np .savetxt (f"option_{ chosen_greek ['title' ]} .csv" , np .column_stack ((self .y_grid , greek_data )), delimiter = ',' , header = f"StockPrice,{ chosen_greek ['title' ]} " , comments = '' )
126131 plt .show ()
127132
128133 def _set_plot_labels (self , ax ):
0 commit comments