Example #1
0
    def defineParameters(cls, numDims, setting):
        # set distributions of the input parameters
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()

        if setting == "normal":
            # set distributions of the input parameters
            mu = np.array([0.5] * numDims)
            diag = np.eye(numDims) * 0.005
            offdiag = np.abs(np.eye(numDims) - 1) * 0.001
            cov = diag + offdiag

            # estimate the density
            names = ", ".join(["x%i" for i in range(numDims)])
            up.new().isCalled(names).withMultivariateNormalDistribution(
                mu, cov, 0, 1)
        elif setting == "uniform":
            for idim in range(numDims):
                up.new().isCalled("x%i" % idim).withUniformDistribution(0, 1)
        elif setting == "tnormal":
            for idim in range(numDims):
                up.new().isCalled("x%i" % idim).withTNormalDistribution(
                    0.5, 0.1, 0, 1)

        cls.params = builder.andGetResult()
Example #2
0
    def testSettings(self):
        builder = ParameterBuilder()
        dp = builder.defineDeterministicParameters()
        up = builder.defineUncertainParameters()

        up.new().isCalled('v').withUniformDistribution(
            0, 10).withRosenblattTransformation()
        dp.new().isCalled('density').hasValue(.3)
        up.new().isCalled('theta').withTNormalDistribution(
            1, 1, -2, 2).withLinearTransformation()
        dp.new().isCalled('radius').hasValue(10)
        up.new().isCalled('height').withBetaDistribution(
            3, 3, 0, 2).withRosenblattTransformation()
        params = builder.andGetResult()

        ap = params.activeParams()

        dist = ap.getIndependentJointDistribution()
        trans = ap.getJointTransformation()

        for _ in range(1000):
            prob1 = dist.rvs()[0]
            unit = trans.probabilisticToUnit(prob1)
            prob2 = trans.unitToProbabilistic(unit)

            assert all(["%g" % x == "%g" % y for x, y in zip(prob1, prob2)])
Example #3
0
    def testSettings(self):
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()
        up.new().isCalled('x').withTNormalDistribution(0, .1, -2, 2)
        up.new().isCalled('y').withTLognormalDistribution(1e-12,
                                                          np.exp(-1),
                                                          alpha=.01)
        up.new().isCalled('z').withUniformDistribution(0, 1).hasValue(0)
        params = builder.andGetResult()

        n = 100

        samplers = {
            'naive':
            MCSampler.withNaiveSampleGenerator(params),
            'latin hypercube':
            MCSampler.withLatinHypercubeSampleGenerator(params, n)
        }

        for name, sampler in list(samplers.items()):
            samples = sampler.nextSamples(n)
            samples.selectProbabilisticSpace().selectActiveElements()
            res = samples.ndarray()
            for idim, label in enumerate(params.activeParams().getNames()):
                xlower, xupper = params.getParameter(
                    label).getDistribution().getBounds()
                assert all(xlower <= res[:, idim])
                assert all(res[:, idim] <= xupper)
Example #4
0
    def testSettings(self):
        # set distributions of the input parameters
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()
        up.new().isCalled('x').withUniformDistribution(0, 1)
        up.new().isCalled('y').withUniformDistribution(0, 1)
        params = builder.andGetResult()

        def postprocessor(x, *args, **kws):
            return {'x': np.array([x[0], -x[0]]), 'time': list(range(len(x)))}

        f = lambda x, **kws: [x[0], 2 * x[0], 4 * x[0], 8 * x[0]]
        toi = [0, 1]

        # set up uq setting
        builder = ASGCUQManagerBuilder().withParameters(params)\
                                        .withTypesOfKnowledge([KnowledgeTypes.SIMPLE,
                                                               KnowledgeTypes.SQUARED])\
                                        .withQoI("x")\
                                        .withTimeStepsOfInterest(toi)\
                                        .useInterpolation()

        builder.defineUQSetting().withSimulation(f)\
                                 .withPostprocessor(postprocessor)

        builder.defineSampler().withGrid().withLevel(3)\
                                          .hasType(GridType_Poly)\
                                          .withDegree(2)\
                                          .withBoundaryLevel(1)

        uqManager = builder.andGetResult()
        uqManager.runNextSamples()

        # define analysis
        uqAnalysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                          .withAnalyticEstimationStrategy()\
                                          .andGetResult()

        # plot result
        ans = {}
        for t in uqManager.getTimeStepsOfInterest():
            grid, alpha = uqManager.getKnowledge().getSparseGridFunction(
                t=t, qoi="x")
            fig = plt.figure()
            ans[t] = plotSG2d(grid,
                              alpha,
                              show_grid_points=True,
                              show_numbers=True)
            fig.show()

        assert all(ans[0] == -ans[1])
        plt.show()
Example #5
0
    def setUp(self):
        self.radix = 'test_anova'

        # --------------------------------------------------------
        # set distributions of the input parameters
        # --------------------------------------------------------
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()
        up.new().isCalled('x').withUniformDistribution(0, 1)
        up.new().isCalled('y').withUniformDistribution(0, 1)
        up.new().isCalled('z').withUniformDistribution(0, 1)

        self.params = builder.andGetResult()

        # --------------------------------------------------------
        # simulation function
        # --------------------------------------------------------
        bs = [
            x for ix, x in enumerate([0.1, 0.2, 1.5]) if ix <= len(self.params)
        ]

        def g(x, a):
            return (abs(4 * x - 2) + a) / (a + 1)

        def f(xs, **kws):
            return np.prod([g(x, b) for x, b in zip(xs, bs)])

        self.simulation = f

        # --------------------------------------------------------
        # analytic reference values
        # --------------------------------------------------------

        def vi(i):
            return 1. / (3 * (1 + bs[i])**2)

        def vij(i, j):
            return 1. / (9 * (1 + bs[i])**2 * (1 + bs[j])**2)

        def vijk(i, j, k):
            return 1. / (27 * (1 + bs[i])**2 * (1 + bs[j])**2 * (1 + bs[k])**2)

        self.v_t = dict([((i, ), vi(i)) for i in range(len(bs))] +
                        [((i, j), vij(i, j))
                         for i, j in [(0, 1), (0, 2), (1, 2)]] +
                        [((0, 1, 2), vijk(0, 1, 2))])
        self.vg = sum([v for v in list(self.v_t.values())])
Example #6
0
    def testSettings(self):
        if os.path.exists('testSetting.gz'):
            os.remove('testSetting.gz')

        # set distributions of the input parameters
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()

        up.new().isCalled('x').withUniformDistribution(0, 2)
        up.new().isCalled('y').withUniformDistribution(0, 2)

        # builder.withLinearTransformation()
        params = builder.andGetResult()

        uq_a = self.makeUQSetting()

        # prepare sample set
        points = np.random.rand(2, 2)
        samples = Samples(params)
        for point in points:
            samples.add(point, dtype=SampleType.ACTIVEUNIT)

        # run first test session
        uq_a.runSamples(samples)
        uq_a_json = uq_a.toJson()

        # restore results from file
        uq_b = self.makeUQSetting()
        uq_b_json = uq_b.toJson()

        # run second test session
        uq_b.runSamples(samples)

        # testing
        uq_c_json = uq_b.toJson()

        assert uq_b_json == uq_c_json
        assert uq_b_json == uq_a_json

        res = uq_b.getResults(qoi='x')

        assert list(res.keys()) == [0]

        for t, data in list(uq_b.toDataMatrix(qoi='x').items()):
            writeDataARFF({'filename': 'uqSetting_%g.arff' % t,
                           'data': data})
Example #7
0
    def setUp(self):
        self.radix = 'test_atan'

        # --------------------------------------------------------
        # set distributions of the input parameters
        # --------------------------------------------------------
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()
        up.new().isCalled('x').withUniformDistribution(0, 1)
        up.new().isCalled('y').withUniformDistribution(0, 1)

        self.params = builder.andGetResult()

        # define model function
        def g(x, **kws):
            return np.arctan(50 * (x[0] - .35)) + np.pi / 2 + 4 * x[1] ** 3 + np.exp(x[0] * x[1] - 1)

        self.simulation = g
Example #8
0
    def defineParameters(self, dtype="uniform"):
        # set distributions of the input parameters
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()

        if dtype == "uniform":
            up.new().isCalled('x').withUniformDistribution(-2, 1)
            up.new().isCalled('y').withUniformDistribution(0, 1)
        elif dtype == "beta":
            up.new().isCalled('x').withBetaDistribution(10, 5, -2, 3)
            up.new().isCalled('y').withBetaDistribution(10, 5, 0, 1)
        elif dtype == "normal":
            up.new().isCalled('x').withNormalDistribution(0.2, 0.1, 0.01)
            up.new().isCalled('y').withNormalDistribution(0.2, 0.1, 0.01)
        elif dtype == "sgde":
            sgdeDist = self.estimate_density()
            up.new().isCalled('x,y').withDistribution(sgdeDist)
        else:
            raise AttributeError("dtype '%s' is unknown" % dtype)

        return builder.andGetResult()
