Skip to content

Commit 686cf6d

Browse files
revert(models): restore forecast function
1 parent 346b806 commit 686cf6d

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

nbs/src/core/models.ipynb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"from typing import Any, Dict, List, Optional, Tuple, Union\n",
6262
"\n",
6363
"import numpy as np\n",
64-
"from coreforecast.exponentially_weighted import exponentially_weighted_mean\n",
6564
"from numba import njit\n",
6665
"from scipy.optimize import minimize_scalar\n",
6766
"from scipy.special import inv_boxcox\n",
@@ -3677,14 +3676,15 @@
36773676
" return sse\n",
36783677
"\n",
36793678
"\n",
3679+
"@njit(nogil=NOGIL, cache=CACHE)\n",
36803680
"def _ses_forecast(x: np.ndarray, alpha: float) -> Tuple[float, np.ndarray]:\n",
36813681
" r\"\"\"Compute the one-step ahead forecast for a simple exponential smoothing fit.\n",
3682-
" \n",
3682+
"\n",
36833683
" Parameters\n",
36843684
" ----------\n",
36853685
" x : numpy.array\n",
36863686
" Clean time series of shape (n, ).\n",
3687-
" alpha : float \n",
3687+
" alpha : float\n",
36883688
" Smoothing parameter.\n",
36893689
"\n",
36903690
" Returns\n",
@@ -3693,9 +3693,16 @@
36933693
" One-step ahead forecast and in-sample fitted values.\n",
36943694
"\n",
36953695
" \"\"\"\n",
3696-
" fitted = exponentially_weighted_mean(x, alpha)\n",
3697-
" forecast = fitted.item(-1)\n",
3698-
" fitted[1:] = fitted[:-1]\n",
3696+
" complement = 1 - alpha\n",
3697+
" fitted = np.empty_like(x)\n",
3698+
" fitted[0] = x[0]\n",
3699+
" j = 0\n",
3700+
"\n",
3701+
" for i in range(1, len(x)):\n",
3702+
" fitted[i] = alpha * x[j] + complement * fitted[j]\n",
3703+
" j += 1\n",
3704+
"\n",
3705+
" forecast = alpha * x[j] + complement * fitted[j]\n",
36993706
" fitted[0] = np.nan\n",
37003707
" return forecast, fitted\n",
37013708
"\n",

python/statsforecast/models.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from typing import Any, Dict, List, Optional, Tuple, Union
1616

1717
import numpy as np
18-
from coreforecast.exponentially_weighted import exponentially_weighted_mean
1918
from numba import njit
2019
from scipy.optimize import minimize_scalar
2120
from scipy.special import inv_boxcox
@@ -2158,6 +2157,7 @@ def _ses_sse(alpha: float, x: np.ndarray) -> float:
21582157
return sse
21592158

21602159

2160+
@njit(nogil=NOGIL, cache=CACHE)
21612161
def _ses_forecast(x: np.ndarray, alpha: float) -> Tuple[float, np.ndarray]:
21622162
r"""Compute the one-step ahead forecast for a simple exponential smoothing fit.
21632163
@@ -2174,9 +2174,16 @@ def _ses_forecast(x: np.ndarray, alpha: float) -> Tuple[float, np.ndarray]:
21742174
One-step ahead forecast and in-sample fitted values.
21752175
21762176
"""
2177-
fitted = exponentially_weighted_mean(x, alpha)
2178-
forecast = fitted.item(-1)
2179-
fitted[1:] = fitted[:-1]
2177+
complement = 1 - alpha
2178+
fitted = np.empty_like(x)
2179+
fitted[0] = x[0]
2180+
j = 0
2181+
2182+
for i in range(1, len(x)):
2183+
fitted[i] = alpha * x[j] + complement * fitted[j]
2184+
j += 1
2185+
2186+
forecast = alpha * x[j] + complement * fitted[j]
21802187
fitted[0] = np.nan
21812188
return forecast, fitted
21822189

0 commit comments

Comments
 (0)