Пример #1
0
def plotDensities(densities, functionName, out=False):
    for setting, stats in list(densities.items()):
        if setting != "nataf":
            U = stats[0]["dist"]
            if "kde" in setting:
                label = r'$f_{\mathcal{S}_M}^{\kappa}(\xi_1, \xi_2)$'
                if "gaussian" in setting:
                    title = "KDE (Gaussian)"
                else:
                    title = "KDE (Epanechnikov)"
            else:
                label = r'$f_{\mathcal{I}}(\xi_1, \xi_2)$'
                if "zero" in setting:
                    title = "SGDE (set-to-zero)"
                else:
                    title = "SGDE (interp. bound.)"
            fig = plt.figure()
            plotDensity2d(U, color_bar_label=label)
            plt.xlabel(r"$\xi_1$")
            plt.ylabel(r"$\xi_2$")
            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(title, fontproperties=load_font_properties())
            if out:
                filename = os.path.join("plots",
                                        "%s_%s" % (functionName, setting))
                print(filename)
                fig.set_size_inches(5.7, 5, forward=True)
                savefig(fig, filename, tikz=True)
                plt.close(fig)

    if not out:
        plt.show()
Пример #2
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")
Пример #3
0
def plotCovarianceConvergence(densities, functionName, out=False):
    _, _, natafType = load_data_set(functionName, numSamples=0, numDims=2)

    covMatrix = natafType["cov"]

    numDensities = len(densities)
    numIterations = 0
    for i, (setting, stats) in enumerate(densities.items()):
        numIterations = max(numIterations, len(stats))

    data = 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."
        elif "nataf" in setting:
            names[i] = "Nataf"
        elif "gaussian" in setting:
            names[i] = "KDE \n Gaussian"
        elif "epanechnikov" in setting:
            names[i] = "KDE \n Epan."
        for j, values in enumerate(stats.values()):
            data[j, i] = np.linalg.norm(values["dist"].cov() - covMatrix)

    pos = np.arange(0, numDensities)

    fig = plt.figure(figsize=(13, 4.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)
    plt.xticks(pos, names)
    plt.ylabel(r"$||\hat{C} - C||$")

    if out:
        savefig(fig,
                os.path.join("plots",
                             "convergence_covariance_%s" % functionName),
                tikz=True)
        plt.close(fig)
    else:
        plt.show()
Пример #4
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")
Пример #5
0
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()
Пример #6
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()
Пример #7
0
def plotpvalueofKolmogorovSmirnovTest(densities, functionName, out=False):
    numDensities = len(densities)
    numIterations = 0
    for i, (setting, stats) in enumerate(densities.items()):
        numIterations = max(numIterations, len(stats))

    data = np.zeros((numIterations, 2 * numDensities))
    names = [None] * data.shape[1]
    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[2 * i] = "SGDE \n set-to-zero \n shuffled"
                names[2 * i + 1] = "SGDE \n set-to-zero \n not shuffled"
            else:
                names[2 * i] = "SGDE \n interp. bound. \n shuffled"
                names[2 * i + 1] = "SGDE \n interp. bound. \n not shuffled"
        elif "nataf" in setting:
            names[2 * i] = "Nataf \n shuffled"
            names[2 * i + 1] = "Nataf \n not shuffled"
        elif "gaussian" in setting:
            names[2 * i] = "KDE \n Gaussian \n shuffled"
            names[2 * i + 1] = "KDE \n Gaussian \n not shuffled"
        elif "epanechnikov" in setting:
            names[2 * i] = "KDE \n Epan. \n shuffled"
            names[2 * i + 1] = "KDE \n Epan. \n not shuffled"
        for j, values in enumerate(stats.values()):
            numDims = values["config"]["numDims"]
            pvalues_shuffled = np.zeros(numDims)
            pvalues_not_shuffled = np.zeros(numDims)
            for idim in range(numDims):
                pvalues_shuffled[idim] = values["samples"]["shuffled"][
                    "kstests"][idim][1]
                pvalues_not_shuffled[idim] = values["samples"]["not_shuffled"][
                    "kstests"][idim][1]
            data[j, 2 * i] = pvalues_shuffled.mean()
            data[j, 2 * i + 1] = pvalues_not_shuffled.mean()

    pos = np.arange(0, len(names))
    xlim = (np.min(pos) - 0.5, np.max(pos) + 0.5)
    fig = plt.figure(figsize=(17, 5))
    plt.violinplot(data,
                   pos,
                   points=60,
                   widths=0.7,
                   showmeans=True,
                   showextrema=True,
                   showmedians=True,
                   bw_method=0.5)
    plt.xticks(pos, names)
    plt.ylabel("$p$-value")
    plt.hlines(0.05, xlim[0], xlim[1], linestyle="--")
    plt.xlim(xlim)

    if "moons" in functionName:
        plt.title("Kolmogorov-Smirnov test",
                  fontproperties=load_font_properties())
    else:
        plt.title("Kolmogorov-Smirnov test",
                  fontproperties=load_font_properties())

    if out:
        savefig(fig,
                os.path.join("plots", "kolmogorov_smirnov_%s" % functionName),
                tikz=True)
        plt.close(fig)
    else:
        plt.show()
Пример #8
0
def plotpvalueofChi2IndependenceTest(densities,
                                     functionName,
                                     c=0.0,
                                     out=False):
    numDensities = len(densities)
    numIterations = 0
    for i, (setting, stats) in enumerate(densities.items()):
        numIterations = max(numIterations, len(stats))

    data = np.zeros((numIterations, 2 * numDensities))
    names = [None] * data.shape[1]
    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[2 * i] = "SGDE \n set-to-zero \n shuffled"
                names[2 * i + 1] = "SGDE \n set-to-zero \n not shuffled"
            else:
                names[2 * i] = "SGDE \n interp. bound. \n shuffled"
                names[2 * i + 1] = "SGDE \n interp. bound. \n not shuffled"
        elif "nataf" in setting:
            names[2 * i] = "Nataf \n shuffled"
            names[2 * i + 1] = "Nataf \n not shuffled"
        elif "gaussian" in setting:
            names[2 * i] = "KDE \n Gaussian \n shuffled"
            names[2 * i + 1] = "KDE \n Gaussian \n not shuffled"
        elif "epanechnikov" in setting:
            names[2 * i] = "KDE \n Epan. \n shuffled"
            names[2 * i + 1] = "KDE \n Epan. \n not shuffled"
        for j, values in enumerate(stats.values()):
            numDims = values["config"]["numDims"]

            # apply the chi 2 test
            bins = np.linspace(0, 1, 10)
            samples = values["samples"]["shuffled"]["uniform_validation"]
            inner_samples = np.array([])
            for sample in samples:
                if c < sample[0] < 1 - c and c < sample[1] < 1 - c:
                    inner_samples = np.append(inner_samples, sample)
            inner_samples = inner_samples.reshape((inner_samples.size // 2), 2)
            h0 = np.histogram2d(inner_samples[:, 0],
                                inner_samples[:, 1],
                                bins=bins)[0][2:-2, 2:-2]
            pvalue_shuffled = chi2_contingency(h0)[1]

            if False and j == 0:
                plt.figure()
                plt.scatter(inner_samples[:, 0], inner_samples[:, 1])

                plt.figure()
                plt.hist2d(inner_samples[:, 0], inner_samples[:, 1], bins=20)
                plt.colorbar()
                plt.title("%s shuffled, %g" %
                          (setting.replace("_", " "), pvalue_shuffled))

            samples = values["samples"]["not_shuffled"]["uniform_validation"]
            inner_samples = np.array([])
            for sample in samples:
                if c < sample[0] < 1 - c and c < sample[1] < 1 - c:
                    inner_samples = np.append(inner_samples, sample)
            inner_samples = inner_samples.reshape((inner_samples.size // 2), 2)
            h0 = np.histogram2d(inner_samples[:, 0],
                                inner_samples[:, 1],
                                bins=bins)[0][2:-2, 2:-2]
            pvalue_not_shuffled = chi2_contingency(h0)[1]

            if False and j == 0:
                plt.figure()
                plt.scatter(inner_samples[:, 0], inner_samples[:, 1])

                plt.figure()
                plt.hist2d(inner_samples[:, 0], inner_samples[:, 1], bins=20)
                plt.colorbar()
                plt.title("%s not shuffled, %g" %
                          (setting.replace("_", " "), pvalue_not_shuffled))

                plt.show()

            data[j, 2 * i] = pvalue_shuffled
            data[j, 2 * i + 1] = pvalue_not_shuffled

    pos = np.arange(0, len(names))
    xlim = (np.min(pos) - 0.5, np.max(pos) + 0.5)
    fig = plt.figure(figsize=(17, 5))
    plt.violinplot(data,
                   pos,
                   points=60,
                   widths=0.7,
                   showmeans=True,
                   showextrema=True,
                   showmedians=True,
                   bw_method=0.5)
    plt.xticks(pos, names)
    plt.ylabel("$p$-value")
    plt.hlines(0.05, xlim[0], xlim[1], linestyle="--")
    plt.xlim(xlim)

    if "moons" in functionName:
        plt.title("$\chi^2$ test", fontproperties=load_font_properties())
    else:
        plt.title("$\chi^2$ test", fontproperties=load_font_properties())

    if out:
        savefig(fig,
                os.path.join(
                    "plots",
                    "chi_squared_%s_c%i" % (functionName, np.round(c * 100))),
                tikz=True)
        plt.close(fig)
    else:
        plt.show()
Пример #9
0
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)
Пример #10
0
                        type=str,
                        help="marginals")
    args = parser.parse_args()

    if args.marginalType == "uniform":
        marginal = Uniform(0, 1)
    elif args.marginalType == "beta":
        marginal = Beta(5, 10)
    else:
        marginal = Normal(0.5, 0.1, 0, 1)

    # plot pdf
    dist = J([marginal] * numDims)
    fig = plt.figure()
    plotDensity2d(dist)
    savefig(fig, "/tmp/%s" % (args.marginalType, ))
    plt.close(fig)

    w = pysgpp.singleFunc(marginal.pdf)

    grids = pysgpp.AbstractPointHierarchyVector()
    grids.push_back(pysgpp.CombiHierarchies.linearLeja(w))
    grids.push_back(pysgpp.CombiHierarchies.linearLeja(w))

    evaluators = pysgpp.FloatScalarAbstractLinearEvaluatorVector()
    evaluators.push_back(pysgpp.CombiEvaluators.polynomialInterpolation())
    evaluators.push_back(pysgpp.CombiEvaluators.polynomialInterpolation())

    # To create a CombigridOperation object with our own configuration, we have to provide a
    # LevelManager as well:
    levelManager = pysgpp.WeightedRatioLevelManager()
Пример #11
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)
Пример #12
0
    def __init__(self, numDims=2, rosenblatt=False):
        self.radix = 'test_genz'
        self.plot = False

        self.pathResults = os.path.join("results")

        self.rosenblatt = rosenblatt
        self.numDims = numDims
        self.correlation = 0.9
        corrMatrix = np.ones((self.numDims, self.numDims)) * self.correlation
        np.fill_diagonal(corrMatrix, 1.0)

        self.dist = NatafDist.beta_marginals(0,
                                             1,
                                             5.0,
                                             10.0,
                                             corrMatrix=corrMatrix,
                                             bounds=np.array([[0, 1], [0, 1]]))
        # --------------------------------------------------------
        # set distributions of the input parameters
        # --------------------------------------------------------
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()
        if rosenblatt:
            up.new().isCalled("x,y").withDistribution(
                self.dist).withRosenblattTransformation()
        else:
            up.new().isCalled("x,y").withDistribution(self.dist)
        self.params = builder.andGetResult()

        # --------------------------------------------------------
        # simulation function: oscillatory genz function
        # --------------------------------------------------------
        self.c = 4.5 * (np.arange(0, self.numDims, 1) + 0.5) / self.numDims

        def f(x, c=self.c, **kws):
            return np.cos(np.sum(c * x))

        self.simulation = f

        # --------------------------------------------------------
        def insert_labels(ax, zlabel):
            ax.set_xticks([0, 0.5, 1])
            ax.set_yticks([0, 0.5, 1])
            ax.set_xlabel(r"$\xi_1$")
            ax.set_ylabel(r"$\xi_2$")
            ax.set_zlabel(zlabel)
            ax.xaxis.labelpad = 13
            ax.yaxis.labelpad = 13
            ax.zaxis.labelpad = 10

        if False:
            fig = plt.figure()
            plotFunction2d(self.dist.pdf,
                           color_bar_label=r'$f(\xi_1, \xi_2)$',
                           addContour=False)
            plt.xlabel(r"$\xi_1$")
            plt.ylabel(r"$\xi_2$")
            savefig(fig, "plots/correlated_beta_2d", tikz=False)

            fig = plt.figure()
            fig, ax, _ = plotFunction3d(self.dist.pdf)
            insert_labels(ax, r"$f(\xi_1, \xi_2)$")
            savefig(fig, "plots/correlated_beta_3d", tikz=False)
        # --------------------------------------------------------
        if False:
            fig = plt.figure()
            plotFunction2d(self.simulation,
                           color_bar_label=r'$u(\xi_1, \xi_2)$',
                           addContour=False)
            plt.xlabel(r"$\xi_1$")
            plt.ylabel(r"$\xi_2$")
            savefig(fig, "plots/oscillating_genz_2d", tikz=False)

            fig, ax, _ = plotFunction3d(self.simulation)
            insert_labels(ax, r"$u(\xi_1, \xi_2)$")
            savefig(fig, "plots/oscillating_genz_3d", mpl3d=True)
Пример #13
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()