Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A vector, where all elements are bounded between 0 and 1, and sum(vector)=1. #56

Open
murrellb opened this issue Sep 3, 2022 · 1 comment

Comments

@murrellb
Copy link

murrellb commented Sep 3, 2022

Love the package.

I often have components of models where I need to parameterize a categorical probability distribution. I use the following trick:

σ(z::Real) = one(z) / (one(z) + exp(-z))
function unconstrained2probvec(v)
    l = length(v)
    remaining = 1.0
    o = zeros(l+1)
    for i in 1:l
        #log(l+1-i) is a "balancing" shift to ensure that a vector of zeros givens you [1/N ... 1/N]
        o[i] = remaining*σ(v[i] - log(l+1-i))
        remaining -= o[i]
    end
    o[end] = max(0.0,remaining)
    return o
end

This takes a vector of N-1 unbounded values and returns a vector of N values, bounded between 0 and 1, that sums to 1. When input is all zeros, the output is all 1/N.

I'm too stupid to figure out how to build this into your package, but if you think it would be sensible to do so, please consider this a feature request.

@willtebbutt
Copy link
Member

I would also be keen to have this as a feature, thanks for bringing it up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants