Example #1
0
def test_ar_select_order():
    # GH#2118
    np.random.seed(12345)
    y = sm.tsa.arma_generate_sample([1, -.75, .3], [1], 100)
    ts = Series(y, index=date_range(start='1/1/1990', periods=100, freq='M'))
    ar = AR(ts)
    res = ar.select_order(maxlag=12, ic='aic')
    assert_(res == 2)
Example #2
0
def test_ar_select_order():
    # 2118
    np.random.seed(12345)
    y = sm.tsa.arma_generate_sample([1, -0.75, 0.3], [1], 100)
    ts = Series(y, index=DatetimeIndex(start="1/1/1990", periods=100, freq="M"))
    ar = AR(ts)
    res = ar.select_order(maxlag=12, ic="aic")
    assert_(res == 2)
Example #3
0
def test_ar_select_order_tstat():
    rs = np.random.RandomState(123)
    tau = 25
    y = rs.randn(tau)
    ts = Series(y, index=date_range(start='1/1/1990', periods=tau, freq='M'))

    ar = AR(ts)
    res = ar.select_order(maxlag=5, ic='t-stat')
    assert_equal(res, 0)
Example #4
0
def test_ar_select_order():
    # 2118
    np.random.seed(12345)
    y = sm.tsa.arma_generate_sample([1, -.75, .3], [1], 100)
    ts = TimeSeries(y, index=DatetimeIndex(start='1/1/1990', periods=100,
                                           freq='M'))
    ar = AR(ts)
    res = ar.select_order(maxlag=12, ic='aic')
    assert_(res == 2)
def test_ar_select_order_tstat():
    rs = np.random.RandomState(123)
    tau = 25
    y = rs.randn(tau)
    ts = Series(y, index=date_range(start="1/1/1990", periods=tau, freq="M"))
    with pytest.warns(FutureWarning):
        ar = AR(ts)
    with pytest.warns(FutureWarning):
        res = ar.select_order(maxlag=5, ic="t-stat")
    assert_equal(res, 0)
def test_ar_select_order():
    # GH#2118
    np.random.seed(12345)
    y = sm.tsa.arma_generate_sample([1, -0.75, 0.3], [1], 100)
    ts = Series(y, index=date_range(start="1/1/1990", periods=100, freq="M"))
    with pytest.warns(FutureWarning):
        ar = AR(ts)
    with pytest.warns(FutureWarning):
        res = ar.select_order(maxlag=12, ic="aic")
    assert res == 2
Example #7
0
def test_ar_select_order_tstat():
    rs = np.random.RandomState(123)
    tau = 25
    y = rs.randn(tau)
    ts = Series(y, index=date_range(start='1/1/1990', periods=tau,
                                    freq='M'))

    ar = AR(ts)
    res = ar.select_order(maxlag=5, ic='t-stat')
    assert_equal(res, 0)
Example #8
0
def AR_model(log_returns, tickers_with_AR):
    print("AR_model() start execute")
    tickers = tickers_with_AR
    #log_returns = to_log_return(data)
    summary = {}
    for ticker in tickers:
        print("AR_model() start execute in ticker: " + ticker)
        log_rtn = log_returns[ticker].dropna()
        model = AR(log_rtn)
        best_order = model.select_order(maxlag=LAGS, ic='aic')
        result = model.fit(best_order)
        aic = result.aic
        summary[ticker] = [best_order, aic, 'AR']
    with open('results/AR_models.txt', 'w') as results_file:
        for k in summary.keys():
            message = "Ticker: {} \t\t\tBest order: {} \tAIC = {}\n".format(
                k, summary[k][0], summary[k][1])
            results_file.write(message)
    return summary
Example #9
0
class VarModel(BaseModel):
    def __init__(self, feat_id, run_id, data=None):
        self.model_type = 'VAR'
        self.opt_p = 1
        super().__init__(feat_id, run_id, data)

    def train(self, data):
        if len(data.columns) > 1:
            self.model = VAR(data)
            self.opt_p = self.model.select_order(30).aic
        else:
            self.model = AR(data)
            self.opt_p = self.model.select_order(30, 'aic')

    def save(self):
        joblib.dump(
            self.opt_p,
            os.path.join(
                'temp', self.run_id, 'models', 'VAR', '{}_VAR.pkl'.format(
                    replace_multiple(
                        self.feat_id,
                        ['/', '\\', ':', '?', '*', '"', '<', '>', '|'], "x"))))

    def load(self):
        self.opt_p = \
            joblib.load(os.path.join('runs', self.run_id, 'models', 'VAR',
                                     '{}_VAR.pkl'.format(replace_multiple(self.feat_id,
                                                                          ['/', '\\',
                                                                           ':', '?',
                                                                           '*', '"',
                                                                           '<', '>',
                                                                           '|'],
                                                                          "x"))))

    def result(self, history, actual, prediction, forecast, anomaly_scores):
        mse = mean_squared_error(actual, prediction)
        mae = mean_absolute_error(actual, prediction)
        rmse = np.sqrt(mse)

        anomaly_scores['points'] = anomaly_scores.index

        future_alert = anomaly_scores.tail(len(forecast))
        past_alert = anomaly_scores.iloc[:len(history)]
        future_alert = future_alert[future_alert['outlier'] == -1]
        past_alert = past_alert[past_alert['outlier'] == -1]

        output = {
            'history': history.tolist(),
            'expected': prediction.tolist(),
            'forecast': forecast.tolist(),
            'rmse': rmse,
            'mse': mse,
            'mae': mae,
            'future_alerts': future_alert.fillna(0).to_dict(orient='record'),
            'past_alerts': past_alert.fillna(0).to_dict(orient='record'),
            'model': self.model_type
        }
        return output

    def predict(self, data, start_idx, end_idx):
        if len(data.columns) > 1:
            self.model = VAR(data)
            result = self.model.fit(self.opt_p)
            y_pred = self.model.predict(result.params,
                                        start=start_idx,
                                        end=end_idx,
                                        lags=self.opt_p)
            return pd.DataFrame(data=y_pred, columns=data.columns.values)
        else:
            self.model = AR(data)
            self.model = self.model.fit(self.opt_p)
            y_pred = self.model.predict(start=start_idx, end=end_idx)
            return pd.DataFrame(data=y_pred, columns=data.columns.values)
def orderSelection(a):
    model = AR(a)
    maxlag = 28
    order = model.select_order(maxlag=maxlag, ic='aic', trend='nc')
    return order
Example #11
-1
def test_ar_select_order_tstat():
    rs = np.random.RandomState(123)
    tau = 25
    y = rs.randn(tau)
    ts = Series(y, index=DatetimeIndex(start="1/1/1990", periods=tau, freq="M"))

    ar = AR(ts)
    res = ar.select_order(maxlag=5, ic="t-stat")
    assert_equal(res, 0)