Ejemplo n.º 1
0
    def testEval(self):
        grid = Grid.createLinearGrid(1)
        grid.getGenerator().regular(3)
        gs = grid.getStorage()

        # prepare surplus vector
        nodalValues = DataVector(gs.getSize())
        nodalValues.setAll(0.0)

        # interpolation on nodal basis
        p = DataVector(gs.getDimension())
        for i in range(gs.getSize()):
            gs.getCoordinates(gs.getPoint(i), p)
            nodalValues[i] = f(p[0])
            # nodalValues[i] = f(p[0], p[1])

        # hierarchization
        alpha = hierarchize(grid, nodalValues)

        # eval the sparse grid function
        x = np.linspace(0, 1, 1000)
        y = [f(xi) for xi in x]
        y1 = [evalSGFunction(grid, alpha, np.array([xi])) for xi in x]
        y2 = evalSGFunctionMulti(grid, alpha, np.array([x]).T)

        assert np.all(y1 - y2 <= 1e-13)
Ejemplo n.º 2
0
    def __discretize(self):
        """
        Discretize squared f to obtain a well suited grid for all
        further computations.
        """
        def f(p, val):
            q = self.__T.unitToProbabilistic(p)
            return val ** 2 * self.__U.pdf(q)
        grid, _, _ = discretize(self.__grid, self.__alpha, f,
                                refnums=4, deg=1, epsilon=1e-6)

        # hierarchize without pdf
        gs = grid.getStorage()
        nodalValues = DataVector(gs.size())
        p = DataVector(gs.dim())
        for i in xrange(gs.size()):
            gs.get(i).getCoords(p)
            nodalValues[i] = evalSGFunction(self.__grid, self.__alpha, p)

        self.__alpha = hierarchize(grid, nodalValues)
        self.__grid = grid

        err = checkInterpolation(self.__grid, self.__alpha, nodalValues)
        if err is True:
            import pdb; pdb.set_trace()
Ejemplo n.º 3
0
def plotSG3d(grid, alpha, n=50, f=lambda x: x):
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X = np.linspace(0, 1, n)
    Y = np.linspace(0, 1, n)
    X, Y = np.meshgrid(X, Y)
    Z = np.zeros(n * n).reshape(n, n)
    for i in xrange(len(X)):
        for j, (x, y) in enumerate(zip(X[i], Y[i])):
            Z[i, j] = f(evalSGFunction(grid, alpha, DataVector([x, y])))

    # get grid points
    gs = grid.getStorage()
    gps = np.zeros([gs.size(), 2])
    p = DataVector(2)
    for i in xrange(gs.size()):
        gs.get(i).getCoords(p)
        gps[i, :] = p.array()

    surf = ax.plot_surface(X,
                           Y,
                           Z,
                           rstride=1,
                           cstride=1,
                           cmap=cm.coolwarm,
                           linewidth=0,
                           antialiased=False)
    ax.scatter(gps[:, 0], gps[:, 1], np.zeros(gs.size()))
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    # ax.set_zlim(0, 2)

    fig.colorbar(surf, shrink=0.5, aspect=5)
    return fig, ax, Z
Ejemplo n.º 4
0
def plotSG3d(grid, alpha, n=50, f=lambda x: x):
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    X = np.linspace(0, 1, n)
    Y = np.linspace(0, 1, n)
    X, Y = np.meshgrid(X, Y)
    Z = np.zeros(n * n).reshape(n, n)
    for i in xrange(len(X)):
        for j, (x, y) in enumerate(zip(X[i], Y[i])):
            Z[i, j] = f(evalSGFunction(grid, alpha, DataVector([x, y])))

    # get grid points
    gs = grid.getStorage()
    gps = np.zeros([gs.size(), 2])
    p = DataVector(2)
    for i in xrange(gs.size()):
        gs.get(i).getCoords(p)
        gps[i, :] = p.array()

    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                           linewidth=0, antialiased=False)
    ax.scatter(gps[:, 0], gps[:, 1], np.zeros(gs.size()))
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    # ax.set_zlim(0, 2)

    fig.colorbar(surf, shrink=0.5, aspect=5)
    return fig, ax, Z
