示例#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 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")
示例#4
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()
示例#5
0
文件: plot1d.py 项目: pfluegdk/SGpp
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
示例#6
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()
示例#7
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()
示例#8
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)
示例#9
0
    # 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))