Custom multivariate distribution in Turing.jl

There were a few issues that I fixed. First, upon closer inspection, it is clear that x1 is a vector of vectors. I updated the code to reflect that. Also, I introduced an error in defining probs, which is now fixed. The function nonneg was not defined, so I added a definition. Finally, size(oc)[2] makes an unnecessary memory allocation. The changing it to size(oc, 2) fixes that minor performance issue.

The code below runs without an error. It should work with Turing as long as all elements should be of type T.

Summary
using Distributions 

nonneg(x) = x >= zero(x)

struct OrthoNNDist{T<:Real} <: DiscreteMultivariateDistribution
	x0::Vector{T}
	oc::Array{T,2}
	x1s::Vector{Vector{T}}
	prob::T
end

#return a new uniform distribution with all vectors in x1s orthogonal to oc
function OrthoNNDist(x0::Vector{T}, oc::Array{T,2}) where {T<:Real}
    x1s = Vector{Vector{T}}(undef,0)
    for i = 1:size(oc, 2)
        x1 = x0 + oc[:, i]
        if nonneg(x1)
            push!(x1s, x1)
        end
        x1 = x0 - oc[:, i]
        if nonneg(x1)
            push!(x1s, x1)
        end
    end
    prob::T = 1.0 / length(x1s)
    return OrthoNNDist(x0, oc, x1s, prob)
end

oc = [3.0 2.0 1.0; 0.0 1.0 1.0]
x0 = [1.0;2.0]

dist = OrthoNNDist(x0, oc)
1 Like