Skip to content

ENH: Implement Multivariate Rejection Sampling (MRS) - #738

Merged
Lucas-Prates merged 20 commits into
developfrom
enh/MRS
Apr 12, 2025
Merged

Lucas-Prates merged 20 commits into
developfrom
enh/MRS

Conversation

@Lucas-Prates

@Lucas-Prates Lucas-Prates commented Nov 28, 2024 •

Copy link
Copy Markdown
Contributor

Pull request type

  • Code changes (bugfix, features)

Checklist

  • Unit tests for the changes have been added
  • Integration tests for the changes have been added
  • Docs have been reviewed and added / updated
  • Lint (black rocketpy/ tests/) has passed locally
  • All tests (pytest tests -m slow --runslow) have passed locally
  • CHANGELOG.md has been updated (if relevant)
  • RST documentation
  • Monte Carlo comparison Feature

New behavior

This PR implements the MRS requested in #162 and described in RocketPy paper.

Breaking change

  • Yes (perhaps)

Additional information

@Lucas-Prates
Lucas-Prates requested a review from a team as a code owner November 28, 2024 21:36
@Lucas-Prates Lucas-Prates added Enhancement New feature or request, including adjustments in current codes Monte Carlo Monte Carlo and related contents labels Nov 28, 2024
@Lucas-Prates Lucas-Prates linked an issue Nov 28, 2024 that may be closed by this pull request
@Lucas-Prates
Lucas-Prates marked this pull request as draft November 28, 2024 21:38
@codecov

codecov Bot commented Nov 28, 2024 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 82.80543% with 38 lines in your changes missing coverage. Please review.

Project coverage is 79.76%. Comparing base (4df0b38) to head (3530eed).
Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
rocketpy/plots/monte_carlo_plots.py 80.00% 16 Missing ⚠️
rocketpy/prints/monte_carlo_prints.py 57.69% 11 Missing ⚠️
...ketpy/simulation/multivariate_rejection_sampler.py 89.81% 11 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #738      +/-   ##
===========================================
+ Coverage    79.11%   79.76%   +0.64%     
===========================================
  Files           96       97       +1     
  Lines        11575    11877     +302     
===========================================
+ Hits          9158     9474     +316     
+ Misses        2417     2403      -14     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Lucas-Prates

Copy link
Copy Markdown
Contributor Author

This PR is ready for a first "Design Review." I would like to get your opinion if this implementation provides what you think on how the user should use the MRS.

Albeit a implementation as a function seems natural, I implemented as a class because:

  1. the function would be humongous;
  2. if the user wants to resample several times, the data monte carlo data is only read once from the harddrive.

It currently works as follows:

  1. Input: monte carlo filepath prefix, mrs filepath prefix, distribution dictionary;
  2. Load input and output data from a monte carlo simulation into memory (python objects - lists of jsons);
  3. To avoid having to read data twice, while loading, precompute some important properties required in the sampler algorithm;
  4. Select and save iteratively accepted samples;
  5. Output: files are saved in the same "scheme" as the MonteCarlo simulation.

I provided a quick and dirty notebook, which will be removed, just to show how the class is being used at the moment.

@Gui-FernandesBR Gui-FernandesBR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have much time so I'll be short

  • The class dies after you sample the data once, this makes it pointless to have a class
  • Instead I'd remove the sample dictionary from arguments.
  • First read the data during initialization. Then you use a function to set the variables you are going to allow be varied. This way u can already anticipate which variables may be varied.
  • We want almost instant results for a MonteCarlo simulation after using MRS.
  • Other thing is that the user must supply the original pdf. Could we possile estimate this from data? (imagine 70k)
  • Finally, plotting is crucial for MRS, or even tables. We'd love to see more on that later.

Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
@Lucas-Prates

ghost commented Feb 24, 2025 •

Copy link
Copy Markdown
Contributor Author

This PR is ready for review again. Changes from last time:

  1. addressed some of the previous suggestions of @phmbressan;
  2. added a comparison_info method that provides a statistical summary print comparison of the results of two monte carlo simulations, similar to the method used in the regular monte carlo class;
  3. added a comparison_plots method that plots boxplots and histograms comparison of the results of two monte carlo simulations;
  4. added a compare_ellipses method that plots the ellipses for the apogee and landing point comparison of the results of two monte carlo simulations;
  5. added the mrs.rst file to the documentation explaining what is the MRS and how to use it;
  6. added the MultivariateRejectionSampler class .rst documentation;
  7. removed the test_mrs.ipynb notebook since the .rst file does the same in a much clearer way;
  8. on my pc, the MRS is 500x faster than running a monte carlo simulation of the equivalent sample size.

For review, I recommend mostly checking the .rst documentation. Here are some previews of the comparisons if you do not have the time to compile the html:

Print comparison:
image

Plot comparison:
image

Ellipses comparison:
image

@Lucas-Prates

ghost commented Feb 24, 2025

