From 9beef4885ccb496f2613d61fdd99603caefdb88b Mon Sep 17 00:00:00 2001 From: Diane Napolitano Date: Thu, 28 Mar 2024 11:28:04 -0400 Subject: [PATCH] FIXED updated unit tests; integer data is no longer a requirement for transition solvers :tada: --- tests/test_transition_matrix_solver.py | 175 +++++++++++++++---------- 1 file changed, 108 insertions(+), 67 deletions(-) diff --git a/tests/test_transition_matrix_solver.py b/tests/test_transition_matrix_solver.py index 7ab73e3..5796c88 100644 --- a/tests/test_transition_matrix_solver.py +++ b/tests/test_transition_matrix_solver.py @@ -7,7 +7,7 @@ ATOL = 1e-04 -def test_matrix_fit_predict(): +def test_matrix_fit_predict_with_integers(): X = np.array( [ [1, 2], @@ -30,15 +30,56 @@ def test_matrix_fit_predict(): ] ) + expected_betas = np.array([[9.99831808e-01, 1.68191521e-04], [1.49085896e-04, 9.99850914e-01]]) + expected_yhat = np.array( + [ + [1.00012998, 1.99987002], + [3.00009177, 3.99990823], + [5.00005356, 5.99994644], + [7.00001535, 7.99998465], + [8.99997714, 10.00002286], + [10.99993892, 12.00006108], + ] + ) + + tms = TransitionMatrixSolver().fit(X, Y) + current_yhat = tms.predict(X) + np.testing.assert_allclose(expected_betas, tms.coefficients, rtol=RTOL, atol=ATOL) + np.testing.assert_allclose(expected_yhat, current_yhat, rtol=RTOL, atol=ATOL) + + +def test_matrix_fit_predict(): + X = np.array( + [ + [0.33333333, 0.66666667], + [0.42857143, 0.57142857], + [0.45454545, 0.54545455], + [0.46666667, 0.53333333], + [0.47368421, 0.52631579], + [0.47826087, 0.52173913], + ] + ) + + Y = np.array( + [ + [0.4, 0.6], + [0.44444444, 0.55555556], + [0.46153846, 0.53846154], + [0.47058824, 0.52941176], + [0.47619048, 0.52380952], + [0.48, 0.52], + ] + ) + expected_betas = np.array([[0.760428, 0.239572], [0.216642, 0.783358]]) expected_yhat = np.array( [ - [1.19371187, 1.80628813], - [3.14785177, 3.85214823], - [5.10199167, 5.89800833], - [7.05613156, 7.94386844], - [9.01027146, 9.98972854], - [10.96441136, 12.03558864], + [0.39790396, 0.60209604], + [0.44969311, 0.55030689], + [0.46381742, 0.53618258], + [0.47040877, 0.52959123], + [0.47422481, 0.52577519], + [0.47671354, 0.52328646], ] ) @@ -51,23 +92,23 @@ def test_matrix_fit_predict(): def test_matrix_fit_predict_with_weights(): X = np.array( [ - [1, 2], - [3, 4], - [5, 6], - [7, 8], - [9, 10], - [11, 12], + [0.33333333, 0.66666667], + [0.42857143, 0.57142857], + [0.45454545, 0.54545455], + [0.46666667, 0.53333333], + [0.47368421, 0.52631579], + [0.47826087, 0.52173913], ] ) Y = np.array( [ - [2, 3], - [4, 5], - [6, 7], - [8, 9], - [10, 11], - [12, 13], + [0.4, 0.6], + [0.44444444, 0.55555556], + [0.46153846, 0.53846154], + [0.47058824, 0.52941176], + [0.47619048, 0.52380952], + [0.48, 0.52], ] ) @@ -82,23 +123,23 @@ def test_matrix_fit_predict_with_weights(): def test_matrix_fit_predict_not_strict(): X = np.array( [ - [1, 2], - [3, 4], - [5, 6], - [7, 8], - [9, 10], - [11, 12], + [0.33333333, 0.66666667], + [0.42857143, 0.57142857], + [0.45454545, 0.54545455], + [0.46666667, 0.53333333], + [0.47368421, 0.52631579], + [0.47826087, 0.52173913], ] ) Y = np.array( [ - [2, 3], - [4, 5], - [6, 7], - [8, 9], - [10, 11], - [12, 13], + [0.4, 0.6], + [0.44444444, 0.55555556], + [0.46153846, 0.53846154], + [0.47058824, 0.52941176], + [0.47619048, 0.52380952], + [0.48, 0.52], ] ) @@ -111,23 +152,23 @@ def test_matrix_fit_predict_not_strict(): def test_ridge_matrix_fit_predict(): X = np.array( [ - [1, 2], - [3, 4], - [5, 6], - [7, 8], - [9, 10], - [11, 12], + [0.33333333, 0.66666667], + [0.42857143, 0.57142857], + [0.45454545, 0.54545455], + [0.46666667, 0.53333333], + [0.47368421, 0.52631579], + [0.47826087, 0.52173913], ] ) Y = np.array( [ - [2, 3], - [4, 5], - [6, 7], - [8, 9], - [10, 11], - [12, 13], + [0.4, 0.6], + [0.44444444, 0.55555556], + [0.46153846, 0.53846154], + [0.47058824, 0.52941176], + [0.47619048, 0.52380952], + [0.48, 0.52], ] ) @@ -162,12 +203,12 @@ def test_matrix_fit_predict_pivoted(): expected_betas = np.array( [ - [0.68274443, 0.18437159, 0.06760119, 0.03363495, 0.0197597, 0.01188814], - [0.13541428, 0.48122828, 0.22128163, 0.0960816, 0.04540571, 0.02058852], - [0.04545795, 0.16052607, 0.38881747, 0.27665629, 0.12758087, 0.00096135], - [0.02289342, 0.06401812, 0.17970185, 0.28708764, 0.28820718, 0.15809178], - [0.01424566, 0.03468587, 0.08136858, 0.21299756, 0.26935036, 0.38735196], - [0.00995853, 0.02159863, 0.04337214, 0.1113991, 0.30326763, 0.51040397], + [9.99706e-01, 1.85000e-04, 5.00000e-05, 2.80000e-05, 1.90000e-05, 1.30000e-05], + [4.80000e-05, 9.99464e-01, 3.43000e-04, 8.10000e-05, 4.00000e-05, 2.40000e-05], + [1.70000e-05, 1.56000e-04, 9.99188e-01, 4.86000e-04, 1.06000e-04, 4.70000e-05], + [1.00000e-05, 4.60000e-05, 2.76000e-04, 9.98960e-01, 5.93000e-04, 1.14000e-04], + [7.00000e-06, 2.40000e-05, 7.40000e-05, 3.88000e-04, 9.98887e-01, 6.20000e-04], + [5.00000e-06, 1.50000e-05, 3.60000e-05, 9.70000e-05, 4.66000e-04, 9.99382e-01], ] ) @@ -178,12 +219,12 @@ def test_matrix_fit_predict_pivoted(): def test_matrix_fit_predict_bad_dimensions(): X = np.array( [ - [1, 2], - [3, 4], - [5, 6], - [7, 8], - [9, 10], - [11, 12], + [0.33333333, 0.66666667], + [0.42857143, 0.57142857], + [0.45454545, 0.54545455], + [0.46666667, 0.53333333], + [0.47368421, 0.52631579], + [0.47826087, 0.52173913], ] ) @@ -207,24 +248,24 @@ def test_matrix_fit_predict_pandas(): X = pandas.DataFrame( [ - [1, 2], - [3, 4], - [5, 6], - [7, 8], - [9, 10], - [11, 12], + [0.33333333, 0.66666667], + [0.42857143, 0.57142857], + [0.45454545, 0.54545455], + [0.46666667, 0.53333333], + [0.47368421, 0.52631579], + [0.47826087, 0.52173913], ], columns=["x1", "x2"], ) Y = pandas.DataFrame( [ - [2, 3], - [4, 5], - [6, 7], - [8, 9], - [10, 11], - [12, 13], + [0.4, 0.6], + [0.44444444, 0.55555556], + [0.46153846, 0.53846154], + [0.47058824, 0.52941176], + [0.47619048, 0.52380952], + [0.48, 0.52], ], columns=["y1", "y2"], )