示例#1
0
def test_mfdcca_save_load():
    mfdcca = fathon.MFDCCA(fu.toAggregated(ts), fu.toAggregated(ts))
    # save and load with empty results
    mfdcca.saveObject(get_object_path('mfdcca_obj'))
    n_load = fu.getObjectMember(get_object_path('mfdcca_obj', ext=True), 'n')
    F_load = fu.getObjectMember(get_object_path('mfdcca_obj', ext=True), 'F')
    q_list_load = fu.getObjectMember(get_object_path('mfdcca_obj', ext=True),
                                     'qList')
    list_h_load = fu.getObjectMember(get_object_path('mfdcca_obj', ext=True),
                                     'listH')
    assert np.array_equal(n_load, [])
    assert np.array_equal(F_load, [])
    assert np.array_equal(q_list_load, [])
    assert np.array_equal(list_h_load, [])
    #save and load with results
    n, F = mfdcca.computeFlucVec(fu.linRangeByStep(10, 500),
                                 fu.linRangeByStep(-1, 1))
    H, I = mfdcca.fitFlucVec(100, 300)
    mfdcca.saveObject(get_object_path('mfdcca_obj'))
    n_load = fu.getObjectMember(get_object_path('mfdcca_obj', ext=True), 'n')
    F_load = fu.getObjectMember(get_object_path('mfdcca_obj', ext=True), 'F')
    q_list_load = fu.getObjectMember(get_object_path('mfdcca_obj', ext=True),
                                     'qList')
    list_h_load = fu.getObjectMember(get_object_path('mfdcca_obj', ext=True),
                                     'listH')
    assert np.array_equal(n_load, n)
    assert np.array_equal(F_load, F)
    assert np.array_equal(q_list_load, fu.linRangeByStep(-1, 1))
    assert np.array_equal(list_h_load, H)
    # MFDFA from file
    mfdcca_2 = fathon.MFDCCA(get_object_path('mfdcca_obj', ext=True))
    H_2, I_2 = mfdcca_2.fitFlucVec(100, 300)
    assert np.array_equal(H_2, H)
    assert np.array_equal(I_2, I)
示例#2
0
def test_mat_dfa_mf():
    mf_dfa = fathon.DFA(fu.toAggregated(mf))
    n_mf, F_mf = mf_dfa.computeFlucVec(scales, revSeg=False, polOrd=1)
    idxs = get_idxs(n_mf, scales)
    n_mf = n_mf[idxs]
    F_mf = F_mf[idxs]
    H, _ = np.polyfit(np.log2(n_mf), np.log2(F_mf), 1)
    assert math.isclose(H, 0.77, rel_tol=1e-2, abs_tol=0)
示例#3
0
def test_mat_dfa_wn():
    w_dfa = fathon.DFA(fu.toAggregated(wn))
    n_w, F_w = w_dfa.computeFlucVec(scales, revSeg=False, polOrd=1)
    idxs = get_idxs(n_w, scales)
    n_w = n_w[idxs]
    F_w = F_w[idxs]
    H, _ = np.polyfit(np.log2(n_w), np.log2(F_w), 1)
    assert math.isclose(H, 0.45, rel_tol=1e-2, abs_tol=0)
示例#4
0
def test_ht_save_load():
    ht = fathon.HT(fu.toAggregated(ts))
    # save and load with empty results
    ht.saveObject(get_object_path('ht_obj'))
    ht_load = fu.getObjectMember(get_object_path('ht_obj', ext=True), 'ht')
    assert np.array_equal(ht_load, [])
    #save and load with results
    ht_mtx = ht.computeHt([100, 300])
    ht.saveObject(get_object_path('ht_obj'))
    ht_load = fu.getObjectMember(get_object_path('ht_obj', ext=True), 'ht')
    assert np.array_equal(ht_load, ht_mtx)
示例#5
0
def test_mat_mfdfa_wn():
    w_mfdfa = fathon.MFDFA(fu.toAggregated(wn))
    n_w, F_w = w_mfdfa.computeFlucVec(scales,
                                      qList=q_list,
                                      revSeg=False,
                                      polOrd=1)
    idxs = get_idxs(n_w, scales)
    n_w = n_w[idxs]
    F_w_vec = np.zeros((len(q_list), len(idxs)))
    for i in range(len(q_list)):
        F_w_vec[i] = F_w[i, idxs]
    Hq = []
    for i in range(len(q_list)):
        Hq.append(np.polyfit(np.log2(n_w), np.log2(F_w_vec[i]), 1)[0])
    np.testing.assert_allclose(
        Hq, [0.4583, 0.4555, 0.4546, 0.4515, 0.4445, 0.4340],
        rtol=1e-4,
        atol=0)
