예제 #1
0
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)

    N = 100
    arr = np.random.normal(1, .2, size=N)
    configs = np.ones_like(arr)
    for i in np.random.uniform(0, len(arr), size=int(.8 * N)):
        configs[int(i)] = 0
    zero_arr = [arr[i] for i in range(len(arr)) if not configs[i] == 0]
    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)

    cov_ab = pe.covariance(test_obs1, a)
    cov_ba = pe.covariance(a, 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)
예제 #2
0
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
예제 #3
0
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)
예제 #4
0
def test_covobs():
    val = 1.123124
    cov = .243423
    name = 'Covariance'
    co = pe.cov_Obs(val, cov, name)
    co.gamma_method()
    co.details()
    assert (co.dvalue == np.sqrt(cov))
    assert (co.value == val)

    do = 2 * co
    assert (do.covobs[name].grad[0] == 2)

    do = co * co
    assert (do.covobs[name].grad[0] == 2 * val)
    assert np.array_equal(do.covobs[name].cov, co.covobs[name].cov)

    pi = [16.7457, -19.0475]
    cov = [[3.49591, -6.07560], [-6.07560, 10.5834]]

    cl = pe.cov_Obs(pi, cov, 'rAP')
    pl = pe.misc.gen_correlated_data(pi, np.asarray(cov), 'rAPpseudo')

    def rAP(p, g0sq):
        return -0.0010666 * g0sq * (1 + np.exp(p[0] + p[1] / g0sq))

    for g0sq in [1, 1.5, 1.8]:
        oc = rAP(cl, g0sq)
        oc.gamma_method()
        op = rAP(pl, g0sq)
        op.gamma_method()
        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])

    do = cl[0] * cl[1]
    assert(np.array_equal(do.covobs['rAP'].grad, np.transpose([pi[1], pi[0]]).reshape(2, 1)))
예제 #5
0
def test_odr_fit(n):
    dim = 10 + int(30 * np.random.rand())
    x = np.arange(dim) + np.random.normal(0.0, 0.15, dim)
    xerr = 0.1 + 0.1 * np.random.rand(dim)
    y = 2 * np.exp(-0.06 * x) + np.random.normal(0.0, 0.15, dim)
    yerr = 0.1 + 0.1 * np.random.rand(dim)

    ox = []
    for i, item in enumerate(x):
        ox.append(pe.pseudo_Obs(x[i], xerr[i], str(i)))

    oy = []
    for i, item in enumerate(x):
        oy.append(pe.pseudo_Obs(y[i], yerr[i], str(i)))

    def f(x, a, b):
        return a * np.exp(-b * x)

    def func(a, x):
        y = a[0] * np.exp(-a[1] * x)
        return y

    data = RealData([o.value for o in ox], [o.value for o in oy],
                    sx=[o.dvalue for o in ox],
                    sy=[o.dvalue for o in oy])
    model = Model(func)
    odr = ODR(data, model, [0, 0], partol=np.finfo(np.float).eps)
    odr.set_job(fit_type=0, deriv=1)
    output = odr.run()

    beta = pe.fits.odr_fit(ox, oy, func)

    pe.Obs.e_tag_global = 5
    for i in range(2):
        beta[i].gamma_method(e_tag=5, 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)
    pe.Obs.e_tag_global = 0
예제 #6
0
def test_standard_fit(n):
    dim = 10 + int(30 * np.random.rand())
    x = np.arange(dim)
    y = 2 * np.exp(-0.06 * x) + np.random.normal(0.0, 0.15, dim)
    yerr = 0.1 + 0.1 * np.random.rand(dim)

    oy = []
    for i, item in enumerate(x):
        oy.append(pe.pseudo_Obs(y[i], yerr[i], str(i)))

    def f(x, a, b):
        return a * np.exp(-b * x)

    popt, pcov = scipy.optimize.curve_fit(f,
                                          x,
                                          y,
                                          sigma=[o.dvalue for o in oy],
                                          absolute_sigma=True)

    def func(a, x):
        y = a[0] * np.exp(-a[1] * x)
        return y

    beta = pe.fits.standard_fit(x, oy, func)

    pe.Obs.e_tag_global = 5
    for i in range(2):
        beta[i].gamma_method(e_tag=5, 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)
    pe.Obs.e_tag_global = 0

    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)
    assert math.isclose(chi2_pyerrors, chi2_scipy, abs_tol=1e-10)
예제 #7
0
def test_least_squares():
    dim = 10 + int(30 * np.random.rand())
    x = np.arange(dim)
    y = 2 * np.exp(-0.06 * x) + np.random.normal(0.0, 0.15, dim)
    yerr = 0.1 + 0.1 * np.random.rand(dim)

    oy = []
    for i, item in enumerate(x):
        oy.append(pe.pseudo_Obs(y[i], yerr[i], str(i)))

    def f(x, a, b):
        return a * np.exp(-b * x)

    popt, pcov = scipy.optimize.curve_fit(f,
                                          x,
                                          y,
                                          sigma=[o.dvalue for o in oy],
                                          absolute_sigma=True)

    def func(a, x):
        y = a[0] * np.exp(-a[1] * x)
        return y

    out = pe.least_squares(x,
                           oy,
                           func,
                           expected_chisquare=True,
                           resplot=True,
                           qqplot=True)
    beta = out.fit_parameters

    str(out)
    repr(out)
    len(out)

    for i in range(2):
        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)

    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)
    assert math.isclose(chi2_pyerrors, chi2_scipy, abs_tol=1e-10)

    out = pe.least_squares(x, oy, func, const_par=[beta[1]])
    assert ((out.fit_parameters[0] - beta[0]).is_zero())
    assert ((out.fit_parameters[1] - beta[1]).is_zero())

    oyc = []
    for i, item in enumerate(x):
        oyc.append(pe.cov_Obs(y[i], yerr[i]**2, 'cov' + str(i)))

    outc = pe.least_squares(x, oyc, func)
    betac = outc.fit_parameters

    for i in range(2):
        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)
예제 #8
0
def test_total_least_squares():
    dim = 10 + int(30 * np.random.rand())
    x = np.arange(dim) + np.random.normal(0.0, 0.15, dim)
    xerr = 0.1 + 0.1 * np.random.rand(dim)
    y = 2 * np.exp(-0.06 * x) + np.random.normal(0.0, 0.15, dim)
    yerr = 0.1 + 0.1 * np.random.rand(dim)

    ox = []
    for i, item in enumerate(x):
        ox.append(pe.pseudo_Obs(x[i], xerr[i], str(i)))

    oy = []
    for i, item in enumerate(x):
        oy.append(pe.pseudo_Obs(y[i], yerr[i], str(i)))

    def f(x, a, b):
        return a * np.exp(-b * x)

    def func(a, x):
        y = a[0] * np.exp(-a[1] * x)
        return y

    data = RealData([o.value for o in ox], [o.value for o in oy],
                    sx=[o.dvalue for o in ox],
                    sy=[o.dvalue for o in oy])
    model = Model(func)
    odr = ODR(data, model, [0, 0], partol=np.finfo(np.float64).eps)
    odr.set_job(fit_type=0, deriv=1)
    output = odr.run()

    out = pe.total_least_squares(ox, oy, func, expected_chisquare=True)
    beta = out.fit_parameters

    str(out)
    repr(out)
    len(out)

    for i in range(2):
        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)

    out = pe.total_least_squares(ox, oy, func, const_par=[beta[1]])

    diff = out.fit_parameters[0] - beta[0]
    assert (diff / beta[0] < 1e-3 * beta[0].dvalue)
    assert ((out.fit_parameters[1] - beta[1]).is_zero())

    oxc = []
    for i, item in enumerate(x):
        oxc.append(pe.cov_Obs(x[i], xerr[i]**2, 'covx' + str(i)))

    oyc = []
    for i, item in enumerate(x):
        oyc.append(pe.cov_Obs(y[i], yerr[i]**2, 'covy' + str(i)))

    outc = pe.total_least_squares(oxc, oyc, func)
    betac = outc.fit_parameters

    for i in range(2):
        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)

    outc = pe.total_least_squares(oxc, oyc, func, const_par=[betac[1]])

    diffc = outc.fit_parameters[0] - betac[0]
    assert (diffc / betac[0] < 1e-3 * betac[0].dvalue)
    assert ((outc.fit_parameters[1] - betac[1]).is_zero())

    outc = pe.total_least_squares(oxc, oy, func)
    betac = outc.fit_parameters

    for i in range(2):
        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)

    outc = pe.total_least_squares(oxc, oy, func, const_par=[betac[1]])

    diffc = outc.fit_parameters[0] - betac[0]
    assert (diffc / betac[0] < 1e-3 * betac[0].dvalue)
    assert ((outc.fit_parameters[1] - betac[1]).is_zero())