Ejemplo n.º 5
0
    def __discretize(self):
        """
        Discretize squared f to obtain a well suited grid for all
        further computations.
        """
        def f(p, val):
            q = self.__T.unitToProbabilistic(p)
            return val**2 * self.__U.pdf(q)

        grid, _, _ = discretize(self.__grid,
                                self.__alpha,
                                f,
                                refnums=4,
                                deg=1,
                                epsilon=1e-6)

        # hierarchize without pdf
        gs = grid.getStorage()
        nodalValues = DataVector(gs.size())
        p = DataVector(gs.dim())
        for i in xrange(gs.size()):
            gs.get(i).getCoords(p)
            nodalValues[i] = evalSGFunction(self.__grid, self.__alpha, p)

        self.__alpha = hierarchize(grid, nodalValues)
        self.__grid = grid

        err = checkInterpolation(self.__grid, self.__alpha, nodalValues)
        if err is True:
            import pdb
            pdb.set_trace()
Ejemplo n.º 6
0
    def __init__(self, grid,
                 alpha,
                 trainData=None,
                 bounds=None,
                 config=None,
                 learner=None,
                 unitIntegrand=True,
                 isPositive=True):
        super(SGDEdist, self).__init__(grid.getStorage().getDimension(),
                                       trainData, bounds)

        self.grid = grid.clone()
        self.alpha = alpha.copy()
        self.alpha_vec = DataVector(alpha)
        if trainData is not None:
            self.trainData = trainData.copy()
        else:
            self.trainData = None

        self.config = config
        self.unitIntegrand = unitIntegrand

        if learner is None and trainData is not None:
            trainData_vec = DataMatrix(trainData)
            self.learner = SparseGridDensityEstimator(self.grid, self.alpha_vec, trainData_vec)
        else:
            self.learner = learner

        if trainData is None:
            self.dim = grid.getStorage().getDimension()
        elif self.dim != grid.getStorage().getDimension():
            raise AttributeError("the dimensionality of the data differs from the one of the grid")

        assert self.grid.getSize() == len(self.alpha)

        if isPositive:
            self.vol = createOperationQuadrature(self.grid).doQuadrature(self.alpha_vec)
        else:
            # do monte carlo quadrature to estimate the volume
            n = 20000
            numDims = grid.getStorage().getDimension()
            generator = LatinHypercubeSampleGenerator(numDims, n)
            samples = np.ndarray((n, numDims))
            sample = DataVector(numDims)
            for i in range(samples.shape[0]):
                generator.getSample(sample)
                samples[i, :] = sample.array()
            values = evalSGFunction(grid, alpha, samples)
            self.vol = np.mean([max(0.0, value) for value in values])

        # scale the coefficients such that it has unit integrand
        self.unnormalized_alpha = np.array(self.alpha / self.vol)
        self.unnormalized_alpha_vec = DataVector(self.unnormalized_alpha)

        self.vol *= self.trans.vol()
        if unitIntegrand and self.vol > 1e-13:
            self.alpha /= self.vol
            self.alpha_vec.mult(1. / self.vol)
Ejemplo n.º 7
0
def plotNodal1d(grid, alpha):
    gs = grid.getStorage()
    x = np.zeros(gs.size())
    nodalValues = np.zeros(gs.size())
    for i in xrange(gs.size()):
        x[i] = gs.get(i).getCoord(0)
        nodalValues[i] = evalSGFunction(grid, alpha, DataVector([x[i]]))

    plt.plot(x, nodalValues, " ", marker="o")
Ejemplo n.º 8
0
def plotSGNodal1d(grid, alpha):
    gs = grid.getStorage()
    A = np.ndarray([gs.getSize(), 2])

    p = DataVector(2)
    for i in range(gs.getSize()):
        gs.getCoordinates(gs.getPoint(i), p)
        A[i, 0] = p[0]
        A[i, 1] = evalSGFunction(grid, alpha, p.array())

    return plotNodal1d(A), A
Ejemplo n.º 9
0
def plotSGNodal3d(grid, alpha):
    gs = grid.getStorage()

    A = np.ndarray([gs.size(), 3])

    p = DataVector(2)
    for i in xrange(gs.size()):
        gs.get(i).getCoords(p)
        A[i, 0] = p[0]
        A[i, 1] = p[1]
        A[i, 2] = evalSGFunction(grid, alpha, p)

    return plotNodal3d(A), A
