def perform_SALib_sobol(model, model_output, output_name, subplot):
    # Returns a dict with a problem specificatin as required by SALib
    problem = get_SALib_problem(model.uncertainties)
    Si = sobol.analyze(problem,
                       model_output,
                       calc_second_order=True,
                       print_to_console=False)

    # If the total-order indices are substantially larger than the first-order indices,
    # then there is likely higher-order interactions occurring. We can look at the second-order indices
    # to see these higher-order interactions

    scores_filtered = {k: Si[k] for k in ['ST', 'ST_conf', 'S1', 'S1_conf']}
    Si_df = pd.DataFrame(scores_filtered, index=problem['names'])
    Si_df.sort_values(by='ST', ascending=False)

    sns.set_style('white')

    indices = Si_df[['S1', 'ST']]
    err = Si_df[['S1_conf', 'ST_conf']]

    indices.plot.bar(yerr=err.values.T, ax=subplot)

    print("\n" + output_name)

    print("")

    print(Si_df)
Ejemplo n.º 2
0
    def test_get_salib_problem(self):
        uncertainties = [RealParameter("a", 0, 10), RealParameter("b", 0, 5)]

        problem = get_SALib_problem(uncertainties)
        self.assertEqual(2, problem['num_vars'])
        self.assertEqual(['a', 'b'], problem['names'])
        self.assertEqual((0, 10), problem['bounds'][0])
        self.assertEqual((0, 5), problem['bounds'][1])
Ejemplo n.º 3
0
 def test_get_salib_problem(self):
     uncertainties = [RealParameter("a", 0, 10),
                      RealParameter("b", 0, 5)]
     
     problem = get_SALib_problem(uncertainties)
     self.assertEqual(2, problem['num_vars'])
     self.assertEqual(['a', 'b'], problem['names'])
     self.assertEqual((0, 10), problem['bounds'][0])
     self.assertEqual((0, 5), problem['bounds'][1])
Ejemplo n.º 4
0
def plot_sobol(model, sa_results, prob):
    if prob == "uncertainties":
        problem = get_SALib_problem(model.uncertainties)
    elif prob == "levers":
        problem = get_SALib_problem(model.levers)
    outcomes = sa_results[1]
    n_outcome = len(outcomes.keys())
    ncols = 4
    nrows = math.ceil(n_outcome / ncols)

    fig, axes = plt.subplots(figsize=(ncols * 4, nrows * 4),
                             ncols=ncols,
                             nrows=nrows)
    locs = list(itertools.product(range(nrows), range(ncols)))

    for i, key in enumerate(outcomes.keys()):
        loc = locs[i]
        ax = axes[loc]
        outcome = outcomes[key]
        Si = sobol.analyze(problem,
                           outcome,
                           calc_second_order=True,
                           print_to_console=False)

        Si_filter = {k: Si[k] for k in ['ST', 'ST_conf', 'S1', 'S1_conf']}
        Si_df = pd.DataFrame(Si_filter, index=problem['names'])
        Si_df = Si_df.T.stack().reset_index()
        Si_df.columns = ["S", "Uncertainty", "Sensitivity Value"]
        Si_df["Uncertainty"] = Si_df["Uncertainty"].apply(
            lambda x: x[:9]).values
        sns.barplot(x="Uncertainty",
                    y="Sensitivity Value",
                    data=Si_df,
                    hue="S",
                    ax=ax)
        ax.set_xlabel("")
        ax.set_xticklabels(labels=Si_df["Uncertainty"].unique(), rotation=90)
        ax.set_title(key)
        ax.grid()
    fig.tight_layout()
Ejemplo n.º 5
0
]

from ema_workbench import (SequentialEvaluator, ema_logging,
                           perform_experiments)
ema_logging.log_to_stderr(ema_logging.INFO)

from SALib.analyze import sobol
from ema_workbench.em_framework.salib_samplers import get_SALib_problem

with SequentialEvaluator(model) as evaluator:
    sa_results = evaluator.perform_experiments(scenarios=1,
                                               uncertainty_sampling='sobol')

experiments, outcomes = sa_results

problem = get_SALib_problem(model.uncertainties)
Si = sobol.analyze(problem,
                   outcomes['max_P'],
                   calc_second_order=True,
                   print_to_console=False)

scores_filtered = {k: Si[k] for k in ['ST', 'ST_conf', 'S1', 'S1_conf']}
Si_df = pd.DataFrame(scores_filtered, index=problem['names'])

sns.set_style('white')
fig, ax = plt.subplots(1)

indices = Si_df[['S1', 'ST']]
err = Si_df[['S1_conf', 'ST_conf']]

indices.plot.bar(yerr=err.values.T, ax=ax)