Skip to content

Commit

Permalink
Address division by zero warnings in arima (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoralez authored Jan 24, 2024
1 parent d89f4b7 commit 678364e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions nbs/src/arima.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 7 additions & 3 deletions statsforecast/arima.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 678364e

Please sign in to comment.