Example #1
0
def roll_prediction_egarch(return_tr, vol_tr, return_ts, vol_ts,
                           training_order, method):

    roll_x = return_tr
    vol_hat = []

    for i in range(len(vol_ts)):

        print 'now processing', i

        tmp_x = roll_x[-training_order:]

        if method == 'garch':
            model = pf.GARCH(tmp_x, p=1, q=1)
            x = model.fit()

        elif method == 'egarch':
            model = pf.EGARCH(tmp_x, p=1, q=1)
            x = model.fit()

        vol_hat.append(np.asarray(model.predict(1))[0][0])
        roll_x = np.concatenate((roll_x, return_ts[i:i + 1]))

    # return rooted mse
    return vol_hat, sqrt(
        mean((vol_ts - np.asarray(vol_hat)) * (vol_ts - np.asarray(vol_hat))))
Example #2
0
def test_lev_bbvi():
    model = pf.EGARCH(data=data, p=1, q=1)
    model.add_leverage()
    x = model.fit('BBVI', iterations=100)
    assert (len(model.latent_variables.z_list) == 6)
    lvs = np.array([i.value for i in model.latent_variables.z_list])
    assert (len(lvs[np.isnan(lvs)]) == 0)
Example #3
0
def test_lev_predict_nans():
    model = pf.EGARCH(data=data, p=2, q=2)
    model.add_leverage()
    x = model.fit()
    x.summary()
    assert (len(
        model.predict(h=5).values[np.isnan(model.predict(h=5).values)]) == 0)
Example #4
0
def test_predict_is_nans():
    model = pf.EGARCH(data=data, q=2, p=2)
    x = model.fit()
    x.summary()
    assert (len(
        model.predict_is(h=5).values[np.isnan(
            model.predict_is(h=5).values)]) == 0)
Example #5
0
def test_lev_sample_model():
    model = pf.EGARCH(data=data, q=2, p=2)
    model.add_leverage()
    x = model.fit('BBVI', iterations=100)
    sample = model.sample(nsims=100)
    assert (sample.shape[0] == 100)
    assert (sample.shape[1] == len(data) - 2)
Example #6
0
def test_lev_couple_terms():
    model = pf.EGARCH(data=data, p=1, q=1)
    model.add_leverage()
    x = model.fit()
    assert (len(model.latent_variables.z_list) == 6)
    lvs = np.array([i.value for i in model.latent_variables.z_list])
    assert (len(lvs[np.isnan(lvs)]) == 0)
Example #7
0
def test_bbvi_mini_batch_elbo():
    model = pf.EGARCH(data=data, p=1, q=1)
    x = model.fit('BBVI',
                  iterations=300,
                  map_start=False,
                  mini_batch=32,
                  record_elbo=True)
    assert (x.elbo_records[-1] > x.elbo_records[0])
Example #8
0
def test_predict_is_intervals_mh():
    model = pf.EGARCH(data=data, q=1, p=1)
    x = model.fit('M-H', nsims=400)
    predictions = model.predict_is(h=10, intervals=True)
    assert (np.all(predictions['99% Prediction Interval'].values +
                   0.000001 >= predictions['95% Prediction Interval'].values))
    assert (np.all(predictions['95% Prediction Interval'].values +
                   0.000001 >= predictions['5% Prediction Interval'].values))
    assert (np.all(predictions['5% Prediction Interval'].values +
                   0.000001 >= predictions['1% Prediction Interval'].values))
Example #9
0
def test_predict_is_intervals():
    model = pf.EGARCH(data=data, q=2, p=2)
    x = model.fit()
    predictions = model.predict_is(h=10, intervals=True)
    assert (np.all(predictions['99% Prediction Interval'].values +
                   0.000001 >= predictions['95% Prediction Interval'].values))
    assert (np.all(predictions['95% Prediction Interval'].values +
                   0.000001 >= predictions['5% Prediction Interval'].values))
    assert (np.all(predictions['5% Prediction Interval'].values +
                   0.000001 >= predictions['1% Prediction Interval'].values))
