# Incorporating time lag in observation models

**URL:** https://discourse.julialang.org/t/incorporating-time-lag-in-observation-models/75002
**Category:** Probabilistic Programming
**Tags:** bayesian-inference, differentialequation
**Created:** [January 21, 2022, 7:57pm UTC](https://discourse.julialang.org/t/incorporating-time-lag-in-observation-models/75002 "2022-01-21T19:57:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vembha](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vembha](https://discourse.julialang.org/u/vembha)
#### Post date: [January 21, 2022, 7:57pm UTC](https://discourse.julialang.org/t/incorporating-time-lag-in-observation-models/75002/1 "2022-01-21T19:57:51Z")

</div>

Hello. I am estimating parameters of a simple SIR epidemic model (shown below) by fitting the model to a known time series data:

**Deterministic model:**

```julia
function SIR_model(du, u, p, t)
    S, I, R = u
    β, γ = p

    du[1] = -β * S * I
    du[2] = β * S * I - γ * I
    du[3] = γ * I
end

```

**Observation model:**

```julia
@model function SIR_fitting(data)
    σ ~ Uniform(0, 50)
    β ~ truncated(Normal(5e-05, 1e-03), 0, 1)
    γ ~ truncated(Normal(0.6, 0.3), 0, 1)

    p = [β,γ]
    prob = ODEProblem(SIR_model, u0, (0, 50), p)
    predicted = solve(prob, Tsit5(), saveat=1.0)

    for i = 1:length(predicted)
        data[i] ~ Normal(predicted[i][2], σ)
    end
end

```

As you can see, I am sampling the observations of `I(t)` from a normal distribution and fitting them to `data`. Everything works perfectly with this setup.

Now, I assume there is a time lag `τ` between data and sampling, representing the delayed reporting of infections in reality. But modifying the last line in the observation model to `data[i] ~ Normal(predicted[i-τ][2], σ)` throws out `Invalid indexing of solution` error. I can understand this is due to non-integer indexing. Can someone please please help me and suggest how to avoid it?

**Note:** I have used the prior distribution of `τ ~ Gamma(1,5)`

Thanks in advance!

---

<div class="post-metadata">

### Author: ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)
#### Post date: [January 22, 2022, 11:04am UTC](https://discourse.julialang.org/t/incorporating-time-lag-in-observation-models/75002/2 "2022-01-22T11:04:43Z")

</div>

_indexing_ the solution object accesses it at discrete timesteps.  
I think what you want here is to interpolate your solution at arbitrary time steps, which can be done by _calling_ (parantheses instead of square brackets) the solution object:

```julia
predicted(i - τ)

```

see here how to handle the solution object in various ways:  
[https://diffeq.sciml.ai/latest/tutorials/ode\_example/#Handling-the-Solution-Type](https://diffeq.sciml.ai/latest/tutorials/ode_example/#Handling-the-Solution-Type)

however you’ll probably still have to make sure that `i - τ` is within bounds.

---

<div class="post-metadata">

### Author: ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)
#### Post date: [January 22, 2022, 11:26am UTC](https://discourse.julialang.org/t/incorporating-time-lag-in-observation-models/75002/3 "2022-01-22T11:26:31Z")

</div>

I am no expert, but I think there are also other ways to model time lags.  
This repository has a great overview of such models: [https://github.com/epirecipes/sir-julia](https://github.com/epirecipes/sir-julia)

---

<div class="post-metadata">

### Author: ![vembha](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vembha](https://discourse.julialang.org/u/vembha)
#### Post date: [January 22, 2022, 12:57pm UTC](https://discourse.julialang.org/t/incorporating-time-lag-in-observation-models/75002/4 "2022-01-22T12:57:47Z")

</div>

Thank you @trahflow. This worked beautifully! 🙂
