def test_compute_mean(params, weights): log_likes = np.ones(2) expected_means = {'a': 1.5, 'b': 2.5} particles = Particles(params, log_likes, np.log(weights)) assert particles.compute_mean() == expected_means
def test_get_std_dev(params, weights, expected_var): log_likes = np.ones(2) particles = Particles(params, log_likes, np.log(weights)) expected_var['a'] = np.sqrt(expected_var['a']) expected_var['b'] = np.sqrt(expected_var['b']) assert particles.compute_std_dev() == pytest.approx(expected_var)
def test_non_positive_def_cov_is_independent(params, weights, expected_var, mocker): log_likes = np.ones(2) particles = Particles(params, log_likes, np.log(weights)) mocker.patch.object(particles, '_is_positive_definite', return_value=False) expected_cov = np.eye(2) * expected_var cov = particles.compute_covariance() np.testing.assert_array_almost_equal(cov, expected_cov)
def test_compute_covariance(mocker): params = {'a': [1.1, 1.0, 0.8], 'b': [2.2, 2.1, 1.9]} log_likes = np.ones(3) log_weights = np.log([0.1, 0.7, 0.2]) particles = Particles(params, log_likes, log_weights) mocker.patch.object(particles, 'compute_mean', return_value=np.array([0.97, 2.06])) scale = 1 / (1 - np.sum(np.array([0.1, 0.7, 0.2])**2)) expected_cov = np.array([[0.0081, 0.0081], [0.0081, 0.0082]]) * scale np.testing.assert_array_almost_equal(particles.compute_covariance(), expected_cov)
def test_log_weights_value_errors(log_weights): params = {'a': [5] * 4} log_likes = [5] * 4 with pytest.raises(ValueError): Particles(params, log_likes, log_weights)
def test_log_likes_value_errors(log_likes): params = {'a': [5] * 4} with pytest.raises(ValueError): Particles(params, log_likes, None)
def test_params_type_error(): with pytest.raises(TypeError): Particles([], None, None)
def test_params_value_error(): params = {'a': [1, 2], 'c': [2], 'b': 4} with pytest.raises(ValueError): Particles(params, None, None)
def particles(dummy_params, dummy_log_likes, dummy_log_weights): return Particles(dummy_params, dummy_log_likes, dummy_log_weights)
def test_compute_variance(params, weights, expected_var): log_likes = np.ones(2) particles = Particles(params, log_likes, np.log(weights)) assert particles.compute_variance() == pytest.approx(expected_var)