示例#1
0
def loadDensity(setting, functionName):
    stats = None
    filename = os.path.join("data", setting,
                            "%s.%s.best.stats.pkl" % (setting, functionName))
    if os.path.exists(filename) and \
            (("moons" in functionName and (("sgde" in setting) or
                                           "kde" in setting)) or
             ("beta" in functionName and (("sgde" in setting) or \
                                          "nataf" in setting or \
                                          "kde" in setting))):
        print("-" * 80)
        print("load setting: %s" % setting)
        fd = open(filename, "r")
        stats = pkl.load(fd)
        fd.close()

        for iteration, values in list(stats.items()):
            jsonObject = None
            if setting in ["sgde_zero", "sgde_boundaries"]:
                jsonObject = json.loads(values['posSGDE_json'])
            elif setting in ["kde_gaussian", "kde_epanechnikov"]:
                jsonObject = json.loads(values['KDEDist_json'])
            elif setting in ["nataf"]:
                jsonObject = json.loads(values["NatafDist_json"])
            if setting != "nataf" and jsonObject is not None:
                stats[iteration]["dist"] = Dist.fromJson(jsonObject)
        print("done")
    return stats
示例#2
0
    def fromJson(cls, jsonObject):
        from pysgpp.extensions.datadriven.uq.dists.Dist import Dist

        key = '_dist'
        if key in jsonObject:
            dist = Dist.fromJson(jsonObject[key])

        return RosenblattTransformation(dist)
    def fromJson(cls, jsonObject):
        from pysgpp.extensions.datadriven.uq.dists.Dist import Dist

        key = '_InverseCDFTransformation__dist'
        if key in jsonObject:
            dist = Dist.fromJson(jsonObject[key])

        return InverseCDFTransformation(dist)
示例#4
0
    def fromJson(cls, jsonObject):
        """
        Restores the Corr object from the json object with its
        attributes.
        @param jsonObject: json object
        @return: the restored Corr object
        """
        key = '_Corr__dists'
        if key in jsonObject:
            vals = jsonObject[key]
            dists = [Dist.fromJson(vals[key]) for key in sorted(vals.keys())]

        return Corr(dists)
示例#5
0
    def fromJson(cls, jsonObject):
        """
        Restores the J object from the json object with its
        attributes.
        @param jsonObject: json object
        @return: the restored J object
        """
        key = '_J__dists'
        if key in jsonObject:
            vals = jsonObject[key]
            dists = [Dist.fromJson(vals[key]) for key in sorted(vals.keys())]
        else:
            raise AttributeError(
                "J: fromJson - the mandatory keyword '%s' does not exist" %
                key)

        return J(dists)
示例#6
0
    def test2DCovarianceMatrix(self):
        # prepare data
        np.random.seed(1234567)
        C = np.array([[0.3, 0.09], [0.09, 0.3]]) / 10.

        U = dists.MultivariateNormal([0.5, 0.5], C, 0, 1)
        samples = U.rvs(2000)
        kde = KDEDist(samples)

        sgde = SGDEdist.byLearnerSGDEConfig(
            samples,
            bounds=U.getBounds(),
            config={
                "grid_level": 5,
                "grid_type": "linear",
                "grid_maxDegree": 1,
                "refinement_numSteps": 0,
                "refinement_numPoints": 10,
                "solver_threshold": 1e-10,
                "solver_verbose": False,
                "regularization_type": "Laplace",
                "crossValidation_lambda": 3.16228e-06,
                "crossValidation_enable": False,
                "crossValidation_kfold": 5,
                "crossValidation_silent": False,
                "sgde_makePositive": True,
                "sgde_makePositive_candidateSearchAlgorithm": "joined",
                "sgde_makePositive_interpolationAlgorithm": "setToZero",
                "sgde_generateConsistentGrid": True,
                "sgde_unitIntegrand": True
            })

        sgde_x1 = sgde.marginalizeToDimX(0)
        sgde_x2 = sgde.marginalizeToDimX(1)

        plt.figure()
        plotDensity1d(sgde_x1, label="x1")
        plotDensity1d(sgde_x2, label="x2")
        plt.title(
            "mean: x1=%g, x2=%g; var: x1=%g, x2=%g" %
            (sgde_x1.mean(), sgde_x2.mean(), sgde_x1.var(), sgde_x2.var()))
        plt.legend()

        jsonStr = sgde.toJson()
        jsonObject = json.loads(jsonStr)
        sgde = Dist.fromJson(jsonObject)

        fig = plt.figure()
        plotDensity2d(U, addContour=True)
        plt.title("analytic")

        fig = plt.figure()
        plotDensity2d(kde, addContour=True)
        plt.title("kde")

        fig = plt.figure()
        plotDensity2d(sgde, addContour=True)
        plt.title("sgde (I(f) = %g)" % (doQuadrature(sgde.grid, sgde.alpha), ))

        # print the results
        print("E(x) ~ %g ~ %g" % (kde.mean(), sgde.mean()))
        print("V(x) ~ %g ~ %g" % (kde.var(), sgde.var()))
        print("-" * 60)

        print(kde.cov())
        print(sgde.cov())

        self.assertTrue(np.linalg.norm(C - kde.cov()) < 1e-2, "KDE cov wrong")
        self.assertTrue(
            np.linalg.norm(np.corrcoef(samples.T) - kde.corrcoeff()) < 1e-1,
            "KDE corrcoef wrong")
        plt.show()