Exemplo n.º 1
0
    def mean(self, grid, alpha, U, T):
        r"""
        Estimate the expectation value using Monte-Carlo.

        \frac{1}{N}\sum\limits_{i = 1}^N f_N(x_i)

        where x_i \in \Gamma
        @return: (mean, error of bootstrapping)
        """
        # init
        _, W, D = self._extractPDFforMomentEstimation(U, T)
        moments = np.zeros(self.__npaths)
        for i in range(self.__npaths):
            samples = self.__getSamples(W, D, bootstrapping=True)
            res = evalSGFunctionMulti(grid, alpha, samples)

            # compute the moment
            moments[i] = np.mean(res)

        # error statistics
        if self.__npaths > 1:
            lower_percentile = np.percentile(moments, q=self.__percentile)
            upper_percentile = np.percentile(moments, q=100 - self.__percentile)
            err = max(lower_percentile, upper_percentile)
        else:
            err = lower_percentile = upper_percentile = np.Inf

        # calculate moment
        samples = self.__getSamples(W, D, bootstrapping=False)
        res = evalSGFunctionMulti(grid, alpha, samples)

        return {"value": np.mean(res),
                "err": err,
                "confidence_interval": (lower_percentile, upper_percentile)}
Exemplo n.º 2
0
    def var(self, grid, alpha, U, T, mean):
        r"""
        Estimate the expectation value using Monte-Carlo.

        \frac{1}{N}\sum\limits_{i = 1}^N (f_N(x_i) - E(f))^2

        where x_i \in \Gamma
        @return: (variance, error of bootstrapping)
        """
        # init
        _, W, D = self._extractPDFforMomentEstimation(U, T)
        moments = np.zeros(self.__npaths)
        for i in range(self.__npaths):
            samples = self.__getSamples(W, D, bootstrapping=True)
            res = evalSGFunctionMulti(grid, alpha, samples)

            # compute the moment
            moments[i] = np.sum((res - np.mean(res))**2) / (len(res) - 1.)

        # error statistics
        if self.__npaths > 1:
            lower_percentile = np.percentile(moments, q=self.__percentile)
            upper_percentile = np.percentile(moments,
                                             q=100 - self.__percentile)
            err = max(lower_percentile, upper_percentile)
        else:
            err = lower_percentile = upper_percentile = np.Inf

        # calculate moment
        samples = self.__getSamples(W, D, bootstrapping=False)

        #         plt.figure()
        #         plt.hist(samples[:, 0], normed=True, cumulative=False, label="histogram")
        #         plt.title("0: lognormal [%g, %g]" % (samples[:, 0].min(),
        #                                              samples[:, 0].max()))
        #         plt.figure()
        #         plt.hist(samples[:, 1], normed=True, cumulative=False, label="histogram")
        #         plt.title("0: beta [%g, %g]" % (samples[:, 1].min(),
        #                                         samples[:, 1].max()))
        #         plt.figure()
        #         plt.hist(samples[:, 2], normed=True, cumulative=False, label="histogram")
        #         plt.title("0: lognormal [%g, %g]" % (samples[:, 2].min(),
        #                                              samples[:, 2].max()))
        #         plt.show()

        res = evalSGFunctionMulti(grid, alpha, samples)

        return {
            "value": np.sum((res - mean)**2) / (len(res) - 1.),
            "err": err,
            "confidence_interval": (lower_percentile, upper_percentile)
        }
Exemplo n.º 3
0
    def evalError(self, data, alpha):
        """
        computes the discrete L2-error of the sparse grid interpolant
        with respect to some MC samples at given time steps
        @param data: DataContainer samples
        @param alpha: numpy array hierarchical coefficients
        @return: mean squared error
        """
        # check if points are in [0, 1]^d
        allPoints = data.getPoints().array()
        allModelValues = data.getValues().array()

        points = np.ndarray((0, data.dim))
        modelValues = np.array([])
        for i, point in enumerate(allPoints):
            if np.all(point >= 0) and np.all(point <= 1):
                points = np.vstack((points, point))
                modelValues = np.append(modelValues, allModelValues[i])

        if allPoints.shape[1] == 0:
            return np.array([])

        surrogateValues = evalSGFunctionMulti(self.grid, alpha, points)

        # compute error
        return np.abs(surrogateValues - modelValues)
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def var(self, grid, alpha, U, T, mean):
        r"""
        Estimate the expectation value using Monte-Carlo.

        \frac{1}{N}\sum\limits_{i = 1}^N (f_N(x_i) - E(f))^2

        where x_i \in \Gamma
        """
        # init
        _, W = self._extractPDFforMomentEstimation(U, T)
        moments = np.zeros(self.__npaths)
        vecMean = DataVector(self.__n)
        vecMean.setAll(mean)
        for i in xrange(self.__npaths):
            samples = self.__getSamples(W, T, self.__n)
            res = evalSGFunctionMulti(grid, alpha, samples)
            res.sub(vecMean)
            res.sqr()

            # compute the moment
            moments[i] = res.sum() / (len(res) - 1.)

        # error statistics
        err = np.Inf

        # calculate moment
        return np.sum(moments) / self.__npaths, err
Exemplo n.º 6
0
    def mean(self, grid, alpha, U, T):
        r"""
        Estimate the expectation value using Monte-Carlo.

        \frac{1}{N}\sum\limits_{i = 1}^N f_N(x_i)

        where x_i \in \Gamma
        """
        # init
        _, W = self._extractPDFforMomentEstimation(U, T)
        moments = np.zeros(self.__npaths)
        for i in xrange(self.__npaths):
            samples = self.__getSamples(W, T, self.__n)
            res = evalSGFunctionMulti(grid, alpha, samples)
            # multiply the result with the corresponding pdf value
#             sample = DataVector(samples.getNcols())
#             for j in xrange(samples.getNrows()):
#                 samples.getRow(j, sample)
#                 res[j] *= W.pdf(sample)

            # compute the moment
            moments[i] = res.sum() / len(res)

        # error statistics
        if self.__npaths > 1:
            err_clt = self.__c * np.sqrt(np.var(moments, ddof=1) / len(moments))
        else:
            err_clt = np.Inf

        # calculate moment
        return np.sum(moments) / self.__npaths, err_clt
Exemplo n.º 7
0
    def var(self, grid, alpha, U, T, mean):
        r"""
        Estimate the expectation value using Monte-Carlo.

        \frac{1}{N}\sum\limits_{i = 1}^N (f_N(x_i) - E(f))^2

        where x_i \in \Gamma
        """
        # init
        _, W = self._extractPDFforMomentEstimation(U, T)
        moments = np.zeros(self.__npaths)
        vecMean = DataVector(self.__n)
        vecMean.setAll(mean)
        for i in xrange(self.__npaths):
            samples = self.__getSamples(W, T, self.__n)
            res = evalSGFunctionMulti(grid, alpha, samples)
            res.sub(vecMean)
            res.sqr()

            # compute the moment
            moments[i] = res.sum() / (len(res) - 1.)

        # error statistics
        err = np.Inf

        # calculate moment
        return np.sum(moments) / self.__npaths, err
Exemplo n.º 8
0
    def mean(self, grid, alpha, U, T):
        r"""
        Estimate the expectation value using Monte-Carlo.

        \frac{1}{N}\sum\limits_{i = 1}^N f_N(x_i)

        where x_i \in \Gamma
        """
        # init
        _, W = self._extractPDFforMomentEstimation(U, T)
        moments = np.zeros(self.__npaths)
        for i in xrange(self.__npaths):
            samples = self.__getSamples(W, T, self.__n)
            res = evalSGFunctionMulti(grid, alpha, samples)
            # multiply the result with the corresponding pdf value
            #             sample = DataVector(samples.getNcols())
            #             for j in xrange(samples.getNrows()):
            #                 samples.getRow(j, sample)
            #                 res[j] *= W.pdf(sample)

            # compute the moment
            moments[i] = res.sum() / len(res)

        # error statistics
        if self.__npaths > 1:
            err_clt = self.__c * np.sqrt(
                np.var(moments, ddof=1) / len(moments))
        else:
            err_clt = np.Inf

        # calculate moment
        return np.sum(moments) / self.__npaths, err_clt
Exemplo n.º 9
0
    def sampleGrids(self, filename):
        ts = self.__learner.getTimeStepsOfInterest()

        names = self.__params.getNames()
        names.append('f_\\mathcal{I}(x)')

        for t in ts:
            grid, surplus = self.__knowledge.getSparseGridFunction(
                self._qoi, t)

            # init
            gs = grid.getStorage()
            dim = gs.dim()

            # -----------------------------------------
            # do full grid sampling of sparse grid function
            # -----------------------------------------
            data = eval_fullGrid(4, dim)
            res = evalSGFunctionMulti(grid, surplus, data)

            data.transpose()
            data.appendRow()
            data.setRow(data.getNrows() - 1, res)
            data.transpose()

            # write results
            writeDataARFF({
                'filename': "%s.t%f.samples.arff" % (filename, t),
                'data': data,
                'names': names
            })

            # -----------------------------------------
            # write sparse grid points to file
            # -----------------------------------------
            data = DataMatrix(gs.size(), dim)
            data.setAll(0.0)

            for i in xrange(gs.size()):
                gp = gs.get(i)
                v = np.array([gp.getCoord(j) for j in xrange(dim)])
                data.setRow(i, DataVector(v))

            # write results
            writeDataARFF({
                'filename': "%s.t%f.gridpoints.arff" % (filename, t),
                'data': data,
                'names': names
            })

            # -----------------------------------------
            # write alpha
            # -----------------------------------------
            writeAlphaARFF("%s.t%f.alpha.arff" % (filename, t), surplus)
Exemplo n.º 10
0
    def sampleGrids(self, filename):
        ts = self.__uqManager.getTimeStepsOfInterest()

        names = self.__params.getNames()
        names.append('f_\\mathcal{I}(x)')

        for t in ts:
            grid, surplus = self.__knowledge.getSparseGridFunction(
                self._qoi, t)

            # init
            gs = grid.getStorage()
            dim = gs.getDimension()

            # -----------------------------------------
            # do full grid sampling of sparse grid function
            # -----------------------------------------
            data = eval_fullGrid(4, dim)
            res = evalSGFunctionMulti(grid, surplus, data)

            data = np.vstack((data.T, res)).T

            # write results
            data_vec = DataMatrix(data)
            writeDataARFF({
                'filename': "%s.t%f.samples.arff" % (filename, t),
                'data': data_vec,
                'names': names
            })
            del data_vec
            # -----------------------------------------
            # write sparse grid points to file
            # -----------------------------------------
            data = np.ndarray((gs.getSize(), dim))
            x = DataVector(dim)
            for i in range(gs.getSize()):
                gp = gs.getPoint(i)
                gs.getCoordinates(gp, x)
                data[i, :] = x.array()

            # write results
            data_vec = DataMatrix(data)
            writeDataARFF({
                'filename': "%s.t%f.gridpoints.arff" % (filename, t),
                'data': data_vec,
                'names': names
            })
            del data_vec
            # -----------------------------------------
            # write alpha
            # -----------------------------------------
            writeAlphaARFF("%s.t%f.alpha.arff" % (filename, t), surplus)
Exemplo n.º 11
0
    def sampleGrids(self, filename):
        ts = self.__learner.getTimeStepsOfInterest()

        names = self.__params.getNames()
        names.append('f_\\mathcal{I}(x)')

        for t in ts:
            grid, surplus = self.__knowledge.getSparseGridFunction(self._qoi, t)

            # init
            gs = grid.getStorage()
            dim = gs.dim()

            # -----------------------------------------
            # do full grid sampling of sparse grid function
            # -----------------------------------------
            data = eval_fullGrid(4, dim)
            res = evalSGFunctionMulti(grid, surplus, data)

            data.transpose()
            data.appendRow()
            data.setRow(data.getNrows() - 1, res)
            data.transpose()

            # write results
            writeDataARFF({'filename': "%s.t%f.samples.arff" % (filename, t),
                           'data': data,
                           'names': names})

            # -----------------------------------------
            # write sparse grid points to file
            # -----------------------------------------
            data = DataMatrix(gs.size(), dim)
            data.setAll(0.0)

            for i in xrange(gs.size()):
                gp = gs.get(i)
                v = np.array([gp.getCoord(j) for j in xrange(dim)])
                data.setRow(i, DataVector(v))

            # write results
            writeDataARFF({'filename': "%s.t%f.gridpoints.arff" % (filename, t),
                           'data': data,
                           'names': names})

            # -----------------------------------------
            # write alpha
            # -----------------------------------------
            writeAlphaARFF("%s.t%f.alpha.arff" % (filename, t),
                           surplus)
Exemplo n.º 12
0
    def evalError(self, data, alpha):
        """
        computes the discrete L2-error of the sparse grid interpolant
        with respect to some MC samples at given time steps
        @param data: DataContainer samples
        @param alpha: DataVector hierarchical coefficients
        @return: mean squared error
        """
        points = data.getPoints()
        values = data.getValues()
        size = points.getNrows()
        if size == 0:
            return 0

        self.error = evalSGFunctionMulti(self.grid, alpha, points)
        # compute L2 error
        self.error.sub(values)
        self.error.sqr()

        return self.error.sum() / len(self.error)
Exemplo n.º 13
0
    def evalError(self, data, alpha):
        """
        computes the discrete L2-error of the sparse grid interpolant
        with respect to some MC samples at given time steps
        @param data: DataContainer samples
        @param alpha: DataVector hierarchical coefficients
        @return: mean squared error
        """
        points = data.getPoints()
        values = data.getValues()
        size = points.getNrows()
        if size == 0:
            return 0

        self.error = evalSGFunctionMulti(self.grid, alpha, points)
        # compute L2 error
        self.error.sub(values)
        self.error.sqr()

        return self.error.sum() / len(self.error)
Exemplo n.º 14
0
    def __estimate(self, vol, grid, alpha, U, T, f, npaths):
        n = npaths * self.__n
        A = self.__getSamples(U, T, n)
        # import matplotlib.pyplot as plt
        # fig = plt.figure()
        # plt.plot(A[:, 0], A[:, 1], ' ', marker='^')
        # fig.show()
        # override the old samples with the new ones
        # A[:, :2] = self.__getSamples(l)
        # A[:, :2] = self.__getDataSamples()
        # fig = plt.figure()
        # plt.plot(A[:, 0], A[:, 1], ' ', marker='^')
        # fig.show()
        # A[:, :2] = self.__getDataSamples()
        # fig = plt.figure()
        # plt.plot(A[:, 0], A[:, 1], ' ', marker='^')
        # fig.show()
        # import ipdb; ipdb.set_trace()

        vals = evalSGFunctionMulti(grid, alpha, A).array()
        fx = np.ndarray([len(vals)], dtype='float')
        p = DataVector(A.getNcols())
        for i, val in enumerate(vals):
            A.getRow(i, p)
            fx[i] = f(p.array(), val)
#             q = T.unitToProbabilistic(p)
#             A.setRow(i, DataVector(q))

        # get the pdf of the values
        # fx *= U.pdf(A.array())

        if self.__isPositive:
            fx = abs(fx)

        # # define here grid for corners and run the samples here too
        # grid_file = '/home/franzefn/Promotion/Projekte/CO2/UQ5analytical/results/co2_leakage_analytical/sgb1deg2/sg_l1/grids/sg.t%g.grid' % t
        # alpha_file = '/home/franzefn/Promotion/Projekte/CO2/UQ5analytical/results/co2_leakage_analytical/sgb1deg2/sg_l1/grids/sg.t%g.alpha.arff' % t
        # borderGrid = readGrid(grid_file)
        # borderAlpha = readAlphaARFF(alpha_file)
        # nodalValues = dehierarchize(grid, alpha)
        # gs = grid.getStorage()
        # bordergs = borderGrid.getStorage()
        # p = DataVector(gs.dim())
        # for i in xrange(gs.size()):
        #     gs.get(i).getCoords(p)
        #     nodalValues[i] -= evalSGFunction(borderGrid, borderAlpha, p)
        # nalpha = hierarchize(grid, nodalValues)
        # # # check if interpolation criterion is fulfilled for splitted grid
        # # p = DataVector(gs.dim())
        # # for i in xrange(gs.size()):
        # #     gp = gs.get(i)
        # #     if bordergs.has_key(gp):
        # #         gp.getCoords(p)
        # #         res1 = evalSGFunction(grid, alpha, p)
        # #         res2 = evalSGFunction(grid, nalpha, p) + evalSGFunction(borderGrid, borderAlpha, p)
        # #         print res1, res2, abs(res1 - res2)
        # # fig = scatterplot_matrix(A.T, ['phi', 'e', 'kl'], linestyle=' ', marker='o')
        # # fig.show()
        # res1 = evalSGFunctionMulti(grid, nalpha, DataMatrix(A)).array()
        # res2 = evalSGFunctionMulti(borderGrid, borderAlpha, DataMatrix(A)).array()
        # res = res1 + res2

        mean = np.ndarray(npaths, dtype='float')
        for i in xrange(npaths):
            mean[i] = np.mean(fx[(i * self.__n):((i + 1) * self.__n)])  # * vol
        return mean
Exemplo n.º 15
0
    def __estimate(self, vol, grid, alpha, U, T, f, npaths):
        n = npaths * self.__n
        A = self.__getSamples(U, T, n)
        # import matplotlib.pyplot as plt
        # fig = plt.figure()
        # plt.plot(A[:, 0], A[:, 1], ' ', marker='^')
        # fig.show()
        # override the old samples with the new ones
        # A[:, :2] = self.__getSamples(l)
        # A[:, :2] = self.__getDataSamples()
        # fig = plt.figure()
        # plt.plot(A[:, 0], A[:, 1], ' ', marker='^')
        # fig.show()
        # A[:, :2] = self.__getDataSamples()
        # fig = plt.figure()
        # plt.plot(A[:, 0], A[:, 1], ' ', marker='^')
        # fig.show()
        # import ipdb; ipdb.set_trace()

        vals = evalSGFunctionMulti(grid, alpha, A).array()
        fx = np.ndarray([len(vals)], dtype='float')
        p = DataVector(A.getNcols())
        for i, val in enumerate(vals):
            A.getRow(i, p)
            fx[i] = f(p.array(), val)
#             q = T.unitToProbabilistic(p)
#             A.setRow(i, DataVector(q))

# get the pdf of the values
# fx *= U.pdf(A.array())

        if self.__isPositive:
            fx = abs(fx)

        # # define here grid for corners and run the samples here too
        # grid_file = '/home/franzefn/Promotion/Projekte/CO2/UQ5analytical/results/co2_leakage_analytical/sgb1deg2/sg_l1/grids/sg.t%g.grid' % t
        # alpha_file = '/home/franzefn/Promotion/Projekte/CO2/UQ5analytical/results/co2_leakage_analytical/sgb1deg2/sg_l1/grids/sg.t%g.alpha.arff' % t
        # borderGrid = readGrid(grid_file)
        # borderAlpha = readAlphaARFF(alpha_file)
        # nodalValues = dehierarchize(grid, alpha)
        # gs = grid.getStorage()
        # bordergs = borderGrid.getStorage()
        # p = DataVector(gs.dim())
        # for i in xrange(gs.size()):
        #     gs.get(i).getCoords(p)
        #     nodalValues[i] -= evalSGFunction(borderGrid, borderAlpha, p)
        # nalpha = hierarchize(grid, nodalValues)
        # # # check if interpolation criterion is fulfilled for splitted grid
        # # p = DataVector(gs.dim())
        # # for i in xrange(gs.size()):
        # #     gp = gs.get(i)
        # #     if bordergs.has_key(gp):
        # #         gp.getCoords(p)
        # #         res1 = evalSGFunction(grid, alpha, p)
        # #         res2 = evalSGFunction(grid, nalpha, p) + evalSGFunction(borderGrid, borderAlpha, p)
        # #         print res1, res2, abs(res1 - res2)
        # # fig = scatterplot_matrix(A.T, ['phi', 'e', 'kl'], linestyle=' ', marker='o')
        # # fig.show()
        # res1 = evalSGFunctionMulti(grid, nalpha, DataMatrix(A)).array()
        # res2 = evalSGFunctionMulti(borderGrid, borderAlpha, DataMatrix(A)).array()
        # res = res1 + res2

        mean = np.ndarray(npaths, dtype='float')
        for i in xrange(npaths):
            mean[i] = np.mean(fx[(i * self.__n):((i + 1) * self.__n)])  # * vol
        return mean