# Random walks inside a model

**URL:** https://discourse.julialang.org/t/random-walks-inside-a-model/49994
**Category:** Probabilistic Programming
**Tags:** question, turing
**Created:** [November 11, 2020, 5:39pm UTC](https://discourse.julialang.org/t/random-walks-inside-a-model/49994 "2020-11-11T17:39:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Camilo1704](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/camilo1704/32/14601_2.png) [@Camilo1704](https://discourse.julialang.org/u/Camilo1704)
#### Post date: [November 11, 2020, 5:39pm UTC](https://discourse.julialang.org/t/random-walks-inside-a-model/49994/1 "2020-11-11T17:39:38Z")

</div>

Hi guys,  
I’m wondering if there is a way to include a variable which is a random walk in a model. Particularly, if inside my model I have the lines of code shown below, is there a walk around to do it more efficiently? I also don’t know how it will be interpreted.

```julia
    δ[1] = 0
    σ_δ ~ Exponential()
    n = length(δ)
    for i in 2:n
        δ[i] ~ Normal(δ[i-1], σ_δ)
    end
   β = logistic.(δ)

```

Any help will be appreciated 🙂

---

<div class="post-metadata">

### Author: ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)
#### Post date: [November 16, 2020, 3:29pm UTC](https://discourse.julialang.org/t/random-walks-inside-a-model/49994/2 "2020-11-16T15:29:40Z")

</div>

This works fine in Turing atm. We currently don’t have a mechanism to make this more efficient, though. But we discussed the topic internally yesterday and it’s very likely there will be additional API functionally to express random walk like constructs in a Turing model, which will in turn speed up inference.

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [November 16, 2020, 4:44pm UTC](https://discourse.julialang.org/t/random-walks-inside-a-model/49994/3 "2020-11-16T16:44:50Z")

</div>

A Gaussian (slightly mean reverting) random walk has a tridiagonal precision matrix. You can just write the joint density of `δ` as multivariate Normal with tridiagonal precision matrix. Perhaps Turing can deal with that already?

---

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [November 16, 2020, 6:11pm UTC](https://discourse.julialang.org/t/random-walks-inside-a-model/49994/4 "2020-11-16T18:11:41Z")

</div>

This currently only works if the precision matrix is dense (see [this issue](https://github.com/TuringLang/DistributionsAD.jl/issues/89)). For short time series, that might still yield a speedup over the explicit loop, but ultimately kind of defeats the purpose of using a precision matrix.

I’ve been working on Gaussian Markov random fields, which can be seen as a generalization of the random walk process. Like a random walk, they can be described using a multivariate normal distribution with a sparse precision matrix. However, there are currently issues with autodiff and the sparse matrix libraries: since Julia calls out to SuiteSparse for sparse matrix operations, it can’t handle dual numbers, so you can’t currently use any gradient-based MCMC methods if your model has sparse `MvNormalCanon` variables.

Anyway, @Camilo1704, you can try this dense version and see if it gives you a speedup:

```julia
@model function RandomWalk(x)
    n = length(x)
    σ_δ ~ Exponential()
    Q = diagm(-1 => -0.5/σ_δ^2 * ones(n-1), 0 => 1/σ_δ^2 * ones(n), 1 => -0.5/σ_δ^2 * ones(n-1))
    x ~ MvNormalCanon(Q)
end

```

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [November 17, 2020, 8:49am UTC](https://discourse.julialang.org/t/random-walks-inside-a-model/49994/5 "2020-11-17T08:49:02Z")

</div>

> I’ve been working on Gaussian Markov random fields, which can be seen as a generalization of the random walk process.

👍, that’s close to my interests as well, e.g. in [https://twitter.com/MoritzSchauer/status/1234726235051302912](https://twitter.com/MoritzSchauer/status/1234726235051302912) we do data assimilation in a state space model with latent state modelled by a Gaussian Markov random field
