Skip to content

ENH: Adaptive Monte Carlo via Convergence Criteria#922

Merged
Gui-FernandesBR merged 9 commits intoRocketPy-Team:developfrom
Malmahrouqi3:enh/adaptive-monte-carlo-convergence
Mar 29, 2026
Merged

ENH: Adaptive Monte Carlo via Convergence Criteria#922
Gui-FernandesBR merged 9 commits intoRocketPy-Team:developfrom
Malmahrouqi3:enh/adaptive-monte-carlo-convergence

Conversation

@Malmahrouqi3
Copy link
Copy Markdown
Member

@Malmahrouqi3 Malmahrouqi3 commented Jan 6, 2026

Pull request type

  • Code changes (bugfix, features)
  • Code maintenance (refactoring, formatting, tests)
  • ReadMe, Docs and GitHub updates

Checklist

  • Tests for the changes have been added (if needed)
  • 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)

Current behavior

#892 where users have to specify a number of simulations for Monte Carlo simulations, which may compel them to set the number as high as possible, exhausting tons of compute inefficiently and needlessly.

New behavior

With the new feature, users can select a specific attribute such that if it meets their desired tolerance midway through, then the rest of sims will instantaneously stop since the acquired flight sims are reasonably sufficient to satisfy the Confidence Interval Width.

test_dispersion.simulate_convergence(
        target_attribute="apogee_time",
        target_confidence=0.95,
        tolerance=0.5,  # in seconds
        max_simulations=1000,
        batch_size=50
    )

Also, here is a quick display of outcomes with Attribute: Apogee (±30 m) & Apogee Time (±0.5 s). Batch size: 50 & 200.
image

Breaking change

  • No

Additional information

I attempted to create unit tests, but I was not successful in that since I could not rely merely on the given input lists. Therefore, I opted for an integration test, where it actually passed locally.

@Malmahrouqi3 Malmahrouqi3 changed the base branch from master to develop January 6, 2026 01:41
@Malmahrouqi3 Malmahrouqi3 marked this pull request as ready for review January 6, 2026 01:52
@Malmahrouqi3 Malmahrouqi3 requested a review from a team as a code owner January 6, 2026 01:52
Copy link
Copy Markdown
Member

@Gui-FernandesBR Gui-FernandesBR left a comment

Choose a reason for hiding this comment

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

The implementation is really clean. I like what I see here!!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces adaptive Monte Carlo simulation functionality that allows simulations to terminate early when a confidence interval criterion is met. Instead of requiring users to specify a fixed number of simulations upfront, the new simulate_convergence() method runs simulations in batches and stops when the confidence interval width for a target attribute falls within a specified tolerance.

Key Changes:

  • Added simulate_convergence() method to MonteCarlo class that runs simulations in batches until convergence or a maximum limit
  • Added integration test for the new convergence simulation method
  • Added example usage in the Monte Carlo notebook documentation

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 15 comments.

File Description
rocketpy/simulation/monte_carlo.py Implements the new simulate_convergence() method that runs batched simulations until confidence interval meets tolerance
tests/integration/simulation/test_monte_carlo.py Adds integration test to verify the convergence simulation behavior
docs/notebooks/monte_carlo_analysis/monte_carlo_class_usage.ipynb Adds example demonstrating usage of the new convergence-based simulation method
CHANGELOG.md Documents the new adaptive Monte Carlo feature

Comment thread rocketpy/simulation/monte_carlo.py
Comment thread rocketpy/simulation/monte_carlo.py
Comment thread rocketpy/simulation/monte_carlo.py
Comment thread rocketpy/simulation/monte_carlo.py Outdated
Comment thread rocketpy/simulation/monte_carlo.py Outdated
Comment thread rocketpy/simulation/monte_carlo.py Outdated
Comment thread rocketpy/simulation/monte_carlo.py Outdated
Comment thread tests/integration/simulation/test_monte_carlo.py Outdated
Comment thread tests/integration/simulation/test_monte_carlo.py
Comment thread rocketpy/simulation/monte_carlo.py
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 6, 2026

Codecov Report

❌ Patch coverage is 8.33333% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.05%. Comparing base (9cf3dd4) to head (87d8652).
⚠️ Report is 49 commits behind head on develop.

Files with missing lines Patch % Lines
rocketpy/simulation/monte_carlo.py 8.33% 11 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #922      +/-   ##
===========================================
+ Coverage    80.27%   81.05%   +0.78%     
===========================================
  Files          104      107       +3     
  Lines        12769    13905    +1136     
===========================================
+ Hits         10250    11271    +1021     
- Misses        2519     2634     +115     

☔ 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.

@Gui-FernandesBR
Copy link
Copy Markdown
Member

