From 678364e0347c97f8dc1bbd46657342d288e71a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Morales?= Date: Tue, 23 Jan 2024 18:21:19 -0600 Subject: [PATCH] Address division by zero warnings in arima (#770) --- nbs/src/arima.ipynb | 10 +++++++--- statsforecast/arima.py | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/nbs/src/arima.ipynb b/nbs/src/arima.ipynb index c64c6ca2b..3655b2754 100644 --- a/nbs/src/arima.ipynb +++ b/nbs/src/arima.ipynb @@ -1258,8 +1258,9 @@ " x -= np.dot(xreg, par[narma + np.arange(ncxreg)])\n", " \n", " res, resid = arima_css(x, arma, phi, theta, ncond)\n", - " \n", - " return 0.5 * np.log(res)\n", + " if res == 0.0:\n", + " return math.inf\n", + " return 0.5 * math.log(res)\n", " \n", " coef = np.array(fixed)\n", " # parscale definition, think about it, scipy doesn't use it\n", @@ -1788,7 +1789,10 @@ " fit['aic'] = offset + nstar * math.log(fit['sigma2']) + 2 * npar\n", " if not math.isnan(fit['aic']):\n", " fit['bic'] = fit['aic'] + npar * (math.log(nstar) - 2)\n", - " fit['aicc'] = fit['aic'] + 2 * npar * (npar + 1) / (nstar - npar - 1)\n", + " if nstar - npar - 1 != 0:\n", + " fit['aicc'] = fit['aic'] + 2 * npar * (npar + 1) / (nstar - npar - 1)\n", + " else:\n", + " fit['aicc'] = math.inf\n", " fit['ic'] = fit[ic]\n", " else:\n", " fit['ic'] = fit['aic'] = fit['bic'] = fit['aicc'] = math.inf\n", diff --git a/statsforecast/arima.py b/statsforecast/arima.py index fc79f09b8..64e1f5c47 100644 --- a/statsforecast/arima.py +++ b/statsforecast/arima.py @@ -890,8 +890,9 @@ def arma_css_op(p, x): x -= np.dot(xreg, par[narma + np.arange(ncxreg)]) res, resid = arima_css(x, arma, phi, theta, ncond) - - return 0.5 * np.log(res) + if res == 0.0: + return math.inf + return 0.5 * math.log(res) coef = np.array(fixed) # parscale definition, think about it, scipy doesn't use it @@ -1243,7 +1244,10 @@ def myarima( fit["aic"] = offset + nstar * math.log(fit["sigma2"]) + 2 * npar if not math.isnan(fit["aic"]): fit["bic"] = fit["aic"] + npar * (math.log(nstar) - 2) - fit["aicc"] = fit["aic"] + 2 * npar * (npar + 1) / (nstar - npar - 1) + if nstar - npar - 1 != 0: + fit["aicc"] = fit["aic"] + 2 * npar * (npar + 1) / (nstar - npar - 1) + else: + fit["aicc"] = math.inf fit["ic"] = fit[ic] else: fit["ic"] = fit["aic"] = fit["bic"] = fit["aicc"] = math.inf