# Code conversion Bayesian Modeling and Computation in Python ch6. Help on AR(1) model implementation

**URL:** <https://discourse.pymc.io/t/code-conversion-bayesian-modeling-and-computation-in-python-ch6-help-on-ar-1-model-implementation/9809>\
**Category:** Questions\
**Created:** [July 2, 2022, 8:43pm UTC](https://discourse.pymc.io/t/code-conversion-bayesian-modeling-and-computation-in-python-ch6-help-on-ar-1-model-implementation/9809 "2022-07-02T20:43:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![roesta07](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/roesta07/32/6060_2.png) [@roesta07](https://discourse.pymc.io/u/roesta07)\
**Post date:** [July 2, 2022, 8:43pm UTC](https://discourse.pymc.io/t/code-conversion-bayesian-modeling-and-computation-in-python-ch6-help-on-ar-1-model-implementation/9809/1 "2022-07-02T20:43:58Z")

</div>

I am trying to convert the following code (written in TensorFlow) to Pymc3 which is from the [Book](https://bayesiancomputationbook.com/markdown/chp_06.html); _Bayesian Modeling and Computation in Python, Martin, Kumar & Lao, chapter 6, Listing 6.14gam\_with\_latent\_ar._ for better understanding.

```auto
def generate_gam_ar_latent(training=True):

    @tfd.JointDistributionCoroutine
    def gam_with_latent_ar():
        seasonality, trend, noise_sigma = yield from gam_trend_seasonality()
        
        # Latent AR(1)
        ar_sigma = yield root(tfd.HalfNormal(.1, name="ar_sigma"))
        rho = yield root(tfd.Uniform(-1., 1., name="rho"))
        def ar_fun(y):
            loc = tf.concat([tf.zeros_like(y[..., :1]), y[..., :-1]],
                            axis=-1) * rho[..., None]
            return tfd.Independent(
                tfd.Normal(loc=loc, scale=ar_sigma[..., None]),
                reinterpreted_batch_ndims=1)
        temporal_error = yield tfd.Autoregressive(
            distribution_fn=ar_fun,
            sample0=tf.zeros_like(trend),
            num_steps=trend.shape[-1],
            name="temporal_error")

        # Linear prediction
        y_hat = seasonality + trend + temporal_error
        if training:
            y_hat = y_hat[..., :co2_by_month_training_data.shape[0]]

        # Likelihood
        observed = yield tfd.Independent(
            tfd.Normal(y_hat, noise_sigma[..., None]),
            reinterpreted_batch_ndims=1,
            name="observed"
        )

    return gam_with_latent_ar

gam_with_latent_ar = generate_gam_ar_latent()

```

I have already converted the trend and seasonality and it seems to be working fine but I am stuck with the AR part.

P.S thank you @RavinKumar @junpenglao, and @aloctavodia for writing this awesome book and making it accessible online.

Also if anyone else is working on converting chapter 6 of the Book for better understanding; please let me know!!

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 3, 2022, 7:14am UTC](https://discourse.pymc.io/t/code-conversion-bayesian-modeling-and-computation-in-python-ch6-help-on-ar-1-model-implementation/9809/2 "2022-07-03T07:14:39Z")

</div>

Did you try the `pm.AR` distribution in v4? It should work out of the box

---

<div class="post-metadata">

**Author:** ![roesta07](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/roesta07/32/6060_2.png) [@roesta07](https://discourse.pymc.io/u/roesta07)\
**Post date:** [July 11, 2022, 5:38am UTC](https://discourse.pymc.io/t/code-conversion-bayesian-modeling-and-computation-in-python-ch6-help-on-ar-1-model-implementation/9809/3 "2022-07-11T05:38:26Z")

</div>

Thank you @junpenglao. Any idea how if not when Pymc4 will start to work in google colab?

---

<div class="post-metadata">

**Author:** ![roesta07](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/roesta07/32/6060_2.png) [@roesta07](https://discourse.pymc.io/u/roesta07)\
**Post date:** [July 11, 2022, 5:40am UTC](https://discourse.pymc.io/t/code-conversion-bayesian-modeling-and-computation-in-python-ch6-help-on-ar-1-model-implementation/9809/4 "2022-07-11T05:40:24Z")

</div>

I think I should make a different Topic for the availability of Pymc4 in google colab!!

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 11, 2022, 8:04am UTC](https://discourse.pymc.io/t/code-conversion-bayesian-modeling-and-computation-in-python-ch6-help-on-ar-1-model-implementation/9809/5 "2022-07-11T08:04:50Z")

</div>

A few Google folks are working on it (me, @RavinKumar and @colcarroll)

---

<div class="post-metadata">

**Author:** ![roesta07](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/roesta07/32/6060_2.png) [@roesta07](https://discourse.pymc.io/u/roesta07)\
**Post date:** [July 11, 2022, 9:02am UTC](https://discourse.pymc.io/t/code-conversion-bayesian-modeling-and-computation-in-python-ch6-help-on-ar-1-model-implementation/9809/6 "2022-07-11T09:02:50Z")

</div>

It would be great!!
