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

"Wrong" order from names(chain, :parameters) #326

Closed
knuesel opened this issue Aug 12, 2021 · 3 comments · Fixed by #332
Closed

"Wrong" order from names(chain, :parameters) #326

knuesel opened this issue Aug 12, 2021 · 3 comments · Fixed by #332

Comments

@knuesel
Copy link

knuesel commented Aug 12, 2021

Consider this example:

@model function mymodel(x)
    σ ~ LogNormal()
    a ~ Normal()
    c ~ Normal()
    b ~ Normal()
    x .~ Normal(a+b+c, σ)
end

model = mymodel(randn(1000))
ch = sample(model, NUTS(0.65), 100)
plot(ch)

This plots the parameters in the order σ, a, c, b. This matches the order of parameters in the underlying AxisArray. However, retrieving the parameter list with names gives a different order:

julia> names(ch, :parameters)
4-element Vector{Symbol}:
 :a
 :b
 
 :c

My use case: I want to add things on the plots created by plot(ch). To modify the plot for the ith parameter I can do plot!(..., subplot=2i). But I can't find a good way to get the list of parameters in the right order...

@cpfiffer
Copy link
Member

Part of this is because names is intended to be used to get out parameters that fall in different sections, so it adds things in a weird order:

MCMCChains.jl/src/chains.jl

Lines 520 to 526 in b1e4c0e

function Base.names(c::Chains, sections)
names = Symbol[]
for section in sections
append!(names, c.name_map[section])
end
return names
end

Could this work for you?

chain.value.axes[2].val

@knuesel
Copy link
Author

knuesel commented Aug 12, 2021

But I'm getting the names from one specific section (:parameters) :

Base.names(chains::Chains, section::Symbol) = convert(Vector{Symbol}, chains.name_map[section])

so it's not clear why the order would be wrong?

Thanks for the suggestion! I think it's equivalent to names(chain) ? It returns the right order, but with lots of other values. So the following workaround seems to work:

params = names(ch)[1:length(names(ch, :parameters))]

@devmotion
Copy link
Member

The MWE is fixed by #332. With the PR one gets

julia> names(ch, :parameters)
4-element Vector{Symbol}:
 
 :a
 :c
 :b

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

Successfully merging a pull request may close this issue.

3 participants