def checkSobolIndices(sobol_indices_analytic, sobol_indices, N, plot=False): print("-------------- Sobol Indices (t = %i, N= %i) ------------------" % (1, N)) for perm in sortPermutations(list(sobol_indices.keys())): print("S_%s = %s ~ %s (err = %g%%)" % (perm, sobol_indices_analytic[perm], sobol_indices[perm], 100 * np.abs(sobol_indices_analytic[perm] - sobol_indices[perm]))) assert np.abs(np.sum(list(sobol_indices.values())) - 1.0) < 1e-14 assert np.abs(np.sum(list(sobol_indices_analytic.values())) - 1.0) < 1e-14 if plot: names = sortPermutations(list(sobol_indices.keys())) values = [sobol_indices[name] for name in names] plotSobolIndices(values, legend=True, names=names) plt.show()
def runAnalysis(self, analysis, uqManager, alabel, blabel, out, plot, results): if out: # ---------------------------------------------- # write stats # ---------------------------------------------- pathResults = os.path.join(self.pathResults, alabel, blabel) if not os.path.exists(pathResults): os.mkdir(pathResults) if self.numDims > 1: print("sobol indices") analysis.writeSensitivityValues(os.path.join(pathResults, alabel)) print("surpluses") analysis.writeSurplusesLevelWise(os.path.join(pathResults, alabel)) print("stats") analysis.writeStats(os.path.join(pathResults, alabel)) print("moments") analysis.writeMoments(os.path.join(pathResults, alabel)) print("sampling") path = os.path.join(pathResults, "samples") if not os.path.exists(path): os.mkdir(path) analysis.sampleGrids(os.path.join(path, alabel)) # ---------------------------------------------- # do some plotting # ---------------------------------------------- ts = uqManager.getKnowledge().getAvailableTimeSteps() sobolIndices = np.zeros((len(ts), 2 ** len(self.params.activeParams()) - 1)) for i, t in enumerate(ts): grid, alpha = uqManager.getKnowledge()\ .getSparseGridFunction(uqManager.getQoI(), t) print("-" * 80) print("plot: t=%g (i=%i), N=%i" % (t, i, grid.getSize())) # scatter plot of surpluses level wise surpluses = analysis.computeSurplusesLevelWise(t) maxLevel = grid.getStorage().getMaxLevel() if out and plot: fig = plotSurplusLevelWise(surpluses, maxLevel) fig.savefig(os.path.join(pathResults, "surpluses_t%g") % t) plt.close(fig) (fig, ax), A = plotSGNodal3d(grid, alpha) ax.set_xlabel("x") ax.set_ylabel("y") fig.savefig(os.path.join(pathResults, "nodal_t%g.png" % t)) plt.close(fig) # plot sparse grid approximation if self.numDims < 3: if self.numDims == 1: fig = plt.figure() plotSG1d(grid, alpha) plt.xlabel("x") elif self.numDims == 2: fig, ax, _ = plotSG3d(grid, alpha) ax.set_xlabel("x") ax.set_ylabel("y") fig.savefig(os.path.join(pathResults, "function_t%g.png" % t)) plt.close(fig) # write nodal values to file writeDataARFF({"filename": os.path.join(pathResults, "nodal_t%g.arff" % t), "names": self.params.activeParams().getNames() + ["value"], "data": DataMatrix(A)}) # show sobol indices me = None te = None if self.numDims > 1: anova = analysis.getAnovaDecomposition(t=t) me = anova.getSobolIndices() print("-------------- Sobol Indices (t = %i) ------------------" % t) for j, perm in enumerate(anova.getSortedPermutations(list(me.keys()))): print("%s: %s" % (perm, me[perm])) sobolIndices[i, j] = me[perm] print(sum(sobolIndices[i, :]), "==", 1) # ---------------------------------------------------------- # total effects te = anova.getTotalEffects() print("-------------- Total Effects (t = %i) -----------------" % t) for key, val in sorted(te.items()): print("%s: %s" % (key, val)) print("---------------------------------------------------------") print() if t not in results["results"]: results["results"][t] = {} results["knowledge_types"] = uqManager.getKnowledgeTypes() results["results"][t][maxLevel] = {} results["results"][t][maxLevel]["grid_size"] = grid.getSize() results["results"][t][maxLevel]["maxLevel"] = maxLevel results["results"][t][maxLevel]["surpluses"] = surpluses results["results"][t][maxLevel]["sobol_indices"] = me results["results"][t][maxLevel]["total_effects"] = te results["results"][t][maxLevel]["stats"] = uqManager.stats results["results"][t][maxLevel]["mean_estimated_per_iteration"] = {} for it, res in list(analysis.mean(ts=[t], reduce=False).items()): results["results"][t][maxLevel]["mean_estimated_per_iteration"][it] = res["value"] # maximum iteration -> final value it = max(results["results"][t][maxLevel]["mean_estimated_per_iteration"].keys()) results["results"][t][maxLevel]["mean_estimated"] = \ results["results"][t][maxLevel]["mean_estimated_per_iteration"][it] results["results"][t][maxLevel]["var_estimated_per_iteration"] = {} for it, res in list(analysis.var(ts=[t], reduce=False).items()): results["results"][t][maxLevel]["var_estimated_per_iteration"][it] = res["value"] # maximum iteration -> final value it = max(results["results"][t][maxLevel]["var_estimated_per_iteration"].keys()) results["results"][t][maxLevel]["var_estimated"] = \ results["results"][t][maxLevel]["var_estimated_per_iteration"][it] # -------------------------------------------- if out and plot and self.numDims > 1: names = anova.getSortedPermutations(list(me.keys())) fig = plotSobolIndices(sobolIndices, ts=ts, legend=True, names=names) fig.savefig(os.path.join(pathResults, "sobol.png")) plt.close(fig)
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()