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

type stability of vector of distributions #561

Open
biona001 opened this issue May 17, 2024 · 0 comments
Open

type stability of vector of distributions #561

biona001 opened this issue May 17, 2024 · 0 comments

Comments

@biona001
Copy link

biona001 commented May 17, 2024

I have a vector y which is a Vector{Float64}, but each y[i] is sampled from a different univariate distribution specified by vecdist[i]. I want to compute some loglikelihood function.

I was surprised functions like GLM.linkinv and GLM.loglik_obs are not type stable? Is this expected behavior?

MWE

using Distributions
using GLM
using Random
import GLM.loglik_obs, GLM.linkinv

function component_loglikelihood(y, vecdist, veclink, η)
    logl = zero(eltype(y))
    for j in eachindex(y)
        dist = vecdist[j]
        link = veclink[j]
        μ_j = GLM.linkinv(link, η[j])
        logl += GLM.loglik_obs(dist, y[j], μ_j, 1.0, 1.0)
    end
    return logl
end

# simulate data
n = 10
vecdist = rand([Bernoulli(), Poisson(), Normal()], n)
veclink = canonicallink.(vecdist)
y = [rand(dist) for dist in vecdist] |> Vector{Float64}
η = randn(n)

# check type
@code_warntype component_loglikelihood(y, vecdist, veclink, η)

The output is shown below (apologize for the picture, copy-pasting into github won't highlight the red colors)

Screenshot 2024-05-17 at 2 09 33 PM

From the picture, logl::Any even though I initialized it as zero(eltype(y)), and μ_j::Any even though μ_j=GLM.linkinv(link, η[j]).

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

1 participant