How to sample many ordered iid variables in turing

swsw
Fri, 21 Mar 2025 07:12:12 +0000
Probabilistic Programming

Hi there,

I’d like to sample many ordered iid varialbles but i find when the number of samples is large the efficiency gets very low. For the subsequent code it works when the number of variables is 30 or so but not when it exceeds 40.

I specify that all of these variables X ~ Uniform(0,1) iid and that X[1]<X[2]<…<X[n]. I know this can be done by Dirichelet distribution but I want to an ordered Uniform is because I want to check the consistency between the results and the analytically calculation easily. I also know this can be done by just sampling n samples then ordering them but I just want to see how Turing.@addlogprob! -Inf can be used to handle it.

Is there a good solution to it?

using Turing, Distributions

@model function demo()
X ~ filldist(Uniform(0, 1), n)

for i in 1:(length(X)-1)
if !(X[i] < X[i+1])
Turing.@addlogprob! -Inf
return
end
end

return X
end

n=50 # n=30 works
n_samples = 10000
r = sort(rand(n)) * 0.8

@time chain = sample(demo(), NUTS(), n_samples, burnin=n_samples*0.5; thinning=20, progress=true, initial_params=r)

3 posts - 2 participants