def test_get_parameter(): cop = GaussianCopula(3) cop[:] = gen_corr() assert isinstance(cop[:], np.ndarray) assert isinstance(cop[:, 0], np.ndarray) assert isinstance(cop[0, 0], (float, int))
def test_set_parameter_value_error(category, value): cop = GaussianCopula(3) with pytest.raises(ValueError): if category == 'full': cop[:] = value elif category == 'slice': cop[:, 0] = value
def get_data(N, P, rng): # Set seed np.random.seed(rng + 100) # Generate X with a little bit of correlation b/w the continuous variables (i.e. 0.3 Pearson coeffincient c.a.) X = np.zeros((N, P)) # Generate Gaussian copula correlated uniforms g_cop = GaussianCopula(dim=P) mycova = np.ones((P, P)) for i in range(P): for j in range(P): mycova[i, j] = 0.3**(np.abs(i - j)) + np.where(i == j, 0, 0.1) g_cop[:] = mycova rv = g_cop.random(N) # Generate correlated covariates X[:, 0:5] = np.asarray([sts.norm.ppf(rv[:, i]) for i in range(5)]).T X[:, 5:P] = np.asarray([sts.binom.ppf(rv[:, i], 1, 0.3) for i in range(5, P)]).T # Generate Z und_lin = -0.5 + 0.2 * X[:, 0] + 0.1 * X[:, 1] + 0.4 * X[:, 20] + sts.uniform.rvs( size=N) / 10 pscore = sts.norm.cdf(und_lin) Z = sts.binom.rvs(1, pscore) # Generate Y mu = 3 + 1.5 * np.sin(np.pi * X[:, 0]) + 0.5 * (X[:, 1] - 0.5)**2 + 1.5 * ( 2 - np.abs(X[:, 2])) + 1.5 * X[:, 3] * (X[:, 20] + 1) ITE = 0.1 + 1 * np.abs(X[:, 0] - 1) * (X[:, 20] + 2) sigma = np.std(mu) / 2 Y = mu + ITE * Z + sts.norm.rvs(0, sigma, N) return Y, X, Z, ITE
def test_get_parameter_index_error(): cop = GaussianCopula(3) cop[:] = gen_corr() with pytest.raises(IndexError): cop[0, 0, 0] with pytest.raises(IndexError): cop[{}]
def test_set_parameter(): corr = gen_corr() cop = GaussianCopula(3) cop[:] = corr cop[:, 0] = corr[:, 0] cop[1, :] = corr[1, :] cop[:, :] = corr cop[1, 2] = 0.5 cop[0] = 0.3
def test_reshape_data_decorator(): copula = GaussianCopula(2) # test that scalar goes through fine assert isinstance(array_io(lambda cop, x: x)(copula, 0.5), float) assert isinstance(array_io(lambda cop, x: x, dim=1)(copula, 0.5), float) # test that vector goes through okay assert array_io(lambda cop, x: x, dim=1)(copula, [0.5, 0.2]).ndim == 1 # test that 1D array gets converted to 2D array res = array_io(lambda cop, x: x, dim=2)(copula, np.zeros(2)) assert res.ndim == 2 # Non-optional input with pytest.raises(AssertionError): array_io(lambda cop, x: x, optional=False)(copula, None)
def test_reshape_data_decorator(): copula = GaussianCopula(2) # test that array output with 1 element becomes scalar and is float x1 = array_io(lambda cop, x: [1])(copula, [0.5, 0.5]) assert x1 == 1 assert isinstance(x1, float) # test that vector output gets squeezed if one of the dimension is 1 for output in ( [[1, 2]], [[1], [2]] ): x2 = array_io(lambda cop, x: output)(copula, [0.5, 0.5]) assert x2.ndim == 1, "vector output must have squeezed" # test that 2D output stays 2D x3 = array_io(lambda cop, x: [[1, 2], [3, 4]])(copula, [0.5, 0.5]) assert x3.ndim == 2, "2D output should remain 2D" # Non-optional input with pytest.raises(ValueError): array_io(lambda cop, x: x, optional=False)(copula, None)
def fitted_gaussian(residual_data): dim = residual_data.shape[1] cop = GaussianCopula(dim) cop.fit(residual_data) return cop
def copula(residual_data: np.ndarray): dim = residual_data.shape[1] cop = GaussianCopula(dim) cop.params = target_params return cop
def test_set_parameter_index_error(index): cop = GaussianCopula(3) with pytest.raises(IndexError): cop[index] = 0.3
def test_fitted_parameters_match_target(residual_data: np.ndarray): dim = residual_data.shape[1] cop = GaussianCopula(dim) cop.fit(residual_data) assert_array_almost_equal(cop.params, target_params, 4)