AR(1) bayesian regression with none negative values

Hi,

I am interested in conducting a Bayesian AR(1) regression. The variable I intend to model should not possess negative values. Therefore, I have opted for a Beta distribution as the likelihood function. However, I have encountered an issue with ar1 potentially producing negative values, resulting in negative alphas. How can I address this error?


priors = {
    "coefs": {"mu": [0.0274, 0.9839], "sigma": [0.1, 0.1], "size": 2},
    "sigma": 10,
    "init": {"mu": 0.016267, "sigma": 0.014459, "size": 1},
}


with pm.Model() as AR:
    pass


t_data = list(range(len(div_yield_numpy)))

AR.add_coord("obs_id", t_data, mutable=True)

with AR:

    t = pm.MutableData("t", t_data, dims="obs_id")
    y = pm.MutableData("y", div_yield_numpy, dims="obs_id")


    coefs = pm.Beta("coefs", priors["coefs"]["mu"], priors["coefs"]["sigma"])
    

    sigma = pm.HalfNormal("sigma", 
                           priors["sigma"]
                          )

    init = pm.Beta.dist(
        priors["init"]["mu"], priors["init"]["sigma"], size=priors["init"]["size"]
    )
 
    ar1 = pm.AR(
        "ar1",
        coefs,
        sigma=sigma,
        init_dist=init,
        constant=True,
        steps=t.shape[0] - (priors["coefs"]["size"] - 1),
        dims="obs_id",
    )

    # The Likelihood
    outcome = pm.Beta("likelihood",
                      mu=ar1,
                      sigma=sigma,
                      observed=y,
                      dims="obs_id"
                     )

    idata_ar = pm.sample_prior_predictive(1000)

A beta distribution is bound between zero and one – are you sure that’s what you want?

The easiest solution is to log your data, and model the log process as normal (pun intended). If you really don’t want to do that, you will need to make a custom AR(1) with non-normal innovations. You can follow this example notebook for the basics on how to make a custom time series distribution, and just swap out the normal innovations inside the ar_step function with something that will respect your non-negativity constraint, e.g. lognormal.

Thank you for your response. Perhaps I misunderstood the situation. I executed the provided code using a normal distribution, and everything seemed to function correctly. However, when I conducted a prior predictive check, I noticed that the model accounts for negative values, even though the observed variable cannot be negative. What would be the most effective approach to address this issue?

I’m not really sure what model you ran to generate that graph. Whatever model it was, it has no way of knowing the observed values can’t be negative, especially before it’s seen the data. As I mentioned, you should either model the log of the data (avoiding the problem entirely), or change the innovation distribution (that’s the pm.Normal.dist(sigma=sigma) inside the ar_step function in the notebook I linked), as well as the ar_init distribution, to be strictly positive.