Copy link
Copy Markdown
Contributor Author

A second comment: I made the design choice to implement the comparison methods in the MonteCarlo class instead of the MultivariateRejectionSampler. Here are the most relevant arguments:

  1. It makes sense to compare monte carlo simulations even outside of MRS, i.e. even one is not a sub-sample of the other. Hence, implementing this inside the MRS would make this usage extremely odd;
  2. It keeps the MultivariateRejectionSampler class implementation really simple and with only one job: to sample.

One option is, of course, to create another class for that job, but it did not seem to be the best option.

@Gui-FernandesBR Gui-FernandesBR changed the title ENH: Implementing Multivariate Rejection Sampling (MRS) in RocketPy ENH: Implement Multivariate Rejection Sampling (MRS) Feb 25, 2025
@Lucas-Prates

ghost commented Feb 27, 2025

Copy link
Copy Markdown
Contributor Author

I have addressed the suggestions made by @Gui-FernandesBR, implemented unit and integration tests for the MRS, and updated the CHANGELOG.

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@Gui-FernandesBR

ghost commented Feb 27, 2025

Copy link
Copy Markdown
Member

@Lucas-Prates good work. Please squash and merge at your earliest convinience

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is really good overall! Great job

Comment thread docs/user/mrs.rst
Comment thread docs/user/mrs.rst Outdated
Comment thread docs/user/mrs.rst Outdated
Comment thread docs/user/mrs.rst
Comment thread rocketpy/plots/monte_carlo_plots.py Outdated
Comment thread rocketpy/plots/monte_carlo_plots.py Outdated
Comment on lines +373 to +389
plt.scatter(
original_apogee_x,
original_apogee_y,
s=5,
marker="^",
color="green",
label="Original Apogee",
)
plt.scatter(
original_impact_x,
original_impact_y,
s=5,
marker="v",
color="blue",
label="Original Landing Point",
)

ghost Mar 2, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Improve colors of the points/ellipses. Using oposite colors is probably best

ghost Mar 3, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There's no such thing as "opposite colors", but matplotlib does offer a fair good guide to selecting colormaps: https://matplotlib.org/stable/users/explain/colors/colormaps.html

I suggest blue/orange or blue/red.

Ideally the user should be able to select their specific color... But I understand the current limitations.

ghost Mar 4, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess the right term in complementary not "oposite". Anyway, I meant something like this:
image

ghost Mar 18, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will use then a "tetradic" color combination I found in this site. Here is the pallete I got, just for reference:

image

ghost Mar 29, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the tetradic colors for the ellipses plot:
image

@Lucas-Prates

ghost commented Mar 31, 2025

Copy link
Copy Markdown
Contributor Author

This PR is ready again for review. Since last time:

1 - addressed the points in Stano's review about documentation and color;
2 - removed some input checks which I think were not that useful but were a bit trick to do correctly;
3 - modified the flatten_dict function to better handle variables in inner levels.

The most important point is 3 since it might introduce a breaking change.

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All looking good to me, great implementation.
I don't see the changes to flatten_dict as a problem.
Nobody is using that function right now.

Waiting for @MateusStano 's final comments so we can proceed.

Comment thread rocketpy/simulation/multivariate_rejection_sampler.py Outdated
@Gui-FernandesBR
Gui-FernandesBR requested a review from Copilot April 7, 2025 13:56

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot reviewed 14 out of 21 changed files in this pull request and generated 1 comment.

Files not reviewed (7)
  • .vscode/settings.json: Language not supported
  • docs/notebooks/monte_carlo_analysis/monte_carlo_analysis_outputs/mrs.outputs.txt: Language not supported
  • docs/reference/classes/MultivariateRejectionSampler.rst: Language not supported
  • docs/reference/index.rst: Language not supported
  • docs/user/index.rst: Language not supported
  • docs/user/mrs.rst: Language not supported
  • docs/user/sensitivity.rst: Language not supported
Comments suppressed due to low confidence (1)

rocketpy/tools.py:595

  • [nitpick] Consider renaming 'flatted_dict' to 'flattened_dict' for clarity and consistency with common terminology.
flatted_dict = {}

Comment thread rocketpy/plots/monte_carlo_plots.py
Comment thread rocketpy/plots/monte_carlo_plots.py Outdated
Comment thread rocketpy/plots/monte_carlo_plots.py Outdated
@Lucas-Prates
Lucas-Prates merged commit 9f2644a into develop Apr 12, 2025
@Lucas-Prates
Lucas-Prates deleted the enh/MRS branch April 12, 2025 09:27
@github-project-automation github-project-automation Bot moved this from Next Version to Closed in LibDev Roadmap Apr 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement New feature or request, including adjustments in current codes Monte Carlo Monte Carlo and related contents

Projects

No open projects
Status: Closed

Development

Successfully merging this pull request may close these issues.

ENH: Implement MRS method on RocketPy!

5 participants