def testMarginalization_3D(self): xlim = [[0, 1], [0, 1], [0, 1]] def f(x): return np.prod([4 * xi * (1 - xi) for xi in x]) d = [0] p = [0.1, 0.2, 0.5] # get marginalized sparse grid function level = 5 grid, alpha = interpolate(f, level, 3) n_grid, n_alpha, _ = doMarginalize(grid, alpha, linearForm=None, dd=d[:]) self.assertEqual(doQuadrature(n_grid, n_alpha), doQuadrature(n_grid, n_alpha)) xlim = [[0, 1], [0, 1], [0, 1]] # Quantity of interest bs = [0.1, 0.2, 1.5] def g(x, a): return abs((4. * x - 2.) + a) / (a + 1.) def h(xs): return np.prod([g(x, b) for x, b in zip(xs, bs)]) d = [0] p = [0.0, 0.3, 0.2] # get marginalized sparse grid function level = 5 grid, alpha = interpolate(h, level, 3) n_grid, n_alpha, _ = doMarginalize(grid, alpha, linearForm=None, dd=d[:]) s1 = doQuadrature(n_grid, n_alpha) n_grid, n_alpha, _ = doMarginalize(grid, alpha, linearForm=None, dd=[0]) s2 = doQuadrature(n_grid, n_alpha) s3 = doQuadrature(grid, alpha) self.assertTrue(abs(s1 - s2) < 1e-10) self.assertTrue(abs(s1 - s3) < 1e-10) self.assertTrue(abs(s2 - s3) < 1e-10)
def mean(self, grid, alpha, U, T, dd): 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 @param grid: Grid @param alpha: DataVector coefficients @param U: J joint pdf @param T: Transformation, joint transformation @param dd: dimensions over which to be integrated @return: expectation value """ if self.linearForm is None: self.linearForm = LinearGaussQuadratureStrategy(grid.getType()) # extract correct pdf for moment estimation _, W, D = self._extractPDFforMomentEstimation(U, T) # flatten dd dd = [d for di in dd for d in di] # do the marginalization ngrid, nalpha, err = doMarginalize(grid, alpha, self.linearForm, dd, (W, D)) return ngrid, nalpha, err
def mean(self, grid, alpha, U, T, dd): 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 @param grid: Grid @param alpha: DataVector coefficients @param U: J joint pdf @param T: Transformation, joint transformation @param dd: dimensions over which to be integrated @return: expectation value """ # extract correct pdf for moment estimation _, W = self._extractPDFforMomentEstimation(U, T) D = T.getTransformations() # flatten dd dd = [d for di in dd for d in di] # get the volume vol = np.prod([D[i].vol() for i in xrange(len(dd))]) # do the marginalization ngrid, nalpha, err = doMarginalize(grid, alpha, dd, (W, D)) # multiply with the volume nalpha.mult(vol) return ngrid, nalpha, err
def estimate(self, vol, grid, alpha, f, U, T, dd): r""" Extraction of the expectation the given sg function interpolating the product of function value and pdf. \int\limits_{[0, 1]^d} v(x) dy where v(x) := u(x) q(x) """ # extract correct pdf for moment estimation vol, W, D = self.__extractPDFforMomentEstimation(U, T, dd) # check if there are just uniform distributions given if all([isinstance(dist, Uniform) for dist in W.getDistributions()]): # for uniformly distributed RVS holds: vol * pdf(x) = 1 vol = 1 u = f else: # interpolate v(x) = f_N^k(x) * pdf(x) def u(p, val): """ function to be interpolated @param p: coordinates of collocation nodes @param val: sparse grid function value at position p """ # extract the parameters we are integrating over q = D.unitToProbabilistic(p) # compute pdf and take just the dd values marginal_pdf = W.pdf(q, marginal=True)[dd] return f(p, val) * np.prod(marginal_pdf) # discretize the function f on a sparse grid n_grid, n_alpha, err = discretize(grid, alpha, u, refnums=self.__refnums, epsilon=self.__epsilon, level=self.level, deg=self.__deg) n_alpha.mult(float(vol)) # Estimate the expectation value o_grid, o_alpha, errMarg = doMarginalize(n_grid, n_alpha, dd) return o_grid, o_alpha, err[1] + errMarg
def estimate(self, vol, grid, alpha, f, U, T, dd): r""" Extraction of the expectation the given sg function interpolating the product of function value and pdf. \int\limits_{[0, 1]^d} v(x) dy where v(x) := u(x) q(x) """ # extract correct pdf for moment estimation vol, W = self.__extractPDFforMomentEstimation(U, T, dd) # check if there are just uniform distributions given if all([isinstance(dist, Uniform) for dist in W.getDistributions()]): # for uniformly distributed RVS holds: vol * pdf(x) = 1 vol = 1 u = f else: # interpolate v(x) = f_N^k(x) * pdf(x) def u(p, val): """ function to be interpolated @param p: coordinates of collocation nodes @param val: sparse grid function value at position p """ # extract the parameters we are integrating over q = T.unitToProbabilistic(p) # compute pdf and take just the dd values marginal_pdf = W.pdf(q, marginal=True)[dd] return f(p, val) * np.prod(marginal_pdf) # discretize the function f on a sparse grid n_grid, n_alpha, err = discretize(grid, alpha, u, refnums=self.__refnums, epsilon=self.__epsilon, level=self.level, deg=self.__deg) n_alpha.mult(float(vol)) # Estimate the expectation value o_grid, o_alpha, errMarg = doMarginalize(n_grid, n_alpha, dd) return o_grid, o_alpha, err[1] + errMarg
def testMarginalization_2D(self): xlim = [[0, 1], [0, 1]] def f(x): return np.prod([4 * xi * (1 - xi) for xi in x]) d = [1] p = [0.5, 0.5] # get marginalized sparse grid function level = 5 grid, alpha = interpolate(f, level, 2) n_grid, n_alpha, err = doMarginalize(grid, alpha, linearForm=None, dd=d) # self.assertTrue(abs(q.quad(f, p, d[:], xlim, 10) - 2./3) < 1e-5) # self.assertTrue(abs(q.monte_carlo(f, p, d[:], xlim, 8192) - 2./3) < 1e-2) # self.assertTrue(abs(createOperationEval(n_grid).eval(n_alpha, DataVector([p.getCoord(1 - d[0])]])) - 2./3) < 1e-3) s1 = doQuadrature(n_grid, n_alpha) s2 = doQuadrature(grid, alpha) self.assertTrue(abs(s1 - s2) < 1e-14)