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

mean errors if distribution keys and the keys of the underlying array are inconsistent #28

Closed
mzgubic opened this issue May 26, 2022 · 2 comments

Comments

@mzgubic
Copy link
Collaborator

mzgubic commented May 26, 2022

I think the error in the following case is the right thing to do.

I wonder whether we should error earlier, when constructing the KeyedDistribution, and never end up with this situation in the first place?

julia> ka = KeyedArray(rand(3), down=["a", "b", "c"])
1-dimensional KeyedArray(NamedDimsArray(...)) with keys:
   down  3-element Vector{String}
And data, 3-element Vector{Float64}:
 ("a")  0.96797267150684
 ("b")  0.812350723246477
 ("c")  0.3433194732682169

julia> kd = KeyedDistribution(MvNormal(ka, [1, 1, 1]), [1, 2, 3])
KeyedMvNormal{MvNormal{Float64, PDMats.PDiagMat{Float64, Vector{Float64}}, KeyedArray{Float64, 1, NamedDimsArray{(:down,), Float64, 1, Vector{Float64}}, Base.RefValue{Vector{String}}}}}(
d: MvNormal{Float64, PDMats.PDiagMat{Float64, Vector{Float64}}, KeyedArray{Float64, 1, NamedDimsArray{(:down,), Float64, 1, Vector{Float64}}, Base.RefValue{Vector{String}}}}(
dim: 3
μ: [0.96797267150684, 0.812350723246477, 0.3433194732682169]
Σ: [1.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 1.0]
)

keys: ([1, 2, 3],)
)


julia> mean(kd)
ERROR: ArgumentError: key vectors must agree; got ["a", "b", "c"] != [1, 2, 3]
Stacktrace:
 [1] unify_one
   @ ~/.julia/packages/AxisKeys/rTIBR/src/broadcast.jl:81 [inlined]
 [2] map
   @ ./tuple.jl:246 [inlined]
 [3] unify_keys
   @ ~/.julia/packages/AxisKeys/rTIBR/src/broadcast.jl:73 [inlined]
 [4] KeyedArray(A::KeyedArray{Float64, 1, NamedDimsArray{(:down,), Float64, 1, Vector{Float64}}, Base.RefValue{Vector{String}}}, k2::Tuple{Vector{Int64}})
   @ AxisKeys ~/.julia/packages/AxisKeys/rTIBR/src/struct.jl:36
 [5] mean(d::KeyedMvNormal{MvNormal{Float64, PDMats.PDiagMat{Float64, Vector{Float64}}, KeyedArray{Float64, 1, NamedDimsArray{(:down,), Float64, 1, Vector{Float64}}, Base.RefValue{Vector{String}}}}})
   @ KeyedDistributions ~/JuliaEnvs/RiskModels/dev/KeyedDistributions/src/KeyedDistributions.jl:184
 [6] top-level scope
   @ REPL[92]:1
@glennmoy
Copy link
Member

related to #17

@mzgubic
Copy link
Collaborator Author

mzgubic commented Nov 3, 2022

This no longer breaks, it uses the keys from the KeyedDistribution.

@mzgubic mzgubic closed this as completed Nov 3, 2022
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