# Use of intermediate variables to condition likelihood

**URL:** <https://discourse.julialang.org/t/use-of-intermediate-variables-to-condition-likelihood/78313>\
**Category:** Probabilistic Programming\
**Tags:** turing, bayesian-inference\
**Created:** [March 22, 2022, 11:37pm UTC](https://discourse.julialang.org/t/use-of-intermediate-variables-to-condition-likelihood/78313 "2022-03-22T23:37:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![3mtmuhPnQv](https://avatars.discourse-cdn.com/v4/letter/3/8491ac/32.png) [@3mtmuhPnQv](https://discourse.julialang.org/u/3mtmuhPnQv)\
**Post date:** [March 22, 2022, 11:37pm UTC](https://discourse.julialang.org/t/use-of-intermediate-variables-to-condition-likelihood/78313/1 "2022-03-22T23:37:58Z")

</div>

I’d like to model prices as following a kind of stochastic difference equation, where jumps from one time step to the next follow  
p\_{t} = p\_{t - 1} + c \exp(- b t\_0) \Delta t + X(t)  
X ~ \text{student\_t}(3, 0, \sqrt{\Delta t} \times (\sigma\_0 + \sigma \exp(- \gamma t))  
Here is the model

```julia
d_price_mean(c::Real, b::Real, t0::Real, dt::Real)::Real = begin
    c * exp(- b * t0) * dt
end

perturbation_volatility(
    sigma1::Real,
    sigma::Real,
    gamma::Real,
    t0::Real, dt::Real)::Real = begin
    sigma1 + sigma * exp(- gamma * t0) * sqrt(dt)
end

@model price_model(
    d_price,
    selldown_time,
    d_selldown_time,
    n
    ) = begin
    c ~ LogNormal(2, 1)
    b ~ LogNormal(2, 1)
    sigma1 ~ LogNormal(-1.3862943611198906, 1)
    sigma ~ LogNormal(-0.6931471805599453, 1)
    gamma ~ LogNormal(2, 1)    
    price_diff = d_price .- d_price_mean.(Ref(c), Ref(b), selldown_time, d_selldown_time)
    vol = perturbation_volatility.(
        Ref(sigma1),
        Ref(sigma),
        Ref(gamma),
        selldown_time,
        d_selldown_time)
    std_price_diff = price_diff ./ vol
    for i = 1:n
        std_price_diff[i] ~ TDist(3)
    end
end

```

When I try sampling from this model, it never finishes even with a trivial amount of data, and when I try running the MAP to test, each of the individual parameters (c, b, \sigma, \sigma\_{1}, \gamma) all converge to exactly their prior modes. This indicates to me that I must be defining the likelihood in a way such that its gradient is exactly the zero vector.

My guess is that for Turing to pick up on a likelihood, it must have an argument to the left of the sampling statement, or else it just defines a new parameter, even if its been defined in the model scope. What would be the right way to write these kinds of reparameterizations?

---

<div class="post-metadata">

**Author:** ![sethaxen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sethaxen/32/35604_2.png) [@sethaxen](https://discourse.julialang.org/u/sethaxen)\
**Post date:** [April 19, 2022, 8:58am UTC](https://discourse.julialang.org/t/use-of-intermediate-variables-to-condition-likelihood/78313/2 "2022-04-19T08:58:18Z")

</div>

In Turing, it is not possible for the left-hand side of a tilde expression to be a function of sampled parameters. In general, I think this would violate conditional probability. Since your transformation is affine, you can instead transform `TDist`. i.e. `a + bx ~ TDist(ν)` is equivalent to `x ~ (TDist(ν) - a) / b` with Distributions’s syntax/

So I think something like this would be equivalent to what you have:

```julia
d_price .~ (TDist(3) .+ (d_price .- price_diff)) .* vol

```
