Skip to content

Commit

Permalink
fix: renamed covdiag to inverrdiag needed for the cholesky decomposit…
Browse files Browse the repository at this point in the history
…ion and corrected its documentation
  • Loading branch information
PiaLJP committed Aug 29, 2024
1 parent 5ced3bd commit 7c56e1a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions pyerrors/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def func_b(a, x):
list: for an uncombined fit: [""]
for a combined fit: list of keys belonging to the corr_matrix saved in the array, must be the same as the keys of the y dict in alphabetical order
If correlated_fit=True is set as well, can provide an inverse covariance matrix (y erros, dy_f included!) of your own choosing for a correlated fit.
The matrix must be a lower triangular matrix constructed from a Cholesky decomposition: The function invert_corr_cov_cholesky(corr, covdiag) can be
used to construct it from a correlation matrix (corr) and the errors dy_f of the data points (covdiag = np.diag(1 / np.asarray(dy_f))).
The matrix must be a lower triangular matrix constructed from a Cholesky decomposition: The function invert_corr_cov_cholesky(corr, inverrdiag) can be
used to construct it from a correlation matrix (corr) and the errors dy_f of the data points (inverrdiag = np.diag(1 / np.asarray(dy_f))).
expected_chisquare : bool
If True estimates the expected chisquare which is
corrected by effects caused by correlated input data (default False).
Expand Down Expand Up @@ -311,12 +311,12 @@ def chisqfunc_uncorr(p):
if (chol_inv[0].shape[0] != chol_inv[0].shape[1]):
raise TypeError('The inverse covariance matrix handed over needs to have the same number of rows as columns.')
if (chol_inv[1] != key_ls):
raise ValueError('The keys of the corr matrix are not same or do not appear in the same order as the x and y values.')
raise ValueError('The keys of inverse covariance matrix are not the same or do not appear in the same order as the x and y values.')
chol_inv = chol_inv[0]
else:
corr = covariance(y_all, correlation=True, **kwargs)
covdiag = np.diag(1 / np.asarray(dy_f))
chol_inv = invert_corr_cov_cholesky(corr, covdiag)
inverrdiag = np.diag(1 / np.asarray(dy_f))
chol_inv = invert_corr_cov_cholesky(corr, inverrdiag)

def general_chisqfunc(p, ivars, pr):
model = anp.concatenate([anp.array(funcd[key](p, xd[key])).reshape(-1) for key in key_ls])
Expand Down
10 changes: 5 additions & 5 deletions pyerrors/obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,14 +1544,14 @@ def covariance(obs, visualize=False, correlation=False, smooth=None, **kwargs):
return cov


def invert_corr_cov_cholesky(corr, covdiag):
def invert_corr_cov_cholesky(corr, inverrdiag):
"""Constructs a lower triangular matrix, chol, via the Cholesky decomposition of the correlation matrix, corr,
and then returns the inverse covariance matrix, chol_inv, as a lower triangular matrix by solving chol * x = covdiag.
and then returns the inverse covariance matrix, chol_inv, as a lower triangular matrix by solving chol * x = inverrdiag.
corr : np.ndarray
correlation matrix
covdiag : np.ndarray
diagonal matrix, the entries are the errors of the data points considered
inverrdiag : np.ndarray
diagonal matrix, the entries are the inverse errors of the data points considered
"""

condn = np.linalg.cond(corr)
Expand All @@ -1560,7 +1560,7 @@ def invert_corr_cov_cholesky(corr, covdiag):
if condn > 1e13:
warnings.warn("Correlation matrix may be ill-conditioned, condition number: {%1.2e}" % (condn), RuntimeWarning)
chol = np.linalg.cholesky(corr)
chol_inv = scipy.linalg.solve_triangular(chol, covdiag, lower=True)
chol_inv = scipy.linalg.solve_triangular(chol, inverrdiag, lower=True)

return chol_inv

Expand Down

0 comments on commit 7c56e1a

Please sign in to comment.