@Malmahrouqi3 please address all Copilot`s comments and fix tests/linters on CI so we can review this PR.

Once again I found the implementation clean and nice. I think this is way to go.

@MateusStano
Copy link
Copy Markdown
Member

@Malmahrouqi3 this looks great! Plan on addressing the comments any time soon?

@Malmahrouqi3
Copy link
Copy Markdown
Member Author

@MateusStano been caught up with school, but I am planning to do so next week!

@Gui-FernandesBR
Copy link
Copy Markdown
Member

@MateusStano been caught up with school, but I am planning to do so next week!

Great to hear from you! I will convert the PR to draft. Feel free to open it to reviwers again whenever you finish all the comments.

@Gui-FernandesBR Gui-FernandesBR marked this pull request as draft March 20, 2026 18:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread rocketpy/simulation/monte_carlo.py
Comment thread tests/integration/simulation/test_monte_carlo.py
Comment thread CHANGELOG.md
Comment thread docs/notebooks/monte_carlo_analysis/monte_carlo_class_usage.ipynb
Comment thread rocketpy/simulation/monte_carlo.py
@Malmahrouqi3 Malmahrouqi3 marked this pull request as ready for review March 29, 2026 00:53
Copy link
Copy Markdown
Member

@Gui-FernandesBR Gui-FernandesBR left a comment

Choose a reason for hiding this comment

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

Impressive work!!

@Gui-FernandesBR Gui-FernandesBR merged commit e0173e2 into RocketPy-Team:develop Mar 29, 2026
22 of 23 checks passed
MateusStano pushed a commit that referenced this pull request Apr 4, 2026
* ENH: added a new function (simulate_convergence)

* DOC: added a cell to show simulate_convergence function usage

* TST: integration test for simulate_convergence

* DOC: updated changelog for this PR

* ENH: ran black to lint intg test file

* new fixes thx to copilot comments

* linted rocketpy/simulation/monte_carlo.py

---------

Co-authored-by: Malmahrouqi3 <mohdsaid497566@gmail.com>
MateusStano added a commit that referenced this pull request Apr 6, 2026
* BUG: Fix hard-coded radius value for parachute added mass calculation (#889)

* Fix hard-coded radius value for parachute added mass calculation

Calculate radius from cd_s using a typical hemispherical parachute drag
coefficient (1.4) when radius is not explicitly provided. This fixes
drift distance calculations for smaller parachutes like drogues.

Formula: R = sqrt(cd_s / (Cd * π))

Closes #860

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Address code review: improve docstrings and add explicit None defaults

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Add CHANGELOG entry for PR #889

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Update rocket.add_parachute to use radius=None for consistency

Changed the default radius from 1.5 to None in the add_parachute method
to match the Parachute class behavior. This ensures consistent automatic
radius calculation from cd_s across both APIs.

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Refactor Parachute class to remove hard-coded radius value and introduce drag_coefficient parameter for radius estimation

Fix hard-coded radius value for parachute added mass calculation

Calculate radius from cd_s using a typical hemispherical parachute drag
coefficient (1.4) when radius is not explicitly provided. This fixes
drift distance calculations for smaller parachutes like drogues.

Formula: R = sqrt(cd_s / (Cd * π))

Closes #860

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Add CHANGELOG entry for PR #889

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Refactor Parachute class to remove hard-coded radius value and introduce drag_coefficient parameter for radius estimation

MNT: Extract noise initialization to fix pylint too-many-statements in Parachute.__init__

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* Refactor environment method access in controller test for clarity

* fix pylint

* fix comments

* avoid breaking change with drag_coefficient

* reafactors Parachute.__init__ method

* fix tests

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <guilherme_fernandes@usp.br>

* ENH: get changes from BUG: All NOAA NOMADS Dependent Atmosphere Models Broken
Fixes #933

* ENH: Add guidelines for simulation safety, Sphinx documentation, and pytest standards (GitHub Copilot) (#937)

* REL: bump version to 1.12

* ENH: Add explicit timeouts to ThrustCurve API requests and update changelog (#940)

* Initial plan

* ENH: Add explicit timeouts to ThrustCurve API requests

Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>

* DOC: Add timeout fix PR to changelog

Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>

* ENH: Restore power_off/on_drag as Function objects; add _input attributes for raw user input and update changelog (#941)

* Initial plan

* ENH: Restore power_off/on_drag as Function, add _input attributes for raw user input

Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>

* DOC: Add PR #941 compatibility fix to changelog

Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>

* Update rocketpy/rocket/rocket.py

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* MNT: ruff pylint

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>
Co-authored-by: MateusStano <mateusstano@usp.br>

* MNT: Remove unused imports and deprecated functions from mathutils/function.py

* BUG: Readd SourceType enumeration for function source types and clean up imports

* BUG: Fix incorrect Jacobian in `only_radial_burn` branch of `SolidMotor.evaluate_geometry` (#944)

* Initial plan

* BUG: Fix incorrect Jacobian in only_radial_burn branch of evaluate_geometry

Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com>

* ENH: move weathercocking_coeff to PointMassRockt

* MNT: ruff

* MNT: fix cyclic import

* BUG: Add wraparound logic for wind direction in environment plots (#939)

* chore: added personal toolkit files

* update branch name in workflow

* chore: update toolkit files

* Fix: add wraparound logic for wind direction and related tests

* style: fix ruff formatting

* Remove unused import

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* refactor: move repetitive logic into helper method

* fix: update test logic in test_environment

* add changelog entry

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* MNT: add numpy import to test_environment.py

* MNT: rename constant for wraparound threshold in _break_direction_wraparound method

* ENH: Adaptive Monte Carlo via Convergence Criteria (#922)

* ENH: added a new function (simulate_convergence)

* DOC: added a cell to show simulate_convergence function usage

* TST: integration test for simulate_convergence

* DOC: updated changelog for this PR

* ENH: ran black to lint intg test file

* new fixes thx to copilot comments

* linted rocketpy/simulation/monte_carlo.py

---------

Co-authored-by: Malmahrouqi3 <mohdsaid497566@gmail.com>

* DOC: add latitude range in docs

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* MNT: remove unnecessary pylint warning

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* MNT: remove unnecessary pylint warning

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* DOC: correctly link to WeatherModelMapping

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* DOCS: checked todo

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* ENH: address copilot comments

* TST: improve tests

* ENH: get changes from BUG: All NOAA NOMADS Dependent Atmosphere Models Broken
Fixes #933

* BUG: Fix hard-coded radius value for parachute added mass calculation (#889)

* Fix hard-coded radius value for parachute added mass calculation

Calculate radius from cd_s using a typical hemispherical parachute drag
coefficient (1.4) when radius is not explicitly provided. This fixes
drift distance calculations for smaller parachutes like drogues.

Formula: R = sqrt(cd_s / (Cd * π))

Closes #860

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Address code review: improve docstrings and add explicit None defaults

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Add CHANGELOG entry for PR #889

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Update rocket.add_parachute to use radius=None for consistency

Changed the default radius from 1.5 to None in the add_parachute method
to match the Parachute class behavior. This ensures consistent automatic
radius calculation from cd_s across both APIs.

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Refactor Parachute class to remove hard-coded radius value and introduce drag_coefficient parameter for radius estimation

Fix hard-coded radius value for parachute added mass calculation

Calculate radius from cd_s using a typical hemispherical parachute drag
coefficient (1.4) when radius is not explicitly provided. This fixes
drift distance calculations for smaller parachutes like drogues.

Formula: R = sqrt(cd_s / (Cd * π))

Closes #860

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Add CHANGELOG entry for PR #889

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

Refactor Parachute class to remove hard-coded radius value and introduce drag_coefficient parameter for radius estimation

MNT: Extract noise initialization to fix pylint too-many-statements in Parachute.__init__

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* Refactor environment method access in controller test for clarity

* fix pylint

* fix comments

* avoid breaking change with drag_coefficient

* reafactors Parachute.__init__ method

* fix tests

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <guilherme_fernandes@usp.br>

* ENH: Add guidelines for simulation safety, Sphinx documentation, and pytest standards (GitHub Copilot) (#937)

* REL: bump version to 1.12

* BUG: Add wraparound logic for wind direction in environment plots (#939)

* chore: added personal toolkit files

* update branch name in workflow

* chore: update toolkit files

* Fix: add wraparound logic for wind direction and related tests

* style: fix ruff formatting

* Remove unused import

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* refactor: move repetitive logic into helper method

* fix: update test logic in test_environment

* add changelog entry

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* MNT: add numpy import to test_environment.py

* MNT: rename constant for wraparound threshold in _break_direction_wraparound method

* DOC: add latitude range in docs

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* MNT: remove unnecessary pylint warning

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* MNT: remove unnecessary pylint warning

Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>

* ENH: address copilot comments

* TST: improve tests

* DOC: correctly link to WeatherModelMapping

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* DOCS: checked todo

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* ENH: Adaptive Monte Carlo via Convergence Criteria (#922)

* ENH: added a new function (simulate_convergence)

* DOC: added a cell to show simulate_convergence function usage

* TST: integration test for simulate_convergence

* DOC: updated changelog for this PR

* ENH: ran black to lint intg test file

* new fixes thx to copilot comments

* linted rocketpy/simulation/monte_carlo.py

---------

Co-authored-by: Malmahrouqi3 <mohdsaid497566@gmail.com>

* DEV: remove unwanted changes from develop

* DEV: Update for hotfix

* TST: add tests

* MNT: remove changes from develop again

* MNT: Refactor longitude and latitude index functions

* MNT: ruff

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com>
Co-authored-by: Gui-FernandesBR <guilherme_fernandes@usp.br>
Co-authored-by: Khushal Kottaru <khushal.kottaru@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Mohammed S. Al-Mahrouqi <malmahrouqi3@gatech.edu>
Co-authored-by: Malmahrouqi3 <mohdsaid497566@gmail.com>
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.

ENH: Implement Adaptive Simulation based on Convergence Criteria

4 participants