# Handling Correlated shocks in HMM

**URL:** <https://discourse.julialang.org/t/handling-correlated-shocks-in-hmm/43355>\
**Category:** Probabilistic Programming\
**Created:** [July 20, 2020, 2:12am UTC](https://discourse.julialang.org/t/handling-correlated-shocks-in-hmm/43355 "2020-07-20T02:12:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![balaji1975](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@balaji1975](https://discourse.julialang.org/u/balaji1975)\
**Post date:** [July 20, 2020, 2:12am UTC](https://discourse.julialang.org/t/handling-correlated-shocks-in-hmm/43355/1 "2020-07-20T02:12:54Z")

</div>

Is there a way to handle the below in Turing?

y(t) = f(y(t-1), s(t)) + g(y(t-1), s(t)) \* e1(t)  
s(t) = p(s(t-1)) + q(s(t-1)) \* e2(t)

y(t) is the data (Tx1) and s(t) is the (hidden) state variable. e1 & e2 are correlated N(0,1) with correlation coeff rho. f,g,p and q are well behaved functions. Objective is to estimate rho and parameters of these functions.

Any suggestions pse?

Thanks!

---

<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:** [July 20, 2020, 6:44am UTC](https://discourse.julialang.org/t/handling-correlated-shocks-in-hmm/43355/2 "2020-07-20T06:44:47Z")

</div>

This model should be straightforward to implement in turing. Try sampling `e ~ MvNormal(Sigma)` where `Sigma` is the covariance matrix of the noise terms.

---

<div class="post-metadata">

**Author:** ![balaji1975](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@balaji1975](https://discourse.julialang.org/u/balaji1975)\
**Post date:** [July 20, 2020, 10:58pm UTC](https://discourse.julialang.org/t/handling-correlated-shocks-in-hmm/43355/3 "2020-07-20T22:58:50Z")

</div>

Something like this?  
e ~ MvNormal(S)  
y = (…) + (…) \* e[1]

Thanks.

---

<div class="post-metadata">

**Author:** ![balaji1975](https://avatars.discourse-cdn.com/v4/letter/b/8edcca/32.png) [@balaji1975](https://discourse.julialang.org/u/balaji1975)\
**Post date:** [July 21, 2020, 8:16am UTC](https://discourse.julialang.org/t/handling-correlated-shocks-in-hmm/43355/4 "2020-07-21T08:16:43Z")

</div>

My Turing implementation of the model:

```julia
@model sv_model(r) = begin
    
    N = length(r)
    h = Array{Float64}(undef, N)
        
    σ ~ TruncatedNormal(0.3, 0.1, 0.001, 10)
    ρ ~ Uniform(-1,1)
    ϕ ~ Uniform(-1,1)
    μ ~ Normal(-1.0, 0.3)
    
    h₀ = μ
    r₀ = 0.0
    r[1] ~ Normal(0.0, exp(0.5*μ))
    h[1] ~ Normal(μ, sqrt(1 - ρ*ρ)*σ)
    
    
    for i = 2:N
        r[i] ~ Normal(0.0, exp(0.5*h[i-1]))
        h[i] ~ Normal(μ + ϕ * (h[i-1] - μ) + σ*ρ*exp(-0.5*h[i-1])*r[i-1], sqrt(1 - ρ*ρ)*σ)
    end

end   

```

With HMC() or NUTS() for algorithms, I get the below error.

```julia
TypeError: in typeassert, expected Float64, got ForwardDiff.Dual{Nothing,Float64,10}

Stacktrace:
 [1] setindex!(::Array{Float64,1}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"

```

With PG(), i get occasionally lucky. Otherwise, most of the times i get the following message (though i can’t see why sigma is \< 0)

```julia
CTaskException:
ArgumentError: Normal: the condition σ >= zero(σ) is not satisfied.
Stacktrace:
 [1] macro expansion at /Users/balaji/.julia/packages/Distributions/RAeyY/src/utils.jl:6 [inlined]
 [2] #Normal#98 at /Users/balaji/.julia/packages/Distributions/RAeyY/src/univariate/continuous/normal.jl:37 [inlined]
 [3] Normal at /Users/balaji/.julia/packages/Distributions/RAeyY/src/univariate/continuous/normal.jl:37 [inlined]
 [4] macro expansion at ./In[133]:18 [inlined]
 [5] ##evaluator#771(::Random._GLOBAL_RNG, ::DynamicPPL.Model{var"###evaluator#771",(:r,),Tuple{Array{Float64,1}},(),

```

The same exact model runs in Stan (julia interface) though.

---

<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:** [July 21, 2020, 11:43am UTC](https://discourse.julialang.org/t/handling-correlated-shocks-in-hmm/43355/5 "2020-07-21T11:43:13Z")

</div>

The performance tips section of the turing manual shows you how to solve that problem.
