def test_fit_pca(fn): dates = generate_random_dates(4) assets = get_assets(3) fn_inputs = { 'returns': pd.DataFrame([[0.02769242, 1.34872387, 0.23460972], [-0.94728692, 0.68386883, -1.23987235], [1.93769376, -0.48275934, 0.34957348], [0.23985234, 0.35897345, 0.34598734]], dates, assets), 'num_factor_exposures': 2, 'svd_solver': 'full' } fn_correct_values = { 'PCA': PCA(), 'PCA.components_': np.array([[0.81925896, -0.40427891, 0.40666118], [-0.02011128, 0.68848693, 0.72496985]]) } pca_fit = PCA.fit with patch.object(PCA, 'fit', autospec=True) as mock_fit: mock_fit.side_effect = pca_fit fn_return_value = fn(**fn_inputs) assert_structure(fn_return_value, fn_correct_values['PCA'], 'PCA') try: # print(dir(fn_return_value.fit)) fn_return_value.fit.assert_called() # old python: # fn_return_value.fit.assert_any_call() except AssertionError: raise Exception('Test Failure: PCA.fit not called') try: fn_return_value.fit.assert_called_with(self=fn_return_value, X=fn_inputs['returns']) except Exception: raise Exception( 'Test Failure: PCA.fit called with the wrong arguments') assert_structure(fn_return_value.components_, fn_correct_values['PCA.components_'], 'PCA.components_') if not does_data_match(fn_return_value.components_, fn_correct_values['PCA.components_']): raise Exception('Test Failure: PCA not fitted correctly\n\n' 'PCA.components_:\n' '{}\n\n' 'Expected PCA.components_:\n' '{}'.format(fn_return_value.components_, fn_correct_values['PCA.components_']))
def test_fit_pca(fn): dates = generate_random_dates(4) assets = get_assets(3) fn_inputs = { 'returns': pd.DataFrame( [ [0.02769242, 1.34872387, 0.23460972], [-0.94728692, 0.68386883, -1.23987235], [1.93769376, -0.48275934, 0.34957348], [0.23985234, 0.35897345, 0.34598734]], dates, assets), 'num_factor_exposures': 2, 'svd_solver': 'full'} fn_correct_values = { 'PCA': PCA(), 'PCA.components_': np.array([ [0.81925896, -0.40427891, 0.40666118], [-0.02011128, 0.68848693, 0.72496985]])} pca_fit = PCA.fit with patch.object(PCA, 'fit', autospec=True) as mock_fit: mock_fit.side_effect = pca_fit fn_return_value = fn(**fn_inputs) assert_structure(fn_return_value, fn_correct_values['PCA'], 'PCA') try: fn_return_value.fit.assert_called() except AssertionError: raise Exception('Test Failure: PCA.fit not called') try: fn_return_value.fit.assert_called_with(self=fn_return_value, X=fn_inputs['returns']) except Exception: raise Exception('Test Failure: PCA.fit called with the wrong arguments') assert_structure(fn_return_value.components_, fn_correct_values['PCA.components_'], 'PCA.components_') if not does_data_match(fn_return_value.components_, fn_correct_values['PCA.components_']): raise Exception('Test Failure: PCA not fitted correctly\n\n' 'PCA.components_:\n' '{}\n\n' 'Expected PCA.components_:\n' '{}'.format(fn_return_value.components_, fn_correct_values['PCA.components_']))
def test_fit_pca(fn): dates = generate_random_dates(4) assets = get_assets(3) fn_inputs = { "returns": pd.DataFrame( [ [0.02769242, 1.34872387, 0.23460972], [-0.94728692, 0.68386883, -1.23987235], [1.93769376, -0.48275934, 0.34957348], [0.23985234, 0.35897345, 0.34598734], ], dates, assets, ), "num_factor_exposures": 2, "svd_solver": "full", } fn_correct_values = { "PCA": PCA(), "PCA.components_": np.array([ [0.81925896, -0.40427891, 0.40666118], [-0.02011128, 0.68848693, 0.72496985], ]), } pca_fit = PCA.fit with patch.object(PCA, "fit", autospec=True) as mock_fit: mock_fit.side_effect = pca_fit fn_return_value = fn(**fn_inputs) assert_structure(fn_return_value, fn_correct_values["PCA"], "PCA") try: fn_return_value.fit.assert_called() except AssertionError: raise Exception("Test Failure: PCA.fit not called") try: fn_return_value.fit.assert_called_with(self=fn_return_value, X=fn_inputs["returns"]) except Exception: raise Exception( "Test Failure: PCA.fit called with the wrong arguments") assert_structure( fn_return_value.components_, fn_correct_values["PCA.components_"], "PCA.components_", ) if not does_data_match(fn_return_value.components_, fn_correct_values["PCA.components_"]): raise Exception("Test Failure: PCA not fitted correctly\n\n" "PCA.components_:\n" "{}\n\n" "Expected PCA.components_:\n" "{}".format(fn_return_value.components_, fn_correct_values["PCA.components_"]))