Example #9
0
    def defineParameters(self, setting=0):
        # set distributions of the input parameters
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()

        if setting == 1:
            up.new().isCalled('y1').withUniformDistribution(-1, 1).hasValue(1.0)
            up.new().isCalled('y2').withUniformDistribution(-1, 1)
            up.new().isCalled('y3').withUniformDistribution(-1, 1).hasValue(0.0)
            self.c_y2 = 0.1
        elif setting == 2:
            up.new().isCalled('y1').withUniformDistribution(-1, 1).hasValue(1.0)
            up.new().isCalled('y2').withUniformDistribution(-1, 1)
            up.new().isCalled('y3').withUniformDistribution(-1, 1)
            self.c_y2 = 0.1
        elif setting == 3:
            up.new().isCalled('y1').withUniformDistribution(-1, 1)
            up.new().isCalled('y2').withUniformDistribution(-1, 1)
            up.new().isCalled('y3').withUniformDistribution(-1, 1)
            self.c_y2 = 1.0
        else:
            raise AttributeError("setting '%i' is unknown" % setting)

        return builder.andGetResult()
Example #10
0
    def testBilinearForms(self):
        # define parameter set
        builder = ParameterBuilder().defineUncertainParameters()
        # builder.new().isCalled('x').withTNormalDistribution(0.5, 0.1, 0, 1)
        # builder.new().isCalled('y').withTNormalDistribution(0.5, 0.1, 0, 1)
        builder.new().isCalled('x').withUniformDistribution(0, 1)
        builder.new().isCalled('y').withUniformDistribution(0, 1)
        params = builder.andGetResult()

        U = params.getIndependentJointDistribution()
        T = params.getJointTransformation()

        def f(x):
            """
            Function to be interpolated
            """
            # return 2.
            # return float(np.sin(4 * x[0]) * np.cos(4 * x[1]))
            return np.prod([4 * xi * (1 - xi) for xi in x])

        # define sparse grid function
        grid = Grid.createLinearGrid(params.getStochasticDim())
        grid.getGenerator().regular(2)
        gs = grid.getStorage()

        nodalValues = DataVector(gs.getSize())
        p = DataVector(gs.getDimension())

        for i in range(gs.getSize()):
            gs.getCoordinates(gs.getPoint(i), p)
            nodalValues[i] = f(p.array())

        v = hierarchize(grid, nodalValues)
        res = DataVector(gs.getSize())

        # -------------------------------------------------------------------------------
        # compute admissible set
        admissibleSet = RefinableNodesSet()
        admissibleSet.create(grid)

        # -------------------------------------------------------------------------------
        # define the strategies
        s0 = UniformQuadratureStrategy()
        s1 = PiecewiseConstantQuadratureStrategy(params=params)
        s2 = BilinearGaussQuadratureStrategy(U=U.getDistributions(),
                                             T=T.getTransformations())
        s3 = SparseGridQuadratureStrategy(U=U.getDistributions())

        A = s0.computeBilinearForm(grid)
        C, _ = s2.computeBilinearForm(grid)
        D, _ = s3.computeBilinearForm(grid)

        # -------------------------------------------------------------------------------
        # compute bilinear form for lists of grid points
        gpsi, basisi = project(grid, [0, 1])
        gpsj, basisj = project(grid, [0, 1])

        C_list, _ = s2.computeBilinearFormByList(gs, gpsi, basisi, gpsj,
                                                 basisj)
        D_list, _ = s3.computeBilinearFormByList(gs, gpsi, basisi, gpsj,
                                                 basisj)

        assert np.all(np.abs(C_list - A) < 1e-13)
        assert np.all(np.abs(C_list - C) < 1e-13)
        assert np.all(np.abs(D_list - D) < 1e-13)
Example #11
0
# coding: utf-8
from pysgpp.extensions.datadriven.uq.parameters import ParameterBuilder
from pysgpp.extensions.datadriven.uq.sampler import MCSampler

from pysgpp import *

import numpy as np
import matplotlib.pyplot as plt
from pysgpp.extensions.datadriven.uq.dists.Lognormal import Lognormal


parameterBuilder = ParameterBuilder()
up = parameterBuilder.defineUncertainParameters()

up.new().isCalled('phi').withLognormalDistribution(0.15, 0.2, alpha=.01)
up.new().isCalled('e').withBetaDistribution(3, 3, 0, 2)
up.new().isCalled('K_L').withLognormalDistribution(1e-12, np.exp(-1), alpha=.01)

# design parameter
up.new().isCalled('Q_CO2').withLognormalDistribution(8.87, 0.2, alpha=.01)  # .hasValue(8.87)
up.new().isCalled('W_z').withBetaDistribution(2, 2, 0, 30)  # .hasValue(30)

