mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-15 03:53:41 +02:00
Merge branch 'develop' into documentation
This commit is contained in:
commit
bb98e1f487
7 changed files with 163 additions and 127 deletions
|
@ -39,8 +39,8 @@ def test_covobs():
|
|||
assert(np.isclose(oc.value, op.value, rtol=1e-14, atol=1e-14))
|
||||
|
||||
[o.gamma_method() for o in cl]
|
||||
assert(pe.covariance(cl[0], cl[1]) == cov[0][1])
|
||||
assert(pe.covariance(cl[0], cl[1]) == cov[1][0])
|
||||
assert(np.isclose(pe.covariance([cl[0], cl[1]])[0, 1], cov[0][1]))
|
||||
assert(np.isclose(pe.covariance([cl[0], cl[1]])[0, 1], cov[1][0]))
|
||||
|
||||
do = cl[0] * cl[1]
|
||||
assert(np.array_equal(do.covobs['rAP'].grad, np.transpose([pi[1], pi[0]]).reshape(2, 1)))
|
||||
|
@ -82,6 +82,19 @@ def test_covobs_init():
|
|||
covobs = pe.cov_Obs([1, 2], np.array([[0.21, 0.2], [0.2, 0.21]]), 'test')
|
||||
|
||||
|
||||
def test_covobs_covariance():
|
||||
a = pe.cov_Obs(2.47, 0.03 ** 2, "Cov_obs 1")
|
||||
b = pe.cov_Obs(-4.3, 0.335 ** 2, "Cov_obs 2")
|
||||
|
||||
x = [a + b, a - b]
|
||||
[o.gamma_method() for o in x]
|
||||
|
||||
covariance = pe.covariance(x)
|
||||
|
||||
assert np.isclose(covariance[0, 0], covariance[1, 1])
|
||||
assert np.isclose(covariance[0, 1], a.dvalue ** 2 - b.dvalue ** 2)
|
||||
|
||||
|
||||
def test_covobs_exceptions():
|
||||
with pytest.raises(Exception):
|
||||
covobs = pe.cov_Obs(0.1, [[0.1, 0.2], [0.1, 0.2]], 'test')
|
||||
|
|
|
@ -60,7 +60,7 @@ def test_least_squares():
|
|||
beta[i].gamma_method(S=1.0)
|
||||
assert math.isclose(beta[i].value, popt[i], abs_tol=1e-5)
|
||||
assert math.isclose(pcov[i, i], beta[i].dvalue ** 2, abs_tol=1e-3)
|
||||
assert math.isclose(pe.covariance(beta[0], beta[1]), pcov[0, 1], abs_tol=1e-3)
|
||||
assert math.isclose(pe.covariance([beta[0], beta[1]])[0, 1], pcov[0, 1], abs_tol=1e-3)
|
||||
|
||||
chi2_pyerrors = np.sum(((f(x, *[o.value for o in beta]) - y) / yerr) ** 2) / (len(x) - 2)
|
||||
chi2_scipy = np.sum(((f(x, *popt) - y) / yerr) ** 2) / (len(x) - 2)
|
||||
|
@ -81,7 +81,7 @@ def test_least_squares():
|
|||
betac[i].gamma_method(S=1.0)
|
||||
assert math.isclose(betac[i].value, popt[i], abs_tol=1e-5)
|
||||
assert math.isclose(pcov[i, i], betac[i].dvalue ** 2, abs_tol=1e-3)
|
||||
assert math.isclose(pe.covariance(betac[0], betac[1]), pcov[0, 1], abs_tol=1e-3)
|
||||
assert math.isclose(pe.covariance([betac[0], betac[1]])[0, 1], pcov[0, 1], abs_tol=1e-3)
|
||||
|
||||
|
||||
def test_alternative_solvers():
|
||||
|
@ -195,7 +195,7 @@ def test_total_least_squares():
|
|||
beta[i].gamma_method(S=1.0)
|
||||
assert math.isclose(beta[i].value, output.beta[i], rel_tol=1e-5)
|
||||
assert math.isclose(output.cov_beta[i, i], beta[i].dvalue ** 2, rel_tol=2.5e-1), str(output.cov_beta[i, i]) + ' ' + str(beta[i].dvalue ** 2)
|
||||
assert math.isclose(pe.covariance(beta[0], beta[1]), output.cov_beta[0, 1], rel_tol=2.5e-1)
|
||||
assert math.isclose(pe.covariance([beta[0], beta[1]])[0, 1], output.cov_beta[0, 1], rel_tol=2.5e-1)
|
||||
|
||||
out = pe.total_least_squares(ox, oy, func, const_par=[beta[1]])
|
||||
|
||||
|
@ -218,7 +218,7 @@ def test_total_least_squares():
|
|||
betac[i].gamma_method(S=1.0)
|
||||
assert math.isclose(betac[i].value, output.beta[i], rel_tol=1e-3)
|
||||
assert math.isclose(output.cov_beta[i, i], betac[i].dvalue ** 2, rel_tol=2.5e-1), str(output.cov_beta[i, i]) + ' ' + str(betac[i].dvalue ** 2)
|
||||
assert math.isclose(pe.covariance(betac[0], betac[1]), output.cov_beta[0, 1], rel_tol=2.5e-1)
|
||||
assert math.isclose(pe.covariance([betac[0], betac[1]])[0, 1], output.cov_beta[0, 1], rel_tol=2.5e-1)
|
||||
|
||||
outc = pe.total_least_squares(oxc, oyc, func, const_par=[betac[1]])
|
||||
|
||||
|
@ -233,7 +233,7 @@ def test_total_least_squares():
|
|||
betac[i].gamma_method(S=1.0)
|
||||
assert math.isclose(betac[i].value, output.beta[i], rel_tol=1e-3)
|
||||
assert math.isclose(output.cov_beta[i, i], betac[i].dvalue ** 2, rel_tol=2.5e-1), str(output.cov_beta[i, i]) + ' ' + str(betac[i].dvalue ** 2)
|
||||
assert math.isclose(pe.covariance(betac[0], betac[1]), output.cov_beta[0, 1], rel_tol=2.5e-1)
|
||||
assert math.isclose(pe.covariance([betac[0], betac[1]])[0, 1], output.cov_beta[0, 1], rel_tol=2.5e-1)
|
||||
|
||||
outc = pe.total_least_squares(oxc, oy, func, const_par=[betac[1]])
|
||||
|
||||
|
|
|
@ -246,17 +246,6 @@ def test_gamma_method_kwargs():
|
|||
assert my_obs.N_sigma['ens'] == pe.Obs.N_sigma_global
|
||||
|
||||
|
||||
def test_covariance_is_variance():
|
||||
value = np.random.normal(5, 10)
|
||||
dvalue = np.abs(np.random.normal(0, 1))
|
||||
test_obs = pe.pseudo_Obs(value, dvalue, 't')
|
||||
test_obs.gamma_method()
|
||||
assert np.abs(test_obs.dvalue ** 2 - pe.covariance(test_obs, test_obs)) <= 10 * np.finfo(np.float64).eps
|
||||
test_obs = test_obs + pe.pseudo_Obs(value, dvalue, 'q', 200)
|
||||
test_obs.gamma_method()
|
||||
assert np.abs(test_obs.dvalue ** 2 - pe.covariance(test_obs, test_obs)) <= 10 * np.finfo(np.float64).eps
|
||||
|
||||
|
||||
def test_fft():
|
||||
value = np.random.normal(5, 100)
|
||||
dvalue = np.abs(np.random.normal(0, 5))
|
||||
|
@ -268,21 +257,6 @@ def test_fft():
|
|||
assert np.abs(test_obs1.dvalue - test_obs2.dvalue) <= 10 * max(test_obs1.dvalue, test_obs2.dvalue) * np.finfo(np.float64).eps
|
||||
|
||||
|
||||
def test_covariance_symmetry():
|
||||
value1 = np.random.normal(5, 10)
|
||||
dvalue1 = np.abs(np.random.normal(0, 1))
|
||||
test_obs1 = pe.pseudo_Obs(value1, dvalue1, 't')
|
||||
test_obs1.gamma_method()
|
||||
value2 = np.random.normal(5, 10)
|
||||
dvalue2 = np.abs(np.random.normal(0, 1))
|
||||
test_obs2 = pe.pseudo_Obs(value2, dvalue2, 't')
|
||||
test_obs2.gamma_method()
|
||||
cov_ab = pe.covariance(test_obs1, test_obs2)
|
||||
cov_ba = pe.covariance(test_obs2, test_obs1)
|
||||
assert np.abs(cov_ab - cov_ba) <= 10 * np.finfo(np.float64).eps
|
||||
assert np.abs(cov_ab) < test_obs1.dvalue * test_obs2.dvalue * (1 + 10 * np.finfo(np.float64).eps)
|
||||
|
||||
|
||||
def test_gamma_method_uncorrelated():
|
||||
# Construct pseudo Obs with random shape
|
||||
value = np.random.normal(5, 10)
|
||||
|
@ -620,6 +594,18 @@ def test_gamma_method_irregular():
|
|||
ol = [a, b]
|
||||
o = (ol[0] - ol[1]) / (ol[1])
|
||||
|
||||
|
||||
def test_covariance_is_variance():
|
||||
value = np.random.normal(5, 10)
|
||||
dvalue = np.abs(np.random.normal(0, 1))
|
||||
test_obs = pe.pseudo_Obs(value, dvalue, 't')
|
||||
test_obs.gamma_method()
|
||||
assert np.isclose(test_obs.dvalue ** 2, pe.covariance([test_obs, test_obs])[0, 1])
|
||||
test_obs = test_obs + pe.pseudo_Obs(value, dvalue, 'q', 200)
|
||||
test_obs.gamma_method()
|
||||
assert np.isclose(test_obs.dvalue ** 2, pe.covariance([test_obs, test_obs])[0, 1])
|
||||
|
||||
|
||||
def test_covariance_symmetry():
|
||||
value1 = np.random.normal(5, 10)
|
||||
dvalue1 = np.abs(np.random.normal(0, 1))
|
||||
|
@ -629,9 +615,9 @@ def test_covariance_symmetry():
|
|||
dvalue2 = np.abs(np.random.normal(0, 1))
|
||||
test_obs2 = pe.pseudo_Obs(value2, dvalue2, 't')
|
||||
test_obs2.gamma_method()
|
||||
cov_ab = pe.covariance(test_obs1, test_obs2)
|
||||
cov_ba = pe.covariance(test_obs2, test_obs1)
|
||||
assert np.abs(cov_ab - cov_ba) <= 10 * np.finfo(np.float64).eps
|
||||
cov_ab = pe.covariance([test_obs1, test_obs2])[0, 1]
|
||||
cov_ba = pe.covariance([test_obs2, test_obs1])[0, 1]
|
||||
assert np.isclose(cov_ab, cov_ba)
|
||||
assert np.abs(cov_ab) < test_obs1.dvalue * test_obs2.dvalue * (1 + 10 * np.finfo(np.float64).eps)
|
||||
|
||||
N = 100
|
||||
|
@ -643,14 +629,73 @@ def test_covariance_symmetry():
|
|||
idx = [i + 1 for i in range(len(configs)) if configs[i] == 1]
|
||||
a = pe.Obs([zero_arr], ['t'], idl=[idx])
|
||||
a.gamma_method()
|
||||
assert np.isclose(a.dvalue**2, pe.covariance(a, a), atol=100, rtol=1e-4)
|
||||
assert np.isclose(a.dvalue ** 2, pe.covariance([a, a])[0, 1], atol=100, rtol=1e-4)
|
||||
|
||||
cov_ab = pe.covariance(test_obs1, a)
|
||||
cov_ba = pe.covariance(a, test_obs1)
|
||||
cov_ab = pe.covariance([test_obs1, a])[0, 1]
|
||||
cov_ba = pe.covariance([a, test_obs1])[0, 1]
|
||||
assert np.abs(cov_ab - cov_ba) <= 10 * np.finfo(np.float64).eps
|
||||
assert np.abs(cov_ab) < test_obs1.dvalue * a.dvalue * (1 + 10 * np.finfo(np.float64).eps)
|
||||
|
||||
|
||||
def test_covariance_sum():
|
||||
length = 2
|
||||
t_fac = 0.4
|
||||
tt = pe.misc.gen_correlated_data(np.zeros(length), 0.99 * np.ones((length, length)) + 0.01 * np.diag(np.ones(length)), 'test', tau=0.5 + t_fac * np.random.rand(length), samples=1000)
|
||||
[o.gamma_method(S=0) for o in tt]
|
||||
|
||||
t_cov = pe.covariance(tt)
|
||||
|
||||
my_sum = tt[0] + tt[1]
|
||||
my_sum.gamma_method(S=0)
|
||||
e_cov = (my_sum.dvalue ** 2 - tt[0].dvalue ** 2 - tt[1].dvalue ** 2) / 2
|
||||
|
||||
assert np.isclose(e_cov, t_cov[0, 1])
|
||||
|
||||
|
||||
def test_covariance_positive_semidefinite():
|
||||
length = 64
|
||||
t_fac = 1.5
|
||||
tt = pe.misc.gen_correlated_data(np.zeros(length), 0.99999 * np.ones((length, length)) + 0.00001 * np.diag(np.ones(length)), 'test', tau=0.5 + t_fac * np.random.rand(length), samples=1000)
|
||||
[o.gamma_method() for o in tt]
|
||||
cov = pe.covariance(tt)
|
||||
assert np.all(np.linalg.eigh(cov)[0] >= -1e-15)
|
||||
|
||||
|
||||
def test_covariance_factorizing():
|
||||
length = 2
|
||||
t_fac = 1.5
|
||||
|
||||
tt = pe.misc.gen_correlated_data(np.zeros(length), 0.75 * np.ones((length, length)) + 0.8 * np.diag(np.ones(length)), 'test', tau=0.5 + t_fac * np.random.rand(length), samples=1000)
|
||||
[o.gamma_method() for o in tt]
|
||||
|
||||
mt0 = -tt[0]
|
||||
mt0.gamma_method()
|
||||
|
||||
assert np.isclose(pe.covariance([mt0, tt[1]])[0, 1], -pe.covariance(tt)[0, 1])
|
||||
|
||||
|
||||
def test_covariance_alternation():
|
||||
length = 12
|
||||
t_fac = 2.5
|
||||
|
||||
tt1 = pe.misc.gen_correlated_data(np.zeros(length), -0.00001 * np.ones((length, length)) + 0.002 * np.diag(np.ones(length)), 'test', tau=0.5 + t_fac * np.random.rand(length), samples=88)
|
||||
tt2 = pe.misc.gen_correlated_data(np.zeros(length), 0.9999 * np.ones((length, length)) + 0.0001 * np.diag(np.ones(length)), 'another_test|r0', tau=0.7 + t_fac * np.random.rand(length), samples=73)
|
||||
tt3 = pe.misc.gen_correlated_data(np.zeros(length), 0.9999 * np.ones((length, length)) + 0.0001 * np.diag(np.ones(length)), 'another_test|r1', tau=0.7 + t_fac * np.random.rand(length), samples=91)
|
||||
|
||||
tt = np.array(tt1) + (np.array(tt2) + np.array(tt3))
|
||||
tt *= np.resize([1, -1], length)
|
||||
|
||||
[o.gamma_method() for o in tt]
|
||||
cov = pe.covariance(tt, True)
|
||||
|
||||
assert np.all(np.linalg.eigh(cov)[0] > -1e-15)
|
||||
|
||||
|
||||
def test_covariance_correlation():
|
||||
test_obs = pe.pseudo_Obs(-4, 0.8, 'test', samples=784)
|
||||
assert np.allclose(pe.covariance([test_obs, test_obs, test_obs], correlation=True), np.ones((3, 3)))
|
||||
|
||||
|
||||
def test_empty_obs():
|
||||
o = pe.Obs([np.random.rand(100)], ['test'])
|
||||
q = o + pe.Obs([], [])
|
||||
|
@ -704,4 +749,4 @@ def test_merge_idx():
|
|||
new_idx = pe.obs._merge_idx(idl)
|
||||
assert(new_idx[-1] > new_idx[0])
|
||||
for i in range(1, len(new_idx)):
|
||||
assert(new_idx[i - 1] < new_idx[i])
|
||||
assert(new_idx[i - 1] < new_idx[i])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue