def plotBoundaryResult(results): settings = { 'beta': { 'sg': [("polyBoundary", 10000, 10, "bound."), ("polyClenshawCurtisBoundary", 10000, 10, "CC-bound.")] } } error_type = "l2test" # extract the ones needed for the table sg_settings = settings[args.model]["sg"] fig = plt.figure() for k, (gridType, maxGridSize, boundaryLevel, gridTypeLabel) in enumerate(sg_settings): key = get_key_sg(gridType, maxGridSize, False, boundaryLevel) n = len(results["sg"][key]["results"]) num_evals = np.ndarray(n) errors = np.ndarray(n) for i, (boundaryLevel, values) in enumerate(results["sg"][key]["results"].items()): num_evals[i] = boundaryLevel # num_evals[i] = values["num_model_evaluations"] errors[i] = values[error_type] ixs = np.argsort(num_evals) plt.plot(num_evals[ixs], errors[ixs], color=load_color(k), marker=load_marker(k), label=r"%s ($\ell=9$)" % gridTypeLabel) plt.xlim(9.5, 0.5) ticks = [1, 2, 3, 4, 5, 6, 7, 8, 9] plt.xticks(ticks, ticks) # plt.xscale("log") plt.yscale("log") plt.ylabel(r"$||u - u_{\mathcal{I}}||_{L_2(\Xi)}$") plt.xlabel(r"$\ell^{\text{b}}$") plt.title(r"Regular SG (poly, $D=2$)", fontproperties=load_font_properties()) lgd = insert_legend(fig, loc="bottom", ncol=1) savefig(fig, "plots/sg_boundary_results")
def plotDataset(functionName, numSamples=10000, numDims=2, out=False): dataset, bounds, _ = load_data_set(functionName, numSamples, numDims=2) fig = plt.figure() plt.plot(dataset[:, 0], dataset[:, 1], "o ", color=load_color(0)) plt.xlabel(r"$\xi_1$") plt.ylabel(r"$\xi_2$") plt.xlim(bounds[0]) plt.ylim(bounds[1]) xticks = np.arange(0, 1.2, 0.2) plt.xticks(xticks, [str(xi) for xi in xticks]) plt.yticks(xticks, [str(xi) for xi in xticks]) plt.title("Two-moons dataset", fontproperties=load_font_properties()) if out: filename = os.path.join("plots", "%s_dataset" % functionName) print(filename) fig.set_size_inches(5.7, 5, forward=True) savefig(fig, filename, tikz=True) plt.close(fig) else: plt.show()
def plotDensity1d(U, n=1000, alpha_value=None, mean_label=None, facecolor=load_color(1), interval_label=None, *args, **kws): bounds = U.getBounds() if len(bounds) == 1: bounds = bounds[0] x = np.linspace(bounds[0], bounds[1], n) y = np.array([U.pdf(xi) for xi in x]) plt.plot(x, y, *args, **kws) if mean_label is not None: plt.vlines(U.mean(), 0.0, U.pdf(U.mean()), color=facecolor, label=mean_label) if alpha_value is not None: # define label for interval plot if interval_label is None: interval_label = r"$[F(\alpha / 2), F(1 - \alpha/2)]$" # show interval that contains 1 - alpha x_min, x_max = U.ppf(alpha_value / 2.), U.ppf(1. - alpha_value / 2.) ixs = np.intersect1d(np.where(x_min <= x), np.where(x <= x_max)) # plt.vlines(x_min, 0.0, y[ixs.min()], color=facecolor, label=interval_label) # plt.vlines(x_max, 0.0, y[ixs.max()], color=facecolor) plt.fill_between(x[ixs], y[ixs], np.zeros(y[ixs].shape[0]), facecolor=facecolor, alpha=0.2, label=interval_label)
def plotSobolIndices(sobolIndices, ts=None, legend=False, adjust_yaxis=True, names=None, mc_reference=None): fig = plt.figure() plots = [] if legend and names is None: raise Exception("plotSobolIndices - attribute names is not set") lgd = None if ts is None: y0 = 0 for i in range(len(sobolIndices)): myplot = plt.bar([0], [sobolIndices[i]], 1, bottom=[y0], color=load_color(i)) y0 += sobolIndices[i] plots = [myplot] + plots if legend: plt.xticks([0.5], ('sobol indices',)) if adjust_yaxis: plt.ylim(0, 1) plt.xlim(-0.2, 2) lgd = plt.legend(plots, [r"$S_{%s}$ = %.3f" % (name, value) for (name, value) in zip(names[::-1], sobolIndices[::-1])], prop=load_font_properties()) else: y0 = np.zeros(sobolIndices.shape[0]) offset = 1 if mc_reference is not None else 0 for i in range(sobolIndices.shape[1]): y1 = y0 + sobolIndices[:, i] color = load_color(i + offset) myplot, = plt.plot(ts, y1, color=color, lw=2) plt.fill_between(ts, y0, y1, color=color, alpha=.5) y0 = y1 plots = [myplot] + plots labels = [r"$S_{%s}$" % (",".join(name),) for name in names[::-1]] if mc_reference is not None: myplot, = plt.plot(mc_reference["ts"], mc_reference["values"], marker=mc_reference["marker"], color=mc_reference["color"]) plt.fill_between(mc_reference["ts"], mc_reference["values"], mc_reference["err"][:, 0], facecolor=mc_reference["color"], alpha=0.2) plt.fill_between(mc_reference["ts"], mc_reference["values"], mc_reference["err"][:, 1], facecolor=mc_reference["color"], alpha=0.2) labels = [mc_reference["label"]] + labels plots = [myplot] + plots if legend: plt.xlim(min(ts), max(ts)) if adjust_yaxis: plt.ylim(0, 1) fig.tight_layout() ax = plt.gca() box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.85, box.height]) lgd = plt.legend(plots, labels, loc='upper left', bbox_to_anchor=(1.02, 1), borderaxespad=0, prop=load_font_properties()) return fig, lgd
def plotLogLikelihood(densities, functionName, out=False): numDensities = len(densities) numIterations = 0 for i, (setting, stats) in enumerate(densities.items()): numIterations = max(numIterations, len(stats)) data = { "train": np.zeros((numIterations, numDensities)), "test": np.zeros((numIterations, numDensities)), "validation": np.zeros((numIterations, numDensities)) } names = [None] * numDensities i = 0 for i, setting in enumerate( ["kde_gaussian", "kde_epanechnikov", "sgde_zero", "sgde_boundaries"]): stats = densities[setting] if "sgde" in setting: if "zero" in setting: names[i] = "SGDE \n set-to-zero" else: names[i] = "SGDE \n interp. bound." trainkey = "ZeroSGDE" elif "nataf" in setting: names[i] = "Nataf" elif "gaussian" in setting: names[i] = "KDE \n Gaussian" trainkey = "KDE" elif "epanechnikov" in setting: names[i] = "KDE \n Epan." trainkey = "KDE" for j, values in enumerate(stats.values()): data["train"][j, i] = values["crossEntropyTrain%s" % trainkey] data["test"][j, i] = values["crossEntropyTest%s" % trainkey] data["validation"][j, i] = values["crossEntropyValidation"] pos = np.arange(0, numDensities) fig = plt.figure(figsize=(13, 6.5)) ax = fig.add_subplot(111) # plt.violinplot(data, pos, points=60, widths=0.7, showmeans=True, # showextrema=True, showmedians=True, bw_method=0.5) width = 0.28 for i, category in enumerate(["train", "test", "validation"]): values = data[category] yval = np.ndarray(values.shape[1]) for j in range(values.shape[1]): yval[j] = np.mean(values[:, j]) rects = ax.bar(pos + i * width, yval, width, color=load_color(i), label=category) for rect in rects: h = -rect.get_height() ax.text(rect.get_x() + (rect.get_width() / 2.), h - 0.2, '%.2f' % h, ha='center', va='bottom') # plt.xticks(pos, names) ax.set_xticks(pos + width) ax.set_xticklabels(names) ax.set_ylabel("cross entropy") yticks = np.arange(-1.5, 0.5, 0.5) ax.set_yticks(yticks) ax.set_yticklabels(yticks) ax.set_ylim(-1.7, 0) lgd = insert_legend(fig, loc="right", ncol=1) if out: savefig(fig, os.path.join("plots", "log_likelihood_%s" % functionName), tikz=True, lgd=lgd) plt.close(fig) else: plt.show()
def example7(dtype="uniform", maxLevel=2): ## This time, we use Clenshaw-Curtis points with exponentially growing number of points per level. ## This is helpful for CC points to make them nested. Nested means that the set of grid points at ## one level is a subset of the set of grid points at the next level. Nesting can drastically ## reduce the number of needed function evaluations. Using these grid points, we will do ## polynomial interpolation at a single point. if dtype == "cc": operation = pysgpp.CombigridOperation.createExpClenshawCurtisPolynomialInterpolation( 2, func) elif dtype == "l2leja": operation = pysgpp.CombigridOperation.createExpL2LejaPolynomialInterpolation( 2, func) else: operation = pysgpp.CombigridOperation.createExpUniformLinearInterpolation( 2, func) ## The level manager provides more options for combigrid evaluation, so let's get it: levelManager = operation.getLevelManager() ## We can add regular levels like before: levelManager.addRegularLevels(maxLevel) ## We can also fetch the used grid points and plot the grid: grid = levelManager.getGridPointMatrix() gridList = np.array([[grid.get(r, c) for c in range(grid.getNcols())] for r in range(grid.getNrows())]) initialize_plotting_style() ## def g(x, y): ## evaluationPoint = pysgpp.DataVector([x, y]) ## result = operation.evaluate(maxLevel, evaluationPoint) ## return result ## fig, ax, _ = plotSG3d(g=g, contour_xy=False) ## ax.scatter(gridList[0], gridList[1], np.zeros(len(gridList[0])), ## color=load_color(0), ## marker='o', s=20) ## ax.set_axis_off() ## ax.set_xlabel(r"$x$") ## ax.set_ylabel(r"$y$") ## ax.set_xticks([0, 0.5, 1]) ## ax.set_yticks([0, 0.5, 1]) ## ax.set_zticks([0, 0.5, 1]) ## ax.xaxis.labelpad = 13 ## ax.yaxis.labelpad = 13 ## ax.set_title(r"$f(x,y) = 16 x(1-x)y(1-y)$", ## fontproperties=load_font_properties()) ## savefig(fig, "/home/franzefn/Desktop/Mario/normal_parabola", mpl3d=True) fig = plt.figure() plt.plot(gridList[0, :], gridList[1, :], " ", color=load_color(0), marker='o', markersize=10) plt.axis('off') currentAxis = plt.gca() currentAxis.add_patch( Rectangle((0, 0), 1, 1, fill=None, alpha=1, linewidth=2)) plt.xlim(0, 1) plt.ylim(0, 1) if dtype == "uniform": plt.title(r"Sparse Grid $\ell=%i$" % (maxLevel + 1, ), fontproperties=load_font_properties()) else: plt.title(r"Sparse Grid $\ell=%i$ (stretched)" % (maxLevel + 1, ), fontproperties=load_font_properties()) savefig(fig, "/home/franzefn/Desktop/tmp/sparse_grid_%s" % dtype, mpl3d=True) maxLevel = 1 for tr in ["fg", "ct"]: ## We can also fetch the used grid points and plot the grid: fig, axarr = plt.subplots(maxLevel + 1, maxLevel + 1, sharex=True, sharey=True, squeeze=True) levels = [] for level in product(list(range(maxLevel + 1)), repeat=2): levels.append(level) ax = axarr[level[0], level[1]] ax.axis('off') for level in levels: print((tr, level)) if tr == "ct" and np.sum(level) > maxLevel: print("skip %s" % (level, )) continue ax = axarr[level[0], level[1]] if level[0] == 0: xs = np.array([gridList[0, 1]]) else: xs = gridList[0, :] if level[1] == 0: ys = np.array([gridList[1, 1]]) else: ys = gridList[1, :] xv, yv = np.meshgrid(xs, ys, sparse=False, indexing='xy') for i in range(len(xs)): for j in range(len(ys)): ax.plot(yv[j, i], xv[j, i], color=load_color(0), marker="o", markersize=10) ax.set_title(r"$(%i, %i)$" % (level[0] + 1, level[1] + 1), fontproperties=load_font_properties()) ax.add_patch( Rectangle((0, 0), 1, 1, fill=None, alpha=1, linewidth=1)) ## plt.xlim(0, 1) ## plt.ylim(0, 1) fig.set_size_inches(6, 6, forward=True) savefig(fig, "/home/franzefn/Desktop/tmp/tableau_%s_%s_l%i" % ( dtype, tr, maxLevel, ), mpl3d=True)
# To create a CombigridOperation object with our own configuration, we have to provide a # LevelManager as well: levelManager = pysgpp.WeightedRatioLevelManager() operation = pysgpp.CombigridOperation(grids, evaluators, levelManager, func) # We can add regular levels like before: levelManager.addRegularLevels(args.level) # We can also fetch the used grid points and plot the grid: grid = levelManager.getGridPointMatrix() gridList = [[grid.get(r, c) for c in range(grid.getNcols())] for r in range(grid.getNrows())] fig = plt.figure() plt.plot(gridList[0], gridList[1], " ", color=load_color(0), marker='o', markersize=10) plt.axis('off') currentAxis = plt.gca() currentAxis.add_patch( Rectangle((0, 0), 1, 1, fill=None, alpha=1, linewidth=2)) plt.xlim(0, 1) plt.ylim(0, 1) plt.title(r"Sparse Grid $\ell=%i$" % args.level, fontproperties=load_font_properties()) savefig(fig, "/tmp/sparse_grid_l%i_%s" % (args.level, args.marginalType))
res = parse_monte_carlo_results(results) time_steps = np.array(list(res.keys())) ixs = np.argsort(time_steps) time_steps = time_steps[ixs] ixs = np.where(time_steps <= 6)[0] time_steps = time_steps[ixs] values = np.ndarray(time_steps.shape) err = np.ndarray((time_steps.size, 2)) for i, t in enumerate(time_steps[ixs]): values[i] = res[t][error_type] err[i, :] = res[t]["%s_error" % error_type] plotMCResults(time_steps[ixs], values, err, color=load_color(0), marker=load_marker(0), label=r"MC (M=$10^5$)") # if args.surrogate in ["pce", "both"]: # for expansion, sampling_strategy, N in pce_settings: # key = get_key_pce(expansion, sampling_strategy, N) # n = len(results["pce"][key]["results"]) # num_evals = np.ndarray(n) # errors = np.ndarray(n) # for i, (num_samples, values) in enumerate(results["pce"][key]["results"].items()): # num_evals[i] = num_samples # errors[i] = values[error_type] # ixs = np.argsort(num_evals) # plt.loglog(num_evals[ixs], errors[ixs], "o-", # label=("pce (%s, %s)" % (expansion, sampling_strategy)).replace("_", " "))
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()