mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-14 19:43:41 +02:00
refactor: chisqfunc rewritten as sum over residuals.
This commit is contained in:
parent
dc7033e51f
commit
ee2944d5b0
1 changed files with 13 additions and 18 deletions
|
@ -580,6 +580,17 @@ def _combined_fit(x, y, func, silent=False, **kwargs):
|
||||||
else:
|
else:
|
||||||
x0 = [0.1] * n_parms
|
x0 = [0.1] * n_parms
|
||||||
|
|
||||||
|
if kwargs.get('correlated_fit') is True:
|
||||||
|
def chisqfunc_residuals_corr(p):
|
||||||
|
model = np.concatenate([np.array(funcd[key](p, np.asarray(xd[key]))).reshape(-1) for key in key_ls])
|
||||||
|
chisq = anp.dot(chol_inv, (y_f - model))
|
||||||
|
return chisq
|
||||||
|
|
||||||
|
def chisqfunc_residuals(p):
|
||||||
|
model = np.concatenate([np.array(funcd[key](p, np.asarray(xd[key]))).reshape(-1) for key in key_ls])
|
||||||
|
chisq = ((y_f - model) / dy_f)
|
||||||
|
return chisq
|
||||||
|
|
||||||
if kwargs.get('correlated_fit') is True:
|
if kwargs.get('correlated_fit') is True:
|
||||||
corr = covariance(y_all, correlation=True, **kwargs)
|
corr = covariance(y_all, correlation=True, **kwargs)
|
||||||
covdiag = np.diag(1 / np.asarray(dy_f))
|
covdiag = np.diag(1 / np.asarray(dy_f))
|
||||||
|
@ -592,15 +603,10 @@ def _combined_fit(x, y, func, silent=False, **kwargs):
|
||||||
chol_inv = scipy.linalg.solve_triangular(chol, covdiag, lower=True)
|
chol_inv = scipy.linalg.solve_triangular(chol, covdiag, lower=True)
|
||||||
|
|
||||||
def chisqfunc_corr(p):
|
def chisqfunc_corr(p):
|
||||||
model = np.concatenate([np.array(funcd[key](p, np.asarray(xd[key]))).reshape(-1) for key in key_ls])
|
return anp.sum(chisqfunc_residuals_corr(p) ** 2)
|
||||||
chisq = anp.sum(anp.dot(chol_inv, (y_f - model)) ** 2)
|
|
||||||
return chisq
|
|
||||||
|
|
||||||
def chisqfunc(p):
|
def chisqfunc(p):
|
||||||
func_list = np.concatenate([[funcd[k]] * len(xd[k]) for k in key_ls])
|
return anp.sum(chisqfunc_residuals(p) ** 2)
|
||||||
model = anp.array([func_list[i](p, x_all[i]) for i in range(len(x_all))])
|
|
||||||
chisq = anp.sum(((y_f - model) / dy_f) ** 2)
|
|
||||||
return chisq
|
|
||||||
|
|
||||||
output.method = kwargs.get('method', 'Levenberg-Marquardt')
|
output.method = kwargs.get('method', 'Levenberg-Marquardt')
|
||||||
if not silent:
|
if not silent:
|
||||||
|
@ -627,17 +633,6 @@ def _combined_fit(x, y, func, silent=False, **kwargs):
|
||||||
chisquare = fit_result.fun
|
chisquare = fit_result.fun
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if kwargs.get('correlated_fit') is True:
|
|
||||||
def chisqfunc_residuals_corr(p):
|
|
||||||
model = np.concatenate([np.array(funcd[key](p, np.asarray(xd[key]))).reshape(-1) for key in key_ls])
|
|
||||||
chisq = anp.dot(chol_inv, (y_f - model))
|
|
||||||
return chisq
|
|
||||||
|
|
||||||
def chisqfunc_residuals(p):
|
|
||||||
model = np.concatenate([np.array(funcd[key](p, np.asarray(xd[key]))).reshape(-1) for key in key_ls])
|
|
||||||
chisq = ((y_f - model) / dy_f)
|
|
||||||
return chisq
|
|
||||||
|
|
||||||
if 'tol' in kwargs:
|
if 'tol' in kwargs:
|
||||||
print('tol cannot be set for Levenberg-Marquardt')
|
print('tol cannot be set for Levenberg-Marquardt')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue