示例#1
0
def test_morris_sample_seed():

    N, problem = problem_setup()

    sample1 = morris_sampler(problem, N, seed=None)
    sample2 = morris_sampler(problem, N, seed=123)

    np.testing.assert_equal(np.any(np.not_equal(sample1, sample2)), True)
示例#2
0
    def test_regression_morris_groups_brute_optim(self, set_seed):

        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami_groups.txt'
        problem = read_param_file(param_file)

        param_values = morris_sampler(problem=problem, N=50,
                                      num_levels=4,
                                      optimal_trajectories=6,
                                      local_optimization=False)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu'], [9.786986, np.NaN],
                        atol=0, rtol=1e-5)

        assert_allclose(Si['sigma'], [6.453729, np.NaN],
                        atol=0, rtol=1e-5)

        assert_allclose(Si['mu_star'], [9.786986, 7.875],
                        atol=0, rtol=1e-5)
示例#3
0
    def test_regression_morris_optimal(self, set_seed):
        '''
        Tests the use of optimal trajectories with Morris.

        Uses brute force approach

        Note that the relative tolerance is set to a very high value
        (default is 1e-05) due to the coarse nature of the num_levels.
        '''
        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami.txt'
        problem = read_param_file(param_file)
        param_values = morris_sampler(problem=problem, N=20,
                                      num_levels=4,
                                      optimal_trajectories=9,
                                      local_optimization=True)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu_star'],
                        [9.786986e+00, 7.875000e+00, 1.388621],
                        atol=0,
                        rtol=1e-5)
示例#4
0
    def test_regression_morris_groups(self, set_seed):
        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami_groups.txt'
        problem = read_param_file(param_file)

        param_values = morris_sampler(problem=problem, N=10000,
                                      num_levels=4,
                                      optimal_trajectories=None)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu_star'], [7.610322, 10.197014],
                        atol=0, rtol=1e-5)
示例#5
0
    def test_regression_morris_vanilla(self, set_seed):
        """Note that this is a poor estimate of the Ishigami
        function.
        """
        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami.txt'
        problem = read_param_file(param_file)
        param_values = morris_sampler(problem, 10000, 4,
                                      optimal_trajectories=None)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu_star'], [7.536586, 7.875, 6.308785],
                        atol=0, rtol=1e-5)
示例#6
0
    def test_regression_morris_groups_local_optim(self, set_seed):
        set_seed
        param_file = 'src/SALib/test_functions/params/Ishigami_groups.txt'
        problem = read_param_file(param_file)

        param_values = morris_sampler(problem=problem, N=500,
                                      num_levels=4,
                                      optimal_trajectories=20,
                                      local_optimization=True)

        Y = Ishigami.evaluate(param_values)

        Si = morris.analyze(problem, param_values, Y,
                            conf_level=0.95, print_to_console=False,
                            num_levels=4)

        assert_allclose(Si['mu_star'],
                        [13.95285, 7.875],
                        rtol=1e-5)
perturbed = p_df.loc[:, is_perturbed]
for col in perturbed_cols:
    if col in cat_cols:
        perturbed[col] = perturbed[col].cat.codes

bounds = list(zip(perturbed.min().tolist(), perturbed.max().tolist()))
problem = {
    'num_vars': len(perturbed_cols),
    'names': perturbed_cols,
    'bounds': bounds
}

# Create Morris samples (p+1)*n
# SALib expects purely numeric values so categoricals are transformed as such
moat_samples = morris_sampler(problem, 10, seed=101)

# Create template
moat_df = df.iloc[0][constant_cols].to_frame().T
moat_df = pd.concat([moat_df] * len(moat_samples), ignore_index=True)

df_samples = pd.DataFrame(data=moat_samples, columns=perturbed_cols)

# Export numeric sample values
moat_df[df_samples.columns] = df_samples

numeric_df = moat_df.copy()
for col in numeric_df:
    if col in cat_cols:
        numeric_df[col] = numeric_df[col].astype('category')
        numeric_df[col] = numeric_df[col].cat.codes