Skip to content

Commit

Permalink
vectorization: gaussian+fit_cmle_mixt+IAD_dist
Browse files Browse the repository at this point in the history
Vectorization in gaussian.get_cdf(), mixture.fit_cmle_mixt(), estimation.IAD_dist(), and estimation.AD_dist().
  • Loading branch information
maximenc committed Mar 30, 2024
1 parent 1ddab72 commit e946fb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions pycop/bivariate/estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def log_likelihood(parameters):
else: # dim = 3
w1, w2, w3, param1, param2, param3 = parameters
params = [w1, w2, w3, param1, param2, param3]
logl = -sum([ np.log(copula.get_pdf(psd_obs[0][i], psd_obs[1][i],params)) for i in range(0, len(psd_obs[0]))])
logl = -np.sum(np.log(copula.get_pdf(psd_obs[0], psd_obs[1], params)))
return logl

# copula.dim gives the number of weights to consider
Expand Down Expand Up @@ -253,8 +253,12 @@ def IAD_dist(copula, data, param):
x_values, y_values = np.linspace(1/n, 1-1/n, n), np.linspace(1/n, 1-1/n, n)

# Compute the parametric (theoretical) copula values
C_copula = np.array([[copula.get_cdf(x, y, param) for x in x_values] for y in y_values])
u_flat = np.array([[x for x in x_values] for y in y_values]).flatten()
v_flat = np.array([[y for x in x_values] for y in y_values]).flatten()

C_copula = copula.get_cdf(np.array(u_flat), np.array(v_flat), param)
C_copula = C_copula.reshape((n,n))

# Calculate the Integrated Anderson-Darling distance
IAD = np.sum(((C_empirical - C_copula) ** 2) / (C_copula - C_copula**2))

Expand All @@ -280,7 +284,12 @@ def AD_dist(copula, data, param):
C_empirical = counts / n

x_values, y_values = np.linspace(1/n, 1-1/n, n), np.linspace(1/n, 1-1/n, n)
C_copula = np.array([[copula.get_cdf(x, y, param) for x in x_values] for y in y_values])

u_flat = np.array([[x for x in x_values] for y in y_values]).flatten()
v_flat = np.array([[y for x in x_values] for y in y_values]).flatten()

C_copula = copula.get_cdf(np.array(u_flat), np.array(v_flat), param)
C_copula = C_copula.reshape((n,n))

AD = np.max(((C_empirical - C_copula) ** 2) / (C_copula - C_copula**2))
return AD
2 changes: 1 addition & 1 deletion pycop/bivariate/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_cdf(self, u, v, param):
y2 = norm.ppf(v, 0, 1)
rho = param[0]

return multivariate_normal.cdf((y1,y2), mean=None, cov=[[1, rho], [rho, 1]])
return multivariate_normal.cdf(np.array([y1, y2]).T, mean=None, cov=[[1, rho], [rho, 1]])

def get_pdf(self, u, v, param):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name = 'pycop',
version = '0.0.12',
version = '0.0.13',
description = 'Copula for multivariate dependence modeling',
long_description=open('README.md', 'r').read(),
long_description_content_type="text/markdown",
Expand Down

0 comments on commit e946fb3

Please sign in to comment.