简体   繁体   中英

SARIMAX model in PyMC3

I would like to write down the following SARIMAX model (2,0,0) (2,0,0,12) in PyMC3 to perform bayesian estimation of its coefficients but I cannot figure out how to start with the seasonal part Has anyone tries something like this?

with pm.Model() as ar2:
    theta = pm.Normal("theta", 0.0, 1.0, shape=2)
    sigma = pm.HalfNormal("sigma", 3)
    likelihood = pm.AR("y", theta, sigma=sigma, observed=data)

    trace = pm.sample(
        1000,
        tune=2000,
        random_seed=13,
    )
    idata = az.from_pymc3(trace)

Although it would be best (eg best performance) if you can get an answer that uses PyMC3 exclusively, in case that does not exist yet, there is an alternative way to do this that uses the SARIMAX model in Statsmodels in combination with PyMC3.

There are too many details to repeat a full answer here, but basically you wrap the log-likelihood and gradient methods associated with a Statsmodels SARIMAX model. Here is a link to an example Jupyter notebook that shows how to do this:

https://www.statsmodels.org/stable/examples/notebooks/generated/statespace_sarimax_pymc3.html

I'm not sure if you'll still need it, however, expanding on cfulton's answer, here is how to fix the error in the statsmodels example ( https://www.statsmodels.org/dev/examples/notebooks/generated/statespace_sarimax_pymc3.html , cell 8):

with pm.Model():
    # Priors
    arL1 = pm.Uniform('ar.L1', -0.99, 0.99)
    maL1 = pm.Uniform('ma.L1', -0.99, 0.99)
    sigma2 = pm.InverseGamma('sigma2', 2, 4)

    # convert variables to tensor vectors
    # # this is wrong:
    theta = tt.as_tensor_variable([arL1, maL1, sigma2])
    # # this is correct:
    theta = tt.as_tensor_variable([arL1, maL1, sigma2], 'v')

    # use a DensityDist (use a lamdba function to "call" the Op)
    # # this is wrong:
    # pm.DensityDist('likelihood', lambda v: loglike(v), observed={'v': theta})
    # # this is correct:
    pm.DensityDist('likelihood', lambda v: loglike(v), observed=theta)


    # Draw samples
    trace = pm.sample(ndraws, tune=nburn, discard_tuned_samples=True, cores=4)

I'm no pymc3/theano expert, but I think the error means that Theano has failed to associate the tensor's name with the values. If you define the name along with the values right at the beginning, it works.

I know it's not a direct answer to your question. Nevertheless, I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM