def test_cumulative_distribution(self, kde_mock): """cumulative_distribution evaluates with the model.""" # Setup model_mock = kde_mock.return_value model_mock.integrate_box_1d.side_effect = [0.0, 0.5, 1.0] model_mock.dataset = MagicMock() model_mock.dataset.mean.return_value = 1 model_mock.dataset.std.return_value = 0.1 fit_data = np.array([1, 2, 3, 4, 5]) instance = GaussianKDE() instance.fit(fit_data) call_data = np.array([-10, 0, 10]) expected_result = np.array([0.0, 0.5, 1.0]) expected_integrate_1d_box_call_args_list = [ ((0.5, -10), {}), # The first argument is the lower_bound (1 - 0.1*5) ((0.5, 0), {}), ((0.5, 10), {}), ] # Run result = instance.cumulative_distribution(call_data) # Check compare_nested_iterables(result, expected_result) kde_mock.assert_called_once_with(fit_data) assert (model_mock.integrate_box_1d.call_args_list == expected_integrate_1d_box_call_args_list)
def test_sample(self, uniform_mock): """Sample use the inverse-transform method to generate new samples.""" # Setup instance = Clayton() instance.tau = 0.5 instance.theta = instance.compute_theta() uniform_mock.return_value = np.array([0.1, 0.2, 0.4, 0.6, 0.8]) expected_result = np.array([ [0.05233100, 0.1], [0.14271095, 0.2], [0.39959746, 0.4], [0.68567125, 0.6], [0.89420523, 0.8] ]) expected_uniform_call_args_list = [ ((0, 1, 5), {}), ((0, 1, 5), {}) ] # Run result = instance.sample(5) # Check assert isinstance(result, np.ndarray) assert result.shape == (5, 2) compare_nested_iterables(result, expected_result) assert uniform_mock.call_args_list == expected_uniform_call_args_list
def test_sample(self, sample_mock): """After being fit, a vine can sample new data.""" # Setup vine = VineCopula(TreeTypes.REGULAR) X = pd.DataFrame([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ], columns=list('ABCD')) vine.fit(X) expected_result = pd.DataFrame([ {'A': 1, 'B': 2, 'C': 3, 'D': 4}, {'A': 1, 'B': 2, 'C': 3, 'D': 4}, {'A': 1, 'B': 2, 'C': 3, 'D': 4}, {'A': 1, 'B': 2, 'C': 3, 'D': 4}, {'A': 1, 'B': 2, 'C': 3, 'D': 4}, ]) sample_mock.return_value = np.array([1, 2, 3, 4]) # Run result = vine.sample(5) # Check compare_nested_iterables(result, expected_result) assert sample_mock.call_count == 5
def test_get_conditional_uni(self, adjacent_mock): """get_conditional_uni return the corresponding univariate adjacent to the parents.""" # Setup left = Edge(None, 1, 2, None, None) left.U = np.array([ ['left_0_0', 'left_0_1'], ['left_1_0', 'left_1_1'] ]) right = Edge(None, 4, 2, None, None) right.U = np.array([ ['right_0_0', 'right_0_1'], ['right_1_0', 'right_1_1'] ]) adjacent_mock.return_value = (0, 1, None) expected_result = ( np.array(['left_1_0', 'left_1_1']), np.array(['right_1_0', 'right_1_1']) ) # Run result = Edge.get_conditional_uni(left, right) # Check compare_nested_iterables(result, expected_result)
def test_sample(self, uniform_mock): """Sample use the inverse-transform method to generate new samples.""" # Setup instance = Bivariate(CopulaTypes.FRANK) instance.tau = 0.5 instance.theta = instance.compute_theta() uniform_mock.return_value = np.array([0.1, 0.2, 0.4, 0.6, 0.8]) expected_result = np.array([[6.080069565509917e-06, 0.1], [6.080069565509917e-06, 0.2], [6.080069565509917e-06, 0.4], [6.080069565509917e-06, 0.6], [4.500185268624483e-06, 0.8]]) expected_uniform_call_args_list = [((0, 1, 5), {}), ((0, 1, 5), {})] # Run result = instance.sample(5) # Check assert isinstance(result, np.ndarray) assert result.shape == (5, 2) compare_nested_iterables(result, expected_result) assert uniform_mock.call_args_list == expected_uniform_call_args_list
def test_sample(self, uniform_mock): """Sample use the inverse-transform method to generate new samples.""" # Setup instance = Frank() instance.tau = 0.5 instance.theta = instance.compute_theta() uniform_mock.return_value = np.array([0.1, 0.2, 0.4, 0.6, 0.8]) expected_result = np.array([ [0.0312640840463779, 0.1], [0.1007998170183327, 0.2], [0.3501836319841291, 0.4], [0.6498163680158703, 0.6], [0.8992001829816683, 0.8] ]) expected_uniform_call_args_list = [ ((0, 1, 5), {}), ((0, 1, 5), {}) ] # Run result = instance.sample(5) # Check assert isinstance(result, np.ndarray) assert result.shape == (5, 2) compare_nested_iterables(result, expected_result) assert uniform_mock.call_args_list == expected_uniform_call_args_list
def test_fit_not_contant(self, select_mock): """if not constant call select_univariate and fit the returned instance. Check that candidates are passed down to select_univariate and that the returned instance is fitted on the input data. """ # Setup candidate = MagicMock() candidates = [candidate] distribution = Univariate(candidates) # Run data = np.array([1, 2, 3, 4, 5]) distribution.fit(data) # Assert assert distribution.fitted assert distribution.constant_value is None # candidates are passed down assert select_mock.call_count == 1 expected_call = call(data, candidates)[1:] actual_call = select_mock.call_args compare_nested_iterables(expected_call, actual_call) # the returned instance is fitted instance = select_mock.return_value assert instance.fit.call_count == 1 expected_call = call(data)[1:] actual_call = instance.fit.call_args compare_nested_iterables(expected_call, actual_call)
def test_get_likelihood_with_parents(self, bivariate_mock): """If edge has parents, their dependences are used to retrieve univariates.""" # Setup index = None left = 0 right = 1 copula_name = 'copula_name' copula_theta = 'copula_theta' instance = Edge(index, left, right, copula_name, copula_theta) instance.D = {0, 1, 2, 3} parent_1 = MagicMock(spec=Edge) parent_1.D = {1, 2, 3} parent_2 = MagicMock(spec=Edge) parent_2.D = {0, 2, 3} univariates = np.array([ [0.25, 0.75], [0.50, 0.50], [0.75, 0.25] ]).T instance_mock = bivariate_mock.return_value instance_mock.probability_density.return_value = [0] instance_mock.partial_derivative.return_value = 'partial_derivative' expected_partial_derivative_call_args = [ ( (np.array([[ [0.25, 0.75], [0.50, 0.50], ]]),), {} ), ( (np.array([[ [0.50, 0.50], [0.25, 0.75] ]]), ), {} ) ] # Run result = instance.get_likelihood(univariates) # Check value, left_given_right, right_given_left = result assert value == 0 assert left_given_right == 'partial_derivative' assert right_given_left == 'partial_derivative' bivariate_mock.assert_called_once_with(copula_type='copula_name') assert instance_mock.theta == 'copula_theta' compare_nested_iterables( instance_mock.partial_derivative.call_args_list, expected_partial_derivative_call_args )
def test__constant_sample(self): """_constant_sample returns a constant array of num_samples length.""" # Setup instance = Univariate() instance._constant_value = 15 expected_result = np.array([15, 15, 15, 15, 15]) # Run result = instance._constant_sample(5) # Check compare_nested_iterables(result, expected_result)
def test_get_likelihood_no_parents(self, bivariate_mock): """get_likelihood will use current node indices if there are no parents.""" # Setup index = 0 left = 0 right = 1 copula_name = 'copula_name' copula_theta = 'copula_theta' instance = Edge(index, left, right, copula_name, copula_theta) univariates = np.array([ [0.25, 0.75], [0.50, 0.50], [0.75, 0.25] ]).T instance_mock = bivariate_mock.return_value instance_mock.probability_density.return_value = [0] instance_mock.partial_derivative.return_value = 'partial_derivative' expected_partial_derivative_call_args = [ ( (np.array([[ [0.25, 0.75], [0.50, 0.50], ]]),), {} ), ( (np.array([[ [0.50, 0.50], [0.25, 0.75] ]]), ), {} ) ] # Run result = instance.get_likelihood(univariates) # Check value, left_given_right, right_given_left = result assert value == 0 assert left_given_right == 'partial_derivative' assert right_given_left == 'partial_derivative' bivariate_mock.assert_called_once_with(copula_type='copula_name') assert instance_mock.theta == 'copula_theta' compare_nested_iterables( instance_mock.partial_derivative.call_args_list, expected_partial_derivative_call_args )
def test__constant_percent_point(self): """constant_percent_point only is self.constant_value in non-zero probabilities.""" # Setup instance = Univariate() instance._constant_value = 3 X = np.array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) expected_result = np.array([3, 3, 3, 3, 3, 3]) # Run result = instance._constant_percent_point(X) # Check compare_nested_iterables(result, expected_result)
def test__constant_probability_density(self): """constant_probability_density only is 1 in self.constant_value.""" # Setup instance = Univariate() instance._constant_value = 3 X = np.array([1, 2, 3, 4, 5]) expected_result = np.array([0, 0, 1, 0, 0]) # Run result = instance._constant_probability_density(X) # Check compare_nested_iterables(result, expected_result)
def test__constant_cumulative_distribution(self): """constant_cumulative_distribution returns only 0 and 1.""" # Setup instance = Univariate() instance._constant_value = 3 X = np.array([1, 2, 3, 4, 5]) expected_result = np.array([0, 0, 1, 1, 1]) # Run result = instance._constant_cumulative_distribution(X) # Check compare_nested_iterables(result, expected_result)
def test_prepare_next_tree_regular_level(self, bivariate_mock, conditional_mock): """prepare_next_tree computes the conditional U matrices on its edges.""" # Setup instance = get_tree(TreeTypes.REGULAR) instance.level = 2 edge = MagicMock(spec=Edge) edge.parents = ['first_parent', 'second_parent'] edge.name = 'copula_type' edge.theta = 'copula_theta' instance.edges = [edge] copula_mock = bivariate_mock.return_value copula_mock.partial_derivative.return_value = np.array([0.0, 0.25, 0.5, 0.75, 1.0]) conditional_mock.return_value = ( ['left_u_1', 'left_u_2'], ['right_u_1', 'right_u_2'] ) expected_univariate = np.array([ [EPSILON, 0.25, 0.50, 0.75, 1 - EPSILON], [EPSILON, 0.25, 0.50, 0.75, 1 - EPSILON] ]) conditional_univariates = np.array([ ['left_u_1', 'right_u_1'], ['left_u_2', 'right_u_2'] ]) expected_partial_derivative_call_args = [ ((conditional_univariates,), {}), ((conditional_univariates[:, np.argsort([1, 0])],), {}) ] # Run instance.prepare_next_tree() # Check compare_nested_iterables(instance.edges[0].U, expected_univariate) bivariate_mock.assert_called_once_with(copula_type='copula_type') conditional_mock.assert_called_once_with('first_parent', 'second_parent') assert copula_mock.theta == 'copula_theta' compare_nested_iterables( copula_mock.partial_derivative.call_args_list, expected_partial_derivative_call_args )
def test_sample_constant(self): """If constant_value is set, all the sample have the same value.""" # Setup instance = GaussianKDE() instance.fitted = True instance.constant_value = 3 instance._replace_constant_methods() expected_result = np.array([3, 3, 3, 3, 3]) # Run result = instance.sample(5) # Check compare_nested_iterables(result, expected_result)
def test_sample_constant(self): """samples can be generated for constant distribution.""" # Setup instance = GaussianUnivariate() instance.constant_value = 3 instance._replace_constant_methods() instance.fitted = True expected_result = np.array([3, 3, 3, 3, 3]) # Run result = instance.sample(5) # Check compare_nested_iterables(result, expected_result)
def test_sample_random_state(self): """When random_state is set, the generated samples are always the same.""" # Setup vine = VineCopula(TreeTypes.REGULAR, random_seed=0) X = pd.DataFrame([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) vine.fit(X) expected_result = pd.DataFrame( [[0.101933, 0.527734, 0.080266, 0.078328]], columns=range(4)) # Run result = vine.sample(1) # Check compare_nested_iterables(result, expected_result)
def test_sample_random_state(self): """If random_state is set, samples will generate the exact same values.""" # Setup instance = KDEUnivariate(random_seed=0) X = np.array([1, 2, 3, 4, 5]) instance.fit(X) expected_result_random_state = np.array( [[5.02156389, 5.45857107, 6.12161148, 4.56801267, 6.14017901]]) # Run result = instance.sample(5) # Check compare_nested_iterables(result, expected_result_random_state)
def test_cumulative_distribution_constant(self): """cumulative_distribution can be computed for constant distribution.""" # Setup instance = GaussianUnivariate() instance.constant_value = 3 instance._replace_constant_methods() instance.fitted = True X = np.array([1, 2, 3, 4, 5]) expected_result = np.array([0, 0, 1, 1, 1]) # Run result = instance.cumulative_distribution(X) # Check compare_nested_iterables(result, expected_result)
def test_sample_random_state(self): """If random_state is set, the samples are the same.""" # Setup instance = Bivariate(CopulaTypes.CLAYTON, random_seed=0) instance.tau = 0.5 instance.theta = instance.compute_theta() expected_result = np.array([[0.68627770, 0.54881350], [0.64059280, 0.71518937], [0.90594782, 0.60276338], [0.96040856, 0.54488318], [0.40876969, 0.42365480]]) # Run result = instance.sample(5) # Check compare_nested_iterables(result, expected_result)
def test_sample_random_state(self): """If random_state is set, the samples are the same.""" # Setup instance = Bivariate(CopulaTypes.FRANK, random_seed=0) instance.tau = 0.5 instance.theta = instance.compute_theta() expected_result = np.array([[3.66330927e-06, 5.48813504e-01], [6.08006957e-06, 7.15189366e-01], [5.27582646e-06, 6.02763376e-01], [5.58315848e-06, 5.44883183e-01], [6.08006957e-06, 4.23654799e-01]]) # Run result = instance.sample(5) # Check compare_nested_iterables(result, expected_result)
def test_prepare_next_tree_first_level(self, bivariate_mock): """prepare_next_tree computes the conditional U matrices on its edges.""" # Setup instance = get_tree(TreeTypes.REGULAR) instance.level = 1 instance.u_matrix = np.array([ [0.1, 0.2], [0.3, 0.4] ]) edge = MagicMock(spec=Edge) edge.L = 0 edge.R = 1 edge.name = 'copula_type' edge.theta = 'copula_theta' instance.edges = [edge] copula_mock = bivariate_mock.return_value copula_mock.partial_derivative.return_value = np.array([0.0, 0.25, 0.5, 0.75, 1.0]) expected_univariate = np.array([ [EPSILON, 0.25, 0.50, 0.75, 1 - EPSILON], [EPSILON, 0.25, 0.50, 0.75, 1 - EPSILON] ]) expected_partial_derivative_call_args = [ ((instance.u_matrix,), {}), ((instance.u_matrix[:, np.argsort([1, 0])],), {}) ] # Run instance.prepare_next_tree() # Check compare_nested_iterables(instance.edges[0].U, expected_univariate) bivariate_mock.assert_called_once_with(copula_type='copula_type') assert copula_mock.theta == 'copula_theta' compare_nested_iterables( copula_mock.partial_derivative.call_args_list, expected_partial_derivative_call_args )
def test_sample(self, random_mock): """After fitting, GaussianUnivariate is able to sample new data.""" # Setup instance = GaussianUnivariate() column = np.array([-1, 0, 1]) instance.fit(column) expected_result = np.array([1, 2, 3, 4, 5]) random_mock.return_value = expected_result # Run result = instance.sample(5) # Check compare_nested_iterables(result, expected_result) assert instance.mean == 0.0 assert instance.std == 0.816496580927726 random_mock.assert_called_once_with(0.0, 0.816496580927726, 5)
def test_probability_density_constant(self, pdf_mock): """If constant_value, probability_density uses the degenerate version.""" # Setup instance = GaussianKDE() instance.fitted = True instance.constant_value = 3 instance._replace_constant_methods() X = np.array([0, 1, 2, 3, 4, 5]) expected_result = np.array([0, 0, 1, 0, 0]) pdf_mock.return_value = np.array([0, 0, 1, 0, 0]) # Run result = instance.probability_density(X) # Check compare_nested_iterables(result, expected_result) pdf_mock.assert_called_once_with(instance, X)
def test_percent_point_constant_raises(self, ppf_mock): """If constant_value, percent_point uses the degenerate version.""" # Setup instance = GaussianKDE() instance.fitted = True instance.constant_value = 3 instance._replace_constant_methods() X = np.array([0.1, 0.5, 0.75]) expected_result = np.array([3, 3, 3]) ppf_mock.return_value = np.array([3, 3, 3]) # Run result = instance.percent_point(X) # Check compare_nested_iterables(result, expected_result) ppf_mock.assert_called_once_with(instance, X)
def test_probability_density(self, kde_mock): """probability_density evaluates with the model.""" # Setup model_mock = kde_mock.return_value model_mock.evaluate.return_value = np.array([0.0, 0.5, 1.0]) fit_data = np.array([1, 2, 3, 4, 5]) instance = GaussianKDE() instance.fit(fit_data) call_data = np.array([-10, 0, 10]) expected_result = np.array([0.0, 0.5, 1.0]) # Run result = instance.probability_density(call_data) # Check compare_nested_iterables(result, expected_result) kde_mock.assert_called_once_with(fit_data) model_mock.evaluate.assert_called_once_with(call_data)
def test_sample(self, kde_mock): """When fitted, we are able to use the model to get samples.""" # Setup model_mock = kde_mock.return_value model_mock.resample.return_value = np.array([[0, 1, 0, 1, 0]]) instance = GaussianKDE() X = np.array([1, 2, 3, 4, 5]) instance.fit(X) expected_result = np.array([0, 1, 0, 1, 0]) # Run result = instance.sample(5) # Check compare_nested_iterables(result, expected_result) assert instance.model == model_mock kde_mock.assert_called_once_with(X) model_mock.resample.assert_called_once_with(5)
def test_sample_row(self, uniform_mock, randint_mock): """After being fit, a vine can sample new data.""" # Setup instance = VineCopula(TreeTypes.REGULAR) X = pd.DataFrame( [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], columns=list('ABCD')) instance.fit(X) uniform_mock.return_value = np.array([0.1, 0.25, 0.5, 0.75]) randint_mock.return_value = 1 expected_result = np.array( [-1.63155227, -0.16358589, -1.63155227, -1.62583869]) # Run result = instance._sample_row() # Check compare_nested_iterables(result, expected_result) uniform_mock.assert_called_once_with(0, 1, 4) randint_mock.assert_called_once_with(0, 4)
def test_sample(self, uniform_mock): """Sample use the inverse-transform method to generate new samples.""" # Setup instance = Gumbel() instance.tau = 0.5 instance.theta = instance.compute_theta() uniform_mock.return_value = np.array([0.1, 0.2, 0.4, 0.6, 0.8]) expected_result = np.array([[0.0360633200000181, 0.1], [0.1142629649994753, 0.2], [0.3446610994349153, 0.4], [0.6171955667476859, 0.6], [0.8636748995382857, 0.8]]) expected_uniform_call_args_list = [((0, 1, 5), {}), ((0, 1, 5), {})] # Run result = instance.sample(5) # Check assert isinstance(result, np.ndarray) assert result.shape == (5, 2) compare_nested_iterables(result, expected_result) assert uniform_mock.call_args_list == expected_uniform_call_args_list