示例#6
0
def test_mat_mfdfa_mf():
    mf_mfdfa = fathon.MFDFA(fu.toAggregated(mf))
    n_mf, F_mf = mf_mfdfa.computeFlucVec(scales,
                                         qList=q_list,
                                         revSeg=False,
                                         polOrd=1)
    idxs = get_idxs(n_mf, scales)
    n_mf = n_mf[idxs]
    F_mf_vec = np.zeros((len(q_list), len(idxs)))
    for i in range(len(q_list)):
        F_mf_vec[i] = F_mf[i, idxs]
    Hq = []
    for i in range(len(q_list)):
        Hq.append(np.polyfit(np.log2(n_mf), np.log2(F_mf_vec[i]), 1)[0])
    np.testing.assert_allclose(
        Hq, [1.4477, 1.3064, 1.0823, 0.8846, 0.6606, 0.5174],
        rtol=1e-4,
        atol=0)
示例#7
0
def test_mat_mfdfa_mn():
    mn_mfdfa = fathon.MFDFA(fu.toAggregated(mn))
    n_mn, F_mn = mn_mfdfa.computeFlucVec(scales,
                                         qList=q_list,
                                         revSeg=False,
                                         polOrd=1)
    idxs = get_idxs(n_mn, scales)
    n_mn = n_mn[idxs]
    F_mn_vec = np.zeros((len(q_list), len(idxs)))
    for i in range(len(q_list)):
        F_mn_vec[i] = F_mn[i, idxs]
    Hq = []
    for i in range(len(q_list)):
        Hq.append(np.polyfit(np.log2(n_mn), np.log2(F_mn_vec[i]), 1)[0])
    np.testing.assert_allclose(
        Hq, [0.7542, 0.7392, 0.7301, 0.7240, 0.7149, 0.7023],
        rtol=1e-4,
        atol=0)
示例#8
0
def test_dfa_save_load():
    dfa = fathon.DFA(fu.toAggregated(ts))
    # save and load with empty results
    dfa.saveObject(get_object_path('dfa_obj'))
    n_load = fu.getObjectMember(get_object_path('dfa_obj', ext=True), 'n')
    F_load = fu.getObjectMember(get_object_path('dfa_obj', ext=True), 'F')
    assert np.array_equal(n_load, [])
    assert np.array_equal(F_load, [])
    #save and load with results
    n, F = dfa.computeFlucVec(fu.linRangeByStep(10, 500))
    H, I = dfa.fitFlucVec(100, 300)
    dfa.saveObject(get_object_path('dfa_obj'))
    n_load = fu.getObjectMember(get_object_path('dfa_obj', ext=True), 'n')
    F_load = fu.getObjectMember(get_object_path('dfa_obj', ext=True), 'F')
    assert np.array_equal(n_load, n)
    assert np.array_equal(F_load, F)
    # DFA from file
    dfa_2 = fathon.DFA(get_object_path('dfa_obj', ext=True))
    H_2, I_2 = dfa_2.fitFlucVec(100, 300)
    assert H_2 == H
    assert I_2 == I
示例#9
0
# REGRESSION TESTS
# ----------------
# - the element of the array tested in tests 2-5,7,8 has no
#   particular meaning, it was just chosen randomly.
# - the numbers to which results are tested against have
#   been produced by fathon and have many significant figures
#   due to the fact that the precision of the testing machine
#   is not known.

#####
# Time series for regression tests
#####
# co2 residuals after yearly cycle removal
ts1 = np.loadtxt(os.path.join(TESTS_PATH, 'ts1.txt'))
ts1 = fu.toAggregated(ts1)
# other co2 residuals after yearly cycle removal
ts2 = np.loadtxt(os.path.join(TESTS_PATH, 'ts2.txt'))
ts2 = fu.toAggregated(ts2)
# multifractal data
ts3 = np.loadtxt(os.path.join(TESTS_PATH, 'ts3.txt'))
ts3 = fu.toAggregated(ts3)

#####


#####
# Regression test 1
# It tests if the Hurst exponent of `ts1` is correct
#####
def test_dfa_lin_step():
        tick_bar_df = testClass.get_concat_data(testClass._bars_dict)['tick_bars']
        volume_bar_df = testClass.get_concat_data(testClass._bars_dict)['volume_bars']
        usd_volume_bar_df = testClass.get_concat_data(testClass._bars_dict)['usd_volume_bars']
        calendar_bar_df = testClass.get_concat_data(testClass._bars_dict)['calendar_bars']
        vr = returns(volume_bar_df.micro_price_close).replace([np.inf, -np.inf], 0)  # volume
        tr = returns(tick_bar_df.micro_price_close).replace([np.inf, -np.inf], 0)  # tick
        dr = returns(usd_volume_bar_df.micro_price_close).dropna().replace([np.inf, -np.inf], 0)  # usd volume
        df_ret = returns(calendar_bar_df.micro_price_close).dropna().replace([np.inf, -np.inf], 0)  # calendar
        bar_returns[date] = {'tick': tr,
                             'volume': vr,
                             'dollar': dr,
                             'calendar': df_ret}

    for j, i in itertools.product(['tick', 'volume', 'dollar', 'calendar'], dates):
        data = (bar_returns[i][j])
        a = fu.toAggregated(np.asanyarray(data))
        # MFDFA Computations
        pymfdfa = fathon.MFDFA(a)
        n, F = pymfdfa.computeFlucVec(winSizes, qs, revSeg=revSeg, polOrd=polOrd)

        mfdfa_n_F_dict[j][i] = dict(zip(n, F))
        # dictionary to match all the n and F values this could be
        # more efficient

        # get the list values of H and intercept
        list_H, list_H_intercept = pymfdfa.fitFlucVec()  # same for H values
        mfdfa_H_dict[j][i] = [list_H, list_H_intercept]

        # get the mass exponents
        tau = pymfdfa.computeMassExponents()
        mfdfa_tau_dict[j][i] = tau
