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

Bijectors #16

Merged
merged 7 commits into from
Nov 16, 2023
Merged

Bijectors #16

merged 7 commits into from
Nov 16, 2023

Conversation

williamjameshandley
Copy link
Contributor

@williamjameshandley williamjameshandley commented Nov 13, 2023

Description

Adds bijective functions to lsbi.stats.multivariate_normal and lsbi.stats.mixture_multivariate normal to/from the unit hypercube from/to physical space.

The first of these is a relatively trivial helper function, the second implements this for mixture models. The inverse model is efficient, whilst the forward requires a bisection search. This bisection search is vectorised, and so is efficient for large numbers of points, but relatively slow for a single point.

Checklist:

  • I have performed a self-review of my own code
  • My code is PEP8 compliant (flake8 lsbi tests)
  • My code contains compliant docstrings (pydocstyle --convention=numpy lsbi)
  • New and existing unit tests pass locally with my changes (python -m pytest)
  • I have added tests that prove my fix is effective or that my feature works
  • I have appropriately incremented the semantic version number in both README.rst and lsbi/_version.py

@williamjameshandley
Copy link
Contributor Author

@ThomasGesseyJones and @kilian1103 may be interested in this.

@ThomasGesseyJones
Copy link

Hi @williamjameshandley, this is very cool! Since your f(t) looks to be differentiable you may be able to get a performance increase from using a vetorizable root finding algorithm that uses derivative information e.g. Newton-Rhapson. Although I imagine the example I give is likely an unsuitable choice due to its difficulties, or outright failure, to converge when you have very low gradients.

Copy link

codecov bot commented Nov 14, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f5512d7) 100.00% compared to head (3407775) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #16   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         6    +1     
  Lines          361       416   +55     
=========================================
+ Hits           361       416   +55     

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

@williamjameshandley williamjameshandley merged commit a6191b3 into master Nov 16, 2023
@williamjameshandley williamjameshandley deleted the bijectors branch November 16, 2023 16:37

Parameters
----------
x : array_like, shape (..., d)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this want/benefit stricter shape checking? I note for example if I have a 100D data likelihood I can

model.likelihood(theta).bijector(np.random.rand(1)[...,None])

i.e. I'm passing something of shape (1,1), rather than (100,) or (1,100) and it returns a valid data draw, but I'm not sure what this actually is!

"""Bijector between U([0, 1])^d and the distribution.

- x in [0, 1]^d is the hypercube space.
- theta in R^d is the physical space.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps this docstring could clarify as this method is valid on likelihood or posterior/prior what physical space is. It feels most natural to define this for parameter space (theta doubly suggesting this) transformations or some comment on it's dual usage (if it is intended/makes sense to use on data distributions too)

y = x[..., i]
a = (m - 10 * np.sqrt(c)).min(axis=-1)
b = (m + 10 * np.sqrt(c)).max(axis=-1)
theta[..., i] = bisect(f, a, b)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this would benefit from some inline comments/ more expanded docstring explaining what is going on here as an additional moving part.

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