Exemple #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()
Exemple #2
0
    "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()),
           linewidth=3,
           color="red")
plt.vlines(distSGDE.mean() + distSGDE.std(),
           0,