def test_compute_grouped_elementary_effects(): model_inputs = np.array([[.39, -.39, -1.64, 0.39, -0.39, -0.39, 0.39, 0.39, -1.64, -0.39, 0.39, -1.64, 1.64, 1.64, 1.64], [-1.64, 1.64, 0.39, -1.64, 1.64, 1.64, -1.64, -1.64, -1.64, 1.64, 0.39, -1.64, 1.64, 1.64, 1.64], [-1.64, 1.64, 0.39, -1.64, 1.64, 1.64, -1.64, -1.64, 0.39, 1.64, -1.64, 0.39, -.39, -.39, -.39] ]) model_results = np.array([13.85, -10.11, 1.12]) problem = {'names': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'], 'bounds': [[]], 'groups': (np.array([[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0]]), ['gp1', 'gp2']), 'num_vars': 15 } ee = compute_elementary_effects(model_inputs, model_results, 3, 2. / 3) mu_star = np.average(np.abs(ee), axis=1) actual = compute_grouped_metric(mu_star, problem['groups'][0].T) desired = np.array([16.86, 35.95]) assert_allclose(actual, desired, atol=1e-1)
def test_compute_elementary_effects(): ''' Inputs for elementary effects taken from Exercise 5 from Saltelli (2008). See page 140-145. `model_inputs` are from trajectory t_1 from table 3.10 on page 141. `desired` is equivalent to column t_1 in table 3.12 on page 145. ''' model_inputs = np.array([ [1.64, -1.64, -1.64, 0.39, -0.39, 0.39, -1.64, - 1.64, -0.39, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64], [1.64, -1.64, -1.64, 0.39, -0.39, -1.64, -1.64, - 1.64, -0.39, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64], [1.64, -1.64, -1.64, 0.39, -0.39, -1.64, -1.64, - 1.64, 1.64, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64], [1.64, -1.64, -1.64, 0.39, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64], [1.64, -1.64, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64], [1.64, -1.64, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, -0.39, -0.39, 0.39, 1.64], [1.64, -1.64, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, -0.39, -0.39, -1.64, 1.64], [1.64, 0.39, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, -0.39, -0.39, -1.64, 1.64], [1.64, 0.39, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, -0.39, -0.39, -1.64, -0.39], [1.64, 0.39, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, 1.64, 1.64, -0.39, -0.39, -1.64, -0.39], [1.64, 0.39, -1.64, -1.64, 1.64, -1.64, -1.64, 0.39, 1.64, 1.64, 1.64, -0.39, -0.39, -1.64, -0.39], [1.64, 0.39, -1.64, -1.64, 1.64, -1.64, -1.64, 0.39, 1.64, 1.64, -0.39, -0.39, -0.39, -1.64, -0.39], [1.64, 0.39, -1.64, -1.64, 1.64, -1.64, 0.39, 0.39, 1.64, 1.64, -0.39, -0.39, -0.39, -1.64, -0.39], [1.64, 0.39, 0.39, -1.64, 1.64, -1.64, 0.39, 0.39, 1.64, 1.64, -0.39, -0.39, -0.39, -1.64, -0.39], [-0.39, 0.39, 0.39, -1.64, 1.64, -1.64, 0.39, 0.39, 1.64, 1.64, -0.39, -0.39, -0.39, -1.64, -0.39], [-0.39, 0.39, 0.39, -1.64, 1.64, -1.64, 0.39, 0.39, 1.64, 1.64, -0.39, -0.39, 1.64, -1.64, -0.39]], dtype=np.float) model_outputs = np.array([24.9, 22.72, 21.04, 16.01, 10.4, 10.04, 8.6, 13.39, 4.69, 8.02, 9.98, 3.75, 1.33, 2.59, 6.37, 9.99], dtype=np.float) delta = 2. / 3 actual = compute_elementary_effects(model_inputs, model_outputs, 16, delta) desired = np.array([[-5.67], [7.18], [1.89], [8.42], [2.93], [3.28], [-3.62], [-7.55], [-2.51], [5.00], [9.34], [0.54], [5.43], [2.15], [13.05]], dtype=np.float) assert_allclose(actual, desired, atol=1e-1)
def test_compute_elementary_effects_small(): ''' Computes elementary effects for two variables, over six trajectories with four levels. ''' model_inputs = np.array([[0, 1. / 3], [0, 1], [2. / 3, 1], [0, 1. / 3], [2. / 3, 1. / 3], [2. / 3, 1], [2. / 3, 0], [2. / 3, 2. / 3], [0, 2. / 3], [1. / 3, 1], [1, 1], [1, 1. / 3], [1. / 3, 1], [1. / 3, 1. / 3], [1, 1. / 3], [1. / 3, 2. / 3], [1. / 3, 0], [1, 0]], dtype=np.float) model_outputs = np.array([0.97, 0.71, 2.39, 0.97, 2.3, 2.39, 1.87, 2.40, 0.87, 2.15, 1.71, 1.54, 2.15, 2.17, 1.54, 2.2, 1.87, 1.0], dtype=np.float) delta = 2. / 3 actual = compute_elementary_effects(model_inputs, model_outputs, 3, delta) desired = np.array( [[2.52, 2.01, 2.30, -0.66, -0.93, -1.30], [-0.39, 0.13, 0.80, 0.25, -0.02, 0.51]]) assert_allclose(actual, desired, atol=1e-0)
def test_compute_elementary_effects_small(): ''' Computes elementary effects for two variables, over six trajectories with four levels. ''' model_inputs = np.array( [[0, 1. / 3], [0, 1], [2. / 3, 1], [0, 1. / 3], [2. / 3, 1. / 3], [2. / 3, 1], [2. / 3, 0], [2. / 3, 2. / 3], [0, 2. / 3], [1. / 3, 1], [1, 1], [1, 1. / 3], [1. / 3, 1], [1. / 3, 1. / 3], [1, 1. / 3], [1. / 3, 2. / 3], [1. / 3, 0], [1, 0]], dtype=float) model_outputs = np.array([ 0.97, 0.71, 2.39, 0.97, 2.3, 2.39, 1.87, 2.40, 0.87, 2.15, 1.71, 1.54, 2.15, 2.17, 1.54, 2.2, 1.87, 1.0 ], dtype=float) delta = 2. / 3 actual = compute_elementary_effects(model_inputs, model_outputs, 3, delta) desired = np.array([[2.52, 2.01, 2.30, -0.66, -0.93, -1.30], [-0.39, 0.13, 0.80, 0.25, -0.02, 0.51]]) assert_allclose(actual, desired, atol=1e-0)
def test_compute_elementary_effects(): ''' Inputs for elementary effects taken from Exercise 5 from Saltelli (2008). See page 140-145. `model_inputs` are from trajectory t_1 from table 3.10 on page 141. `desired` is equivalent to column t_1 in table 3.12 on page 145. ''' model_inputs = np.array( [[ 1.64, -1.64, -1.64, 0.39, -0.39, 0.39, -1.64, -1.64, -0.39, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64 ], [ 1.64, -1.64, -1.64, 0.39, -0.39, -1.64, -1.64, -1.64, -0.39, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64 ], [ 1.64, -1.64, -1.64, 0.39, -0.39, -1.64, -1.64, -1.64, 1.64, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64 ], [ 1.64, -1.64, -1.64, 0.39, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64 ], [ 1.64, -1.64, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, 1.64, -0.39, 0.39, 1.64 ], [ 1.64, -1.64, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, -0.39, -0.39, 0.39, 1.64 ], [ 1.64, -1.64, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, -0.39, -0.39, -1.64, 1.64 ], [ 1.64, 0.39, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, -0.39, -0.39, -1.64, 1.64 ], [ 1.64, 0.39, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, -0.39, 1.64, -0.39, -0.39, -1.64, -0.39 ], [ 1.64, 0.39, -1.64, -1.64, -0.39, -1.64, -1.64, 0.39, 1.64, 1.64, 1.64, -0.39, -0.39, -1.64, -0.39 ], [ 1.64, 0.39, -1.64, -1.64, 1.64, -1.64, -1.64, 0.39, 1.64, 1.64, 1.64, -0.39, -0.39, -1.64, -0.39 ], [ 1.64, 0.39, -1.64, -1.64, 1.64, -1.64, -1.64, 0.39, 1.64, 1.64, -0.39, -0.39, -0.39, -1.64, -0.39 ], [ 1.64, 0.39, -1.64, -1.64, 1.64, -1.64, 0.39, 0.39, 1.64, 1.64, -0.39, -0.39, -0.39, -1.64, -0.39 ], [ 1.64, 0.39, 0.39, -1.64, 1.64, -1.64, 0.39, 0.39, 1.64, 1.64, -0.39, -0.39, -0.39, -1.64, -0.39 ], [ -0.39, 0.39, 0.39, -1.64, 1.64, -1.64, 0.39, 0.39, 1.64, 1.64, -0.39, -0.39, -0.39, -1.64, -0.39 ], [ -0.39, 0.39, 0.39, -1.64, 1.64, -1.64, 0.39, 0.39, 1.64, 1.64, -0.39, -0.39, 1.64, -1.64, -0.39 ]], dtype=float) model_outputs = np.array([ 24.9, 22.72, 21.04, 16.01, 10.4, 10.04, 8.6, 13.39, 4.69, 8.02, 9.98, 3.75, 1.33, 2.59, 6.37, 9.99 ], dtype=float) delta = 2. / 3 actual = compute_elementary_effects(model_inputs, model_outputs, 16, delta) desired = np.array( [[-5.67], [7.18], [1.89], [8.42], [2.93], [3.28], [-3.62], [-7.55], [-2.51], [5.00], [9.34], [0.54], [5.43], [2.15], [13.05]], dtype=float) assert_allclose(actual, desired, atol=1e-1)
problem = { "num_vars" : len(par_morris), "names" : par_morris, "groups" : None, "bounds" : [[0, lev-1]] * len(par_morris) } X = traj_id.iloc[idxs].values.astype(float) #%%Get morris output delta = lev / (2 * (lev - 1)) ee={} for var in df.columns: Y = df[var].values ee[var] = compute_elementary_effects( #For bookkeeping purposes X, Y, problem["num_vars"]+1, delta) ee[var] = pd.DataFrame(data = ee[var].T, columns = problem["names"]) #%%Correct errors #In one instance Kh_aqf was lower than Kh_aqt (trajectory 9), #since we prescribed Kv_aqt and due to a high Kh_Kv, this got converted to a Kh_aqt higher than Kh_aqf. #This flipper around the elementary effects for N_aqt and f_aqt, which we correct for here. ee["old_water"]["N_aqt"][8] = ee["old_water"]["N_aqt"][8]*-1 ee["old_water"]["f_aqt"][8] = ee["old_water"]["f_aqt"][8]*-1 # #ee["offshore_fw"]["alpha"][6] = 0 #%%Calculate Morris metrics output = {}