Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute residuals #58

Closed
asinig opened this issue Mar 8, 2022 · 6 comments
Closed

Compute residuals #58

asinig opened this issue Mar 8, 2022 · 6 comments

Comments

@asinig
Copy link

asinig commented Mar 8, 2022

I'm currently trying to perform some forecastings on a set of daily time series and I was wondering whether is there a way to get the predictions on the training data, that are used to compute the residuals (difference between actual and predictions in the train). In StatsForecast class there is no possibility for doing that. I'm mainly interested to obtain them with auto_arima approach, but it could be extended also for the remaining approaches.

Is it possible to add a method or attribute to get them?

Thank you

@jmoralez
Copy link
Member

jmoralez commented Mar 8, 2022

Hi @asinig. The StatsForecast class is designed to be used only in forecasting, so it doesn't save anything related to training. However the implementation for the ARIMA model saves its residuals, so you can access them if you use it directly. Here's an example:

from statsforecast.arima import auto_arima_f
from statsforecast.utils import AirPassengers

mod = auto_arima_f(AirPassengers, period=12)
mod['residuals'][:5]
#  array([0.06466322, 0.03356584, 0.03380615, 0.02255185, 0.01075387])

Let us know if this helps.

@mergenthaler
Copy link
Member

@all-contributors please add @asinig for idea

@allcontributors
Copy link
Contributor

@mergenthaler

I've put up a pull request to add @asinig! 🎉

@asinig
Copy link
Author

asinig commented Mar 14, 2022

Hi @asinig. The StatsForecast class is designed to be used only in forecasting, so it doesn't save anything related to training. However the implementation for the ARIMA model saves its residuals, so you can access them if you use it directly. Here's an example:

from statsforecast.arima import auto_arima_f
from statsforecast.utils import AirPassengers

mod = auto_arima_f(AirPassengers, period=12)
mod['residuals'][:5]
#  array([0.06466322, 0.03356584, 0.03380615, 0.02255185, 0.01075387])

Let us know if this helps.

@jmoralez Thank you for the example and I apologize for the delay in responding. I'm struggling a bit to fully understand how it works, since most of the library implementation is not commented. I saw that auto_arima and auto_arima_f do the same thing, and I was able to derive the residuals. What I can't figure out is that mod['arma'] returns the SARIMA model orders but I can't figure out in what order. In the example above mod['arma'] is a tuple, specifically (1, 0, 0, 12, 1, 1) where
mod['coef'] = {'ar1': -0.30005076872006264}, so mod['arma'] returns a tuple representing the orders (p,d,q, ?, time horizon,?,?). I tried to figure out looking at the implementation in the arima.py file (line 729), but I still don't understand what the ? values represent, because in this case they can't be P,D,Q (the seasonal orders) otherwise I would have had the seasonal ma coefficient printed in mod['coef'].

Let me know please, thank you again

@jmoralez
Copy link
Member

jmoralez commented Mar 14, 2022

Hi @asinig. Sorry, I agree we have to work on the documentation. What mod['arma'] returns is the same as defined here

arma
A compact form of the specification, as a vector giving the number of AR, MA, seasonal AR and seasonal MA coefficients, plus the period and the number of non-seasonal and seasonal differences.

So it is (p, q, P, Q, period, d, D). You may find the arima_string function (which returns ARIMA(p, d, q)(P, D, Q)[period]) useful as well:

from statsforecast.arima import arima_string, auto_arima_f
from statsforecast.utils import AirPassengers

mod = auto_arima_f(AirPassengers, period=12)
mod['arma']
# (1, 0, 0, 0, 12, 1, 1)
arima_string(mod)
# 'ARIMA(1,1,0)(0,1,0)[12]'

@asinig
Copy link
Author

asinig commented Mar 14, 2022

Hi @asinig. Sorry, I agree we have to work on the documentation. What mod['arma'] returns is the same as defined here

arma
A compact form of the specification, as a vector giving the number of AR, MA, seasonal AR and seasonal MA coefficients, plus the period and the number of non-seasonal and seasonal differences.

So it is (p, q, P, Q, period, d, D). You may find the arima_string function (which returns ARIMA(p, d, q)(P, D, Q)[period]) useful as well:

from statsforecast.arima import arima_string, auto_arima_f
from statsforecast.utils import AirPassengers

mod = auto_arima_f(AirPassengers, period=12)
mod['arma']
# (1, 0, 0, 0, 12, 1, 1)
arima_string(mod)
# 'ARIMA(1,1,0)(0,1,0)[12]'

this is clearer, thank you! @jmoralez

@asinig asinig closed this as completed Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants