# Observation is sum of random variables in Turing

**URL:** <https://discourse.julialang.org/t/observation-is-sum-of-random-variables-in-turing/15963>\
**Category:** Statistics\
**Tags:** statistics, turing\
**Created:** [October 6, 2018, 11:47am UTC](https://discourse.julialang.org/t/observation-is-sum-of-random-variables-in-turing/15963 "2018-10-06T11:47:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 6, 2018, 11:47am UTC](https://discourse.julialang.org/t/observation-is-sum-of-random-variables-in-turing/15963/1 "2018-10-06T11:47:39Z")

</div>

I’m playing around with Turing.jl for the first time and am trying to infer my weight from a sequence of noisy observations. The reason I’m looking at MCMC is I want to experiment with different noise models and not only use a Kalman filter.

My problem is I can’t figure out how to tell Turing that my observation is  
`obs = true_weight + gaussian + exponential`. I tried the code below

```julia
using Turing
weight = 75 .+ cumsum(0.2randn(20)) # Generate testdata
N = length(weight)

@model weightfilter(y) = begin
    N = length(y)
    shock = Vector{Real}(undef,N-1) # noise vector
    state = Vector{Real}(undef,N) # The true weight state
    for i = 1:N-1
        shock[i] ~ Exponential(.5) # Ate before etc.
        # noise[i] ~ Normal(0, 0.2) # Hydration level etc.
    end
    state[1] ~ Normal(77, 4) # Initial state
    for n in 2:N
        state[n] ~ Normal(state[n-1], .2) # The state drifts normally
        stateshock = state[n] + shock[n-1] # The measurement is corrupted with exponential noise
        y[n] ~ Normal(stateshock, 0.2) # Measurement is also corrupted with gaussian noise
    end
end;

iterations = 500
chain = sample(weightfilter(weight), SMC(iterations)); 

```

but it does not seem to produce any reasonable results. I would ideally like to set the initial condition of `state` to the observations to make the sampling converge faster, but that does not seem to be an available option.  
Any input on how to accomplish this would be greatly appreciated 🙂

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 6, 2018, 9:54pm UTC](https://discourse.julialang.org/t/observation-is-sum-of-random-variables-in-turing/15963/2 "2018-10-06T21:54:58Z")

</div>

According to [https://github.com/TuringLang/Turing.jl/issues/574](https://github.com/TuringLang/Turing.jl/issues/574), this example should be working on Linux and Mac after replacing normal Julia vectors with Turing’s `TArray` types.