Example #10
0
def test_lev_predict_is_intervals_bbvi():
    model = pf.EGARCH(data=data, q=1, p=1)
    model.add_leverage()
    x = model.fit('BBVI', iterations=100)
    predictions = model.predict_is(h=10, intervals=True)
    assert (np.all(predictions['99% Prediction Interval'].values +
                   0.000001 >= predictions['95% Prediction Interval'].values))
    assert (np.all(predictions['95% Prediction Interval'].values +
                   0.000001 >= predictions['5% Prediction Interval'].values))
    assert (np.all(predictions['5% Prediction Interval'].values +
                   0.000001 >= predictions['1% Prediction Interval'].values))
Example #11
0
def oneshot_prediction_egarch(return_tr, vol_tr, return_ts, vol_ts, method):

    if method == 'garch1':
        model = pf.GARCH(return_tr, p=1, q=1)
        x = model.fit()

        tr_sigma2, _, ___ = model._model(model.latent_variables.get_z_values())
        vol_tr_hat = tr_sigma2**0.5

    elif method == 'egarch1':
        model = pf.EGARCH(return_tr, p=1, q=1)
        x = model.fit()

        tr_sigma2, _, ___ = model._model(model.latent_variables.get_z_values())
        vol_tr_hat = np.exp(tr_sigma2 / 2.0)

    tmp_pre = np.asarray(model.predict(len(vol_ts)))
    vol_ts_hat = []
    for i in tmp_pre:
        vol_ts_hat.append(i[0])

    return vol_ts_hat, vol_tr_hat, sqrt(mean((vol_ts - np.asarray(vol_ts_hat))*(vol_ts - np.asarray(vol_ts_hat)))), \
sqrt(mean((vol_tr[1:] - np.asarray(vol_tr_hat))*(vol_tr[1:] - np.asarray(vol_tr_hat))))
Example #12
0
def test_predict_is_length():
    model = pf.EGARCH(data=data, p=2, q=2)
    x = model.fit()
    assert (model.predict_is(h=5).shape[0] == 5)
Example #13
0
def test_pml():
    model = pf.EGARCH(data=data, p=1, q=1)
    x = model.fit('PML')
    assert (len(model.latent_variables.z_list) == 5)
    lvs = np.array([i.value for i in model.latent_variables.z_list])
    assert (len(lvs[np.isnan(lvs)]) == 0)
Example #14
0
def test_lev_predict_length():
    model = pf.EGARCH(data=data, p=2, q=2)
    model.add_leverage()
    x = model.fit()
    x.summary()
    assert (model.predict(h=5).shape[0] == 5)
Example #15
0
def test_no_terms():
    model = pf.EGARCH(data=data, p=0, q=0)
    x = model.fit()
    assert (len(model.latent_variables.z_list) == 3)
    lvs = np.array([i.value for i in model.latent_variables.z_list])
    assert (len(lvs[np.isnan(lvs)]) == 0)
Example #16
0
def test_lev_bbvi_elbo():
    model = pf.EGARCH(data=data, p=1, q=1)
    model.add_leverage()
    x = model.fit('BBVI', iterations=300, map_start=False, record_elbo=True)
    assert (x.elbo_records[-1] > x.elbo_records[0])
 def bte(self, topic_index=0):
     model = pf.EGARCH(self.topic_counts[topic_index], p=1, q=1)
     x = model.fit()
     x.summary()
     model.plot_fit()
Example #18
0
def test_predict_nonconstant():
    model = pf.EGARCH(data=data, p=2, q=2)
    x = model.fit()
    predictions = model.predict(h=10, intervals=False)
    assert (not np.all(predictions.values == predictions.values[0]))
Example #19
0
def test_lev_predict_is_nonconstant():
    model = pf.EGARCH(data=data, p=2, q=2)
    model.add_leverage()
    x = model.fit()
    predictions = model.predict_is(h=5, intervals=False)
    assert (not np.all(predictions.values == predictions.values[0]))
Example #20
0
def test_lev_ppc():
    model = pf.EGARCH(data=data, q=2, p=2)
    model.add_leverage()
    x = model.fit('BBVI', iterations=100)
    p_value = model.ppc()
    assert (0.0 <= p_value <= 1.0)
Example #21
0
def test_bbvi_mini_batch():
    model = pf.EGARCH(data=data, p=1, q=1)
    x = model.fit('BBVI', iterations=100, map_start=False, mini_batch=32)
    assert (len(model.latent_variables.z_list) == 5)
    lvs = np.array([i.value for i in model.latent_variables.z_list])
    assert (len(lvs[np.isnan(lvs)]) == 0)