Coder Social home page Coder Social logo

Comments (3)

bashtage avatar bashtage commented on June 11, 2024

The mean is jointly estimated with the variance parameters. If you want the exact in-sample mean, you would need to first demean the data using the rolling mean, and then fit a model with ZeroMean. This would involve recreating the model for each sample.

If you use the fit with first and last, then it will jointly estimate everything.

The fastest way is to use the previous fit values for starting values. Here is a demo:

import arch
from arch.data import sp500
import datetime as dt

r = 100 * sp500.load().iloc[:, -2].pct_change().dropna()

last_obs = 1100
now = dt.datetime.now()
for i in range(1000, last_obs):
    res = arch.arch_model(r.iloc[i - 1000 : i]).fit(disp="off")
print(f"{(dt.datetime.now() - now).total_seconds()} (new model, no starting values)")

now = dt.datetime.now()
for i in range(1000, last_obs):
    arch.arch_model(r).fit(disp="off", first_obs=i - 1000, last_obs=i)
print(f"{(dt.datetime.now() - now).total_seconds()} (no starting values)")

last = None
now = dt.datetime.now()
for i in range(1000, last_obs):
    res = arch.arch_model(r.iloc[i - 1000 : i]).fit(disp="off", starting_values=last)
    last = res.params
print(f"{(dt.datetime.now() - now).total_seconds()} (starting values)")

On my machine I see

1.941473 (new model, no starting values)
1.928971 (no starting values)
1.236577 (starting values)

One final option is to only occasionally update the parameters. This updates parameters every 10 observations. Otherwise it uses the last values.

last = None
now = dt.datetime.now()
for i in range(1000, last_obs):
    mod = arch.arch_model(r.iloc[i - 1000 : i])
    if i % 10 == 0 or last is None:
        res = mod.fit(disp="off", starting_values=last)
        last = res.params
    mod.forecast(res.params, horizon=1)
print(
    f"{(dt.datetime.now() - now).total_seconds()} (starting values, occasionally update)"
)
0.224142 (starting values, occasionally update)

from arch.

bashtage avatar bashtage commented on June 11, 2024

One final answer -- when using first_obs and last_obs, the parameters are estimated only using the selected sample.

from arch.

gavincyi avatar gavincyi commented on June 11, 2024

Thanks for your prompt response. All the above makes perfect sense to me.

I wonder if you think adding an argument to allow demean in the rolling basis is a good idea, i.e. fit(demean=True, ...) passes the demean samples before fitting in the model. If so, I can create a PR for it.

from arch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.