示例#11
0
def test_dcca_save_load():
    dcca = fathon.DCCA(fu.toAggregated(ts), fu.toAggregated(ts))
    # save and load with empty results
    dcca.saveObject(get_object_path('dcca_obj'))
    n_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True), 'n')
    F_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True), 'F')
    n_rho_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                    'nRho')
    rho_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True), 'rho')
    n_thr_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                    'nThr')
    conf_up_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                      'confUp')
    conf_down_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                        'confDown')
    assert np.array_equal(n_load, [])
    assert np.array_equal(F_load, [])
    assert np.array_equal(n_rho_load, [])
    assert np.array_equal(rho_load, [])
    assert np.array_equal(n_thr_load, [])
    assert np.array_equal(conf_up_load, [])
    assert np.array_equal(conf_down_load, [])
    #save and load with results
    n, F = dcca.computeFlucVec(fu.linRangeByStep(10, 200))
    H, I = dcca.fitFlucVec(100, 150)
    dcca.saveObject(get_object_path('dcca_obj'))
    n_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True), 'n')
    F_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True), 'F')
    n_rho_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                    'nRho')
    rho_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True), 'rho')
    n_thr_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                    'nThr')
    conf_up_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                      'confUp')
    conf_down_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                        'confDown')
    assert np.array_equal(n_load, n)
    assert np.array_equal(F_load, F)
    assert np.array_equal(n_rho_load, [])
    assert np.array_equal(rho_load, [])
    assert np.array_equal(n_thr_load, [])
    assert np.array_equal(conf_up_load, [])
    assert np.array_equal(conf_down_load, [])
    # DCCA from file
    dcca_2 = fathon.DCCA(get_object_path('dcca_obj', ext=True))
    H_2, I_2 = dcca_2.fitFlucVec(100, 150)
    assert np.array_equal(H_2, H)
    assert np.array_equal(I_2, I)
    # rho
    n_rho, rho = dcca.computeRho(fu.linRangeByStep(10, 30))
    dcca.saveObject(get_object_path('dcca_obj'))
    n_rho_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                    'nRho')
    rho_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True), 'rho')
    n_thr_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                    'nThr')
    conf_up_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                      'confUp')
    conf_down_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                        'confDown')
    assert np.array_equal(n_load, n)
    assert np.array_equal(F_load, F)
    assert np.array_equal(n_rho_load, n_rho)
    assert np.array_equal(rho_load, rho)
    assert np.array_equal(n_thr_load, [])
    assert np.array_equal(conf_up_load, [])
    assert np.array_equal(conf_down_load, [])
    # thresholds
    n_thr, conf_up, conf_down = dcca.rhoThresholds(len(ts),
                                                   fu.linRangeByStep(10, 30),
                                                   10, 0.95)
    dcca.saveObject(get_object_path('dcca_obj'))
    n_thr_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                    'nThr')
    conf_up_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                      'confUp')
    conf_down_load = fu.getObjectMember(get_object_path('dcca_obj', ext=True),
                                        'confDown')
    assert np.array_equal(n_load, n)
    assert np.array_equal(F_load, F)
    assert np.array_equal(n_rho_load, n_rho)
    assert np.array_equal(rho_load, rho)
    assert np.array_equal(n_thr_load, n_thr)
    assert np.array_equal(conf_up_load, conf_up)
    assert np.array_equal(conf_down_load, conf_down)
示例#12
0
import numpy as np
import matplotlib.pyplot as plt
import fathon
from fathon import fathonUtils as fu

print('This is fathon v{}'.format(fathon.__version__))

a = np.random.randn(10000)
b = np.random.randn(10000)

a = fu.toAggregated(a)
b = fu.toAggregated(b)

pymfdcca = fathon.MFDCCA(a, b)

winSizes = fu.linRangeByStep(10, 2000)
qs = np.arange(-3, 4, 0.1)
revSeg = True
polOrd = 1

n, F = pymfdcca.computeFlucVec(winSizes, qs, revSeg=revSeg, polOrd=polOrd)

list_H, list_H_intercept = pymfdcca.fitFlucVec()

plt.plot(np.log(n), np.log(F[0, :]), 'ro')
plt.plot(np.log(n),
         list_H_intercept[0] + list_H[0] * np.log(n),
         'k-',
         label='h_{:.1f} = {:.2f}'.format(qs[0], list_H[0]))
half_idx = int(len(qs) / 2)
plt.plot(np.log(n), np.log(F[half_idx, :]), 'co')