params = parameterBuilder.andGetResult()

n = 9

a = MCSampler(params).nextSamples(n).ndarray()
b = MCSampler(params).nextSamples(n + 1).ndarray()


print(a)
Example #12
0
from pysgpp.extensions.datadriven.uq.plot.plot3d import plotFunction3d, plotSG3d, \
    plotDensity3d
from pysgpp.extensions.datadriven.learner.Types import BorderTypes

import numpy as np
import matplotlib.pyplot as plt
from pysgpp.extensions.datadriven.uq.plot.plot2d import plotDensity2d
import matplotlib
from matplotlib import rc

font = {'family': 'sans-serif', 'sans-serif': ['Helvetica'], 'size': 22}
matplotlib.rc('font', **font)
rc('text', usetex=True)

# define random variables
builder = ParameterBuilder()
up = builder.defineUncertainParameters()

up.new().isCalled('x_0').withTNormalDistribution(0.0, 0.5, -2, 1)
up.new().isCalled('x_1').withTNormalDistribution(
    0.5, 0.2, 0, 1).withInverseCDFTransformation()
up.new().isCalled('x_2,x_3,x_4,...').withSGDEDistribution(
    dist).withInverseCDFTransformation()

params = builder.andGetResult()

# define UQ setting
builder = ASGCUQManagerBuilder()
builder.withParameters(params)\
       .withTypesOfKnowledge([KnowledgeTypes.SIMPLE,
                              KnowledgeTypes.SQUARED])\
Example #13
0
    def testSettings(self):
        builder = ParameterBuilder()
        dp = builder.defineDeterministicParameters()
        up = builder.defineUncertainParameters()

        # ============================================
        # 1)
        up.new().isCalled('v')\
                .withDistribution(Uniform(0, 1))\
                .withRosenblattTransformation()
        # --------------------------------------------
        # 2)
        up.new().isCalled('density')\
                .withDistribution(Uniform(-1, 1))\
                .hasValue(0.0)
        # --------------------------------------------
        # 3)
        up.new().isCalled('K')\
                .withDistribution(TNormal(0, 1, -3, 2))\
                .hasValue(-3)
        # --------------------------------------------
        # 4)
        up.new().isCalled('theta')\
                .withDistribution(TNormal(0, 1, -2, 2))\
                .withLinearTransformation()
        # --------------------------------------------
        # 5)
        up.new().isCalled('blub')\
                .withUniformDistribution(-1, 1)
        # --------------------------------------------
        # 6)
        dp.new().isCalled('radius').hasValue(2)
        # ============================================

        params = builder.andGetResult()

        # test dimensions
        assert params.getDim() == 6
        assert params.getStochasticDim() == 3
        assert len(params.activeParams()) == 3
        assert params.getStochasticDim() == len(params.activeParams())
        assert params.getDim() - len(params.uncertainParams()) == \
            len(params.deterministicParams())
        assert params.getStochasticDim() == len(params.getDistributions()) - 2

        jsonStr = params.getJointTransformation().toJson()
        jsonObject = json.loads(jsonStr)
        trans = Transformation.fromJson(jsonObject)

        # test transformations
        ap = params.activeParams()
        assert params.getStochasticDim() == len(ap)
        sampler = MCSampler.withNaiveSampleGenerator(params)

        for sample in sampler.nextSamples(100):
            for x in sample.getActiveUnit():
                assert 0 <= x <= 1
            bounds = params.getBounds()
            q = sample.getExpandedProbabilistic()
            for xlim1, xlim2, x in np.vstack((bounds.T, q)).T:
                assert xlim1 <= x <= xlim2

        params.removeParam(0)
        assert params.getStochasticDim() == len(ap) - 1
        sampler = MCSampler.withNaiveSampleGenerator(params)

        for sample in sampler.nextSamples(100):
            for x in sample.getActiveUnit():
                assert 0 <= x <= 1
            bounds = params.getBounds()
            q = sample.getExpandedProbabilistic()
            for xlim1, xlim2, x in np.vstack((bounds.T, q)).T:
                assert xlim1 <= x <= xlim2
