Exemple #1
0
distKDE = analysis.estimateDensity(dtype="gaussianKDE")
config = {
    "grid_level": 6,
    "grid_type": "Linear",
    "refinement_numSteps": 0,
    "refinement_numPoints": 10,
    "regularization_type": "Laplace",
    "crossValidation_lambda": 3.16228e-06,
    "crossValidation_enable": True,
    "crossValidation_kfold": 5,
    "crossValidation_silent": False
}
distSGDE = analysis.estimateDensity(dtype="sgde", config=config)

print("mean(u) = %g ~ %g (KDE) ~ %g (SGDE)" %
      (analysis.mean()[0], distKDE.mean(), distSGDE.mean()))
print("var(u) = %g ~ %g (KDE) ~ %g (SGDE)" %
      (analysis.var()[0], distKDE.var(), distSGDE.var()))
# ---------------------------------------------------------------------------
y = analysis.eval(analysis.generateUnitSamples())

fig = plt.figure()
plotDensity1d(distSGDE, label="SGDE", linewidth=3)
plt.vlines(distSGDE.mean(),
           0,
           distSGDE.pdf(distSGDE.mean()),
           linewidth=3,
           color="red")
plt.vlines(distSGDE.mean() - distSGDE.std(),
           0,
           distSGDE.pdf(distSGDE.mean() - distSGDE.std()),
Exemple #2
0
    def testAnova(self):
        # ----------------------------------------------------------
        # define the learner
        # ----------------------------------------------------------
        # define UQ setting
        builder = ASGCUQManagerBuilder()
        builder.withParameters(self.params)\
               .withTypesOfKnowledge([KnowledgeTypes.SIMPLE,
                                      KnowledgeTypes.SQUARED,
                                      KnowledgeTypes.EXPECTATIONVALUE])\
               .useInterpolation()

        builder.defineUQSetting().withSimulation(self.simulation)

        samplerSpec = builder.defineSampler()
        samplerSpec.withGrid().hasType(GridType_PolyBoundary)\
                              .withLevel(4)\
                              .withDegree(5)\
                              .withBoundaryLevel(1)

        # ----------------------------------------------------------
        # discretize the stochastic space with the ASGC method
        # ----------------------------------------------------------
        uqManager = builder.andGetResult()

        # ----------------------------------------------
        # first run
        while uqManager.hasMoreSamples():
            uqManager.runNextSamples()

        # ----------------------------------------------------------
        # specify ASGC estimator
        # ----------------------------------------------------------
        analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                        .withAnalyticEstimationStrategy()\
                                        .andGetResult()

        # ----------------------------------------------------------
        # expectation values and variances
        _, _ = analysis.mean(), analysis.var()

        # ----------------------------------------------------------
        # estimated anova decomposition
        anova = analysis.getAnovaDecomposition(nk=len(self.params))

        # ----------------------------------------------------------
        # check interpolation and decomposition
        m = np.random.rand(100, self.params.getDim())
        for i in range(m.shape[0]):
            self.assertTrue(abs(analysis.eval(m[i, :]) - anova.eval(m[i, :])) < 1e-14)

        # ----------------------------------------------------------
        # main effects
        me = anova.getSobolIndices()

        print("-------------- Sobol Indices (t = %i) ------------------" % 1)
        for (key, val) in sorted(me.items()):
            print("%s: %s" % (key, val))
        print(sum([val for val in list(me.values())]), "==", 1)

        # ----------------------------------------------------------
        # total effects
        te = anova.getTotalEffects()
        print("-------------- Total Effects (t = %i) -----------------" % 1)
        for key, val in sorted(te.items()):
            print("%s: %s" % (key, val))
        print("---------------------------------------------")
        print()

        names = anova.getSortedPermutations(list(me.keys()))
        values = [me[name] for name in names]
        fig, _ = plotSobolIndices(values, legend=True, names=names)
        fig.show()
        plt.show()
Exemple #3
0
    def run_adaptive_sparse_grid(self, gridType, level, maxGridSize, refinement,
                                 boundaryLevel=None, isFull=False, out=False,
                                 plot=False):

        test_samples, test_values = self.getTestSamples()

        # ----------------------------------------------------------
        # define the learner
        # ----------------------------------------------------------
        uqManager = TestEnvironmentSG().buildSetting(self.params,
                                                     self.simulation,
                                                     level,
                                                     gridType,
                                                     deg=20,
                                                     maxGridSize=maxGridSize,
                                                     isFull=isFull,
                                                     adaptive=refinement,
                                                     adaptPoints=10,
                                                     adaptRate=0.05,
                                                     epsilon=1e-10,
                                                     boundaryLevel=boundaryLevel)
        # ----------------------------------------------
        # first run
        while uqManager.hasMoreSamples():
            uqManager.runNextSamples()

        # ----------------------------------------------------------
        # specify ASGC estimator
        # ----------------------------------------------------------
        analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                        .withAnalyticEstimationStrategy()\
                                        .andGetResult()
        analysis.setVerbose(False)
        # ----------------------------------------------------------
        # expectation values and variances
        sg_mean, sg_var = analysis.mean(), analysis.var()
        stats = {}
        iterations = uqManager.getKnowledge().getAvailableIterations()
        for k, iteration in enumerate(iterations):
            # ----------------------------------------------------------
            # estimated anova decomposition
            anova = analysis.getAnovaDecomposition(iteration=iteration,
                                                   nk=len(self.params))
            # estimate the l2 error
            grid, alpha = uqManager.getKnowledge().getSparseGridFunction(iteration=iteration)
            test_values_pred = evalSGFunction(grid, alpha, test_samples)
            l2test, l1test, maxErrorTest, meanError, varError = \
                self.getErrors(test_values, test_values_pred,
                               sg_mean[iteration][0], sg_var[iteration][0])
            # ----------------------------------------------------------
            # main effects
            sobol_indices = anova.getSobolIndices()
            total_effects = computeTotalEffects(sobol_indices)

            print("-" * 60)
            print("iteration=%i, N=%i" % (iteration, grid.getSize()))
            print("E[x] = %g ~ %g (err=%g)" % (self.E_ana[0], sg_mean[iteration]["value"],
                                               np.abs(self.E_ana[0] - sg_mean[iteration]["value"])))
            print("V[x] = %g ~ %g (err=%g)" % (self.V_ana[0], sg_var[iteration]["value"],
                                               np.abs(self.V_ana[0] - sg_var[iteration]["value"])))

            stats[grid.getSize()] = {'num_model_evaluations': grid.getSize(),
                                     'l2test': l2test,
                                     'l1test': l1test,
                                     'maxErrorTest': maxErrorTest,
                                     'mean_error': meanError,
                                     'var_error': varError,
                                     'mean_estimated': sg_mean[iteration]["value"],
                                     'var_estimated': sg_var[iteration]["value"],
                                     'sobol_indices_estimated': sobol_indices,
                                     'total_effects_estimated': total_effects}

            if plot:
                self.plotResultsSG(grid, alpha, level,
                                   maxGridSize, refinement,
                                   iteration, out)

        if out:
            # store results
            filename = os.path.join(self.pathResults,
                                    "%s_%s_d%i_%s_l%i_Nmax%i_r%s_N%i.pkl" % (self.radix,
                                                                             "sg" if not isFull else "fg",
                                                                             self.numDims,
                                                                             grid.getTypeAsString(),
                                                                             level,
                                                                             maxGridSize,
                                                                             refinement,
                                                                             grid.getSize()))
            fd = open(filename, "w")
            pkl.dump({'surrogate': 'sg',
                      'model': "full" if self.numDims == 4 else "reduced",
                      'num_dims': self.numDims,
                      'grid_type': grid.getTypeAsString(),
                      'level': level,
                      'max_grid_size': maxGridSize,
                      'is_full': isFull,
                      'refinement': refinement,
                      'mean_analytic': self.E_ana[0],
                      'var_analytic': self.V_ana[0],
                      'results': stats},
                     fd)
            fd.close()
Exemple #4
0
    def run_regular_sparse_grid(self, gridType, level, maxGridSize,
                                boundaryLevel=1,
                                isFull=False,
                                out=False,
                                plot=False):
        np.random.seed(1234567)

        test_samples, test_values = self.getTestSamples()

        stats = {}
        while True:
            print("-" * 80)
            print("level = %i" % level)
            uqManager = TestEnvironmentSG().buildSetting(self.params,
                                                         self.simulation,
                                                         level,
                                                         gridType,
                                                         deg=20,
                                                         maxGridSize=maxGridSize,
                                                         isFull=isFull,
                                                         boundaryLevel=boundaryLevel)

            if uqManager.sampler.getSize() > maxGridSize:
                print("DONE: %i > %i" % (uqManager.sampler.getSize(), maxGridSize))
                break

            # ----------------------------------------------
            # first run
            while uqManager.hasMoreSamples():
                uqManager.runNextSamples()

            # ----------------------------------------------------------
            # specify ASGC estimator
            analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                            .withAnalyticEstimationStrategy()\
                                            .andGetResult()

            analysis.setVerbose(False)
            # ----------------------------------------------------------
            # expectation values and variances
            sg_mean, sg_var = analysis.mean(), analysis.var()

            # ----------------------------------------------------------
            # estimate the l2 error
            grid, alpha = uqManager.getKnowledge().getSparseGridFunction()
            test_values_pred = evalSGFunction(grid, alpha, test_samples)
            l2test, l1test, maxErrorTest, meanError, varError = \
                self.getErrors(test_values, test_values_pred,
                               sg_mean["value"], sg_var["value"])
            print("-" * 60)
            print("test:  |.|_2 = %g" % l2test)
            print("E[x] = %g ~ %g (err=%g)" % (self.E_ana[0], sg_mean["value"],
                                               np.abs(self.E_ana[0] - sg_mean["value"])))
            print("V[x] = %g ~ %g (err=%g)" % (self.V_ana[0], sg_var["value"],
                                               np.abs(self.V_ana[0] - sg_var["value"])))
            # ----------------------------------------------------------
            # estimated anova decomposition
            if self.inputSpace != "sgde":
                anova = analysis.getAnovaDecomposition(nk=len(self.params))
                sobol_indices = anova.getSobolIndices()
                total_effects = computeTotalEffects(sobol_indices)
            else:
                sobol_indices = {}
                total_effects = {}
            # ----------------------------------------------------------
            stats[level] = {'num_model_evaluations': grid.getSize(),
                            'l2test': l2test,
                            'l1test': l1test,
                            'maxErrorTest': maxErrorTest,
                            'mean_error': meanError,
                            'var_error': varError,
                            'mean_estimated': sg_mean["value"],
                            'var_estimated': sg_var["value"],
                            'sobol_indices_estimated': sobol_indices,
                            'total_effects_estimated': total_effects}

            if plot:
                self.plotResultsSG(grid, alpha, level, maxGridSize, False, 0, out)
            level += 1

        if out:
            # store results
            filename = os.path.join(self.pathResults,
                                    "%s_%s_d%i_%s_Nmax%i_r%i_N%i.pkl" % (self.radix,
                                                                         "sg" if not isFull else "fg",
                                                                         self.numDims,
                                                                         grid.getTypeAsString(),
                                                                         maxGridSize,
                                                                         False,
                                                                         grid.getSize()))
            fd = open(filename, "w")
            pkl.dump({'surrogate': 'sg',
                      'num_dims': self.numDims,
                      'grid_type': grid.getTypeAsString(),
                      'max_grid_size': maxGridSize,
                      'level': level,
                      'boundaryLevel': boundaryLevel,
                      'is_full': isFull,
                      'refinement': False,
                      'mean_analytic': self.E_ana[0],
                      'var_analytic': self.V_ana[0],
                      'results': stats},
                     fd)
            fd.close()
Exemple #5
0
    def run_sparse_grids(self,
                         gridType,
                         level,
                         maxGridSize,
                         isFull,
                         refinement=None,
                         out=False):
        # ----------------------------------------------------------
        # define the learner
        # ----------------------------------------------------------
        uqManager = TestEnvironmentSG().buildSetting(self.params,
                                                     self.simulation,
                                                     level,
                                                     gridType,
                                                     deg=10,
                                                     maxGridSize=maxGridSize,
                                                     isFull=isFull,
                                                     adaptive=refinement,
                                                     adaptPoints=3,
                                                     epsilon=1e-3)

        # ----------------------------------------------
        # first run
        while uqManager.hasMoreSamples():
            uqManager.runNextSamples()

        # ----------------------------------------------------------
        # specify ASGC estimator
        # ----------------------------------------------------------
        analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                        .withAnalyticEstimationStrategy()\
                                        .andGetResult()

        # ----------------------------------------------------------
        # expectation values and variances
        sg_mean, sg_var = analysis.mean(), analysis.var()

        print("-" * 60)
        print("V[x] = %g ~ %s" % (self.var, sg_var))

        iterations = uqManager.getKnowledge().getAvailableIterations()
        stats = [None] * len(iterations)
        for k, iteration in enumerate(iterations):
            # ----------------------------------------------------------
            # estimated anova decomposition
            anova = analysis.getAnovaDecomposition(iteration=iteration,
                                                   nk=len(self.params))

            # estimate the l2 error
            test_samples = np.random.random((1000, self.effectiveDims))
            test_values = np.ndarray(1000)
            for i, sample in enumerate(test_samples):
                test_values[i] = self.simulation(sample)
            grid, alpha = uqManager.getKnowledge().getSparseGridFunction()
            test_values_pred = evalSGFunction(grid, alpha, test_samples)
            l2test = np.sqrt(np.mean(test_values - test_values_pred)**2)
            # ----------------------------------------------------------
            # main effects
            sobol_indices = anova.getSobolIndices()
            total_effects = computeTotalEffects(sobol_indices)

            stats[k] = {
                'num_model_evaluations': grid.getSize(),
                'l2test': l2test,
                'var_estimated': sg_var[0],
                'var_analytic': self.var,
                'sobol_indices_estimated': sobol_indices,
                'total_effects_estimated': total_effects
            }

        if out:
            # store results
            filename = os.path.join(
                "results", "%s_%s_d%i_%s_l%i_Nmax%i_%s_N%i.pkl" %
                (self.radix, "sg" if not isFull else "fg", self.effectiveDims,
                 grid.getTypeAsString(), level, maxGridSize, refinement,
                 grid.getSize()))
            fd = open(filename, "w")
            pkl.dump(
                {
                    'surrogate': 'sg',
                    'model': "full" if self.effectiveDims == 4 else "reduced",
                    'num_dims': self.effectiveDims,
                    'grid_type': grid.getTypeAsString(),
                    'level': level,
                    'max_grid_size': maxGridSize,
                    'is_full': isFull,
                    'refinement': refinement,
                    'sobol_indices_analytic': self.sobol_indices,
                    'total_effects_analytic': self.total_effects,
                    'results': stats
                }, fd)
            fd.close()

        return sobol_indices, grid.getSize()
Exemple #6
0
    def run_regular_sparse_grid(self,
                                gridTypeStr,
                                level,
                                maxGridSize,
                                boundaryLevel=1,
                                out=False):
        np.random.seed(1234567)
        test_samples, test_values = self.getTestSamples()
        gridType = Grid.stringToGridType(gridTypeStr)

        stats = {}
        while True:
            print("-" * 80)
            print("level = %i, boundary level = %i" % (level, boundaryLevel))
            print("-" * 80)
            uqManager = TestEnvironmentSG().buildSetting(
                self.params,
                self.simulation,
                level,
                gridType,
                deg=20,
                maxGridSize=maxGridSize,
                boundaryLevel=min(level, boundaryLevel),
                knowledgeTypes=[KnowledgeTypes.SIMPLE])

            if uqManager.sampler.getSize() > maxGridSize:
                print("DONE: %i > %i" %
                      (uqManager.sampler.getSize(), maxGridSize))
                break

            # ----------------------------------------------
            # first run
            while uqManager.hasMoreSamples():
                uqManager.runNextSamples()

            # ----------------------------------------------------------
            if False:
                grid, alpha = uqManager.knowledge.getSparseGridFunction()
                samples = DataMatrix(grid.getSize(), self.numDims)
                grid.getStorage().getCoordinateArrays(samples)
                samples = self.dist.ppf(samples.array())
                fig = plt.figure()
                plotFunction2d(self.simulation,
                               color_bar_label=r"$u(\xi_1, \xi_2)$")
                plt.scatter(
                    samples[:, 0],
                    samples[:, 1],
                    color=load_color(3),
                    label=r"SG (CC-bound., $\ell=%i, \ell^{\text{b}}=%i$)" %
                    (level, boundaryLevel))
                plt.xlabel(r"$\xi_1$")
                plt.xlabel(r"$\xi_2$")
                lgd = insert_legend(fig, loc="bottom", ncol=1)
                savefig(fig,
                        "plots/genz_with_grid_l%i_b%i" %
                        (level, boundaryLevel),
                        lgd,
                        tikz=False)
            # ----------------------------------------------------------
            # specify ASGC estimator
            analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                            .withMonteCarloEstimationStrategy(n=1000,
                                                                              npaths=10)\
                                            .andGetResult()

            analysis.setVerbose(False)
            # ----------------------------------------------------------
            # expectation values and variances
            sg_mean, sg_var = analysis.mean(), analysis.var()

            # ----------------------------------------------------------
            # estimate the l2 error
            grid, alpha = uqManager.getKnowledge().getSparseGridFunction()
            test_values_pred = evalSGFunction(grid, alpha, test_samples)
            l2test, l1test, maxErrorTest = \
                self.getErrors(test_values, test_values_pred)
            print("-" * 60)
            print("test:  |.|_2 = %g" % l2test)
            # ----------------------------------------------------------
            stats[level] = {
                'num_model_evaluations': grid.getSize(),
                'l2test': l2test,
                'l1test': l1test,
                'maxErrorTest': maxErrorTest,
                'mean_estimated': sg_mean["value"],
                'var_estimated': sg_var["value"]
            }

            level += 1

        if out:
            # store results
            radix = "%s_sg_d%i_%s_Nmax%i_N%i_b%i" % (
                self.radix, self.numDims, grid.getTypeAsString(), maxGridSize,
                grid.getSize(), boundaryLevel)
            if self.rosenblatt:
                radix += "_rosenblatt"

            filename = os.path.join(self.pathResults, "%s.pkl" % radix)
            fd = open(filename, "w")
            pkl.dump(
                {
                    'surrogate': 'sg',
                    'num_dims': self.numDims,
                    'grid_type': grid.getTypeAsString(),
                    'max_grid_size': maxGridSize,
                    'is_full': False,
                    'refinement': False,
                    'rosenblatt': self.rosenblatt,
                    'boundaryLevel': boundaryLevel,
                    'results': stats
                }, fd)
            fd.close()
Exemple #7
0
    def run_regular_sparse_grid_boundary(self,
                                         gridTypeStr,
                                         level,
                                         maxGridSize,
                                         boundaryLevel=1,
                                         out=False):
        np.random.seed(1234567)
        test_samples, test_values = self.getTestSamples()
        gridType = Grid.stringToGridType(gridTypeStr)

        stats = {}
        while boundaryLevel <= level:
            print("-" * 80)
            print("level = %i, boundary level = %i" % (level, boundaryLevel))
            print("-" * 80)
            uqManager = TestEnvironmentSG().buildSetting(
                self.params,
                self.simulation,
                level,
                gridType,
                deg=20,
                maxGridSize=maxGridSize,
                boundaryLevel=boundaryLevel,
                knowledgeTypes=[KnowledgeTypes.SIMPLE])

            # ----------------------------------------------
            # first run
            while uqManager.hasMoreSamples():
                uqManager.runNextSamples()

            # ----------------------------------------------------------
            # specify ASGC estimator
            analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                            .withMonteCarloEstimationStrategy(n=1000,
                                                                              npaths=10)\
                                            .andGetResult()

            analysis.setVerbose(False)
            # ----------------------------------------------------------
            # expectation values and variances
            sg_mean, sg_var = analysis.mean(), analysis.var()

            # ----------------------------------------------------------
            # estimate the l2 error
            grid, alpha = uqManager.getKnowledge().getSparseGridFunction()
            test_values_pred = evalSGFunction(grid, alpha, test_samples)
            l2test, l1test, maxErrorTest = \
                self.getErrors(test_values, test_values_pred)
            print("-" * 60)
            print("test:  |.|_2 = %g" % l2test)
            # ----------------------------------------------------------
            # ----------------------------------------------------------
            stats[boundaryLevel] = {
                'num_model_evaluations': grid.getSize(),
                'l2test': l2test,
                'l1test': l1test,
                'maxErrorTest': maxErrorTest,
                'mean_estimated': sg_mean["value"],
                'var_estimated': sg_var["value"]
            }

            boundaryLevel += 1

        if out:
            # store results
            filename = os.path.join(
                self.pathResults, "%s_sg_d%i_%s_Nmax%i_N%i_b%i.pkl" %
                (self.radix, self.numDims, grid.getTypeAsString(), maxGridSize,
                 grid.getSize(), boundaryLevel))
            fd = open(filename, "w")
            pkl.dump(
                {
                    'surrogate': 'sg',
                    'num_dims': self.numDims,
                    'grid_type': grid.getTypeAsString(),
                    'max_grid_size': maxGridSize,
                    'is_full': False,
                    'refinement': False,
                    'results': stats
                }, fd)
            fd.close()