mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-14 11:33:42 +02:00
Vectorized gamma_method (#207)
* feat: vectorized gamma_method function added. * feat: vectorized gamma method generalized to also work on other objects like Corr or Fit_result. * feat: alias gamma_method for vectorized gamma_method added. * docs: example 5 updated to include vectorized gamma_method. * docs: output of example 5 updated.
This commit is contained in:
parent
6b7846486d
commit
1e438356fd
3 changed files with 76 additions and 46 deletions
|
@ -1,4 +1,5 @@
|
|||
import autograd.numpy as np
|
||||
import numpy as np
|
||||
import autograd.numpy as anp
|
||||
import os
|
||||
import copy
|
||||
import matplotlib.pyplot as plt
|
||||
|
@ -126,9 +127,9 @@ def test_function_overloading():
|
|||
|
||||
fs = [lambda x: x[0] + x[1], lambda x: x[1] + x[0], lambda x: x[0] - x[1], lambda x: x[1] - x[0],
|
||||
lambda x: x[0] * x[1], lambda x: x[1] * x[0], lambda x: x[0] / x[1], lambda x: x[1] / x[0],
|
||||
lambda x: np.exp(x[0]), lambda x: np.sin(x[0]), lambda x: np.cos(x[0]), lambda x: np.tan(x[0]),
|
||||
lambda x: np.log(x[0]), lambda x: np.sqrt(np.abs(x[0])),
|
||||
lambda x: np.sinh(x[0]), lambda x: np.cosh(x[0]), lambda x: np.tanh(x[0])]
|
||||
lambda x: anp.exp(x[0]), lambda x: anp.sin(x[0]), lambda x: anp.cos(x[0]), lambda x: anp.tan(x[0]),
|
||||
lambda x: anp.log(x[0]), lambda x: anp.sqrt(anp.abs(x[0])),
|
||||
lambda x: anp.sinh(x[0]), lambda x: anp.cosh(x[0]), lambda x: anp.tanh(x[0])]
|
||||
|
||||
for i, f in enumerate(fs):
|
||||
t1 = f([a, b])
|
||||
|
@ -306,9 +307,9 @@ def test_derived_observables():
|
|||
test_obs = pe.pseudo_Obs(2, 0.1 * (1 + np.random.rand()), 't', int(1000 * (1 + np.random.rand())))
|
||||
|
||||
# Check if autograd and numgrad give the same result
|
||||
d_Obs_ad = pe.derived_observable(lambda x, **kwargs: x[0] * x[1] * np.sin(x[0] * x[1]), [test_obs, test_obs])
|
||||
d_Obs_ad = pe.derived_observable(lambda x, **kwargs: x[0] * x[1] * anp.sin(x[0] * x[1]), [test_obs, test_obs])
|
||||
d_Obs_ad.gamma_method()
|
||||
d_Obs_fd = pe.derived_observable(lambda x, **kwargs: x[0] * x[1] * np.sin(x[0] * x[1]), [test_obs, test_obs], num_grad=True)
|
||||
d_Obs_fd = pe.derived_observable(lambda x, **kwargs: x[0] * x[1] * anp.sin(x[0] * x[1]), [test_obs, test_obs], num_grad=True)
|
||||
d_Obs_fd.gamma_method()
|
||||
|
||||
assert d_Obs_ad == d_Obs_fd
|
||||
|
@ -1283,12 +1284,14 @@ def test_format_uncertainty():
|
|||
pe.obs._format_uncertainty(1, np.NaN)
|
||||
pe.obs._format_uncertainty(np.NaN, np.inf)
|
||||
|
||||
|
||||
def test_format():
|
||||
o1 = pe.pseudo_Obs(0.348, 0.0123, "test")
|
||||
assert o1.__format__("+3") == '+0.3480(123)'
|
||||
assert o1.__format__("+2") == '+0.348(12)'
|
||||
assert o1.__format__(" 2") == ' 0.348(12)'
|
||||
|
||||
|
||||
def test_f_string_obs():
|
||||
o1 = pe.pseudo_Obs(0.348, 0.0123, "test")
|
||||
print(f"{o1}")
|
||||
|
@ -1297,6 +1300,7 @@ def test_f_string_obs():
|
|||
print(f"{o1:-1}")
|
||||
print(f"{o1: 8}")
|
||||
|
||||
|
||||
def test_f_string_cobs():
|
||||
o_real = pe.pseudo_Obs(0.348, 0.0123, "test")
|
||||
o_imag = pe.pseudo_Obs(0.348, 0.0123, "test")
|
||||
|
@ -1307,7 +1311,24 @@ def test_f_string_cobs():
|
|||
print(f"{o1:-1}")
|
||||
print(f"{o1: 8}")
|
||||
|
||||
|
||||
def test_compute_drho_fails():
|
||||
obs = pe.input.json.load_json("tests/data/compute_drho_fails.json.gz")
|
||||
obs.gm()
|
||||
assert np.isclose(obs.dvalue, 0.0022150779611891094)
|
||||
|
||||
|
||||
def test_vec_gm():
|
||||
obs = pe.misc.gen_correlated_data(np.arange(3), np.array([[0.0364 , 0.03627262, 0.03615699],
|
||||
[0.03627262, 0.03688438, 0.03674798],
|
||||
[0.03615699, 0.03674798, 0.03732882]]), "qq", 3.8, 1000)
|
||||
pe.gm(obs[0], S=0)
|
||||
assert obs[0].S["qq"] == 0
|
||||
pe.gm(obs, S=1.3)
|
||||
assert np.all(np.vectorize(lambda x: x.S["qq"])(obs) == 1.3)
|
||||
aa = np.array([obs, obs, obs])
|
||||
pe.gamma_method(aa, S=2.2)
|
||||
assert np.all(np.vectorize(lambda x: x.S["qq"])(aa) == 2.20)
|
||||
cc = pe.Corr(obs)
|
||||
pe.gm(cc, S=4.12)
|
||||
assert np.all(np.vectorize(lambda x: x.S["qq"])(cc.content) == 4.12)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue