Skip to content

Commit

Permalink
add one in remaining place and remove ols check
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoralez committed Dec 19, 2023
1 parent 229ba2d commit fdfb4b7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
24 changes: 17 additions & 7 deletions nbs/src/arima.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1217,12 +1217,9 @@
" if seasonal['period'] > 1 and seasonal['order'][1] > 0:\n",
" dx = diff(dx, seasonal['period'], seasonal['order'][1])\n",
" dxreg = diff(dxreg, seasonal['period'], seasonal['order'][1])\n",
" if len(dx) > dxreg.shape[1]:\n",
" model = sm.OLS(dx, dxreg)\n",
" result = model.fit()\n",
" fit = {'coefs': result.params, 'stderrs': result.bse}\n",
" else:\n",
" raise RuntimeError\n",
" model = sm.OLS(dx, dxreg)\n",
" result = model.fit()\n",
" fit = {'coefs': result.params, 'stderrs': result.bse}\n",
" isna = np.isnan(x) | np.isnan(xreg).any(1)\n",
" n_used = (~isna).sum() - len(Delta)\n",
" init0 = np.append(init0, fit['coefs'])\n",
Expand Down Expand Up @@ -1755,7 +1752,7 @@
" missing = np.isnan(x)\n",
" missing_idxs = np.where(~missing)[0]\n",
" firstnonmiss = missing_idxs.min()\n",
" lastnonmiss = missing_idxs.max()\n",
" lastnonmiss = missing_idxs.max() + 1\n",
" n = np.sum(~missing[firstnonmiss:lastnonmiss])\n",
" m = seasonal['period']\n",
" seas_order = seasonal['order']\n",
Expand Down Expand Up @@ -3862,6 +3859,19 @@
"#| hide\n",
"AutoARIMA().fit(np.array([1]*36)).predict(20, level=80)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75f835a8-3459-4668-955c-2f1ed69aa7d4",
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"number = 4.0\n",
"preds = AutoARIMA().fit(np.array([number])).predict(3)\n",
"np.testing.assert_array_equal(preds['mean'], number)"
]
}
],
"metadata": {
Expand Down
11 changes: 4 additions & 7 deletions statsforecast/arima.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,12 +849,9 @@ def maInvert(ma):
if seasonal["period"] > 1 and seasonal["order"][1] > 0:
dx = diff(dx, seasonal["period"], seasonal["order"][1])
dxreg = diff(dxreg, seasonal["period"], seasonal["order"][1])
if len(dx) > dxreg.shape[1]:
model = sm.OLS(dx, dxreg)
result = model.fit()
fit = {"coefs": result.params, "stderrs": result.bse}
else:
raise RuntimeError
model = sm.OLS(dx, dxreg)
result = model.fit()
fit = {"coefs": result.params, "stderrs": result.bse}
isna = np.isnan(x) | np.isnan(xreg).any(1)
n_used = (~isna).sum() - len(Delta)
init0 = np.append(init0, fit["coefs"])
Expand Down Expand Up @@ -1210,7 +1207,7 @@ def myarima(
missing = np.isnan(x)
missing_idxs = np.where(~missing)[0]
firstnonmiss = missing_idxs.min()
lastnonmiss = missing_idxs.max()
lastnonmiss = missing_idxs.max() + 1
n = np.sum(~missing[firstnonmiss:lastnonmiss])
m = seasonal["period"]
seas_order = seasonal["order"]
Expand Down
16 changes: 8 additions & 8 deletions statsforecast/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4706,21 +4706,21 @@ def __init__(
):
"""TSB model.
Teunter-Syntetos-Babai: A modification of Croston's method that replaces the inter-demand
Teunter-Syntetos-Babai: A modification of Croston's method that replaces the inter-demand
intervals with the demand probability $d_t$, which is defined as follows.
$$
d_t = \\begin{cases}
1 & \\text{if demand occurs at time t} \\\
1 & \\text{if demand occurs at time t} \\\
0 & \\text{otherwise.}
\\end{cases}
$$
Hence, the forecast is given by
Hence, the forecast is given by
$$\hat{y}_t= \hat{d}_t\hat{z_t}$$
Both $d_t$ and $z_t$ are forecasted using SES. The smooting paramaters of each may differ,
Both $d_t$ and $z_t$ are forecasted using SES. The smooting paramaters of each may differ,
like in the optimized Croston's method.
References
Expand All @@ -4730,11 +4730,11 @@ def __init__(
Parameters
----------
alpha_d : float
Smoothing parameter for demand.
Smoothing parameter for demand.
alpha_p : float
Smoothing parameter for probability.
alias : str
Custom name of the model.
Smoothing parameter for probability.
alias : str
Custom name of the model.
prediction_intervals : Optional[ConformalIntervals]
Information to compute conformal prediction intervals.
By default, the model will compute the native prediction
Expand Down

0 comments on commit fdfb4b7

Please sign in to comment.