Example #14
0
    def __init__(self):
        self.radix = 'ishigami'

        # --------------------------------------------------------
        # set distributions of the input parameters
        # --------------------------------------------------------
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()
        up.new().isCalled('x').withUniformDistribution(-np.pi, np.pi)
        up.new().isCalled('y').withUniformDistribution(-np.pi, np.pi)
        up.new().isCalled('z').withUniformDistribution(-np.pi, np.pi)
        self.params = builder.andGetResult()
        self.numDims = self.params.getStochasticDim()

        # --------------------------------------------------------
        # simulation function
        # --------------------------------------------------------
        def f(xs, a=7, b=0.1, **kws):
            x1, x2, x3 = xs
            return np.sin(x1) + a * np.sin(x2)**2 + b * x3**4 * np.sin(x1)

        self.simulation = f

        # --------------------------------------------------------
        # analytic reference values
        # --------------------------------------------------------

        def var(a=7, b=0.1):
            return a * a / 8. + b * np.pi**4 / 5. + b * b * np.pi**8 / 18. + 0.5

        def vi(i, a=7, b=0.1):
            if i == 0:
                return b * np.pi**4 / 5. + b * b * np.pi**8 / 50. + 0.5
            elif i == 1:
                return a * a / 8.
            else:
                return 0.0

        def vij(i, j, a=7, b=0.1):
            if i == 0 and j == 2:
                return 8 * b * b * np.pi**8 / 225.
            else:
                return 0.0

        def vijk(i, j, k):
            return 0.0

        def sobol_index(perm):
            if len(perm) == 1:
                return vi(perm[0]) / var()
            elif len(perm) == 2:
                return vij(perm[0], perm[1]) / var()
            elif len(perm) == 3:
                return vijk(perm[0], perm[1], perm[2]) / var()
            else:
                raise AttributeError("len of perm must be in {1, 2, 3}")

        self.var = var()
        self.sobol_indices = {}
        for k in range(self.numDims):
            for perm in combinations(list(range(self.numDims)), r=k + 1):
                self.sobol_indices[tuple(perm)] = sobol_index(perm)

        self.total_effects = computeTotalEffects(self.sobol_indices)
Example #15
0
    def __init__(self, numDims=2, rosenblatt=False):
        self.radix = 'test_genz'
        self.plot = False

        self.pathResults = os.path.join("results")

        self.rosenblatt = rosenblatt
        self.numDims = numDims
        self.correlation = 0.9
        corrMatrix = np.ones((self.numDims, self.numDims)) * self.correlation
        np.fill_diagonal(corrMatrix, 1.0)

        self.dist = NatafDist.beta_marginals(0,
                                             1,
                                             5.0,
                                             10.0,
                                             corrMatrix=corrMatrix,
                                             bounds=np.array([[0, 1], [0, 1]]))
        # --------------------------------------------------------
        # set distributions of the input parameters
        # --------------------------------------------------------
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()
        if rosenblatt:
            up.new().isCalled("x,y").withDistribution(
                self.dist).withRosenblattTransformation()
        else:
            up.new().isCalled("x,y").withDistribution(self.dist)
        self.params = builder.andGetResult()

        # --------------------------------------------------------
        # simulation function: oscillatory genz function
        # --------------------------------------------------------
        self.c = 4.5 * (np.arange(0, self.numDims, 1) + 0.5) / self.numDims

        def f(x, c=self.c, **kws):
            return np.cos(np.sum(c * x))

        self.simulation = f

        # --------------------------------------------------------
        def insert_labels(ax, zlabel):
            ax.set_xticks([0, 0.5, 1])
            ax.set_yticks([0, 0.5, 1])
            ax.set_xlabel(r"$\xi_1$")
            ax.set_ylabel(r"$\xi_2$")
            ax.set_zlabel(zlabel)
            ax.xaxis.labelpad = 13
            ax.yaxis.labelpad = 13
            ax.zaxis.labelpad = 10

        if False:
            fig = plt.figure()
            plotFunction2d(self.dist.pdf,
                           color_bar_label=r'$f(\xi_1, \xi_2)$',
                           addContour=False)
            plt.xlabel(r"$\xi_1$")
            plt.ylabel(r"$\xi_2$")
            savefig(fig, "plots/correlated_beta_2d", tikz=False)

            fig = plt.figure()
            fig, ax, _ = plotFunction3d(self.dist.pdf)
            insert_labels(ax, r"$f(\xi_1, \xi_2)$")
            savefig(fig, "plots/correlated_beta_3d", tikz=False)
        # --------------------------------------------------------
        if False:
            fig = plt.figure()
            plotFunction2d(self.simulation,
                           color_bar_label=r'$u(\xi_1, \xi_2)$',
                           addContour=False)
            plt.xlabel(r"$\xi_1$")
            plt.ylabel(r"$\xi_2$")
            savefig(fig, "plots/oscillating_genz_2d", tikz=False)

            fig, ax, _ = plotFunction3d(self.simulation)
            insert_labels(ax, r"$u(\xi_1, \xi_2)$")
            savefig(fig, "plots/oscillating_genz_3d", mpl3d=True)