def arima_model(vEndog, mExog=None, tPDQ=None): """ Fits an ARIMA model. Order can be specified or determined by auto_arima. Differently from other models, it does not work on patsy/R formula syntax. :param vEndog: DataFrame column/numpy vector containing endogenous data (which will be regressed upon itself) :param mExog: vector/matrix containing exogenous data. Defaults to None :param tPDQ: tuple (p, d, q) containing order of the model; p: number of autorregressions (AR) q: number of differentiations (I) q: number of past prevision errors/moving averages (MA) If None (default), performs an auto_arima() :return mod: fitted model instance """ ## Creating model # If order is specified if tPDQ is not None: # Conditional on whether there are exogenous variables if mExog is None: mod_arima = ARIMA(endog=vEndog, order=tPDQ).fit(cov_type='robust') else: mod_arima = ARIMA(endog=vEndog, exog=mExog, order=tPDQ).fit(cov_type='robust') # If order isn't specified, use auto_arima() else: mod_arima = auto_arima(y=vEndog, X=mExog) mod_arima = mod_arima.fit(y=vEndog, cov_type='robust') ## Printing summary and diagnostics print(mod_arima.summary()) print("For heteroskdasticity, check Prob(H), where H0: homoskedasticity, and the standardized residual graph.") print("If there is hetero., the model error can't be a white noise (which is the desired thing).") print("Estimaed Density and Jarque-Bera have information on normality.") print("In the correlogram, all lollipops must be inside of the shaded area.") # Plots mod_arima.plot_diagnostics(figsize=(10, 10)) plt.show() # Residual means tMean0 = stats.ttest_1samp(mod_arima.resid(), 0, nan_policy='omit') print(f"P-value for the test that residual mean is equal to 0: {np.around(tMean0[1], 5)}.") print("If p < 0.05, H0 is rejected and the residual mean is different from 0 (not ideal).") ## Returning return mod_arima
def arima( other_args: List[str], stock: pd.Series, ): """Arima model Parameters ---------- other_args : str Command line arguments to be processed with argparse stock : pd.Series Stock data Returns ---------- model_name : str Command line arguments to be processed with argparse model : pd.Series Model series residuals : List[float] Residuals from model fitting stock data """ parser = argparse.ArgumentParser( add_help=False, prog="arima", description=""" ARIMA model """, ) parser.add_argument( "-i", "--ic", action="store", dest="s_ic", type=str, default="aic", choices=["aic", "aicc", "bic", "hqic", "oob"], help="information criteria.", ) parser.add_argument( "-s", "--seasonal", action="store_true", default=False, dest="b_seasonal", help="Use weekly seasonal data.", ) parser.add_argument( "-o", "--order", action="store", dest="s_order", type=str, help="arima model order (p,d,q) in format: pdq.", ) try: ns_parser = parse_known_args_and_warn(parser, other_args) if not ns_parser: return "", None, list() if ns_parser.s_order: t_order = tuple(int(ord) for ord in list(ns_parser.s_order)) model_fit = ARIMA(stock.values, order=t_order).fit() model = pd.Series(model_fit.fittedvalues[1:], index=stock.index[1:]) model_name = f"ARIMA {t_order}" residuals = model_fit.resid else: if ns_parser.b_seasonal: model_fit = pmdarima.auto_arima( stock.values, error_action="ignore", seasonal=True, m=5, information_criteria=ns_parser.s_ic, ) else: model_fit = pmdarima.auto_arima( stock.values, error_action="ignore", seasonal=False, information_criteria=ns_parser.s_ic, ) model = pd.Series(model_fit.predict_in_sample()[1:], index=stock.index[1:]) model_name = f"ARIMA {model_fit.order}" residuals = model_fit.resid() return model_name, model, residuals except Exception as e: print(e, "\n") return "", None, list()