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

match inner and outer keys #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

match inner and outer keys #46

wants to merge 2 commits into from

Conversation

mzgubic
Copy link
Collaborator

@mzgubic mzgubic commented Nov 8, 2022

Closes #41

@codecov
Copy link

codecov bot commented Nov 8, 2022

Codecov Report

Merging #46 (d3e3cd0) into main (479bf73) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main       #46   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           89        96    +7     
=========================================
+ Hits            89        96    +7     
Impacted Files Coverage Δ
src/KeyedDistributions.jl 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@@ -54,6 +54,12 @@ for T in (:Distribution, :Sampleable)
"lengths of key vectors $key_lengths must match " *
"size of distribution $(_size(d))"
))
if d isa Distribution && mean(d) isa KeyedArray && !(axiskeys(mean(d)) == keys)
Copy link
Member

@glennmoy glennmoy Nov 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't we run into the same issue with cov? or any other function that might be return a KeyedArray?

Would it be better to rekey the underlying parameters and just mask the exception altogether?

I feel like wrapping a MvN around a KeyedArray, then wrapping that in a KeyedDistribution is akin to rekeying anyway?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we won't need to look at cov actually

julia> mvn = MvNormal(ka, sigma);

julia> mean(mvn)
1-dimensional KeyedArray(NamedDimsArray(...)) with keys:
   t  3-element Vector{String}
And data, 3-element Vector{Float64}:
 ("a")  0.3981265148452625
 ("b")  0.23250652731094346
 ("c")  0.09181862970601418

julia> cov(mvn)
3×3 Matrix{Float64}:
 0.0702741  0.0       0.0
 0.0        0.689994  0.0
 0.0        0.0       0.800233

# which is because of

julia> cholesky(sigma)
Cholesky{Float64, Matrix{Float64}}
U factor:
3×3 UpperTriangular{Float64, Matrix{Float64}}:
 0.265093  0.0       0.0
          0.830659  0.0
                   0.894558

and yes this

Would it be better to rekey the underlying parameters and just mask the exception altogether?

is a good idea

Copy link
Collaborator Author

@mzgubic mzgubic Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to rekey the underlying parameters and just mask the exception altogether?

I think this is actually quite hard. I think we would have to recreate the underlying distribution with the keyless_unnamed arrays. The issue is that constructors for distributions differ. We could possibly do something nasty like keyless_unname.(fieldnames...) and call the default constructor but it sounds like asking for trouble.

Does that leave us with throwing an exception? Or should we simply continue ostriching the issue of mismatching inner and outer keys?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to prevent confusion where possible, so support having a

  1. add the mean sanity check to give a warning/exception generally
  2. rekey for common distributions such as MvNormal (with warning)

Support can then be extended to other distributions as need arises, and we can document the care that should be taken when using a general distribution with keyed fields

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 this pull request may close these issues.

KeyedDistribution allows mismatch between the keys and the underlying distribution keys
3 participants