def __init__(self, uq_parameters, index_set=None, method=None): self.uq_parameters = uq_parameters dimensions = len(uq_parameters) # For increased flexibility, if the index_set is not given, we will assume a tensor grid basis if index_set is None: # determine the orders! orders_to_use = [] for u in range(0, dimensions): orders_to_use.append(np.int(uq_parameters[u].order - 1)) # Use the tensor grid option! self.index_set = IndexSet("Tensor grid", orders_to_use) else: # Now before we set self.index_set = index_set, we check to make sure that # the number of basis used is -1 the number of points! orders_to_use = [] count = 0 for u in range(0, dimensions): orders_to_use.append(np.int(uq_parameters[u].order)) if orders_to_use[u] <= index_set.orders[u]: count = count + 1 if count > 0: error_function( 'IndexSet: Basis orders: Ensure that the basis order is always -1 the number of points!' ) self.index_set = index_set if method is not None: self.method = method else: self.method = 'QR'
def getIndexSet(self): """ Returns all the elements of an index set :param IndexSet self: An instance of the IndexSet class :return: iset: An n-by-d array of index set elements. Here n represents the cardinality of the index set while d represents the number of dimensions. :rtype: ndarray """ name = self.index_set_type if name == "Total order": index_set = total_order_index_set(self.orders) elif name == "Sparse grid": sparse_index, a, SG_set = sparse_grid_index_set(self.level, self.growth_rule, self.dimension) # Note sparse grid rule depends on points! return sparse_index, a, SG_set elif name == "Tensor grid": index_set = tensor_grid_index_set(self.orders) elif name == "Hyperbolic basis": index_set = hyperbolic_index_set(self.orders, self.q) else: error_function('indexset __init__: invalid value for index_set_type!') index_set = [0] return index_set
def __init__(self, points, lower=None, upper=None, param_type=None, shape_parameter_A=None, shape_parameter_B=None, derivative_flag=None): # Check what lower and upper are... self.order = points if lower is None: self.lower = -1.0 else: self.lower = lower if upper is None: self.upper = 1.0 else: self.upper = upper if param_type is None: self.param_type = 'Uniform' else: self.param_type = param_type if shape_parameter_A is None: self.shape_parameter_A = 0 else: self.shape_parameter_A = shape_parameter_A if shape_parameter_B is None: self.shape_parameter_B = 0 else: self.shape_parameter_B = shape_parameter_B if derivative_flag is None: self.derivative_flag = 0 else: self.derivative_flag = derivative_flag if self.param_type == 'TruncatedGaussian': if upper is None or lower is None: error_function( 'parameter __init__: upper and lower bounds are required for a TruncatedGaussian distribution!' ) # Check that lower is indeed above upper if self.lower >= self.upper: error_function( 'parameter __init__: upper bounds must be greater than lower bounds!' )
def getPDF(self, N, graph=None): """ Returns the PDF of the parameter :param Parameter self: An instance of the Parameter class :param integer N: Number of points along the x-axis :param boolean graph: Select 1 for python to plot the PDF on a graph. Default is 0, in which case no graph is produced :return: x, 1-by-N matrix that contains the values of the x-axis along the support of the parameter :rtype: ndarray :return: w, 1-by-N matrix that contains the values of the PDF of the parameter :rtype: ndarray **Sample declaration** :: >> var1 = Parameter(points=12, shape_parameter_A=0.5, param_type='Exponential') >> x, y = var1.getPDF(50) """ if self.param_type is "Gaussian": x, y = analytical.Gaussian(N, self.shape_parameter_A, self.shape_parameter_B) elif self.param_type is "Beta": x, y = analytical.BetaDistribution(N, self.shape_parameter_A, self.shape_parameter_B, self.lower, self.upper) elif self.param_type is "Gamma": x, y = analytical.Gamma(N, self.shape_parameter_A, self.shape_parameter_B) elif self.param_type is "Weibull": x, y = analytical.WeibullDistribution(N, self.shape_parameter_A, self.shape_parameter_B) elif self.param_type is "Cauchy": x, y = analytical.CauchyDistribution(N, self.shape_parameter_A, self.shape_parameter_B) elif self.param_type is "Uniform": x, y = analytical.UniformDistribution(N, self.lower, self.upper) elif self.param_type is "TruncatedGaussian": x, y = analytical.TruncatedGaussian(N, self.shape_parameter_A, self.shape_parameter_B, self.lower, self.upper) elif self.param_type is "Exponential": x, y = analytical.ExponentialDistribution(N, self.shape_parameter_A) else: error_function('parameter getPDF(): invalid parameter type!') if graph is None: return x, y elif graph == 1: fig = plt.figure() plt.plot(x, y, 'k-') plt.xlabel('x') plt.ylabel('PDF') plt.show() else: error_function('parameter getPDF(): invalid value for graph!') return x, y
def getSquareA(self, maximum_number_of_evals): flag = self.method if flag == "QR" or flag is None: option = 1 # default option! elif flag == "Random": option = 2 else: error_function( "ERROR in EffectiveQuadSubsampling --> getAsubsampled(): For the third input choose from either 'QR' or 'Random'" ) A, quadrature_pts, quadrature_wts = getA(self) dimension = len(self.uq_parameters) m, n = A.shape if maximum_number_of_evals < n: print 'Dimensions of A prior to subselection:' print m, n print 'The maximum number of evaluations you requested' print maximum_number_of_evals error_function( "ERROR in EffectiveQuadSubsampling --> getAsubsampled(): The maximum number of evaluations must be greater or equal to the number of basis terms" ) # Now compute the rank revealing QR decomposition of A! if option == 1: P = mgs_pivoting(A.T) #Q, R, P = qr(A.T, pivoting=True) else: P = np.random.randint(0, len(quadrature_pts) - 1, len(quadrature_pts) - 1) # Now truncate number of rows based on the maximum_number_of_evals selected_quadrature_points = P[0:maximum_number_of_evals] # Form the "square" A matrix. Asquare = getRows(np.mat(A), selected_quadrature_points) #print np.linalg.cond(Asquare) esq_pts = getRows(np.mat(quadrature_pts), selected_quadrature_points) esq_wts = quadrature_wts[selected_quadrature_points] W = np.mat(np.diag(np.sqrt(esq_wts))) return Asquare, esq_pts, W, selected_quadrature_points
def solve_constrainedLSQ(A,b,C,d): """ Solves the direct, constraint least squares problem ||Ax-b||_2 subject to Cx=d using the method of direct elimination :param numpy matrix A: an m-by-n matrix :param numpy matrix b: an m-by-1 matrix :param numpy ndarray C: an k-by-n matrix :param numpy ndarray d: an k-by-1 matrix :return: x, the coefficients of the least squares problem. :rtype: ndarray """ A = np.mat(A) C = np.mat(C) # Size of matrices! m, n = A.shape p, q = b.shape k, l = C.shape s, t = d.shape # Check that the number of elements in b are equivalent to the number of rows in A if m != p: error_function('ERROR: mismatch in sizes of A and b') elif k != s: error_function('ERROR: mismatch in sizes of C and d') Q , R = qr_Householder(C.T, 1) # Thin QR factorization on C' R = R[0:n, 0:n] u = np.linalg.inv(R.T) * d A_hat = A * Q z, w = A_hat.shape # Now split A Ahat_1 = A_hat[:, 0:len(u)] Ahat_2 = A_hat[:, len(u) : w] r = b - Ahat_1 * u # Solve the least squares problem v = solveLSQ(Ahat_2, r) x = Q * np.vstack([u, v]) return x
def getCardinality(self): """ Returns the cardinality (total number of elements) of the index set :param IndexSet self: An instance of the IndexSet class :return: cardinality: Total number of elements in the index set :rtype: integer """ name = self.index_set_type if name == "Total order": index_set = total_order_index_set(self.orders) elif name == "Sparse grid": index_set, a, SG_set = sparse_grid_index_set(self.level, self.growth_rule, self.dimension) # Note sparse grid rule depends on points! return sparse_index, a, SG_set elif name == "Tensor grid": index_set = tensor_grid_index_set(self.orders) elif name == "Hyperbolic basis": index_set = hyperbolic_index_set(self.orders, self.q) else: error_function('indexset __init__: invalid value for index_set_type!') # Now m or n is equivalent to the return len(index_set)
def recurrence_coefficients(self, order=None): # Preliminaries. N = 8000 # no. of points for analytical distributions. if order is None: order = self.order # 1. Beta distribution if self.param_type is "Beta": #param_A = self.shape_parameter_B - 1 # bug fix @ 9/6/2016 #param_B = self.shape_parameter_A - 1 #if(param_B <= 0): # error_function('ERROR: parameter_A (beta shape parameter A) must be greater than 1!') #if(param_A <= 0): # error_function('ERROR: parameter_B (beta shape parameter B) must be greater than 1!') #ab = jacobi_recurrence_coefficients_01(param_A, param_B , order) alpha = self.shape_parameter_A beta = self.shape_parameter_B lower = self.lower upper = self.upper x, w = analytical.BetaDistribution(N, alpha, beta, lower, upper) ab = custom_recurrence_coefficients(order, x, w) # 2. Uniform distribution elif self.param_type is "Uniform": self.shape_parameter_A = 0.0 self.shape_parameter_B = 0.0 ab = jacobi_recurrence_coefficients(self.shape_parameter_A, self.shape_parameter_B, order) #lower = self.lower #upper = self.upper #x, w = analytical.UniformDistribution(N, lower, upper) #ab = custom_recurrence_coefficients(order, x, w) # 3. Analytical Gaussian defined on [-inf, inf] elif self.param_type is "Gaussian": mu = self.shape_parameter_A sigma = np.sqrt(self.shape_parameter_B) x, w = analytical.Gaussian(N, mu, sigma) ab = custom_recurrence_coefficients(order, x, w) # 4. Analytical Exponential defined on [0, inf] elif self.param_type is "Exponential": lambda_value = self.shape_parameter_A x, w = analytical.ExponentialDistribution(N, lambda_value) ab = custom_recurrence_coefficients(order, x, w) # 5. Analytical Cauchy defined on [-inf, inf] elif self.param_type is "Cauchy": x0 = self.shape_parameter_A gammavalue = self.shape_parameter_B x, w = analytical.CauchyDistribution(N, x0, gammavalue) ab = custom_recurrence_coefficients(order, x, w) # 5. Analytical Gamma defined on [0, inf] elif self.param_type is "Gamma": k = self.shape_parameter_A theta = self.shape_parameter_B x, w = analytical.GammaDistribution(N, k, theta) ab = custom_recurrence_coefficients(order, x, w) # 6. Analytical Weibull defined on [0, inf] elif self.param_type is "Weibull": lambda_value = self.shape_parameter_A k = self.shape_parameter_B x, w = analytical.WeibullDistribution(N, lambda_value, k) ab = custom_recurrence_coefficients(order, x, w) # 3. Analytical Truncated Gaussian defined on [a,b] elif self.param_type is "TruncatedGaussian": mu = self.shape_parameter_A sigma = np.sqrt(self.shape_parameter_B) a = self.lower b = self.upper x, w = analytical.TruncatedGaussian(N, mu, sigma, a, b) ab = custom_recurrence_coefficients(order, x, w) #elif self.param_type == "Gaussian" or self.param_type == "Normal": # param_B = self.shape_parameter_B - 0.5 # if(param_B <= -0.5): # utils.error_function('ERROR: parameter_B (variance) must be greater than 0') # else: # ab = hermite_recurrence_coefficients(self.shape_parameter_A, param_B, order) else: error_function( 'ERROR: parameter type is undefined. Choose from Gaussian, Uniform, Gamma, Weibull, Cauchy, Exponential, TruncatedGaussian or Beta' ) return ab