# How to impose ordered constraints to model variables

**URL:** <https://discourse.julialang.org/t/how-to-impose-ordered-constraints-to-model-variables/36264>\
**Category:** New to Julia\
**Tags:** question, package, turing\
**Created:** [March 20, 2020, 2:39pm UTC](https://discourse.julialang.org/t/how-to-impose-ordered-constraints-to-model-variables/36264 "2020-03-20T14:39:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![takuizum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/takuizum/32/12040_2.png) [@takuizum](https://discourse.julialang.org/u/takuizum)\
**Post date:** [March 20, 2020, 2:39pm UTC](https://discourse.julialang.org/t/how-to-impose-ordered-constraints-to-model-variables/36264/1 "2020-03-20T14:39:56Z")

</div>

Hi, everyone.

Lately, I have been learning Bayesian inference with Julia and Turing and trying to implement IRT(Item response model; logistic regression with latent variables).  
Today, I ask a question related to ordered variables in Turing model.

Now, I want to model polytomous data using Graded Response Model (ordered logistic model with latent variables), which models categorical variables with some ordered threshold parameters.

I referenced a link below,

> **[Ordered Logistic regression and Probabilistic Programming](https://medium.com/@liza_p_semenova/ordered-logistic-regression-and-probabilistic-programming-502d8235ad3f)**
>
> with examples in Stan, PyMC3 and Turing

then wrote the model as described below.

```julia
@model graded(data) = begin 
	N, J = size(data)
    θ = Vector{Real}(undef, N)
	α = Vector{Real}(undef, J)
	β = Vector{Vector}(undef, J)
	β_diff = Vector{Vector}(undef, J)
	for j in 1:J
		cat = sort(unique(skipmissing(data[:,j])))
		K = length(cat)
		α[j] ~ LogNormal(0, 2)
		# Assign normal prior with ordered constraints
		β[j] = Vector{Real}(undef, K-1)
		β_diff[j] = Vector{Real}(undef, length(cat) - 2)
		for k in 1:K - 1
		    if k == 1
		        β[j][k] ~ Normal(-3, 1)
		    else
		        β_diff[j][k-1] ~ Normal(0, 1)
		        β[j][k] = β[j][k - 1] + exp(β_diff[j][k-1])
		    end
		end
	end
	# assign distributon to each element
	for i in 1:N
		θ[i] ~ Normal(0, 1)
	end
   	for i in 1:N
		for j in 1:J
			data[i,j] ~ OrderedLogistic(α[j]*θ[i], β[j])
	    end
    end
end

```

This model seems to work well judging from simulation data, however, the prior settings about β and β\_diff are not so intuitive that customizing prior is some tricky.

So, I come up with to use truncated distributions as follows.

```julia
		β[j] = Vector{Real}(undef, K-1)
		bounds = [-Inf; sort(rand(Normal(0, 2), K-2)); Inf]
		for k in 1:K-1
			β[j][k] ~ truncated(Normal(0, 2), bounds[k], bounds[k+1])
		end

```

But this model returns the error below.

```julia
ERROR: cutpoints are not sorted

```

Do you know the more sophisticated idea to handle ordered categorical variables in Turing?
