diff --git a/examples/basque.ipynb b/examples/basque.ipynb index 251da4d..6ff2d56 100644 --- a/examples/basque.ipynb +++ b/examples/basque.ipynb @@ -651,7 +651,7 @@ " time_periods=[1976, 1977],\n", " pre_periods=list(range(1955, 1975)),\n", " tol=0.01,\n", - " verbose=False\n", + " verbose=False,\n", ")" ] } diff --git a/examples/factor-model.ipynb b/examples/factor-model.ipynb index ee6bb5a..9475e58 100644 --- a/examples/factor-model.ipynb +++ b/examples/factor-model.ipynb @@ -348,7 +348,7 @@ " X0=X0,\n", " X1=X1,\n", " Z0=Z0,\n", - " Z1=Z1\n", + " Z1=Z1,\n", ")" ] } diff --git a/examples/germany.ipynb b/examples/germany.ipynb index 726d3d4..4ac04b3 100644 --- a/examples/germany.ipynb +++ b/examples/germany.ipynb @@ -551,7 +551,7 @@ " time_periods=[1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000],\n", " custom_V=synth_train.V,\n", " tol=0.01,\n", - " verbose=False\n", + " verbose=False,\n", ")" ] } diff --git a/pysyncon/inference.py b/pysyncon/inference.py index c5548d8..d6b2ebc 100644 --- a/pysyncon/inference.py +++ b/pysyncon/inference.py @@ -143,7 +143,7 @@ def confidence_intervals( if step_sz <= tol: # Failed to guess a sensible step-size :( step_sz = 1.1 * tol - + conf_interval = dict() n_periods = len(post_periods) for idx, post_period in enumerate(post_periods, 1): diff --git a/tests/test_conformal_interence.py b/tests/test_conformal_interence.py index 665f168..7cebd83 100644 --- a/tests/test_conformal_interence.py +++ b/tests/test_conformal_interence.py @@ -218,27 +218,6 @@ def test_no_weights(self): conformal_inf = ConformalInference() self.assertRaises(ValueError, conformal_inf.confidence_intervals, **kwargs) - def test_step_sz_options(self): - self.scm.fit(X0=self.X0, X1=self.X1, Z0=self.Z0, Z1=self.Z1) - - kwargs = { - "alpha": self.alpha, - "scm": self.scm, - "Z0": self.Z0, - "Z1": self.Z1, - "pre_periods": self.pre_periods, - "tol": self.tol, - "max_iter": self.max_iter, - "step_sz": self.step_sz, - "verbose": self.verbose, - } - - conformal_inf = ConformalInference() - conformal_inf.confidence_intervals(post_periods=self.post_periods, **kwargs) - conformal_inf.confidence_intervals( - post_periods=[self.post_periods[0]], **kwargs - ) - def test_root_search(self): cases_roots_x0 = [ ((-1, 3), 0.5), diff --git a/tests/test_synth.py b/tests/test_synth.py index 642054f..28885ec 100644 --- a/tests/test_synth.py +++ b/tests/test_synth.py @@ -302,12 +302,13 @@ def test_confidence_intervals(self): synth.confidence_interval, alpha=0.5, time_periods=[4], + tol=0.01, method="foo", ) # With dataprep supplied try: - synth.confidence_interval(alpha=0.5, time_periods=[4], dataprep=dataprep) + synth.confidence_interval(alpha=0.5, time_periods=[4], dataprep=dataprep, tol=0.01) except Exception as e: self.fail(f"Confidence interval failed: {e}.") @@ -317,24 +318,25 @@ def test_confidence_intervals(self): synth.confidence_interval, alpha=0.05, time_periods=[4], + tol=0.01, dataprep=dataprep, ) # Without dataprep supplied try: - synth.confidence_interval(alpha=0.5, time_periods=[4]) + synth.confidence_interval(alpha=0.5, time_periods=[4], tol=0.01) except Exception as e: self.fail(f"Confidence interval failed: {e}.") # Too few time periods for alpha value self.assertRaises( - ValueError, synth.confidence_interval, alpha=0.05, time_periods=[4] + ValueError, synth.confidence_interval, alpha=0.05, time_periods=[4], tol=0.01 ) # Without dataprep supplied or matrices synth.dataprep = None self.assertRaises( - ValueError, synth.confidence_interval, alpha=0.5, time_periods=[4] + ValueError, synth.confidence_interval, alpha=0.5, time_periods=[4], tol=0.01 ) # No pre-periods supplied @@ -346,6 +348,7 @@ def test_confidence_intervals(self): synth.confidence_interval, alpha=0.5, time_periods=[4], + tol=0.01, X0=X0, X1=X1, Z0=Z0, @@ -359,35 +362,13 @@ def test_confidence_intervals(self): alpha=0.05, time_periods=[4], pre_periods=[1, 2, 3], + tol=0.01, X0=X0, X1=X1, Z0=Z0, Z1=Z1, ) - # Add fit options - _, n_c = X0.shape - custom_V = np.full(n_c, 1 / n_c) - optim_method = "BFGS" - optim_initial = "ols" - optim_options = {"max_iter": 1000} - try: - synth.confidence_interval( - alpha=0.5, - time_periods=[4], - pre_periods=[1, 2, 3], - X0=X0, - X1=X1, - Z0=Z0, - Z1=Z1, - custom_V=custom_V, - optim_method=optim_method, - optim_initial=optim_initial, - optim_options=optim_options, - ) - except Exception as e: - self.fail(f"Confidence interval failed: {e}.") - # Dataframes supplied instead of series X1 = X1.to_frame() Z1 = Z1.to_frame() @@ -397,6 +378,7 @@ def test_confidence_intervals(self): alpha=0.5, time_periods=[4], pre_periods=[1, 2, 3], + tol=0.01, X0=X0, X1=X1, Z0=Z0, diff --git a/tests/test_synth_germany.py b/tests/test_synth_germany.py index cdeb097..dfa96a5 100644 --- a/tests/test_synth_germany.py +++ b/tests/test_synth_germany.py @@ -157,6 +157,7 @@ def setUp(self): 2000, ], "max_iter": 50, + "tol": 0.1, "verbose": False, } @@ -199,9 +200,12 @@ def test_cis(self): optim_initial=self.optim_initial, custom_V=self.custom_V, ) - + cis = pd.DataFrame.from_dict(self.cis) cis.index.name = "time" pd.testing.assert_frame_equal( - cis, synth.confidence_interval(custom_V=self.custom_V, **self.ci_args), check_exact=False, atol=0.025 + cis, + synth.confidence_interval(custom_V=self.custom_V, **self.ci_args), + check_exact=False, + atol=0.025, )