mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-14 19:43:41 +02:00
Merge branch 'develop' into documentation
This commit is contained in:
commit
75cc221421
5 changed files with 49 additions and 8 deletions
|
@ -184,6 +184,26 @@ def test_fit_corr_independent():
|
|||
assert (out[1] - out_corr[1]).is_zero(atol=1e-5)
|
||||
|
||||
|
||||
def test_linear_fit_guesses():
|
||||
for err in [10, 0.1, 0.001]:
|
||||
xvals = []
|
||||
yvals = []
|
||||
for x in range(1, 8, 2):
|
||||
xvals.append(x)
|
||||
yvals.append(pe.pseudo_Obs(x + np.random.normal(0.0, err), err, 'test1') + pe.pseudo_Obs(0, err / 100, 'test2', samples=87))
|
||||
lin_func = lambda a, x: a[0] + a[1] * x
|
||||
with pytest.raises(Exception):
|
||||
pe.least_squares(xvals, yvals, lin_func)
|
||||
[o.gamma_method() for o in yvals];
|
||||
with pytest.raises(Exception):
|
||||
pe.least_squares(xvals, yvals, lin_func, initial_guess=[5])
|
||||
|
||||
bad_guess = pe.least_squares(xvals, yvals, lin_func, initial_guess=[999, 999])
|
||||
good_guess = pe.least_squares(xvals, yvals, lin_func, initial_guess=[0, 1])
|
||||
assert np.isclose(bad_guess.chisquare, good_guess.chisquare, atol=1e-8)
|
||||
assert np.all([(go - ba).is_zero(atol=1e-6) for (go, ba) in zip(good_guess, bad_guess)])
|
||||
|
||||
|
||||
def test_total_least_squares():
|
||||
dim = 10 + int(30 * np.random.rand())
|
||||
x = np.arange(dim) + np.random.normal(0.0, 0.15, dim)
|
||||
|
@ -223,7 +243,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]])[0, 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=3.5e-1)
|
||||
|
||||
out = pe.total_least_squares(ox, oy, func, const_par=[beta[1]])
|
||||
|
||||
|
@ -246,7 +266,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]])[0, 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=3.5e-1)
|
||||
|
||||
outc = pe.total_least_squares(oxc, oyc, func, const_par=[betac[1]])
|
||||
|
||||
|
@ -261,7 +281,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]])[0, 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=3.5e-1)
|
||||
|
||||
outc = pe.total_least_squares(oxc, oy, func, const_par=[betac[1]])
|
||||
|
||||
|
|
|
@ -736,6 +736,23 @@ def test_covariance_factorizing():
|
|||
assert np.isclose(pe.covariance([mt0, tt[1]])[0, 1], -pe.covariance(tt)[0, 1])
|
||||
|
||||
|
||||
def test_covariance_smooth_eigenvalues():
|
||||
for c_coeff in range(0, 14, 2):
|
||||
length = 14
|
||||
sm = 5
|
||||
t_fac = 1.5
|
||||
tt = pe.misc.gen_correlated_data(np.zeros(length), 1 - 0.1 ** c_coeff * np.ones((length, length)) + 0.1 ** c_coeff * np.diag(np.ones(length)), 'test', tau=0.5 + t_fac * np.random.rand(length), samples=200)
|
||||
[o.gamma_method() for o in tt]
|
||||
full_corr = pe.covariance(tt, correlation=True)
|
||||
cov = pe.covariance(tt, smooth=sm, correlation=True)
|
||||
|
||||
full_evals = np.linalg.eigh(full_corr)[0]
|
||||
sm_length = np.where(full_evals < np.mean(full_evals[:-sm]))[0][-1]
|
||||
|
||||
evals = np.linalg.eigh(cov)[0]
|
||||
assert np.all(np.isclose(evals[:sm_length], evals[0], atol=1e-8))
|
||||
|
||||
|
||||
def test_covariance_alternation():
|
||||
length = 12
|
||||
t_fac = 2.5
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue