def test_pdf_bivariate(): # the dim > 2 case is tested when we fitted the copula. Test the bivariate case here cop = ClaytonCopula(-0.3075057, 2) u = np.array([ [0.199296, 0.645346], [0.135279, 0.30006] ]) assert_array_almost_equal(cop.pdf(u), [1.152112, 1.018026])
def test_clayton_random_generates_correctly(copula): n = 10 assert copula.random(n).shape == (n, copula.dim) expected = (n, 2) cop = ClaytonCopula(0) assert cop.random(n).shape == expected cop.params = 1.5 assert cop.random(n).shape == expected
def allclayton(x, y, thetaInit=1.4): sample = len(x) # Convert to normall # result = allnorm(x, y) u = result["u"] v = result["v"] sigma = result["sigma"] hes_norm = result["hes_norm"] # x - mean, y - mean # xbar = x - sigma[2] ybar = y - sigma[3] # Calculate theta # data = [] for i in range(len(u)): data.append([u[i][0], v[i][0]]) data = np.array(data) cop = ClaytonCopula(theta=thetaInit) cop.fit(data) theta = cop.params # Save frequent calculations # v_pow_minus_theta = v ** (-theta) u_pow_minus_theta = u ** (-theta) minus_sample = -sample # Find logLikelihood of theta # cop1 = (sample * np.log(1 + theta)) - ((theta + 1) * np.sum(np.log(u * v))) - ((np.sum(np.log((u ** (-theta)) + (v ** (-theta)) -1))) * (2 + (1/theta))) - (0.5 * sample * np.log(2 * pi * (sigma[0] ** 2))) - (0.5 * np.sum(xbar ** 2) / (sigma[0] ** 2)) - (0.5 * sample * np.log(2 * pi * (sigma[1] ** 2))) - (0.5 * np.sum(ybar ** 2) / (sigma[1] ** 2)) # Calculate hessian of log-copula's density # hes_cop = (minus_sample / ((theta + 1)**2)) -2 * (theta ** (-3) * np.sum(np.log(u_pow_minus_theta + v_pow_minus_theta - 1))) - 2*(theta ** (-2)) * np.sum(np.divide(np.multiply(np.log(u), u_pow_minus_theta) + np.multiply(np.log(v), v_pow_minus_theta),u_pow_minus_theta + v_pow_minus_theta - 1)) - (2 + (1/theta))*np.sum(np.divide(np.multiply(np.multiply(np.log(u) ** 2, u ** (-theta)) + np.multiply(np.log(v) ** 2, v_pow_minus_theta), u_pow_minus_theta + v_pow_minus_theta - 1) - ((np.multiply((u_pow_minus_theta), np.log(u)) + np.multiply((v_pow_minus_theta), np.log(v)))** 2), (((u ** (-theta)) + (v ** (-theta)) - 1) ** 2))) s = minus_sample / hes_cop hes_prior_cop = -1 / (s ** 2) # Opou loc valame scale apo ton deutero log kai meta. log_prior = np.log(norm.pdf(theta, loc=0, scale=s)) + np.log(expon.pdf(sigma[0], scale=1)) + np.log(expon.pdf(sigma[1], scale=1)) BF = 1 # Vgazoume to inv kai vazoume -1/ BFu = cop1 + log_prior + 0.5 * np.log(-1/(det(hes_norm) * (hes_cop - hes_prior_cop))) hes = det(hes_norm) * (hes_cop - hes_prior_cop) if theta < 10**(-5): theta = 0 cop1 = 0 BFu = cop1 + log_prior + 0.5 * np.log(-det(np.matmul(hes_norm, hes_cop - hes_prior_cop))) result = {"theta": theta, "cop1": cop1, "hes": hes, "hes_prior_cor": hes_prior_cop, "BF": BF, "BFu": BFu} return result
def fitted_clayton(residual_data): cop = ClaytonCopula(dim=residual_data.shape[1]) cop.fit(residual_data) return cop
def test_param_set_raises_error(dim, theta, err): cop = ClaytonCopula(dim=dim) with pytest.raises(ValueError, match=err): cop.params = theta
def test_itau_fit(residual_data: np.ndarray): cop = ClaytonCopula(dim=residual_data.shape[1]) cop.fit(residual_data, method='itau') assert_almost_equal(cop.params, 0.7848391, 4)
def copula(residual_data: np.ndarray): dim = residual_data.shape[1] cop = ClaytonCopula(dim=dim) cop.params = 0.3075057 return cop
def test_fitted_parameters_match_target(residual_data: np.ndarray): cop = ClaytonCopula(dim=residual_data.shape[1]) cop.fit(residual_data) assert_almost_equal(cop.params, 0.3075057, 4)
def test_dipsi_raises_errors(theta, degree, err): cop = ClaytonCopula(theta, dim=2) with pytest.raises(NotImplementedError, match=err): cop.dipsi([0.6534646, 0.3093621], degree=degree)