Ejemplo n.º 10
0
def plotSGNodal3d(grid, alpha):
    gs = grid.getStorage()

    A = np.ndarray([gs.size(), 3])

    p = DataVector(2)
    for i in xrange(gs.size()):
        gs.get(i).getCoords(p)
        A[i, 0] = p[0]
        A[i, 1] = p[1]
        A[i, 2] = evalSGFunction(grid, alpha, p)

    return plotNodal3d(A), A
Ejemplo n.º 11
0
def plotSG1d(grid, alpha, n=1000, f=lambda x: x, show_grid_points=False,
             **kws):
    x = np.linspace(0, 1, n)
    y = [f(evalSGFunction(grid, alpha, np.array([xi])))
         for xi in x]

    plt.plot(x, y, **kws)

    if show_grid_points:
        gs = grid.getStorage()
        for i in range(gs.getSize()):
            x = gs.getCoordinate(gs.getPoint(i), 0)
            plt.scatter(x, 0, color="black")
Ejemplo n.º 12
0
    def eval(self, samples, ts=None, dtype=KnowledgeTypes.SIMPLE):
        ans = {}
        if ts is None:
            ts = self.__uqManager.getTimeStepsOfInterest()

        qoi = self.__uqManager.getQoI()
        grid = self.__knowledge.getGrid(qoi)
        for t in ts:
            alpha = self.__knowledge.getAlpha(qoi, t, dtype)
            ans[t] = evalSGFunction(grid, alpha, samples)

        if len(ts) == 1:
            ans = ans[ts[0]]

        return ans
Ejemplo n.º 13
0
    def eval(self, samples, ts=None, dtype=KnowledgeTypes.SIMPLE):
        ans = {}
        if ts is None:
            ts = self.__knowledge.getAvailableTimeSteps()

        grid = self.__knowledge.getGrid(self._qoi)
        for t in ts:
            alpha = self.__knowledge.getAlpha(self._qoi, t, dtype)
            res = evalSGFunction(grid, alpha, samples)
            ans[t] = res

        if len(ts) == 1:
            ans = ans[ts[0]]

        return ans
Ejemplo n.º 14
0
    def eval(self, samples, ts=None, dtype=KnowledgeTypes.SIMPLE):
        ans = {}
        if ts is None:
            ts = self.__knowledge.getAvailableTimeSteps()

        grid = self.__knowledge.getGrid(self._qoi)
        for t in ts:
            alpha = self.__knowledge.getAlpha(self._qoi, t, dtype)
            res = evalSGFunction(grid, alpha, samples)
            ans[t] = res

        if len(ts) == 1:
            ans = ans[ts[0]]

        return ans
Ejemplo n.º 15
0
    def pdf(self, x):
        # convert the parameter to the right format
        x = self._convertEvalPoint(x)

        # transform the samples to the unit hypercube
        if self.trans is not None:
            x_unit = self.trans.probabilisticToUnitMatrix(x)
        else:
            x_unit = x

        # evaluate the sparse grid density
        fx = evalSGFunction(self.grid, self.alpha, x_unit)

        # if there is just one value given, extract it from the list
        if len(fx) == 1:
            fx = fx[0]

        return max(0, fx)
Ejemplo n.º 16
0
    def __evalHigherOrderComponent(self, fi, x):
        """
        Evaluate the higher order components
        @param fi: linear combination of marginalized functions
        @param x: DataVector coordinate to be evaluated
        """
        # constant term
        sign, f0 = fi['const']
        s = sign * f0

        # higher order terms terms
        for sign, pperm in fi['var']:
            grid, alpha = self.__expectation_funcs[pperm]
            p = DataVector([x[ix] for ix in pperm])
            val = evalSGFunction(grid, alpha, p)
            s += sign * val

        return s
Ejemplo n.º 17
0
    def __evalHigherOrderComponent(self, fi, x):
        """
        Evaluate the higher order components
        @param fi: linear combination of marginalized functions
        @param x: DataVector coordinate to be evaluated
        """
        # constant term
        sign, f0 = fi['const']
        s = sign * f0

        # higher order terms terms
        for sign, pperm in fi['var']:
            grid, alpha = self.__expectation_funcs[pperm]
            p = DataVector([x[ix] for ix in pperm])
            val = evalSGFunction(grid, alpha, p)
            s += sign * val

        return s
