def redraw(self, random_method: str): """Redraw the random number with the given method :param random_method: the random method to use """ problem = { "num_vars": self.num_dim, "names": list(range(self.num_dim)), "bounds": [[0, 1]] * self.num_dim, } if random_method == "pseudo_random": seq = np.random.random((self.bucket_size, 2)) elif random_method == "sobol_sequence": seq = sobol_sequence.sample(self.bucket_size, 2) elif random_method == "saltelli": seq = saltelli.sample(problem, self.bucket_size, calc_second_order=False) elif random_method == "latin_hypercube": seq = latin.sample(problem, self.bucket_size) elif random_method == "finite_differences": seq = finite_diff.sample(problem, self.bucket_size) elif random_method == "fast": seq = fast_sampler.sample(problem, self.bucket_size, M=45) else: raise ValueError(f"Unknown random method {random_method}") self.random_draws[random_method] = seq
def _sobol_sampling(n, doe_variables, **kwargs): dim = len(doe_variables) points = sobol_sequence.sample(n, dim) for i, bounds in enumerate(doe_variables.values()): points[:, i] = points[:, i] * (bounds[1] - bounds[0]) + bounds[0] return points
def random_boundary_points(self, n, random="pseudo"): x_corner = np.vstack(( self.xmin, [self.xmax[0], self.xmin[1]], self.xmax, [self.xmin[0], self.xmax[1]], )) n -= 4 if n <= 0: return x_corner l1 = self.xmax[0] - self.xmin[0] l2 = l1 + self.xmax[1] - self.xmin[1] l3 = l2 + l1 if random == "sobol": u = np.ravel(sobol_sequence.sample(n + 4, 1))[2:] u = u[np.logical_not(np.isclose(u, l1 / self.perimeter))] u = u[np.logical_not(np.isclose(u, l3 / self.perimeter))] u = u[:n] else: u = np.random.rand(n) u *= self.perimeter x = [] for l in u: if l < l1: x.append([self.xmin[0] + l, self.xmin[1]]) elif l < l2: x.append([self.xmax[0], self.xmin[1] + l - l1]) elif l < l3: x.append([self.xmax[0] - l + l2, self.xmax[1]]) else: x.append([self.xmin[0], self.xmax[1] - l + l3]) return np.vstack((x_corner, x))
def test_sobol_sequence(): # example from Joe & Kuo: http://web.maths.unsw.edu.au/~fkuo/sobol/ S = sobol_sequence.sample(10,3) expected = [[0,0,0],[0.5,0.5,0.5],[0.75,0.25,0.25],[0.25,0.75,0.75], [0.375,0.375,0.625],[0.875,0.875,0.125],[0.625,0.125,0.875], [0.125,0.625,0.375],[0.1875,0.3125,0.9375],[0.6875,0.8125,0.4375]] assert_allclose(S, expected, atol=5e-2, rtol=1e-1)
def random_boundary_points(self, n, random="pseudo"): n -= self.nvertices if n <= 0: return self.vertices if random == "sobol": u = np.ravel(sobol_sequence.sample(n + self.nvertices, 1))[1:] l = 0 for i in range(0, self.nvertices - 1): l += self.diagonals[i, i + 1] u = u[np.logical_not(np.isclose(u, l / self.perimeter))] u = u[:n] else: u = np.random.rand(n) u *= self.perimeter u.sort() x = [] i = -1 l0 = 0 l1 = l0 + self.diagonals[i, i + 1] v = (self.vertices[i + 1] - self.vertices[i]) / self.diagonals[i, i + 1] for l in u: if l > l1: i += 1 l0, l1 = l1, l1 + self.diagonals[i, i + 1] v = (self.vertices[i + 1] - self.vertices[i]) / self.diagonals[i, i + 1] x.append((l - l0) * v + self.vertices[i]) return np.vstack((self.vertices, x))
def random_boundary_points(self, n, random="pseudo"): if random == "pseudo": u = np.random.rand(n, 1) elif random == "sobol": u = sobol_sequence.sample(n, 1) theta = 2 * np.pi * u X = np.hstack((np.cos(theta), np.sin(theta))) return self.radius * X + self.center
def test_sobol_sequence(): # example from Joe & Kuo: http://web.maths.unsw.edu.au/~fkuo/sobol/ S = sobol_sequence.sample(10, 3) expected = [[0, 0, 0], [0.5, 0.5, 0.5], [0.75, 0.25, 0.25], [0.25, 0.75, 0.75], [0.375, 0.375, 0.625], [0.875, 0.875, 0.125], [0.625, 0.125, 0.875], [0.125, 0.625, 0.375], [0.1875, 0.3125, 0.9375], [0.6875, 0.8125, 0.4375]] assert_allclose(S, expected, atol=5e-2, rtol=1e-1)
def random_points(self, n, random="pseudo"): """http://mathworld.wolfram.com/DiskPointPicking.html""" if random == "pseudo": rng = np.random.rand(n, 2) elif random == "sobol": rng = sobol_sequence.sample(n, 2) r, theta = rng[:, 0], 2 * np.pi * rng[:, 1] x, y = np.cos(theta), np.sin(theta) return self.radius * (np.sqrt(r) * np.vstack((x, y))).T + self.center
def random_boundary_points(self, n, random="pseudo"): """http://mathworld.wolfram.com/HyperspherePointPicking.html """ if random == "pseudo": X = np.random.normal(size=(n, self.dim)) elif random == "sobol": U = sobol_sequence.sample(n + 1, self.dim)[1:] X = stats.norm.ppf(U) X = preprocessing.normalize(X) return self.radius * X + self.center
def random_boundary_points(self, n, random="pseudo"): """http://mathworld.wolfram.com/HyperspherePointPicking.html """ if random == "pseudo": X = np.random.normal(size=(n, self.dim)) elif random == "sobol": # Remove the first point [0, 0, ...] and the second point [0.5, 0.5, ...] U = sobol_sequence.sample(n + 2, self.dim)[2:] X = stats.norm.ppf(U) X = preprocessing.normalize(X) return self.radius * X + self.center
def kucherenko_sampling(problem, N, cov, mu, s=1): ''' Implementation of the alghoritm proposed in: S. Kucherenko, S. Tarantola, P. Annoni. Estimation of global sensitivity indices for models with dependent variables Comput. Phys. Commun., 183 (4) (2012), pp. 937-946 to generate two sets of independent and conditional variables. -------- problem: dict N: int independent variables mu, cov: mean of variables; covariance matrix s: int splitting position between independent and conditional varaibles ''' factors_order = cov.columns.values D = problem['num_vars'] # How many values of the Sobol sequence to skip skip_values = 1000 base_sequence = sobol_sequence.sample(N + skip_values, 2 * D) u = base_sequence[skip_values:, :D] u_ = base_sequence[skip_values:, D:] zu = norm.ppf(u) L = np.linalg.cholesky(cov) x = mu + np.dot(L, zu.T) v_ = u_[:, :s] w_ = u_[:, s:] y = x.T[:, :s] z = x.T[:, s:] cov_new = np.cov(x) mu_y = np.mean(y, axis=0).reshape(y.shape[1], 1) mu_z = np.mean(z, axis=0).reshape(z.shape[1], 1) zc_n = conditional_sampling(w_, y, mu_y, mu_z, cov_new, s, True) yc_n = conditional_sampling(v_, z, mu_z, mu_y, cov_new, s, False) x_df = pd.DataFrame(np.hstack([y, z]), columns=factors_order) xc_df = pd.DataFrame(np.hstack([yc_n, zc_n]), columns=factors_order) return x_df, xc_df
def random_points(self, n, random="pseudo"): """https://math.stackexchange.com/questions/87230/picking-random-points-in-the-volume-of-sphere-with-uniform-probability """ if random == "pseudo": U = np.random.rand(n, 1) X = np.random.normal(size=(n, self.dim)) elif random == "sobol": rng = sobol_sequence.sample(n + 1, self.dim + 1)[1:] U, X = rng[:, 0:1], rng[:, 1:] X = stats.norm.ppf(X) X = preprocessing.normalize(X) X = U**(1 / self.dim) * X return self.radius * X + self.center
def exact_first_total(problem, N, Model): num_vars = problem['num_vars'] # sample skip_values = 1000 base_sequence = sobol_sequence.sample(N + skip_values, 2 * num_vars) A = base_sequence[skip_values:, :num_vars] B = base_sequence[skip_values:, num_vars:] # variables to store lca scores y_A = np.zeros(N) y_B = np.zeros(N) y_j = np.zeros(N) # Monte Carlo simulations without resampling y_A = [Model(A[i, :]) for i in range(N)] y_B = [Model(B[i, :]) for i in range(N)] var_y = np.var(np.array([y_A, y_B]).flatten()) first_index = np.zeros(num_vars) total_index = np.zeros(num_vars) # First order for j in range(num_vars): J1 = copy(B) # all factors change but j-th expectation_1 = np.zeros(N) for i in range(N): J1[:, j] = copy(A[i, j]) # j-th factor stays the same y_J1 = [Model(J1[k, :]) for k in range(N)] expectation_1[i] = np.mean(y_J1) first_index[j] = np.var(expectation_1) / var_y # Total order for j in range(num_vars): expectation_T = np.zeros(N) for i in range(N): JT_row = copy(A[i, :]) # all factors stay the same but j-th JT = np.kron(np.ones(N), JT_row).reshape(N, num_vars) JT[:, j] = copy(B[:, j]) y_JT = [Model(JT[k, :]) for k in range(N)] expectation_T[i] = np.mean(y_JT) total_index[j] = 1 - np.var(expectation_T) / var_y df = pd.DataFrame([first_index, total_index], index=['S1 exact', 'ST exact']) df = df.transpose() return df
def test_output(): for i in range(10): n_runs = rd.randint(1, 1000) n_dimensions = rd.randint(1, 100) #ground truth samples_salib = sobol_sequence.sample(n_runs, n_dimensions) #our implementation Sampler = SobolSample(n_runs, n_dimensions) samples = Sampler.generate_all_samples() assert np.allclose(samples, samples_salib)
def _generate_random_params(n, sample_method=StochasticMethod.SALTELLI, var_params=None, seed=None): ''' Compute stochastic impacts for later analysis of incertitude ''' if var_params is None: var_params = _variable_params().values() if seed is None: seed = int(time() * 1000) random.seed(seed) # Extract variable names var_param_names = list([ param if isinstance(param, str) else param.name for param in var_params ]) problem = { 'num_vars': len(var_param_names), 'names': var_param_names, 'bounds': [[0, 1]] * len(var_param_names) } print("Generating samples ...") if sample_method == StochasticMethod.SALTELLI: X = saltelli.sample(problem, n, calc_second_order=True) elif sample_method == StochasticMethod.RAND: X = np.random.rand(n, len(var_param_names)) elif sample_method == StochasticMethod.SOBOL: print("sobol !") X = sobol_sequence.sample(n * (len(var_param_names) * 2 + 2), len(var_param_names)) # elif sample_method == StochasticMethod.LATIN : # X = latin.sample(problem, n) else: raise Exception("Unkown rand method " + sample_method) # Map normalized 0-1 random values into real values print("Transforming samples ...") params = dict() for i, param_name in enumerate(var_param_names): param = _param_registry()[param_name] params[param_name] = param.rand(X[:, i]).tolist() # Add fixed parameters for param in _param_registry().values(): if param.name not in var_param_names: params[param.name] = param.default return params, problem
def redraw(self, random_method): problem = {'num_vars': 2, 'names': ['x', 'y'], 'bounds': [[0, 1]] * 2} if random_method == 'pseudo_random': seq = np.random.random((NUM_DATA_POINTS, 2)) elif random_method == 'sobol_sequence': seq = sobol_sequence.sample(NUM_DATA_POINTS, 2) elif random_method == 'saltelli': seq = saltelli.sample(problem, NUM_DATA_POINTS, calc_second_order=False) elif random_method == 'latin_hypercube': seq = latin.sample(problem, NUM_DATA_POINTS) elif random_method == 'finite_differences': seq = finite_diff.sample(problem, NUM_DATA_POINTS) elif random_method == 'fast': seq = fast_sampler.sample(problem, NUM_DATA_POINTS, M=45) self.random_draws[random_method] = seq
def test_output(): for i in range(10): iterations = np.random.randint(1, 1000) num_params = np.random.randint(1, 100) # ground truth samples_salib = sobol_sequence.sample(iterations, num_params) # our implementation sampling_dict = { "iterations": iterations, "num_params": num_params, "skip_samples": 0, } samples_gsa = sobol_samples(sampling_dict) assert np.allclose(samples_gsa, samples_salib)
def random_boundary_points(self, n, random="pseudo"): x_corner = np.vstack((self.x1, self.x2, self.x3)) n -= 3 if n <= 0: return x_corner if random == "sobol": u = np.ravel(sobol_sequence.sample(n + 3, 1))[1:] u = u[np.logical_not(np.isclose(u, self.l12 / self.perimeter))] u = u[np.logical_not(np.isclose(u, (self.l12 + self.l23) / self.perimeter))] u = u[:n] else: u = np.random.rand(n) u *= self.perimeter x = [] for l in u: if l < self.l12: x.append(l * self.n12 + self.x1) elif l < self.l12 + self.l23: x.append((l - self.l12) * self.n23 + self.x2) else: x.append((l - self.l12 - self.l23) * self.n31 + self.x3) return np.vstack((x_corner, x))
def main_fun(num_try=1, width=20, depth=4, iter_num=50000, learning_rate=5.0e-4): def u_exact(x, t): u = np.sin(np.pi * x) * np.exp(-t) return u def f_exact(x, t): f = -1.0 * np.exp(-t) * np.sin(np.pi * x) \ + np.pi ** 2 * np.sin(np.pi * x) * np.exp(-t) return f layers = [2] + [width] * depth + [1] L = len(layers) #### randomaly sampled points space_dim = 1 Ntr = 100 xt_f = sobol_sequence.sample(Ntr, space_dim + 1)[1:, :] x_f = np.reshape(xt_f[:, 0], [-1, 1]) t_f = np.reshape(xt_f[:, 1], [-1, 1]) x_f = -1.0 + 2.0 * x_f f_value = f_exact(x_f, t_f) def g_fun(x, t, u): return t * (-1.0 - x) * (1.0 - x) * u + tf.sin(np.pi * x) def xavier_init(size): in_dim = size[0] out_dim = size[1] xavier_stddev = np.sqrt(2.0 / (in_dim + out_dim)) return tf.Variable(tf.truncated_normal([in_dim, out_dim], stddev=xavier_stddev, dtype=tf.float64), dtype=tf.float64) def neural_net(X, weights, biases): num_layers = len(weights) + 1 H = X # 2.0*(X - self.lb)/(self.ub - self.lb) - 1.0 for l in range(0, num_layers - 2): W = weights[l] b = biases[l] H = tf.nn.tanh(tf.add(tf.matmul(H, W), b)) W = weights[-1] b = biases[-1] Y = tf.add(tf.matmul(H, W), b) return Y def net_u(x_u, t_u, weights, biases): u = neural_net(tf.concat([x_u, t_u], 1), weights, biases) return u def net_f(x_f, t_f, weights, biases): u = g_fun(x_f, t_f, net_u(x_f, t_f, weights, biases)) u_x = tf.gradients(u, x_f)[0] u_xx = tf.gradients(u_x, x_f)[0] u_t = tf.gradients(u, t_f)[0] return u_t - u_xx f_target = tf.to_double(np.reshape(f_value, [-1, 1])) x_f_tf = tf.to_double(tf.reshape(x_f, [-1, 1])) t_f_tf = tf.to_double(tf.reshape(t_f, [-1, 1])) x_test = x_f t_test = t_f x_test_tf = x_f_tf t_test_tf = t_f_tf ut = u_exact(x_test, t_test) loss_vec = np.zeros((num_try, 1), dtype=np.float64) error_vec = np.zeros((num_try, 1), dtype=np.float64) loss_mat = [] error_u_mat = [] for num_run in range(num_try): min_loss = 1.0e16 loss_record = [] error_u_record = [] weights = [ xavier_init([layers[l], layers[l + 1]]) for l in range(0, L - 1) ] biases = [ tf.Variable(tf.zeros((1, layers[l + 1]), dtype=tf.float64), trainable=True) for l in range(0, L - 1) ] f_pred = net_f(x_f_tf, t_f_tf, weights, biases) loss = tf.reduce_mean(tf.square(f_target - f_pred)) # \ optimizer_adam = tf.train.AdamOptimizer(learning_rate) train_op_adam = optimizer_adam.minimize(loss) error_u_opt = 0 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) uu = g_fun(x_test_tf, t_test_tf, net_u(x_test_tf, t_test_tf, weights, biases)) for i in range(iter_num): sess.run(train_op_adam) if i % 1000 == 0: temp_loss = sess.run(loss) if temp_loss < min_loss: min_loss = temp_loss ut_opt = np.reshape(sess.run(uu), [-1, 1]) error_u_opt = np.linalg.norm( ut_opt - ut, 2) / np.linalg.norm(ut, 2) loss_record.append(temp_loss) ut0 = np.reshape(sess.run(uu), [-1, 1]) error_u0 = np.linalg.norm(ut0 - ut, 2) / np.linalg.norm( ut, 2) error_u_record.append(error_u0) loss_vec[num_run] = min_loss error_vec[num_run] = error_u_opt loss_mat.append(np.reshape(loss_record, [-1, 1])) error_u_mat.append(np.reshape(error_u_record, [-1, 1])) return float(np.mean(error_vec))
@author: Atri ''' from SALib.sample import sobol_sequence from com.ase.extn.cart import base from com.ase.extn.tway import twaysample from com.ase.extn.constants import configs import numpy as np import collections import matplotlib.pyplot as plt sensitivity = 'r' if sensitivity is 'r': param_values_0_1 = sobol_sequence.sample(10, 1) param_values_1_10 = (1 + (9*param_values_0_1)) if configs.r_0_to_1 is True: param_values = np.append(param_values_0_1,1) else: param_values = np.append(param_values_1_10,1) param_values.sort() else: param_values = 2 + sobol_sequence.sample(10, 1) param_values = np.append(param_values,3) param_values.sort() base.print_detail = False chart_type = 'cost'
plt.xlabel('t') plt.ylabel('x') plt.title('1D Burgers\' equation: Exact solution') plt.tight_layout() plt.savefig('C-NN-FW-FIG/Exact-Burgers.png', dpi=1000) #plt.show() plt.close(fig) #Nu = 300 Nf = 2000 init_time = 0.0 tt, xx = np.meshgrid(np.ndarray.flatten(t_exa[:51]), np.ndarray.flatten(x_exa)) xf_train = sobol_sequence.sample(Nf + 1, 2)[1:, :] xf_train[:, 0] = -8.0 + 16.0 * xf_train[:, 0] # x xf_train[:, 1] = 5.0 * xf_train[:, 1] # t #xf_test = np.concatenate((x_exa, 8.2*np.ones((x_exa.shape[0],1))),axis=1) xf_test = np.concatenate((xx.reshape((-1, 1)), tt.reshape((-1, 1))), axis=1) xu_test = xf_test yf_train = f_exact(xf_train[:, 0:1], xf_train[:, 1:2]) #yf_train = yf_train+np.linalg.cholesky(previous_cov_mat[:Nf,:Nf])@ np.random.randn(Nf,1) xu_train = np.concatenate((x_exa, 0.0 * np.ones((x_exa.shape[0], 1))), axis=1) xu_train = np.concatenate( (xu_train, np.concatenate( (-8.0 * np.ones((t_exa[:51].shape[0], 1)), t_exa[:51]), axis=1)),
def sample(n, dim=1, cdf=[None], method='random'): """ Samples using chosen method from a distribution Arguments: n: int, number of samples dim: int, number of dimensions cdf: [1xd] array of arrays, e.g. [cdf_d1, cdf_d2]. Each cdf should have be either: - an empirical cdf of array [values, probabilities] - a function, e.g. for normal distribution: def norm(sample_in): return [stats.norm.ppf(s, loc=0.03, scale=0.002) for s in sample_in] if no cdf is defined, sampling will be from the uniform [0-1] distribution method: str, options: 'random' - simple random sampling 'sobol' - quasi random using SAlib's generator returns: [nxd] array of sample points """ # ========================================================= # Make uniform sample # ========================================================= if method.lower() == 'random': unf_sample = np.random.uniform(size=[dim, n]) elif method.lower() == 'sobol': unf_sample = sobol_sequence.sample(n + 1, dim) unf_sample = unf_sample[1:] # skip first row (zeros) unf_sample = unf_sample.T if len(cdf) != dim: cdf = [None] * dim print ('Number of defined cdfs unequal to number of dimensions.' + 'Sampling from uniform distribution for every dimension') # ========================================================= # Transform sample to desired distribution # ========================================================= def unfdummy(sample_in): """dummy function for uniform transformation""" return sample_in def emp(sample_in, empirical_cdf): """ Empirical function""" return [statsfunc.empirical_ppf(s, empirical_cdf[1], empirical_cdf[0]) for s in sample_in] samplefuncs = [] for i in range(dim): if callable(cdf[i]): samplefuncs.append(cdf[i]) elif cdf[i] is None: samplefuncs.append(unfdummy) else: samplefuncs.append(empfunc(cdf[i]).run) return [samplefunc(unf_sample[i]) for i, samplefunc in enumerate(samplefuncs)]
Created on Mar 10, 2016 @author: Atri ''' from SALib.sample import sobol_sequence import matplotlib.pyplot as plt import numpy as np x = range(0,1) plt.axis([0, 1, 0,1]) plt.subplot(1,3,1) param_values_0_1 = sobol_sequence.sample(100, 2) for dots in param_values_0_1: plt.plot(dots[0],dots[1],'ro') plt.subplot(1,3,2) param_values_0_1 = sobol_sequence.sample(1000, 2) for dots in param_values_0_1: plt.plot(dots[0],dots[1],'ro') plt.subplot(1,3,3) param_values_0_1 = sobol_sequence.sample(2000, 2) for dots in param_values_0_1: plt.plot(dots[0],dots[1],'ro') ''' plt.axis([0, 1, 0,1])
def random_points(self, n, random="pseudo"): if random == "pseudo": x = np.random.rand(n, 1) elif random == "sobol": x = sobol_sequence.sample(n + 1, 1)[1:] return self.diam * x + self.l
def random_points(self, n, random="pseudo"): if random == "pseudo": x = np.random.rand(n, 1) elif random == "sobol": x = sobol_sequence.sample(n + 1, 1)[1:] return (self.diam * x + self.l).astype(config.real(np))
def _set_sequence(self): #TODO could get rid of first part of sequence self.sequence = ss.sample(self.length + self._SKIP, self.n_dims)
def random_points(self, n, random="pseudo"): if random == "pseudo": x = np.random.rand(n, self.dim) elif random == "sobol": x = sobol_sequence.sample(n + 1, self.dim)[1:] return (self.xmax - self.xmin) * x + self.xmin
import time def u(X): ### fabricated solution x = X[:,0:1] t = X[:,1:2] return np.sin(2.*np.pi*x)*np.exp(-t) def U(X, c): ### source term computed using the fabricated solution x = X[:,0:1] t = X[:,1:2] return -np.sin(2*np.pi*x)*np.exp(-t)+c*4*np.pi**2*np.sin(2*np.pi*x)*np.exp(-t) N_U = 30 # number of training inputs for \Omega_1 (see the book chapter for notations) c = 0.1 # diffusivity xU = sobol_sequence.sample(N_U+1,2)[1:,:] # training inputs for \Omega 1 #noise = 0.0 # noise-free noise = 0.05 # 5% noise np.random.seed(seed=1234) # fix the realization of Guassian white noise for sake of reproducing yU = U(xU, c) # training outputs for \Omega_1 or observation y_1 yU = yU + noise * np.std(yU) * np.random.randn(N_U,1) # add the noise N_u = 10 # number of points for \Omega_2 and \Omega_3 x_vec = np.linspace(0.0,1.0,N_u).reshape((-1,1)) init_pts = np.concatenate((x_vec,np.zeros((N_u,1))),axis=1) # training inputs for enforcing initial conditions left_b = np.concatenate((np.zeros((N_u,1)),x_vec),axis=1) # training inputs for enfrocing left boundary condition right_b = np.concatenate((np.ones((N_u,1)),x_vec),axis=1) # training inputs for enforcing right boundary condition
def generate_sobol_population(pop_size, ndim): spop = sobol_sequence.sample(pop_size, ndim) return spop
def _get_sobol_points(self): return ss.sample(self.n_iter + np.random.randint(1000), self.n_dims)[-self.n_iter:]