예제 #1
0
def plotConvergenceResults(results):
    settings = {
        'beta': {
            'sg': [
                ("polyBoundary", 10000, False, 1, "bound."),
                ("polyClenshawCurtisBoundary", 10000, False, 1, "CC-bound."),
                #                                 ("modpoly", 10000, False, 1, "modified"),
                #                                 ("modPolyClenshawCurtis", 10000, False, 1, "modified-CC"),
                ("polyBoundary", 10000, True, 1, "bound."),
                ("polyClenshawCurtisBoundary", 10000, True, 1, "CC-bound."),
                ("poly", 10000, False, 1, "no bound.")
                #                                 ("polyBoundary", 2000, False, 1, "poly-bound., exp"),
                #                                 ("polyBoundary", 2001, False, 1, "poly-bound., l2"),
                #                                 ("polyBoundary", 2002, False, 1, "poly-bound., simple")
            ]
        }
    }

    error_type = "l2test"
    # extract the ones needed for the table
    sg_settings = settings[args.model]["sg"]
    fig = plt.figure()
    for k, (gridType, maxGridSize, rosenblatt, boundaryLevel,
            gridTypeLabel) in enumerate(sg_settings):
        key = get_key_sg(gridType, maxGridSize, rosenblatt, boundaryLevel)
        n = len(results["sg"][key]["results"])
        num_evals = np.ndarray(n)
        errors = np.ndarray(n)
        for i, (level,
                values) in enumerate(results["sg"][key]["results"].items()):
            num_evals[i] = values["num_model_evaluations"]
            errors[i] = values[error_type]
        print(num_evals)
        ixs = np.argsort(num_evals)
        if "bound" in gridTypeLabel and "no" not in gridTypeLabel:
            if rosenblatt:
                label = "%s ($\\ell^{\\text{b}}=%i$, Rosen.)" % (gridTypeLabel,
                                                                 boundaryLevel)
            else:
                label = r"%s ($\ell^{\text{b}}=%i$)" % (gridTypeLabel,
                                                        boundaryLevel)
        else:
            if rosenblatt:
                label = r"%s (Rosenblatt)" % (gridTypeLabel, )
            else:
                label = r"%s" % (gridTypeLabel, )
        plt.loglog(num_evals[ixs],
                   errors[ixs],
                   "o-",
                   color=load_color(k),
                   marker=load_marker(k),
                   label=label)

    plt.ylabel(r"$||u - u_{\mathcal{I}}||_{L_2(\Xi)}$")
    plt.xlabel(r"\# number of grid points")
    plt.title(r"Regular SG (poly, $D=2$)",
              fontproperties=load_font_properties())
    lgd = insert_legend(fig, loc="bottom", ncol=1)
    savefig(fig, "plots/sg_convergence_results")
예제 #2
0
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")
예제 #3
0
            if i == 0 or numGridPoints[-1] < n_grid_points:
                print(
                    (gridType, basisType, levelManagerType, n, n_grid_points))

                l2error = np.sqrt(np.mean((y - y_surrogate)**2))
                l2errors = np.append(l2errors, l2error)
                numGridPoints = np.append(
                    numGridPoints,
                    refinement_wrapper.operation.numGridPoints())

        results[basisType, levelManagerType,
                gridType] = numGridPoints, l2errors

    print("E(u) ~ %g" % np.mean(y))
    print("V(u) ~ %g" % np.var(y))

    fig = plt.figure()
    ax = plt.subplot(111)

    for (basisType, levelManagerType,
         gridType), (numGridPoints, l2errors) in list(results.items()):
        plt.loglog(numGridPoints,
                   l2errors,
                   label="%s %s %s" % (gridType, levelManagerType, basisType))

    plt.xlabel("number of grid points")
    plt.ylabel("L2 error")
    plt.tight_layout()
    insert_legend(fig, loc="right", ncol=2)
    plt.show()
예제 #4
0
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()
예제 #5
0
            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):
                values[i] = res[t][error_type]
                err[i, :] = values[i]

            if refinement is None:
                label = r"SG ($N=%i$)" % (maxGridSize,)
            else:
                label = r"aSG ($N=%i$)" % (maxGridSize,)

            plotMCResults(time_steps,
                          values,
                          err,
                          color=load_color(k + 1),
                          marker=load_marker(k + 1),
                          label=label)

    plt.ylabel("$\mathbb{V}$ of $y_%s$" % args.qoi[-1])
    lgd = insert_legend(fig, loc="bottom", ncol=2)
    savefig(fig, "plots/kraichnan_orszag_%s_s%i_%s_%s" % (args.model,
                                                          args.setting,
                                                          args.qoi,
                                                          error_type))
    plt.close(fig)
예제 #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()