33from unittest .mock import patch
44
55from pdesolvers .optionspricing .monte_carlo import MonteCarloPricing
6+ from pdesolvers .optionspricing .black_scholes_formula import BlackScholesFormula
67from pdesolvers .enums .enums import OptionType , Greeks
78
89@pytest .fixture
@@ -17,6 +18,16 @@ def mc_pricing_params():
1718 'sim' : 5
1819 }
1920
21+ @pytest .fixture
22+ def bs_pricing_params ():
23+ return {
24+ 'S0' : 100.0 ,
25+ 'strike_price' : 100.0 ,
26+ 'r' : 0.05 ,
27+ 'sigma' : 0.2 ,
28+ 'expiry' : 1.0
29+ }
30+
2031class TestMonteCarlo :
2132
2233 @patch ('pdesolvers.optionspricing.monte_carlo.MonteCarloPricing.simulate_gbm' )
@@ -83,11 +94,51 @@ def test_check_simulate_gbm_initial_values(self, mc_pricing_params):
8394
8495 assert (results [:,0 ] == mc_pricing_params ['S0' ]).all ()
8596
86- def test_check_get_execution_time_raises_exception_when_no_simulation_is_run (self , mc_pricing_params ):
97+ def test_check_get_execution_time_raises_error_when_no_simulation_is_run (self , mc_pricing_params ):
8798
8899 test_mc = MonteCarloPricing (
89100 OptionType .EUROPEAN_CALL , ** mc_pricing_params
90101 )
91102
92103 with pytest .raises (RuntimeError , match = "Execution time is not available because the simulation has not been run yet." ):
93104 test_mc .get_execution_time ()
105+
106+ def test_check_plot_payoff_raises_error_when_no_simulation_is_run (self , mc_pricing_params ):
107+
108+ test_mc = MonteCarloPricing (
109+ OptionType .EUROPEAN_CALL , ** mc_pricing_params
110+ )
111+
112+ with pytest .raises (RuntimeError , match = "Plots cannot be generated because the simulation has not been run yet." ):
113+ test_mc .plot_distribution_of_payoff ()
114+
115+ def test_check_plot_price_distribution_raises_error_when_no_simulation_is_run (self , mc_pricing_params ):
116+
117+ test_mc = MonteCarloPricing (
118+ OptionType .EUROPEAN_CALL , ** mc_pricing_params
119+ )
120+
121+ with pytest .raises (RuntimeError , match = "Plots cannot be generated because the simulation has not been run yet." ):
122+ test_mc .plot_distribution_of_final_prices ()
123+
124+ def test_check_plot_price_paths_raises_error_when_no_simulation_is_run (self , mc_pricing_params ):
125+
126+ test_mc = MonteCarloPricing (
127+ OptionType .EUROPEAN_CALL , ** mc_pricing_params
128+ )
129+
130+ with pytest .raises (RuntimeError , match = "Plots cannot be generated because the simulation has not been run yet." ):
131+ test_mc .plot_price_paths ()
132+
133+ def test_check_plot_convergence_raises_error_when_no_list_supplied (self , mc_pricing_params , bs_pricing_params ):
134+
135+ test_mc = MonteCarloPricing (
136+ OptionType .EUROPEAN_CALL , ** mc_pricing_params
137+ )
138+
139+ results = test_mc .simulate_gbm ()
140+
141+ price = BlackScholesFormula (OptionType .EUROPEAN_CALL , ** bs_pricing_params )
142+
143+ with pytest .raises (ValueError , match = "Number of simulations need to be defined." ):
144+ test_mc .plot_convergence_analysis (price )
0 commit comments