Skip to content

Commit e0076cc

Browse files
authored
[Fix] corrected expected_chisquare by adding the number of priors (fjosw#274)
* [Fix] corrected expected_chisquare by adding the number of priors * test/fits_test.py: dof and expected chisquare the same in uncorrelated fit w. prior to uncorrelated data
1 parent 85ae9d7 commit e0076cc

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

pyerrors/fits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ def prepare_hat_matrix():
472472
hat_vector = prepare_hat_matrix()
473473
A = W @ hat_vector
474474
P_phi = A @ np.linalg.pinv(A.T @ A) @ A.T
475-
expected_chisquare = np.trace((np.identity(y_all.shape[-1]) - P_phi) @ W @ cov @ W)
475+
expected_chisquare = np.trace((np.identity(y_all.shape[-1]) - P_phi) @ W @ cov @ W) + len(loc_priors)
476476
output.chisquare_by_expected_chisquare = output.chisquare / expected_chisquare
477477
if not silent:
478478
print('chisquare/expected_chisquare:', output.chisquare_by_expected_chisquare)

tests/fits_test.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,3 +1611,81 @@ def chisqfunc_compact(d):
16111611
qqplot(x, y, func, result)
16121612

16131613
return output
1614+
1615+
def test_dof_prior_fit():
1616+
"""Performs an uncorrelated fit with a prior to uncorrelated data then
1617+
the expected chisquare and the usual dof need to agree"""
1618+
N = 5
1619+
1620+
def fitf(a, x):
1621+
return a[0] + 0 * x
1622+
1623+
x = [1. for i in range(N)]
1624+
y = [pe.cov_Obs(i, .1, '%d' % (i)) for i in range(N)]
1625+
[o.gm() for o in y]
1626+
res = pe.fits.least_squares(x, y, fitf, expected_chisquare=True, priors=[pe.cov_Obs(3, 1, 'p')])
1627+
assert res.chisquare_by_expected_chisquare == res.chisquare_by_dof
1628+
1629+
num_samples = 400
1630+
N = 10
1631+
1632+
x = norm.rvs(size=(N, num_samples)) # generate random numbers
1633+
1634+
r = np.zeros((N, N))
1635+
for i in range(N):
1636+
for j in range(N):
1637+
if(i==j):
1638+
r[i, j] = 1.0 # element in correlation matrix
1639+
1640+
errl = np.sqrt([3.4, 2.5, 3.6, 2.8, 4.2, 4.7, 4.9, 5.1, 3.2, 4.2]) # set y errors
1641+
for i in range(N):
1642+
for j in range(N):
1643+
if(i==j):
1644+
r[i, j] *= errl[i] * errl[j] # element in covariance matrix
1645+
1646+
c = cholesky(r, lower=True)
1647+
y = np.dot(c, x)
1648+
x = np.arange(N)
1649+
x_dict = {}
1650+
y_dict = {}
1651+
for i,item in enumerate(x):
1652+
x_dict[str(item)] = [x[i]]
1653+
1654+
for linear in [True, False]:
1655+
data = []
1656+
for i in range(N):
1657+
if linear:
1658+
data.append(pe.Obs([[i + 1 + o for o in y[i]]], ['ens'+str(i)]))
1659+
else:
1660+
data.append(pe.Obs([[np.exp(-(i + 1)) + np.exp(-(i + 1)) * o for o in y[i]]], ['ens'+str(i)]))
1661+
1662+
[o.gamma_method() for o in data]
1663+
1664+
data_dict = {}
1665+
for i,item in enumerate(x):
1666+
data_dict[str(item)] = [data[i]]
1667+
1668+
corr = pe.covariance(data, correlation=True)
1669+
chol = np.linalg.cholesky(corr)
1670+
covdiag = np.diag(1 / np.asarray([o.dvalue for o in data]))
1671+
chol_inv = scipy.linalg.solve_triangular(chol, covdiag, lower=True)
1672+
chol_inv_keys = [""]
1673+
chol_inv_keys_combined_fit = [str(item) for i,item in enumerate(x)]
1674+
1675+
if linear:
1676+
def fitf(p, x):
1677+
return p[1] + p[0] * x
1678+
fitf_dict = {}
1679+
for i,item in enumerate(x):
1680+
fitf_dict[str(item)] = fitf
1681+
else:
1682+
def fitf(p, x):
1683+
return p[1] * anp.exp(-p[0] * x)
1684+
fitf_dict = {}
1685+
for i,item in enumerate(x):
1686+
fitf_dict[str(item)] = fitf
1687+
1688+
fit_exp = pe.least_squares(x, data, fitf, expected_chisquare=True, priors = {0:pe.cov_Obs(1.0, 1, 'p')})
1689+
fit_cov = pe.least_squares(x, data, fitf, correlated_fit = True, inv_chol_cov_matrix = [chol_inv,chol_inv_keys], priors = {0:pe.cov_Obs(1.0, 1, 'p')})
1690+
assert np.isclose(fit_exp.chisquare_by_expected_chisquare,fit_exp.chisquare_by_dof,atol=1e-8)
1691+
assert np.isclose(fit_exp.chisquare_by_expected_chisquare,fit_cov.chisquare_by_dof,atol=1e-8)

0 commit comments

Comments
 (0)