def mean(self, grid, alpha, U, T): r""" Extraction of the expectation the given sparse grid function interpolating the product of function value and pdf. \int\limits_{[0, 1]^d} f_N(x) * pdf(x) dx """ # extract correct pdf for moment estimation vol, W = self._extractPDFforMomentEstimation(U, T) D = T.getTransformations() # compute the integral of the product gs = grid.getStorage() acc = DataVector(gs.size()) acc.setAll(1.) tmp = DataVector(gs.size()) err = 0 # run over all dimensions for i, dims in enumerate(W.getTupleIndices()): dist = W[i] trans = D[i] # get the objects needed for integration the current dimensions gpsi, basisi = project(grid, dims) if isinstance(dist, SGDEdist): # if the distribution is given as a sparse grid function we # need to compute the bilinear form of the grids # accumulate objects needed for computing the bilinear form gpsj, basisj = project(dist.grid, range(len(dims))) # compute the bilinear form bf = BilinearGaussQuadratureStrategy() A, erri = bf.computeBilinearFormByList(gpsi, basisi, gpsj, basisj) # weight it with the coefficient of the density function self.mult(A, dist.alpha, tmp) else: # the distribution is given analytically, handle them # analytically in the integration of the basis functions if isinstance(dist, Dist) and len(dims) > 1: raise AttributeError('analytic quadrature not supported for multivariate distributions') if isinstance(dist, Dist): dist = [dist] trans = [trans] lf = LinearGaussQuadratureStrategy(dist, trans) tmp, erri = lf.computeLinearFormByList(gpsi, basisi) # print error stats # print "%s: %g -> %g" % (str(dims), err, err + D[i].vol() * erri) # import ipdb; ipdb.set_trace() # accumulate the error err += D[i].vol() * erri # accumulate the result acc.componentwise_mult(tmp) moment = alpha.dotProduct(acc) return vol * moment, err
def estimate(self, vol, grid, alpha, f, U, T): r""" Extraction of the expectation the given sparse grid function interpolating the product of function value and pdf. \int\limits_{[0, 1]^d} f(x) * pdf(x) dx """ # first: discretize f fgrid, falpha, discError = discretize(grid, alpha, f, self.__epsilon, self.__refnums, self.__pointsNum, self.level, self.__deg, True) # extract correct pdf for moment estimation vol, W, pdfError = self.__extractDiscretePDFforMomentEstimation(U, T) D = T.getTransformations() # compute the integral of the product gs = fgrid.getStorage() acc = DataVector(gs.size()) acc.setAll(1.) tmp = DataVector(gs.size()) for i, dims in enumerate(W.getTupleIndices()): sgdeDist = W[i] # accumulate objects needed for computing the bilinear form gpsi, basisi = project(fgrid, dims) gpsj, basisj = project(sgdeDist.grid, range(len(dims))) A = self.__computeMassMatrix(gpsi, basisi, gpsj, basisj, W, D) # A = ScipyQuadratureStrategy(W, D).computeBilinearForm(fgrid) self.mult(A, sgdeDist.alpha, tmp) acc.componentwise_mult(tmp) moment = falpha.dotProduct(acc) return vol * moment, discError[1] + pdfError
def computeSystemMatrixForVarianceProjected(self, gs, gpsi, basisi, gpsj, basisj, dist, trans, dims): if isinstance(dist, SGDEdist): # project distribution on desired dimensions # get the objects needed for integrating # the current dimensions gpsk, basisk = project(dist.grid, list(range(len(dims)))) # compute the bilinear form # -> the measure needs to be uniform, since it is already # encoded in the sparse grid density self.trilinearForm.setDistributionAndTransformation( [Uniform(0, 1)] * gs.getDimension(), None) A_idim, erri = self.trilinearForm.computeTrilinearFormByList( gs, gpsk, basisk, dist.alpha, gpsi, basisi, gpsj, basisj) else: # the distribution is given analytically, handle them # analytically in the integration of the basis functions if isinstance(dist, Dist) and len(dims) > 1: # print "WARNINING: results are just approximated -> not independent random variables" # marginalize the densities and continue marg_dists = [None] * len(dims) for i, idim in enumerate(dims): marg_dists[i] = dist.marginalizeToDimX(idim) dist = marg_dists trans = trans.getTransformations() if isinstance(dist, Dist): dist = [dist] trans = [trans] self.bilinearForm.setDistributionAndTransformation(dist, trans) A_idim, erri = self.bilinearForm.computeBilinearFormByList( gs, gpsi, basisi, gpsj, basisj) return A_idim, erri
def computeSystemMatrixForMeanProjected(self, gs, gpsi, basisi, dist, trans, dims): if isinstance(dist, SGDEdist): # if the distribution is given as a sparse grid function we # need to compute the bilinear form of the grids # accumulate objects needed for computing the bilinear form assert len(dims) == dist.grid.getStorage().getDimension() gpsj, basisj = project(dist.grid, list(range(len(dims)))) # compute the bilinear form # -> the measure needs to be uniform, since it is already # encoded in the sparse grid density self.bilinearForm.setDistributionAndTransformation( [Uniform(0, 1)] * gs.getDimension(), None) A, erri = self.bilinearForm.computeBilinearFormByList( gs, gpsi, basisi, gpsj, basisj) # weight it with the coefficient of the density function tmp = A.dot(dist.alpha) else: # the distribution is given analytically, handle them # analytically in the integration of the basis functions if isinstance(dist, Dist) and len(dims) > 1: # print "WARNINING: results are just approximated -> not independent random variables" # marginalize the densities and continue marg_dists = [None] * len(dims) for i, idim in enumerate(dims): marg_dists[i] = dist.marginalizeToDimX(idim) dist = marg_dists trans = trans.getTransformations() if isinstance(dist, Dist): dist = [dist] trans = [trans] self.linearForm.setDistributionAndTransformation(dist, trans) tmp, erri = self.linearForm.computeLinearFormByList( gs, gpsi, basisi) return tmp, erri
def var(self, grid, alpha, U, T, mean): r""" Extraction of the expectation the given sparse grid function interpolating the product of function value and pdf. \int\limits_{[0, 1]^d} (f(x) - E(f))^2 * pdf(x) dx """ # extract correct pdf for moment estimation vol, W = self._extractPDFforMomentEstimation(U, T) D = T.getTransformations() # copy the grid, and add a trapezoidal boundary # ngrid = GridDescriptor().fromGrid(grid)\ # .withBorder(BorderTypes.TRAPEZOIDBOUNDARY)\ # .createGrid() # compute nodalValues # ngs = ngrid.getStorage() # nodalValues = DataVector(ngs.size()) # p = DataVector(ngs.dim()) # for i in xrange(ngs.size()): # ngs.get(i).getCoords(p) # nodalValues[i] = evalSGFunction(grid, alpha, p) - mean # # # hierarchize the new function # nalpha = hierarchize(ngrid, nodalValues) ngs = grid.getStorage() ngrid, nalpha = grid, alpha # compute the integral of the product times the pdf acc = DataMatrix(ngs.size(), ngs.size()) acc.setAll(1.) err = 0 for i, dims in enumerate(W.getTupleIndices()): dist = W[i] trans = D[i] # get the objects needed for integrating # the current dimensions gpsi, basisi = project(ngrid, dims) if isinstance(dist, SGDEdist): # project distribution on desired dimensions # get the objects needed for integrating # the current dimensions gpsk, basisk = project(dist.grid, range(len(dims))) # compute the bilinear form tf = TrilinearGaussQuadratureStrategy([dist], trans) A, erri = tf.computeTrilinearFormByList(gpsk, basisk, dist.alpha, gpsi, basisi, gpsi, basisi) else: # we compute the bilinear form of the grids # compute the bilinear form if len(dims) == 1: dist = [dist] trans = [trans] bf = BilinearGaussQuadratureStrategy(dist, trans) A, erri = bf.computeBilinearFormByList(gpsi, basisi, gpsi, basisi) # accumulate the results acc.componentwise_mult(A) # accumulate the error err += acc.sum() / (acc.getNrows() * acc.getNcols()) * erri # compute the variance tmp = DataVector(acc.getNrows()) self.mult(acc, nalpha, tmp) moment = vol * nalpha.dotProduct(tmp) moment = moment - mean ** 2 return moment, err
def var(self, grid, alpha, U, T, mean): r""" Extraction of the expectation the given sparse grid function interpolating the product of function value and pdf. \int\limits_{[0, 1]^d} (f(x) - E(f))^2 * pdf(x) dx """ # extract correct pdf for moment estimation vol, W = self._extractPDFforMomentEstimation(U, T) D = T.getTransformations() # copy the grid, and add a trapezoidal boundary # ngrid = GridDescriptor().fromGrid(grid)\ # .withBorder(BorderTypes.TRAPEZOIDBOUNDARY)\ # .createGrid() # compute nodalValues # ngs = ngrid.getStorage() # nodalValues = DataVector(ngs.size()) # p = DataVector(ngs.dim()) # for i in xrange(ngs.size()): # ngs.get(i).getCoords(p) # nodalValues[i] = evalSGFunction(grid, alpha, p) - mean # # # hierarchize the new function # nalpha = hierarchize(ngrid, nodalValues) ngs = grid.getStorage() ngrid, nalpha = grid, alpha # compute the integral of the product times the pdf acc = DataMatrix(ngs.size(), ngs.size()) acc.setAll(1.) err = 0 for i, dims in enumerate(W.getTupleIndices()): dist = W[i] trans = D[i] # get the objects needed for integrating # the current dimensions gpsi, basisi = project(ngrid, dims) if isinstance(dist, SGDEdist): # project distribution on desired dimensions # get the objects needed for integrating # the current dimensions gpsk, basisk = project(dist.grid, range(len(dims))) # compute the bilinear form tf = TrilinearGaussQuadratureStrategy([dist], trans) A, erri = tf.computeTrilinearFormByList( gpsk, basisk, dist.alpha, gpsi, basisi, gpsi, basisi) else: # we compute the bilinear form of the grids # compute the bilinear form if len(dims) == 1: dist = [dist] trans = [trans] bf = BilinearGaussQuadratureStrategy(dist, trans) A, erri = bf.computeBilinearFormByList(gpsi, basisi, gpsi, basisi) # accumulate the results acc.componentwise_mult(A) # accumulate the error err += acc.sum() / (acc.getNrows() * acc.getNcols()) * erri # compute the variance tmp = DataVector(acc.getNrows()) self.mult(acc, nalpha, tmp) moment = vol * nalpha.dotProduct(tmp) moment = moment - mean**2 return moment, err
def mean(self, grid, alpha, U, T): r""" Extraction of the expectation the given sparse grid function interpolating the product of function value and pdf. \int\limits_{[0, 1]^d} f_N(x) * pdf(x) dx """ # extract correct pdf for moment estimation vol, W = self._extractPDFforMomentEstimation(U, T) D = T.getTransformations() # compute the integral of the product gs = grid.getStorage() acc = DataVector(gs.size()) acc.setAll(1.) tmp = DataVector(gs.size()) err = 0 # run over all dimensions for i, dims in enumerate(W.getTupleIndices()): dist = W[i] trans = D[i] # get the objects needed for integration the current dimensions gpsi, basisi = project(grid, dims) if isinstance(dist, SGDEdist): # if the distribution is given as a sparse grid function we # need to compute the bilinear form of the grids # accumulate objects needed for computing the bilinear form gpsj, basisj = project(dist.grid, range(len(dims))) # compute the bilinear form bf = BilinearGaussQuadratureStrategy() A, erri = bf.computeBilinearFormByList(gpsi, basisi, gpsj, basisj) # weight it with the coefficient of the density function self.mult(A, dist.alpha, tmp) else: # the distribution is given analytically, handle them # analytically in the integration of the basis functions if isinstance(dist, Dist) and len(dims) > 1: raise AttributeError( 'analytic quadrature not supported for multivariate distributions' ) if isinstance(dist, Dist): dist = [dist] trans = [trans] lf = LinearGaussQuadratureStrategy(dist, trans) tmp, erri = lf.computeLinearFormByList(gpsi, basisi) # print error stats # print "%s: %g -> %g" % (str(dims), err, err + D[i].vol() * erri) # import ipdb; ipdb.set_trace() # accumulate the error err += D[i].vol() * erri # accumulate the result acc.componentwise_mult(tmp) moment = alpha.dotProduct(acc) return vol * moment, err