Skip to content

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

Open
@murrellb

Description

@murrellb

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions