Monte-Carlo Sensitivity Analysis Python Package
Gregory H. Halverson (they/them)
gregory.h.halverson@jpl.nasa.gov
Lead developer and designer
NASA Jet Propulsion Laboratory 329G
Margaret C. Johnson (she/her)
maggie.johnson@jpl.nasa.gov
Sensitivity and uncertainty analysis
NASA Jet Propulsion Laboratory 398L
This Python package is distributed using the pip package manager. Install it with the package name monte-carlo-sensitivity
with dashes.
pip install monte-carlo-sensitivity
The monte-carlo-sensitivity
package provides tools for performing sensitivity analysis using Monte Carlo methods.
Import this package in Python as monte_carlo_sensitivity
with underscores.
Below are examples of how to use the key functions in the package:
The sensitivity_analysis
function performs sensitivity analysis by perturbing input variables and observing the effect on output variables.
import pandas as pd
from monte_carlo_sensitivity import sensitivity_analysis
# Example input DataFrame
input_df = pd.DataFrame({
"input_var1": [1, 2, 3],
"input_var2": [4, 5, 6],
"output_var": [7, 8, 9]
})
# Define a forward process function
def forward_process(df):
df["output_var"] = df["input_var1"] + df["input_var2"]
return df
# Perform sensitivity analysis
perturbation_df, sensitivity_metrics = sensitivity_analysis(
input_df=input_df,
input_variables=["input_var1", "input_var2"],
output_variables=["output_var"],
forward_process=forward_process,
n=100
)
print(perturbation_df)
print(sensitivity_metrics)
The perturbed_run
function performs a Monte Carlo sensitivity analysis by perturbing an input variable and observing the effect on an output variable.
import pandas as pd
from monte_carlo_sensitivity.perturbed_run import perturbed_run
# Example input DataFrame
input_df = pd.DataFrame({
"input_var1": [1, 2, 3],
"input_var2": [4, 5, 6],
"output_var": [7, 8, 9]
})
# Define a forward process function
def forward_process(df):
df["output_var"] = df["input_var1"] + df["input_var2"]
return df
# Perform a perturbed run
results = perturbed_run(
input_df=input_df,
input_variable="input_var1",
output_variable="output_var",
forward_process=forward_process,
n=100
)
print(results)
The joint_perturbed_run
function evaluates the sensitivity of output variables to joint perturbations in input variables.
import pandas as pd
from monte_carlo_sensitivity.joint_perturbed_run import joint_perturbed_run
# Example input DataFrame
input_df = pd.DataFrame({
"input_var1": [1, 2, 3],
"input_var2": [4, 5, 6],
"output_var": [7, 8, 9]
})
# Define a forward process function
def forward_process(df):
df["output_var"] = df["input_var1"] + df["input_var2"]
return df
# Perform a joint perturbed run
results = joint_perturbed_run(
input_df=input_df,
input_variable=["input_var1", "input_var2"],
output_variable=["output_var"],
forward_process=forward_process,
n=100
)
print(results)