bashtage / arch

ARCH models in Python
Other
1.32k stars 247 forks source link

Using statsmodels ARMAResults as a mean model #101

Closed Anjum48 closed 8 years ago

Anjum48 commented 8 years ago

Is it possible to use ARMAResults calculated using statsmodels as a mean model so that a volatility process can be added?

I wasn't sure how this might work but I was thinking of something along the lines of this:

from arch.univariate.mean import ARCHModel
from arch.univariate import ARCH

model = ARCHModel(returns)
model.mean = arma_res  # statsmodels ARMAResults
model.volatility = ARCH()

results = model.fit(update_freq=5)
timseries commented 8 years ago

I think this would be very useful functionality.

KenMbuthia commented 8 years ago

I too second this idea.

However this could be that we have a fundamental misunderstanding on how ARCH model works? However in my understanding it would be best to first estimate the ARMA parameters then use them in GARCH.

I too have struggled with trying to set the mean model for GARCH using this module with the objective of building a GARCH model from its componets as described at http://arch.readthedocs.org/en/latest/univariate/univariate_volatility_modeling.html#building-a-model-from-components . Is it that we do not understand how to specify the mean model with prior estimated ARMA parameters? If this is the case then a step by step example on how this could be achieved could be added in the examples. Going through the examples at http://arch.readthedocs.org/en/latest/univariate/mean.html it is not clear on how one could achieve this or if it is currently possible. This would be highly appreciated and guide us on the path of better understanding this module.

Hoping not to go out of bounds, my main motivation for the above request is based on trying to replicate the results from https://www.quantstart.com/articles/ARIMA-GARCH-Trading-Strategy-on-the-SP500-Stock-Market-Index-Using-R . The Series by M. Halls Moore is quite a wonderful and interesting guide on how to practically apply time series analysis. So far in following the series, I have been able to follow along using statsmodels. However, the series is implemented using R and I have a preference for python so switching though possible, is not preferable. I have found this package to be the most up to date and best in terms of GARCH models. Statsmodels has garch though it is still in sandbox and there seems like some methods have yet to be implemented. Another solution would be to use r2py for using r packages but I still have preference to python.

I hope, you would consider this request and that also this is the appropriate place to place this requests.

timseries commented 8 years ago

I was thinking more along the lines of generating a one-liner prediction function using the ARFIMA-GARCH recurrence equations so that the mean process and confidence interval around the mean process could be forecast in one go.

Anjum48 commented 8 years ago

@KenMbuthia I was also following the Quantstart article in Python like yourself. I managed to get pretty far using statsmodels, but hit a road block soon as it got to using GARCH. I think arch is a brilliant package for it's simplicity but I think it would be even better it it played nice with the tsa components of statsmodels. I think I started writing a mean model class, but unfortunately didn't get very far before switching to R

bashtage commented 8 years ago

statsmodels's ARMA code is unfortunately too bespoke to statsmodels to be generally useful.

This said, one can always use 2-step estimation where:

  1. Fit ARMA(X) model and get residuals
  2. Fit a GARCH model with no mean to get variances.
b3yang commented 8 years ago

like what @bashtage has suggested, think it can be done by something like this (spx shown below on ARMA(1,1) with GARCH(1,1))

    _arma_model = sm.tsa.arima_model.ARMA(input_ts, (1,1))
    _model_result = _arma_model.fit()
    arch_model(_model_result.resid, mean='Zero', p=1, q=1)

image

b3yang commented 8 years ago

for forecasting though, how would you incorporate the forecasted variance into the mean model? my guess would be something like append to resid then use _arma_predict_out_of_sample, do any of you see a problem here?

bashtage commented 8 years ago

I think it isn't likely that the two can be formally combined, so either 2 step estimation as described above or one can use a AR(p) process to approximate an ARMA model of any order.

ramdhan1989 commented 3 years ago

like what @bashtage has suggested, think it can be done by something like this (spx shown below on ARMA(1,1) with GARCH(1,1))

    _arma_model = sm.tsa.arima_model.ARMA(input_ts, (1,1))
    _model_result = _arma_model.fit()
    arch_model(_model_result.resid, mean='Zero', p=1, q=1)

image

I try this but arch return zero if specify mean='zero'

bashtage commented 3 years ago

What do you mean returns zero?

olofahlen commented 3 years ago

@bashtage You said statsmodels is too bespoke. If my goal is to specify an ARIMA model for the conditional mean by inheriting arch.univariate.base.ARCHModel and implementing the methods, do you have any comments on how well this might work by wrapping the relevant class from statsmodels? Are you aware of anyone who might already have done this?

bashtage commented 3 years ago

Wrapping wouldn't require delving into the inner working of statsmodels, so it should theoretically be possible. I think one woucl wrap ARIMA and use the mean parameters to generate residuals, and then residuals to estimate volatility, and return the log-likelihood of these. This is a good idea and would be a welcome contribution.