def test__unflatten_gaussian_copula(self): """Test unflatte gaussian copula""" # Setup sampler = Mock(autospec=Sampler) sampler._prepare_sampled_covariance.return_value = [[0.4, 0.2], [0.2, 0.0]] # Run model_parameters = { 'distribs': { 'foo': { 'std': 0.5 } }, 'covariance': [[0.4, 0.1], [0.1]], 'distribution': 'GaussianUnivariate' } result = Sampler._unflatten_gaussian_copula(sampler, model_parameters) # Asserts expected = { 'distribs': { 'foo': { 'fitted': True, 'std': 1.6487212707001282, 'type': 'GaussianUnivariate' } }, 'distribution': 'GaussianUnivariate', 'covariance': [[0.4, 0.2], [0.2, 0.0]] } assert result == expected
def test__unflatten_gaussian_copula(self): """_unflatten_gaussian_copula add the distribution, type and fitted kwargs.""" # Setup data_navigator = MagicMock() modeler = MagicMock() modeler.model_kwargs = { 'distribution': 'distribution_name' } sampler = Sampler(data_navigator, modeler) model_parameters = { 'some': 'key', 'covariance': [ [1], [0, 1] ], 'distribs': { 0: { 'first': 'distribution', 'std': 0 }, 1: { 'second': 'distribution', 'std': 0 } } } expected_result = { 'some': 'key', 'distribution': 'distribution_name', 'covariance': [ [1, 0], [0, 1] ], 'distribs': { 0: { 'type': 'distribution_name', 'fitted': True, 'first': 'distribution', 'std': 1 }, 1: { 'type': 'distribution_name', 'fitted': True, 'second': 'distribution', 'std': 1 } } } # Run result = sampler._unflatten_gaussian_copula(model_parameters) # Check assert result == expected_result data_navigator.assert_not_called() modeler.assert_not_called()
def test__unflatten_gaussian_copula_negative_std(self): """_unflatten_gaussian_copula will transform negative or 0 std into positive.""" # Setup data_navigator = MagicMock() modeler = MagicMock() modeler.model_kwargs = { 'distribution': 'distribution_name' } sampler = Sampler(data_navigator, modeler) model_parameters = { 'some': 'key', 'covariance': [ [1], [0, 1] ], 'distribs': { 0: { 'first': 'distribution', 'std': 0 }, 1: { 'second': 'distribution', 'std': -1 } } } expected_result = { 'some': 'key', 'distribution': 'distribution_name', 'covariance': [ [1, 0], [0, 1] ], 'distribs': { 0: { 'type': 'distribution_name', 'fitted': True, 'first': 'distribution', 'std': 1 }, 1: { 'type': 'distribution_name', 'fitted': True, 'second': 'distribution', 'std': np.exp(-1) } } } # Run result = sampler._unflatten_gaussian_copula(model_parameters) # Check assert result == expected_result data_navigator.assert_not_called() modeler.assert_not_called()
def test__unflatten_gaussian_copula_not_matrix_symmetric(self): """unflatte with not matrix symmetric""" # Setup model_parameters = { 'some': 'key', 'covariance': [[1], [0, 1]], 'distribs': { 0: { 'first': 'distribution', 'std': 0 }, 1: { 'second': 'distribution', 'std': 0 } } } modeler_mock = Mock() modeler_mock.model_kwargs = {'distribution': 'distribution_name'} prepare_mock = Mock() prepare_mock.return_value = [[1], [0, 1]] check_mock = Mock() check_mock.return_value = False make_mock = Mock() make_mock.return_value = np.array([[1, 0], [0, 1]]) # Run sampler_mock = Mock() sampler_mock.modeler = modeler_mock sampler_mock._prepare_sampled_covariance = prepare_mock sampler_mock._check_matrix_symmetric_positive_definite = check_mock sampler_mock._make_positive_definite = make_mock result = Sampler._unflatten_gaussian_copula(sampler_mock, model_parameters) # Asserts assert result['covariance'] == [[1, 0], [0, 1]] prepare_mock.assert_called_once_with([[1], [0, 1]]) check_mock.assert_called_once_with([[1], [0, 1]]) make_mock.assert_called_once_with([[1], [0, 1]])