Ejemplo n.º 18
0
    def pdf(self, x):
        # convert the parameter to the right format
        if isList(x):
            x = DataVector(x)
        elif isNumerical(x):
            return evalSGFunction(self.grid, self.alpha, DataVector([x]))

        if isinstance(x, DataMatrix):
            A = x
        elif isinstance(x, DataVector):
            A = DataMatrix(1, len(x))
            A.setRow(0, x)
        else:
            raise AttributeError('data type "%s" is not supported in SGDEdist' % type(x))

        # evaluate the sparse grid density
        fx = evalSGFunctionMulti(self.grid, self.alpha, A)

        # if there is just one value given, extract it from the list
        if len(fx) == 1:
            fx = fx[0]

        return fx
Ejemplo n.º 19
0
    def pdf(self, x):
        # convert the parameter to the right format
        if isList(x):
            x = DataVector(x)
        elif isNumerical(x):
            return evalSGFunction(self.grid, self.alpha, DataVector([x]))

        if isinstance(x, DataMatrix):
            A = x
        elif isinstance(x, DataVector):
            A = DataMatrix(1, len(x))
            A.setRow(0, x)
        else:
            raise AttributeError(
                'data type "%s" is not supported in SGDEdist' % type(x))

        # evaluate the sparse grid density
        fx = evalSGFunctionMulti(self.grid, self.alpha, A)

        # if there is just one value given, extract it from the list
        if len(fx) == 1:
            fx = fx[0]

        return fx
Ejemplo n.º 20
0
def plotSG3d(grid,
             alpha,
             n=36,
             f=lambda x: x,
             grid_points_at=0,
             z_min=np.Inf,
             isConsistent=True,
             show_grid=True,
             surface_plot=False,
             xoffset=0.0,
             yoffset=1.0):
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    x = np.linspace(0, 1, n + 1, endpoint=True)
    y = np.linspace(0, 1, n + 1, endpoint=True)
    Z = np.zeros((n + 1, n + 1))

    xv, yv = np.meshgrid(x, y, sparse=False, indexing='xy')
    for i in range(len(x)):
        for j in range(len(y)):
            Z[j, i] = f(
                evalSGFunction(grid,
                               alpha,
                               np.array([xv[j, i], yv[j, i]]),
                               isConsistent=isConsistent))

    if surface_plot:
        cmap = load_default_color_map()
        norm = colors.Normalize(vmin=Z.min(), vmax=Z.max(), clip=False)
        ax.plot_surface(xv, yv, Z, rstride=1, cstride=1, norm=norm, cmap=cmap)
    else:
        ax.plot_wireframe(xv, yv, Z, color="black")

    z_min, z_max = min(np.min(Z), z_min), np.max(Z)
    ax.set_zlim(z_min, z_max)
    if np.any(np.abs(alpha) > 1e-13):
        cset = ax.contour(xv, yv, Z, zdir='z', offset=z_min, cmap=cm.coolwarm)
        cset = ax.contour(xv,
                          yv,
                          Z,
                          zdir='x',
                          offset=xoffset,
                          cmap=cm.coolwarm)
        cset = ax.contour(xv,
                          yv,
                          Z,
                          zdir='y',
                          offset=yoffset,
                          cmap=cm.coolwarm)

    if show_grid:
        # get grid points
        gs = grid.getStorage()
        gps = np.zeros([gs.getSize(), 2])
        p = DataVector(2)
        for i in range(gs.getSize()):
            gs.getCoordinates(gs.getPoint(i), p)
            p0, p1 = p.array()
            color = "red" if alpha[i] < 0 else "blue"
            ax.plot(np.array([p0]),
                    np.array([p1]),
                    np.array([grid_points_at]),
                    " ",
                    c=color,
                    marker="o",
                    ms=15)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    # ax.set_zlim(0, 2)

    #     fig.colorbar(surf, shrink=0.5, aspect=5)
    return fig, ax, Z
Ejemplo n.º 21
0
def plotSG1d(grid, alpha, n=1000, f=lambda x: x, **kws):
    x = np.linspace(0, 1, n)
    y = [f(evalSGFunction(grid, alpha, DataVector([xi])))
         for xi in x]

    plt.plot(x, y, **kws)
 def f(p):
     val = evalSGFunction(grid, alpha, p)
     return val ** k
 def f(p):
     val = evalSGFunction(grid, alpha, p)
     return val ** k