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

ENH: Introducing local sensitivity analysis #575

Merged
merged 37 commits into from
Sep 8, 2024

Conversation

Lucas-Prates
Copy link
Contributor

@Lucas-Prates Lucas-Prates commented Mar 11, 2024

Pull request type

  • Other (please describe): This PR is a draft to implement sensitivity analysis in RocketPy. It is a work in progress and needs validation.

Checklist

  • Lint (black rocketpy/ tests/) has passed locally

Current behavior

The Sensitivity Analysis notebook teaches the users how to perform the simulations, plot the distribution
of some flight variables (e.g. apogee), and computes the prediction ellipses for the landing point.

New behavior

Our goal is to take sensitivity analysis even further. Briefly, we attempt to answer the following question: Which parameters would reduce the variability of the variable of interest (e.g. apogee) the most if we measured them with greater precision?

To that end, a bit of theory is developed, check the technical document. What was developed resembles the work of [1], a core reference in sensitivity analysis for engineering. His approach is a global sensitivity analysis with a full model containing interaction terms. Our first implementation considers a local sensitivity analysis using only first-order terms.

A quick and dirty test of the functionality of the SensitivityModel class is provided the "sensitivity_model_usage" notebook. This notebook is currently giving weird results! The linear approximations for the variables are, for some reason I still have to figure out, not good enough. This was not happening at previous experimentations that suggested that this approached worked. I have to look carefully at what is happening, but I did not want to delay the PR.

The concepts are discussed in-depth in the "sensitivity_analysis_parameter_importance" notebook (the notebook was not updated to the new SensitivityModel yet!)

Breaking change

  • No

Additional information

Technical Document

[1] Sobol, Ilya M. "Global sensitivity indices for nonlinear mathematical models and their Monte Carlo estimates." Mathematics and computers in simulation

@Lucas-Prates Lucas-Prates requested a review from a team as a code owner March 11, 2024 23:07
Copy link

codecov bot commented Mar 11, 2024

Codecov Report

Attention: Patch coverage is 15.20000% with 212 lines in your changes missing coverage. Please review.

Project coverage is 74.40%. Comparing base (3a4c742) to head (522c8fe).
Report is 38 commits behind head on develop.

Files with missing lines Patch % Lines
rocketpy/sensitivity/sensitivity_model.py 14.28% 102 Missing ⚠️
rocketpy/tools.py 6.12% 46 Missing ⚠️
rocketpy/prints/sensitivity_prints.py 16.66% 40 Missing ⚠️
rocketpy/plots/sensitivity_plots.py 25.00% 24 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #575      +/-   ##
===========================================
- Coverage    75.87%   74.40%   -1.47%     
===========================================
  Files           85       89       +4     
  Lines        10085    10335     +250     
===========================================
+ Hits          7652     7690      +38     
- Misses        2433     2645     +212     

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

@Gui-FernandesBR Gui-FernandesBR marked this pull request as draft April 4, 2024 04:33
@Lucas-Prates Lucas-Prates force-pushed the enh/sensitivity_analysis branch from 58b1cdf to 1c7f84d Compare May 14, 2024 21:45
@Lucas-Prates Lucas-Prates changed the base branch from develop to enh/class_dispersion May 14, 2024 21:46
@Lucas-Prates Lucas-Prates added Enhancement New feature or request, including adjustments in current codes Monte Carlo Monte Carlo and related contents labels May 14, 2024
@Lucas-Prates Lucas-Prates changed the title ENH: (DRAFT) Introducing Parameter Importance in Sensitivity Analysis ENH: Introducing local sensitivity analysis May 14, 2024
@Gui-FernandesBR Gui-FernandesBR marked this pull request as ready for review May 15, 2024 13:14
@Gui-FernandesBR Gui-FernandesBR added this to the Release v1.X.0 milestone May 15, 2024
@Gui-FernandesBR Gui-FernandesBR linked an issue May 15, 2024 that may be closed by this pull request
@Gui-FernandesBR
Copy link
Member

Tests are not passing

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/runner/work/RocketPy/RocketPy/rocketpy/__init__.py", line 2, in <module>
    from .environment import Environment, EnvironmentAnalysis
  File "/home/runner/work/RocketPy/RocketPy/rocketpy/environment/__init__.py", line 1, in <module>
    from .environment import Environment
  File "/home/runner/work/RocketPy/RocketPy/rocketpy/environment/environment.py", line [16](https://github.com/RocketPy-Team/RocketPy/actions/runs/9096367936/job/25001616031?pr=575#step:6:17), in <module>
    from ..tools import exponential_backoff
  File "/home/runner/work/RocketPy/RocketPy/rocketpy/tools.py", line 556, in <module>
    parameters_list: list[str],
TypeError: 'type' object is not subscriptable
Error: Process completed with exit code 1.

Could you fix it before our review, please? That would help us. @Lucas-Prates

@Lucas-Prates
Copy link
Contributor Author

Lucas-Prates commented May 15, 2024

Could you fix it before our review, please? That would help us. @Lucas-Prates

Sure, I will fix it briefly. This simplified type hinting started at python 3.9. I will make sure the tests pass this time. :P

@Gui-FernandesBR
Copy link
Member

Please be aware of #444, we are not supporting type hinting or annotations yet.

Base automatically changed from enh/class_dispersion to develop May 21, 2024 22:52
setup.py Outdated Show resolved Hide resolved
rocketpy/tools.py Outdated Show resolved Hide resolved
rocketpy/tools.py Outdated Show resolved Hide resolved
rocketpy/tools.py Outdated Show resolved Hide resolved
rocketpy/sensitivity/sensivity_model.py Outdated Show resolved Hide resolved
rocketpy/sensitivity/sensivity_model.py Outdated Show resolved Hide resolved
rocketpy/sensitivity/sensivity_model.py Outdated Show resolved Hide resolved
rocketpy/sensitivity/sensivity_model.py Outdated Show resolved Hide resolved
Copy link
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.

This is just a partial review. I could not run your codes to validate it is working, but I suggested a few changes that might improve the quality (i.e. readability) of the code.

rocketpy/tools.py Outdated Show resolved Hide resolved
rocketpy/tools.py Outdated Show resolved Hide resolved
rocketpy/tools.py Outdated Show resolved Hide resolved
rocketpy/tools.py Show resolved Hide resolved
rocketpy/tools.py Outdated Show resolved Hide resolved
rocketpy/tools.py Outdated Show resolved Hide resolved
rocketpy/__init__.py Outdated Show resolved Hide resolved
rocketpy/sensitivity/sensivity_model.py Outdated Show resolved Hide resolved
rocketpy/sensitivity/sensivity_model.py Outdated Show resolved Hide resolved
rocketpy/sensitivity/sensivity_model.py Outdated Show resolved Hide resolved
@Gui-FernandesBR Gui-FernandesBR force-pushed the enh/sensitivity_analysis branch from 171ad30 to 8bbb7c4 Compare June 25, 2024 01:55
@Gui-FernandesBR Gui-FernandesBR force-pushed the enh/sensitivity_analysis branch from 8bbb7c4 to 259d026 Compare July 6, 2024 00:24
@Gui-FernandesBR Gui-FernandesBR marked this pull request as draft July 6, 2024 01:24
@Lucas-Prates Lucas-Prates marked this pull request as ready for review July 26, 2024 23:13
@Gui-FernandesBR Gui-FernandesBR force-pushed the enh/sensitivity_analysis branch from a84127e to f0d0d62 Compare August 13, 2024 12:59
@Lucas-Prates
Copy link
Contributor Author

Lucas-Prates commented Aug 14, 2024

The PR is ready for review again. I fixed a bug on the computations of the sensitivity score and it explained why the model was failing overall. Now it works for several target variables and for more complex environments (e.g. forecasted atmosphere).

The sensitivity model usage and sensitivity simulation notebooks are the most important to review. There are still a couple of things left to do:

  • Improve and extend the writing in the sensitivity model usage notebook;
  • Understand why the approximation for x_impact and y_impact is failing.

@Gui-FernandesBR Gui-FernandesBR force-pushed the enh/sensitivity_analysis branch 2 times, most recently from d7be791 to 4b3fd03 Compare August 22, 2024 13:44
@Lucas-Prates Lucas-Prates force-pushed the enh/sensitivity_analysis branch from 2e13896 to 522c8fe Compare September 8, 2024 16:38
@Lucas-Prates Lucas-Prates marked this pull request as ready for review September 8, 2024 16:38
@Lucas-Prates
Copy link
Contributor Author

@Gui-FernandesBR and @MateusStano, this PR is ready for review again! I believe the review should be somewhat quick. The code has already been thoroughly review, so I recommend focusing on two files:

  • docs/technical/sensitivity.rst
  • docs/user/sensitivity.rst

In both cases, my recommendation is to read the text and check if it is clear. I wouldn't mind much about the mathematics, I took a second look at it and "looks fine." Moreover, I would like to know from you if the example I provided makes sense. It is very simplified, but I would hope it somehow creates the impression that sensitivity analysis can be useful.

@Gui-FernandesBR, I tried to address the "LAE is too large" by giving a pragmatic recommendation of how to use the tool. Giving an exact value of what is "too large" is, in my opinion, not easy/feasible right now.

Copy link
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.

LGTM

@Gui-FernandesBR
Copy link
Member

@Gui-FernandesBR and @MateusStano, this PR is ready for review again! I believe the review should be somewhat quick. The code has already been thoroughly review, so I recommend focusing on two files:

  • docs/technical/sensitivity.rst
  • docs/user/sensitivity.rst

In both cases, my recommendation is to read the text and check if it is clear. I wouldn't mind much about the mathematics, I took a second look at it and "looks fine." Moreover, I would like to know from you if the example I provided makes sense. It is very simplified, but I would hope it somehow creates the impression that sensitivity analysis can be useful.

@Gui-FernandesBR, I tried to address the "LAE is too large" by giving a pragmatic recommendation of how to use the tool. Giving an exact value of what is "too large" is, in my opinion, not easy/feasible right now.

@Lucas-Prates your proposed example is exactly what we needed when we created the issue. Problem solved, thank you so much!

@Gui-FernandesBR Gui-FernandesBR merged commit e8212fa into develop Sep 8, 2024
12 of 14 checks passed
@Gui-FernandesBR Gui-FernandesBR deleted the enh/sensitivity_analysis branch September 8, 2024 22:29
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
Status: Closed
Development

Successfully merging this pull request may close these issues.

Sensitivity Analysis on Monte Carlo Simulations
4 participants