# Constrained ordered prior for ordered logistic model

**URL:** <https://discourse.julialang.org/t/constrained-ordered-prior-for-ordered-logistic-model/55670>\
**Category:** Probabilistic Programming\
**Tags:** turing, distributions, bayesian-inference\
**Created:** [February 20, 2021, 11:09am UTC](https://discourse.julialang.org/t/constrained-ordered-prior-for-ordered-logistic-model/55670 "2021-02-20T11:09:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal](https://avatars.discourse-cdn.com/v4/letter/n/49beb7/32.png) [@Neal](https://discourse.julialang.org/u/Neal)\
**Post date:** [February 20, 2021, 11:09am UTC](https://discourse.julialang.org/t/constrained-ordered-prior-for-ordered-logistic-model/55670/1 "2021-02-20T11:09:57Z")

</div>

Hello, I have a task for modeling ordered discrete response outcome but I struggled to make my prior ordered for ordered logistic model so `theta[6] > theta[5] > theta[4] > theta[3] > theta[2] > theta[1]`. In Stan, I can declare parameters `ordered[6] theta` so it’s constraint that way.

```nohighlight
data{
    int y[120];
}

parameters{
    ordered[6] theta;
}

model{
    theta ~ normal(0,1);
    for (i in 1:120) {
        y[i] ~ ordered_logistic(0,theta);
    }
}

```

In Turing, how do I reproduce above?

```nohighlight
y = [6, 5, 4, 3, 2, 1,
     1, 2 ,3 ,4 ,5 ,6]
y = repeat(y, 10)

@model function gdemo(y)
    theta ~ filldist(Normal(1, 2),6)
    y .~ OrderedLogistic(0, theta)
end
chn = sample(gdemo(y), NUTS(), 1000)

julia> ERROR: TaskFailedException: cutpoints are not sorted

```

---

<div class="post-metadata">

**Author:** ![devmotion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devmotion/32/9201_2.png) [@devmotion](https://discourse.julialang.org/u/devmotion)\
**Post date:** [February 20, 2021, 1:07pm UTC](https://discourse.julialang.org/t/constrained-ordered-prior-for-ordered-logistic-model/55670/2 "2021-02-20T13:07:25Z")

</div>

I guess you can transform the unordered vector `theta` to an ordered one with some transformation. E.g., you could use the same transformation as Stan: [10.6 Ordered vector | Stan Reference Manual](https://mc-stan.org/docs/2_26/reference-manual/ordered-vector.html)

---

<div class="post-metadata">

**Author:** ![BradGroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradgroff/32/21156_2.png) [@BradGroff](https://discourse.julialang.org/u/BradGroff)\
**Post date:** [February 20, 2021, 1:47pm UTC](https://discourse.julialang.org/t/constrained-ordered-prior-for-ordered-logistic-model/55670/3 "2021-02-20T13:47:14Z")

</div>

It can be done with an intermediate variable, something like this:

```julia
@model function gdemo(y)
    x ~ filldist(AStrictlyPositiveDistribution, 6)
    theta = cumsum(x)
    y .~ OrderedLogistic(0, theta)
    return theta
end

```

I haven’t run the code so probably it doesn’t work without some syntactical massaging but the idea works. The `return` statement is important too since otherwise you won’t have `theta` in your traces. You could of course reconstruct with the same logic.

---

<div class="post-metadata">

**Author:** ![alexten](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexten/32/209104_2.png) [@alexten](https://discourse.julialang.org/u/alexten)\
**Post date:** [August 2, 2024, 9:16pm UTC](https://discourse.julialang.org/t/constrained-ordered-prior-for-ordered-logistic-model/55670/4 "2024-08-02T21:16:47Z")

</div>

> [@Neal](#):
>
> ```julia
> y = [6, 5, 4, 3, 2, 1,
> 1, 2 ,3 ,4 ,5 ,6]
> y = repeat(y, 10)
> 
> @model function gdemo(y)
> theta ~ filldist(Normal(1, 2),6)
> y .~ OrderedLogistic(0, theta)
> end
> chn = sample(gdemo(y), NUTS(), 1000)
> 
> ```

I found using a Dirichlet prior useful. Something like:

```julia
odds(p) = p / (1 - p)

@model function gdemo(y)
    q ~ Dirichlet(fill(1, 6))
    theta = log.(odds.(cumsum(q[1:end-1])))
    y .~ OrderedLogistic(0, theta)
end

```

Then you get posterior estimates of the probabilities of each response category (i.e., `q[i]` will be Pr(Y \leq i) - Pr(Y \leq i-1)).
