Ejemplo n.º 1
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()
Ejemplo n.º 2
0
# ----------------------------------------------
# build analysis
analysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                .withAnalyticEstimationStrategy()\
                                .andGetResult()

analysis.computeMoments()['data']

# ----------------------------------------------
fig, _, _ = plotSG3d(analysis.getGrid(), analysis.getSurpluses())
fig.show()
# ----------------------------------------------
# show sobol indices

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

# main effects
me = anova.getSobolIndices()
te = anova.getTotalEffects()

names = anova.getSortedPermutations(list(me.keys()))
values = [me[name] for name in names]
fig = plotSobolIndices(values, legend=True, names=names)
fig.show()

# ---------------------------------------------------------------------------
distKDE = analysis.estimateDensity(dtype="gaussianKDE")
config = {
    "grid_level": 6,
    "grid_type": "Linear",
Ejemplo n.º 3
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()
Ejemplo n.º 4
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()
Ejemplo n.º 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()