Esempio n. 1
0
def test_morris_to_df():
    params = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8']

    problem = {
        'num_vars': 8,
        'names': params,
        'groups': None,
        'bounds': [[0.0, 1.0],
                   [0.0, 1.0],
                   [0.0, 1.0],
                   [0.0, 1.0],
                   [0.0, 1.0],
                   [0.0, 1.0],
                   [0.0, 1.0],
                   [0.0, 1.0]]
    }

    param_values = morris_sample.sample(problem, N=1000, num_levels=4,
                                        optimal_trajectories=None)
    Y = Sobol_G.evaluate(param_values)
    Si = morris.analyze(problem, param_values, Y)
    Si_df = Si.to_df()

    assert isinstance(Si_df, pd.DataFrame), \
        "Morris Si: Expected DataFrame, got {}".format(type(Si_df))

    expected_index = set(params)
    assert set(Si_df.index) == expected_index, "Incorrect index in DataFrame"

    col_names = ['mu', 'mu_star', 'sigma', 'mu_star_conf']
    assert set(Si_df.columns) == set(col_names), \
        "Unexpected column names in DataFrame. Expected {}, got {}".format(
            col_names, Si_df.columns)
Esempio n. 2
0
def test_morris_to_df():
    params = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8']

    problem = {
        'num_vars':
        8,
        'names':
        params,
        'groups':
        None,
        'bounds': [[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 1.0],
                   [0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]
    }

    param_values = morris_sample.sample(problem,
                                        N=1000,
                                        num_levels=4,
                                        optimal_trajectories=None)
    Y = Sobol_G.evaluate(param_values)
    Si = morris.analyze(problem, param_values, Y)
    Si_df = Si.to_df()

    assert isinstance(Si_df, pd.DataFrame), \
        "Morris Si: Expected DataFrame, got {}".format(type(Si_df))

    expected_index = set(params)
    assert set(Si_df.index) == expected_index, "Incorrect index in DataFrame"

    col_names = ['mu', 'mu_star', 'sigma', 'mu_star_conf']
    assert set(Si_df.columns) == set(col_names), \
        "Unexpected column names in DataFrame. Expected {}, got {}".format(
            col_names, Si_df.columns)
Esempio n. 3
0
def test_Sobol_G_using_sobol():
    '''
    Tests the accuracy of the Sobol/Saltelli procedure using the Sobol_G
    test function, comparing the results from the Sobol/Saltelli analysis
    against the analytically computed sensitivity index from the Sobol_G
    function.
    '''
    problem = {'num_vars': 6,
               'names': ['x1', 'x2', 'x3', 'x4', 'x5', 'x6'],
               'bounds': [[0, 1], [0, 1], [0, 1], [0, 1],[0, 1], [0, 1]]}
    N = 5000
    a = np.array([78, 12, 0.5, 2, 97, 33])
    param_values = saltelli.sample(problem, N, calc_second_order=False)
    model_results = Sobol_G.evaluate(param_values, a)
    Si = sobol.analyze(problem, model_results, calc_second_order=False)
#     expected = Sobol_G.total_sensitivity_index(a)
#     assert_allclose(Si['ST'], expected)
    expected = Sobol_G.sensitivity_index(a)
    assert_allclose(Si['S1'], expected, atol=1e-2, rtol=1e-6)
Esempio n. 4
0
def test_Sobol_G_using_sobol():
    '''
    Tests the accuracy of the Sobol/Saltelli procedure using the Sobol_G
    test function, comparing the results from the Sobol/Saltelli analysis
    against the analytically computed sensitivity index from the Sobol_G
    function.
    '''
    problem = {'num_vars': 6,
               'names': ['x1', 'x2', 'x3', 'x4', 'x5', 'x6'],
               'bounds': [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]}
    N = 5000
    a = np.array([78, 12, 0.5, 2, 97, 33])
    param_values = saltelli.sample(problem, N, calc_second_order=False)
    model_results = Sobol_G.evaluate(param_values, a)
    Si = sobol.analyze(problem, model_results, calc_second_order=False)
#     expected = Sobol_G.total_sensitivity_index(a)
#     assert_allclose(Si['ST'], expected)
    expected = Sobol_G.sensitivity_index(a)
    assert_allclose(Si['S1'], expected, atol=1e-2, rtol=1e-6)
Esempio n. 5
0
#             [-3.14159265359, 3.14159265359],
#             [-3.14159265359, 3.14159265359]]
# }

# Files with a 4th column for "group name" will be detected automatically, e.g.
# param_file = '../../SALib/test_functions/params/Ishigami_groups.txt'

# Generate samples
param_values = sample(problem, N=1000, num_levels=4,
                      optimal_trajectories=None)

# To use optimized trajectories (brute force method),
# give an integer value for optimal_trajectories

# Run the "model" -- this will happen offline for external models
Y = Sobol_G.evaluate(param_values)

# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = morris.analyze(problem, param_values, Y, conf_level=0.95,
                    print_to_console=True,
                    num_levels=4, num_resamples=100)
# Returns a dictionary with keys 'mu', 'mu_star', 'sigma', and 'mu_star_conf'
# e.g. Si['mu_star'] contains the mu* value for each parameter, in the
# same order as the parameter file

fig, (ax1, ax2) = plt.subplots(1, 2)
horizontal_bar_plot(ax1, Si, {}, sortby='mu_star', unit=r"tCO$_2$/year")
covariance_plot(ax2, Si, {}, unit=r"tCO$_2$/year")

fig2 = plt.figure()
Esempio n. 6
0
# Set random seed (does not affect quasi-random Sobol sampling)
seed = 1
np.random.seed(seed)
rd.seed(seed)

# Read the parameter range file and generate samples
param_file = './SALib/test_functions/params/Sobol_G.txt'
pf = read_param_file(param_file)

# Generate samples (choose method here)
param_values = saltelli.sample(100, pf['num_vars'], calc_second_order = True)
# param_values = morris_oat.sample(100, pf['num_vars'], num_levels = 10, grid_jump = 5)
# param_values = fast_sampler.sample(100, pf['num_vars'])

# Samples are given in range [0, 1] by default. Rescale them to your parameter bounds. (If using normal distributions, use "scale_samples_normal" instead)
scale_samples(param_values, pf['bounds'])

# For Method of Morris, save the parameter values in a file (they are needed in the analysis)
# FAST and Sobol do not require this step, unless you want to save samples to input into an external model
np.savetxt('SGInput.txt', param_values, delimiter=' ')

# Run the "model" and save the output in a text file
# This will happen offline for external models
Y = Sobol_G.evaluate(param_values)
np.savetxt("SGOutput.txt", Y, delimiter=' ')

# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
sobol.analyze(param_file, 'SGOutput.txt', column = 0, calc_second_order = True)
# morris.analyze(param_file, 'SGInput.txt', 'SGOutput.txt', column = 0)
# extended_fast.analyze(param_file, 'SGOutput.txt', column = 0)
Esempio n. 7
0
 def model(self, *x):
     values = np.hstack(x).reshape((-1, 8))
     return Sobol_G.evaluate(values)