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

axes(::ReshapedDistribution) should throw, added missing ndims #1892

Closed
wants to merge 12 commits into from

Conversation

stevengj
Copy link
Contributor

@stevengj stevengj commented Aug 30, 2024

As noted on discourse, this method was missing. Also added ndims.

Update: from the discussion below, since d::ReshapedDistribution is not indexable, it seems that the actual problem is that axes(d) is defined at all thanks to the generic fallback in Base. So, this PR is updated to throw a MethodError.

Update: @devmotion preferred not to have axes(d) throw an error, so I removed it.

@devmotion
Copy link
Member

AFAIK we never intended to define or use axes for distributions. ReshapedDistributions is not special at all here - we define length (number of elements of the variates) and size (size of the variates) for all distributions but generally distributions shouldn't be something AbstractArray like.

My impression is that the problem is actually the use of axes - which only works with a single argument (unintentionally) since the completely generic definition in Base falls back to size.

@codecov-commenter
Copy link

codecov-commenter commented Aug 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.99%. Comparing base (e1340f0) to head (adbfadf).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1892   +/-   ##
=======================================
  Coverage   85.99%   85.99%           
=======================================
  Files         144      144           
  Lines        8666     8667    +1     
=======================================
+ Hits         7452     7453    +1     
  Misses       1214     1214           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@devmotion
Copy link
Member

It seems maybe the actual problem was a typo in the Turing model: https://discourse.julialang.org/t/axes-not-working-on-reshaped-array/118820/5?u=devmotion

@stevengj
Copy link
Contributor Author

stevengj commented Aug 30, 2024

If you have size, shouldn't you have axes? Or does it not support indexing? If you don't want axes(a, k), probably axes(a) should be redefined to throw a MethodError to circumvent Base's definition.

Should probably still have ndims?

@stevengj stevengj changed the title add missing axes(::ReshapedDistribution, k) method axes(::ReshapedDistribution) should throw, added missing ndims Aug 30, 2024
@devmotion
Copy link
Member

I'm not sure, I tend to think it's fine as it is.

Distributions are not arrays and therefore we don't implement the array interface (and hence I think we might also not want to implement ndims). That should also be clear from the docs.

Distributions support length and size, and both of them are not array-specific (eg in Base they are also implemented for Number and AbstractChar). That should also be clear from the docs.

Apparently someone decided to implement axes in a very generic way, for any type and only relying on size. But that doesn't really do any harm - it doesn't error but also it's not mentioned anywhere in the docs, it's not part of the distributions interface, so users shouldn't rely on it.

Anyone could write generic functions that only depend on eg length or size but might not make sense for distributions (there might be more in Base?) - it seems infeasible to implement a MethodError for all these cases?

@stevengj
Copy link
Contributor Author

It seems odd to implement size and not ndims, and you seemed to be missing an easy generic API to get this information.

But I'm fine with removing axes if that's what you want.

@devmotion
Copy link
Member

It seems odd to implement size and not ndims, and you seemed to be missing an easy generic API to get this information.

AFAIK it hasn't been requested yet, so I assume therefore it's not defined. Since the API of distributions already is quite large, I would like to not add to it without clear need.

Note also that size is defined for all distributions (but special in so far as it refers to the size of the variates - whose exact types might not be known - but not the distribution itself), so probably if ndims is added at some point it should be defined more generally.

So I'd suggest closing the PR, now that it has become clear that the initial problem was an incorrectly defined Turing model.

@stevengj stevengj closed this Sep 1, 2024
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.

3 participants