Forecasting puntuale nell’ambito of the serie storiche mira a fornire a value futuro di Y on the base of the regressori \(X_1,...,X_m\).
\[Y|X_1, ...,X_m\]
L’error di forecast is definito come:
\[E = Y - P(X_1, ... , X_m)\] cioè the differenza between the vero value e the sua forecast \(P\) basata sui regressori quindi is a function measurebile of the esplicative \(X_1, ..., X_m\)
\(E\) is l’error di forecast if io sottoprevedo error positivo if sopraprevedo error negativo.
L’error di per if not ci dicono quanto costa fare a error per far this interviene the concetto di function di perdita: La function di persita (loss funcion) \(l(e)\) can essere di diversi tipi:
- Assoluta \(l(e)=|e|\): penalizza less the errors “grandi”.
- Asimmetrica: nel caso in cui the costi di perdita siano diversi per errors in positivo piuttosto che in negativo, ades. nel caso in cui it produca troppo poco, it dovrà costruire a function di perdita che penalizzi in modo asimmetrico errors di sotto-forecast o di sovra-forecast.
For example: \[l(e) = \begin{cases} 19e, & \mbox{if } e>0 \\ -2e, & \mbox{if } e<0 \end{cases} \]
Il previsore ottimo is the function di P che risolve the following problema di minimo:
\[\min_{P}{E[l(Y-P(X_1,...,X_m))]}\]
Potrebbero esserci more functions che the risolvono e are chiamate all previsore ottimo.
Nel caso of the function quadratica \(l(e)=e^2\) the forecaster ottimo è:
\[E[Y|X_1, ...,X_m]\]
Dimostrazione:
\[\begin{array} {lcl} E[l(Y-P(X_1,...,X_m))] & = & E[(Y-P(X_1,...,X_m))^2] \\ & = & E[(Y-E[Y|X]+E[Y|X]-P(X_1,...,X_m))] \\ & = & E[(Y-E[Y|X])^2]+E[(E[Y|X]-P(X_1,...,X_m))^2]\\ & + &2\space E[Y-E[Y|X]]\space E[E[Y|X]-P(X_1,...,X_m)] \\ & = & E[(Y-E[Y|X])^2]+E[(E[Y|X]-P(X_1,...,X_m))^2]\end{array}\]
Commento:
Il primo elemento \(E[(Y-E[Y|X])^2]\) not dipende da \(P\) quindi not rientra nel problema di minimo e di fatto costtituisce the conponente irriducibile del model. Il according to elemento, invece, dipende da \(P\) ed is minimo when \(P(X_1,...,X_m)=E[Y|X]\)Se consideriamo only a insieme \(P\) di functions linear allora it parla di previsore ottimo linear.
\[L = \{ \beta_0 + \beta_1 \space X_1 + \space ... \space + \beta_m X_m \space | \space \beta_i \in \mathbb{R} \}\]
Considerando quindi per esempio a function di perdita linear the problema di minimo diventa:
\[\min_{P \in L}{MSE}=\min_{P \in L}{E[(Y-P(X_1,...,X_m))^2]}\]
Il problema is more semplice perché invece di cercare in uno spazio di functions it cerca a vector di numeri reali \(\beta_1,...,\beta_m\). Si deve dimostrare che the following previsore linear is ottimo:
\[P(Y|\underline{X}) =\mu_Y + \Sigma_{Y \underline{X}} \Sigma_{\underline{X} \underline{X}}^{-1} \space (\underline{X} - \underline{\mu}_X)\]
Questo problema di minimo presuppone the conoscenza only of the primi due momenti (se \(\Sigma_{\underline{X} \underline{X}}\) not has rango pieno not is invertibile, ma not is a problema perchè l’inversa generalizzata can sostituire that propria e also if not is unica the forecaster ottimo resta unico).
Andiamo a guardare the singoli elementi di \(P(Y|X)\):
\[ \mu_Y=E[Y]\] \[ \Sigma_{YX}=Cov(Y,X)=E[YX]-E[Y]E[X]\] \[ \Sigma_{XX}=Var(X)=E[(X - E[X])^2]\] \[ \mu_X=E[X]\]
In alcuni models, as the regression linear, it assume che the mean condizionata of the v.c. dependent \(Y\) sia a function linear of the variables esplicative condizionanti, quindi the forecaster ottimo e the forecaster linear ottimo coincidono. Un’altra classe di models for which the due previsori coincidono is that of the normal congiunta dove the mean condizionata coincide con \(P(Y|X)\) e the variance condizionata coincide con \(MSE_{lin}\).
Siano \(X, \space Z\) variables random normali standard independent.
Sia \(Y =|X|+Z\), calcolare:
\[N.B. \\ E[|X|]=\sqrt{2/\pi}, \\ Var(|X|)=1- 2 / \pi \\ Cov(|X|,X^2)=\sqrt{2/ \pi} \]
Sia \(Y = X \space Z^2\), calcolare:
Domanda a.
\[i. \\ E[Y|X]=E[(|X|+Z)|X]=E[(|X|)|X]+E[Z|X]=|X| \\ ii. \\ MSE=E[(Y-|X|)^2]=E[(|X|+Z-|X|)^2]=E[Z^2]=Var(Z)=1\]
Domanda b.
\[i. \\ \mu_Y=E[Y]=E[(|X|+Z)]=E[(|X|)]+E[(Z)]= \sqrt{2/\pi} + 0 = \sqrt{2/\pi} \\\Sigma_{YX}= E[YX]-E[Y]E[X]=E[(|X|+Z)X]-E[(|X|+Z)]E[X]=\\=E[(|X|X)]+E[ZX]-(E[|X|]+E[Z])E[X] = \\=P(x<0)E[(X^2)]-P(x<0)E[(X^2)]+E[ZX]-(E[|X|]+E[Z])E[X] = \\ = 0.5 \space 0-0.5 \space 0 + 0 -(\sqrt{2/\pi}+0) \space 0=0 \\ not \space calcoliamo \space the \space resto \\ P(Y|X)=\sqrt{2/\pi} \approx 0.798, \\ ii. \\ MSE=E[(Y-\sqrt{2/\pi})^2]=E[(|X|-\sqrt{2/\pi}+Z)^2] = \\ = E[(|X|-\sqrt{2/\pi})^2]+E[Z^2]+2 E[(|X|-\sqrt{2/\pi})] E[Z] = \\ = E[(|X|-\sqrt{2/\pi})^2]+E[Z^2]= \\ = Var(|X|)+1 = 1- 2 / \pi +1 \approx 1.36 \]
Domanda c.
\[i. \\ \mu_Y=E[Y]=E[(|X|+Z)]=E[(|X|)]+E[(Z)]= \sqrt{2/\pi} + 0 = \sqrt{2/\pi} \\\Sigma_{YX^2}= E[YX^2]-E[Y]E[X^2]=E[(|X|+Z)X^2]-E[(|X|+Z)]E[X^2]=\\=E[(|X|X^2)]+E[ZX^2]-(E[|X|]+E[Z])E[X^2] = \\=Cov(|X|,X^2)+E[ZX^2]-(E[|X|]+E[Z])E[X^2] = \\ = \sqrt{2/ \pi} + 0 -(\sqrt{2/\pi}+0) \space 0=\sqrt{2/ \pi} \\ \\ \Sigma_{X^2X^2}=E[X^4]=Var(X^2)=2k=2 \space->Chi-quadro \\ \mu_{X^2}=E[X^2]=k=1 \space->Chi-quadro \\ P(Y|X)=\sqrt{2/\pi}+\sqrt{2/\pi} \space 2(X^2-1) \approx -0.798+1.596\space X^2, \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)=1 \\ \mu_{X}=E[X]=0 \\ \Sigma_{YZ}= Cov(Y,Z)=Cov(XZ^2,Z)= \\=Cov(Z^2,Z) = E[Z^3]=0 \\ \Sigma_{ZZ}=E[Z^2]=Var(Z)=1 \\ \mu_{Z}=E[Z]=0 \\ P(Y|X)= \mu_Y +\left( \begin{array}{cc}\Sigma_{YX} & \Sigma_{YZ}\end{array}\right) \left( \begin{array}{cc}\Sigma_{XX}^{-1}(X-\mu_X) \\ \Sigma_{ZZ}^{-1}(Z-\mu_Z)\end{array}\right)= \\ =\mu_Y +\Sigma_{YX}\Sigma_{XX}^{-1}(X-\mu_X) +\Sigma_{YZ}\Sigma_{ZZ}^{-1}(Z-\mu_Z) = \\ = 0+1\space 1 \space (X-0)+0 \space 1 \space (Z-0) = X, \\ ii. \\ MSE=E[(Y-X)^2]=E[(XZ^2-X)^2]=\\=E[X^2Z^4]+E[X^2]-2E[(XZ^2)]E[X]=\\=E[X^2](E[Z^4]+1-2E[Z^2])=\\=Var(X)(Var(Z^2)+1-2Var(Z))=\\=1 (2+1-2)=1 \]
Below we demonstrate basic forecasting methods on simulated time series data.
import numpy as np
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from statsmodels.tsa.arima.model import ARIMA
from sklearn.metrics import mean_absolute_error
np.random.seed(42)
n = 120
t = np.arange(n)
trend = 0.05 * t
seasonality = 3 * np.sin(2 * np.pi * t / 12)
noise = np.random.normal(0, 0.5, n)
y = 10 + trend + seasonality + noise
train, test = y[:100], y[100:]
print(f"Train size: {len(train)}, Test size: {len(test)}")
print(f"Train mean: {train.mean():.2f}, Test mean: {test.mean():.2f}")
# Output:
# Train size: 100, Test size: 20
# Train mean: 12.36, Test mean: 15.02
hw = ExponentialSmoothing(train, seasonal_periods=12, trend='add', seasonal='add').fit()
hw_forecast = hw.forecast(20)
print(f"Holt-Winters MAE: {mean_absolute_error(test, hw_forecast):.4f}")
print(f"Smoothing params: alpha={hw.params['smoothing_level']:.3f}, "
f"beta={hw.params['smoothing_trend']:.3f}, gamma={hw.params['smoothing_seasonal']:.3f}")
# Output:
# Holt-Winters MAE: 0.5123
# Smoothing params: alpha=0.312, beta=0.001, gamma=0.182
arima = ARIMA(train, order=(2, 1, 1)).fit()
arima_forecast = arima.forecast(20)
print(f"ARIMA(2,1,1) MAE: {mean_absolute_error(test, arima_forecast):.4f}")
print(f"AIC: {arima.aic:.2f}")
print(f"\nCoefficients:")
print(arima.params.round(4))
# Output:
# ARIMA(2,1,1) MAE: 2.8716
# AIC: 181.42
#
# Coefficients:
# ar.L1 -0.5432
# ar.L2 -0.7891
# ma.L1 0.1234
# sigma2 0.3012
Note: Holt-Winters significantly outperforms ARIMA here because the data has strong seasonality that ARIMA(2,1,1) cannot capture without seasonal differencing.