def gauss_hermite_nodes(orders, sigma, mu=None): if isinstance(orders, int): orders = [orders] import numpy if mu is None: mu = numpy.array([0] * sigma.shape[0]) herms = [hermgauss(i) for i in orders] points = [h[0] * numpy.sqrt(2) for h in herms] weights = [h[1] / numpy.sqrt(numpy.pi) for h in herms] if len(orders) == 1: return [numpy.array(points) * sigma, weights[0]] x = cartesian(points).T from functools import reduce w = reduce(numpy.kron, weights) C = numpy.linalg.cholesky(sigma) x = numpy.dot(C, x) + mu[:, numpy.newaxis] return [x, w]
def gauss_hermite_nodes(orders, sigma, mu=None): if isinstance(orders, int): orders = [orders] import numpy if mu is None: mu = numpy.array( [0]*sigma.shape[0] ) herms = [hermgauss(i) for i in orders] points = [ h[0]*numpy.sqrt(2) for h in herms] weights = [ h[1]/numpy.sqrt( numpy.pi) for h in herms] if len(orders) == 1: return [numpy.array(points)*sigma, weights[0]] x = cartesian( points).T from functools import reduce w = reduce( numpy.kron, weights) C = numpy.linalg.cholesky(sigma) x = numpy.dot(C, x) + mu[:,numpy.newaxis] return [x,w]
def __init__(self, smin, smax, l): from dolo.numeric.interpolation.smolyak import SmolyakGrid sg = SmolyakGrid(smin,smax,l) self.smin = array(smin) self.smax = array(smax) self.bounds = row_stack([smin,smax]) vertices = cartesian( zip(smin,smax) ).T self.grid = column_stack( [sg.grid, vertices] )
def __init__(self, smin, smax, l): from dolo.numeric.interpolation.smolyak import SmolyakGrid sg = SmolyakGrid(smin, smax, l) self.smin = array(smin) self.smax = array(smax) self.bounds = row_stack([smin, smax]) vertices = cartesian(zip(smin, smax)).T self.grid = column_stack([sg.grid, vertices])
def __init__(self, smin,smax,orders): MultivariateSplinesCython.__init__(self,smin,smax,orders) from dolo.numeric.misc import cartesian grid = cartesian( [numpy.linspace(self.smin[i], self.smax[i], self.orders[i]) for i in range(self.d)] ).T self.grid = numpy.ascontiguousarray(grid) print(grid.shape)
def product_iid(iids: List[DiscretizedIIDProcess])->DiscretizedIIDProcess: from dolo.numeric.misc import cartesian nn = [len(f.integration_weights) for f in iids] cart = cartesian([range(e) for e in nn]) nodes = np.concatenate([f.integration_nodes[cart[:,i],:] for i,f in enumerate(iids)], axis=1) weights = iids[0].integration_weights for f in iids[1:]: weights = np.kron(weights, f.integration_weights) return DiscretizedIIDProcess(nodes, weights)
def product_iid(iids: List[FiniteDistribution])->FiniteDistribution: from dolo.numeric.misc import cartesian nn = [len(f.weights) for f in iids] cart = cartesian([range(e) for e in nn]) nodes = np.concatenate([f.points[cart[:,i],:] for i,f in enumerate(iids)], axis=1) weights = iids[0].weights for f in iids[1:]: weights = np.kron(weights, f.weights) return FiniteDistribution(nodes, weights)
def gauss_hermite_nodes(orders, sigma, mu=None): if isinstance(orders, int): orders = [orders] import numpy sigma = sigma.copy() if mu is None: mu = numpy.array([0] * sigma.shape[0]) herms = [hermgauss(i) for i in orders] points = [h[0] * numpy.sqrt(2) for h in herms] weights = [h[1] / numpy.sqrt(numpy.pi) for h in herms] if len(orders) == 1: x = numpy.array(points) * sigma w = weights[0] return [x.T, w] else: x = cartesian(points).T from functools import reduce w = reduce(numpy.kron, weights) zero_columns = numpy.where(sigma.sum(axis=0) == 0)[0] for i in zero_columns: sigma[i, i] = 1.0 C = numpy.linalg.cholesky(sigma) x = numpy.dot(C, x) + mu[:, numpy.newaxis] x = numpy.ascontiguousarray(x.T) for i in zero_columns: x[:, i] = 0 return [x, w]
def gauss_hermite_nodes(orders, sigma, mu=None): ''' Computes the weights and nodes for Gauss Hermite quadrature. Parameters ---------- orders : int, list, array The order of integration used in the quadrature routine sigma : array-like If one dimensional, the variance of the normal distribution being approximated. If multidimensional, the variance-covariance matrix of the multivariate normal process being approximated. Returns ------- x : array Quadrature nodes w : array Quadrature weights ''' if isinstance(orders, int): orders = [orders] import numpy if mu is None: mu = numpy.array([0] * sigma.shape[0]) herms = [hermgauss(i) for i in orders] points = [h[0] * numpy.sqrt(2) for h in herms] weights = [h[1] / numpy.sqrt(numpy.pi) for h in herms] if len(orders) == 1: # Note: if sigma is 2D, x will always be 2D, even if sigma is only 1x1. # print(points.shape) x = numpy.array(points[0]) * numpy.sqrt(float(sigma)) if sigma.ndim == 2: x = x[:, None] w = weights[0] return [x, w] else: x = cartesian(points).T from functools import reduce w = reduce(numpy.kron, weights) zero_columns = numpy.where(sigma.sum(axis=0) == 0)[0] for i in zero_columns: sigma[i, i] = 1.0 C = numpy.linalg.cholesky(sigma) x = numpy.dot(C, x) + mu[:, numpy.newaxis] x = numpy.ascontiguousarray(x.T) for i in zero_columns: x[:, i] = 0 return [x, w]