예제 #1
0
    def plotResultsPCE(self, pce, train_samples, expansion, sampling_strategy,
                       num_samples, degree_1d, out):
        fig, ax, _ = plotFunction3d(lambda x: pce.evaluate(x), xlim=[-2, 1], ylim=[0, 1])
        ax.scatter(train_samples[0, :],
                   train_samples[1, :],
                   np.zeros(num_samples))
        if out:
            filename = os.path.join(self.pathResults,
                                    "%s_eval_pce_d%i_%s_%s_N%i_deg%i.pdf" % (self.radix,
                                                                             self.numDims,
                                                                             expansion,
                                                                             sampling_strategy,
                                                                             num_samples,
                                                                             degree_1d))
            plt.savefig(filename)

        fig, ax, _ = plotError3d(lambda x: self.simulation(x),
                                 lambda x: pce.evaluate(x),
                                 xlim=[-2, 1], ylim=[0, 1])
        if out:
            filename = os.path.join(self.pathResults,
                                    "%s_eval_pce_d%i_%s_%s_N%i_deg%i.pdf" % (self.radix,
                                                                             self.numDims,
                                                                             expansion,
                                                                             sampling_strategy,
                                                                             num_samples,
                                                                             degree_1d))
            plt.savefig(filename)

        if not out:
            plt.show()
예제 #2
0
    def discretize2d_linear(self):
        # discretize the product of both
        grid1, alpha1 = self.interpolate(2, 3, 2)
        grid2, alpha2 = self.interpolate(2, 3, 3)
        jgrid, jalpha = discretizeProduct(grid1, alpha1, grid2, alpha2)

        # get reference values
        def f(x):
            return evalSGFunction(grid1, alpha1, x) * evalSGFunction(
                grid2, alpha2, x)

        n = 50
        fig, ax, y1 = plotFunction3d(f, n=n)
        ax.set_title("product")
        fig.show()
        fig, ax, y2 = plotSG3d(jgrid, jalpha, n=n)
        ax.set_title(
            "(size=%i, maxlevel=%i, deg=%i), err = %g" %
            (jgrid.getStorage().getSize(), jgrid.getStorage().getMaxLevel(),
             getDegree(jgrid), np.max(abs(y1 - y2))))
        fig.show()

        assert np.max(np.abs(y1 - y2)) < 1e-13
        plt.show()
예제 #3
0
fun = sin

# plot analytic function
if plot:
    if numDims == 1:
        fig = plt.figure()
        plotFunction1d(fun)
        plt.title("analytic")
        fig.show()
    elif numDims == 2:
        fig = plt.figure()
        plotFunction2d(fun)
        plt.title("analytic")
        fig.show()

        fig, ax, _ = plotFunction3d(fun)
        ax.set_title("analytic")
        ax.set_zlim(-2, 2)
        fig.show()
        plt.savefig("sin_analytic.pdf")

# get a sparse grid approximation
grid = Grid.createLinearGrid(numDims)
grid.getGenerator().regular(level)
gs = grid.getStorage()

alpha = hierarchizeFun(fun)
print("l=%i: (gs=%i)" % (level, grid.getSize()))
print("-" * 80)

# plot the result
예제 #4
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)