# Autoregressive Model

**URL:** <https://discourse.pymc.io/t/autoregressive-model/5765>\
**Category:** Questions\
**Created:** [September 10, 2020, 8:40pm UTC](https://discourse.pymc.io/t/autoregressive-model/5765 "2020-09-10T20:40:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vb690](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vb690/32/3432_2.png) [@vb690](https://discourse.pymc.io/u/vb690)\
**Post date:** [September 10, 2020, 8:40pm UTC](https://discourse.pymc.io/t/autoregressive-model/5765/1 "2020-09-10T20:40:09Z")

</div>

Hello everyone,

I start saying that I am very new both to PyMC3 and Bayesian statistical modelling.  
What I was trying to do was to write an AR(1) model for fitting the toy data produced by this function

```python
def generate_poisson_ar(lam_int, slope_a, slope_b, mu_noise, sigma_noise,
                        burn_factor=2, time_steps=48):
    intercept = np.random.poisson(lam_int)
    slope = np.random.beta(slope_a, slope_b)
    process = [intercept]
    true_parameters = {
        'Intercpet': intercept,
        'Slope': slope
    }
    for time_step in range(time_steps * burn_factor):

        new_value = intercept + int(slope * process[time_step]) + \
            np.random.normal(mu_noise, sigma_noise)
        new_value = max(0, new_value)
        process.append(new_value)

    process = np.array(process[-time_steps:])
    return process, true_parameters  

process, true_parameters = generate_poisson_ar(
    lam_int=200,
    slope_a=2,
    slope_b=5,
    mu_noise=50,
    sigma_noise=50
)
X = process[:-1]
y = process[1:]

```

But instead of using the PyMC3 `AR1` class I wrote this model:

```python
prior_mu = 200
prior_alpha = 2
prior_beta = 5
with pm.Model() as ar_model:

        intercept = pm.Poisson(
            mu=prior_mu,
            name='Intercept'
        )
        slope = pm.Beta(
            alpha=prior_alpha,
            beta=prior_beta,
            name='Slope'
        )

        mu = intercept + slope*X

        outcome = pm.Poisson(
            mu=mu,
            observed=y,
            name='y'
        )

```

My questions are:

1. Do you think the model I defined is a sensible alternative to use the example provided on the PyMC3 docs (the one using the `AR1` class)?
2. Inspecting both the traceplot and the the posterior predictions  
 ![single](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/a/ae767a6ee3d8880f939c058294acb1403b0ec2fa.jpeg)  
made by the model it seems that my solution work ok-ish (the MCMC chains are not very well mixed), however I get these errors:

```auto
Sampling 4 chains for 2_000 tune and 1_000 draw iterations (8_000 + 4_000 draws total) took 72 seconds.
The acceptance probability does not match the target. It is 0.9144978498054566, but should be close to 0.8. Try to increase the number of tuning steps.
The estimated number of effective samples is smaller than 200 for some parameters.

```

Can anyone help me shading some light on the reasons behind these messages?

Thank you 😊

---

<div class="post-metadata">

**Author:** ![RavinKumar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ravinkumar/32/958_2.png) [@RavinKumar](https://discourse.pymc.io/u/RavinKumar)\
**Post date:** [October 25, 2021, 4:06pm UTC](https://discourse.pymc.io/t/autoregressive-model/5765/2 "2021-10-25T16:06:48Z")

</div>

Hi,  
I know its been over a year so maybe this answer isn’t as useful but posting here for posterity’s sake. Those are MCMC sampler diagnostics that are warning that the sampler may have run into some trouble. This other thread provides more clarification as to how to interpret the messages.

> [@The number of effective samples is smaller than 25% for some parameters](https://discourse.pymc.io/t/the-number-of-effective-samples-is-smaller-than-25-for-some-parameters/1050/2):
>
> The low number of effective samples is usually an indication of strong autocorrelation in the chain. You can plot the trace autocorrelation or look at the trace plot directly. Usually, this could be improved by using more efficient sampler like NUTS. However, in this case since you are mixing discrete random variable with continuous variables, sampling is likely going to be a bit difficult. You can find more information about effective sample size from Gelman et al’s book [Bayesian Data Analysi…](http://www.stat.columbia.edu/~gelman/book/)

---

<div class="post-metadata">

**Author:** ![vb690](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vb690/32/3432_2.png) [@vb690](https://discourse.pymc.io/u/vb690)\
**Post date:** [October 26, 2021, 7:50am UTC](https://discourse.pymc.io/t/autoregressive-model/5765/3 "2021-10-26T07:50:04Z")

</div>

Hi 🙂

I am now (a tiny bit) more versed in PyMC3 and I realized several issues with my approach. Nevertheless you answer has been very useful!

Thank you  
Valerio
