8000 Added Brownian motion class for manifolds by dralston78 · Pull Request #1938 · geomstats/geomstats · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added Brownian motion class for manifolds #1938

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

Merged
merged 9 commits into from
Mar 8, 2024

Conversation

dralston78
Copy link
Contributor
@dralston78 dralston78 commented Jan 25, 2024

Checklist

  • My pull request has a clear and explanatory title.
  • If necessary, my code is vectorized.
  • I added appropriate unit tests.
  • I made sure the code passes all unit tests. (refer to comment below)
  • My PR follows PEP8 guidelines. (refer to comment below)
  • My PR follows geomstats coding style and API.
  • My code is properly documented and I made sure the documentation renders properly. (Link)
  • I linked to issues and PRs that are relevant to this PR.

Description

I implemented a class to generate a realization of a Brownian motion walk on a manifold in intrinsic coordinates. While Brownian motion on a manifold is often defined as a Stratonovich stochastic differential equation (SDE), I have used the converted form to an Ito SDE in order to use the Euler–Maruyama method. I added a unit test to verify the axioms of Brownian motion in Euclidean space.

Issue

Additional context

This class requires the pytorch backend.

The method to generate a path requires two conditions on the underlying manifold. First, the manifold must be described in terms of its intrinsic coordinates. Second, the manifold must be equipped with a metric that has a cometric_matrix method and a christoffels method.

I proposed this idea to Professor Miolane at the end of her seminar class earlier this year, and she encouraged me to create an implementation. However, as I don't have a background in software engineering, I wouldn't be surprised if there are some bad practices in my code/tests, or other flaws. I'm looking forward to getting your feedback and learning more!

Copy link
codecov bot commented Jan 25, 2024

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (db4c02e) 91.23% compared to head (16e8d90) 91.53%.

Files Patch % Lines
geomstats/distributions/brownian_motion.py 95.46% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1938      +/-   ##
==========================================
+ Coverage   91.23%   91.53%   +0.31%     
==========================================
  Files         142      149       +7     
  Lines       12822    13573     +751     
==========================================
+ Hits        11697    12423     +726     
- Misses       1125     1150      +25     
Flag Coverage Δ
autograd 87.52% <95.46%> (+0.03%) ⬆️
numpy 90.00% <27.28%> (-<0.01%) ⬇️
pytorch 85.43% <95.46%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link
Collaborator
@luisfpereira luisfpereira left a comment

Choose a reason for hiding this comment

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

Nice @dralston78!

I've add a couple of commit changing the following (let me know if you disagree with something):

  • space.metric instead of .metric because if space is equipped with another metric at a later stage, then it is taken into consideration by the code; otherwise it is ambiguous. (Same with space.dim)

  • I've removed the check on pytorch because someone may have closed form solutions for christoffels and cometric_matrix. if they don't have, then the existing error messages should be enough for users understand they need to change to autograd/pytorch

  • I've removed the check space.default_coords_type != "intrinsic" because unfortunately this flag is misused and sometimes some spaces have intrinsic coordinates, but have a string specifying which kind of coordinates it refers to (e.g. PoincareBall -> "ball"). It is related with Change default_coords_type to boolean flag intrinsic=True #1725. I'll do such a change asap.

  • I've removed the checks not hasattr(space.metric, "christoffels") and not hasattr(space.metric, "cometric_matrix") because these methods always exist, though usually raise NotImplementedError. This is a design issues to which we haven't yet found a solution

  • I've removed the check on christoffels and cometric_matrix shapes because I don't want to pay the price of running both once only for shape checking.

  • for the reasons above, we are not doing checks and assume people know when they can use BrownianMotion - after fixing intrinsic, we can to an instantiation time check that will be equivalent to all the current tests

  • I've added a name to the reference in the docstring, so that we can reference it in the text in case we need.

  • I've updated sample_path docstrings.

  • I've collected paths in a list and then used stack so that we avoid instantiating an array of zeros

  • I've vectorized sample_path, i.e. allowed it to receive multiple points, by adding ellipses to gs.einsum and ensuring we sample independently for each point.

Regarding tests:

  • I've done small changes, but no update in the behavior (they're very nice!)

Copy link
Collaborator
@ninamiolane ninamiolane left a comment

Choose a reason for hiding this comment

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

LGTM! Just minor comments on the docstrings + need "unit" tests.


@autograd_and_torch_only
@pytest.mark.slow
class TestBrownianMotion(TestCase):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unit-tests should be unit, ie we should have at least one unit test per function.

Missing test_sample_path and missing test__step

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @ninamiolane, thank you for this clarification! With regards to this comment, would the ideal structure be to have a test class TestBrownianMotion, and then have this class have two methods test_sample_path and test__step?

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 tests are already very good here, @ninamiolane. Two reasons: 1) this computation is relatively slow, so it is better to not add too many redundant tests; 2) there's randomness involved (we are sampling from a normal), which mean we cannot do a "normal test" (the checks done by @dralston78 are pretty clever).

@luisfpereira
Copy link
Collaborator

@dralston78, with #1943 merged, we can now check for intrinsic (a boolean; it should be True for BrownianMotion use) at instantiation time (we may still need to check if the flag is properly used everywhere, but that can be done afterwards).

@dralston78
Copy link
Contributor Author

@dralston78, with #1943 merged, we can now check for intrinsic (a boolean; it should be True for BrownianMotion use) at instantiation time (we may still need to check if the flag is properly used everywhere, but that can be done afterwards).

Great! Would you like me to add a private method _instantiation_check that accomplishes the same checks I originally had? Or should we assume users are aware of the requirements?

@luisfpereira
Copy link
Collaborator

@dralston78, adding back check_coordinates taking into account how intrinsic works now will do the job.

…ifold is parameterized in terms of intrinsic coordinates.
@luisfpereira
Copy link
Collaborator

@ninamiolane, this looks ready to me. Can we merge?

@luisfpereira luisfpereira merged commit 642d1ac into geomstats:main Mar 8, 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
0