def test_2_vector_api(data: np.ndarray): assert np.isclose(corr(data[:, 0], data[:, 1])[0, 1], -0.1687177, atol=DP) data[-1, 0] = np.nan with np.errstate(invalid='ignore'): # Adding ignore because comparing nans raise invalid runtime warning assert np.isnan(corr(data[:, 0], data[:, 1])[0, 1]) assert np.isclose(corr(data[:, 0], data[:, 1], use='complete')[0, 1], -0.16634284, atol=DP) assert np.isclose(corr(data[:, 0], data[:, 1], use='complete')[0, 1], -0.16634284, atol=DP)
def test_raise_error_if_x_y_not_vector(): with pytest.raises(ValueError): corr(np.zeros((10, 2)), np.zeros(10)) with pytest.raises(ValueError): corr(np.zeros(10), np.zeros((10, 2))) with pytest.raises(ValueError): corr(np.zeros((10, 2)), np.zeros((10, 2)))
def test_corr(data): assert_array_almost_equal( corr(data), np.array([[1.0000000, -0.16871767, -0.23407923], [-0.1687177, 1.00000000, 0.05121912], [-0.2340792, 0.05121912, 1.00000000]]), DP)
def test_raise_error_if_x_y_different_length(): with pytest.raises(ValueError): corr(np.zeros(10), np.ones(5))
def test_raise_error_if_only_input_not_matrix(): with pytest.raises(ValueError): corr(np.zeros(10))
def test_raise_error_if_invalid_use(data: np.ndarray): with pytest.raises(ValueError): corr(data, use='SOMETHING STRANGE')
def test_raise_error_if_invalid_method(data: np.ndarray): with pytest.raises(ValueError): corr(data, method='WRONG METHOD')
def tau_rho_sample(copula: BaseCopula, nsim: int, method="spearman") -> float: u = copula.random(nsim) u = u[~np.isnan(u).any(1)] return corr(u, method=method)[0, 1]