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
def beta_marginals(cls, lwr, upr, alpha, beta, covMatrix=None, corrMatrix=None, bounds=None): range = upr - lwr mean = lwr + alpha / (alpha + beta) * range stddev = np.sqrt(alpha * beta / (alpha + beta + 1.0)) / (alpha + beta) * range if corrMatrix is None: corrMatrix = Dist().corrcoeff(covMatrix=covMatrix) num_dims = corrMatrix.shape[0] nataf = NatafDensity() nataf.initialize_random_variable_types([STD_BETA] * num_dims) nataf.initialize_random_variable_parameters([mean] * num_dims, [stddev] * num_dims, [[alpha, beta]] * num_dims) nataf.initialize_random_variable_correlations(corrMatrix) return cls(nataf, bounds=bounds, params={ "name": "beta", "lwr": lwr, "upr": upr, "alpha": alpha, "beta": beta, "corrMatrix": corrMatrix, "covMatrix": covMatrix })
def gamma_marginals(cls, alpha, beta, covMatrix=None, corrMatrix=None, bounds=None): mean = alpha * beta stddev = np.sqrt(alpha) * beta if corrMatrix is None: corrMatrix = Dist().corrcoeff(covMatrix=covMatrix) num_dims = corrMatrix.shape[0] nataf = NatafDensity() nataf.initialize_random_variable_types([GAMMA] * num_dims) nataf.initialize_random_variable_parameters([mean] * num_dims, [stddev] * num_dims, [[alpha, beta]] * num_dims) nataf.initialize_random_variable_correlations(corrMatrix) return cls(nataf, bounds=bounds, params={ "name": "gamma", "alpha": alpha, "beta": beta, "corrMatrix": corrMatrix, "covMatrix": covMatrix })
def normal_marginals(cls, mean, stddev, covMatrix=None, corrMatrix=None, bounds=None): if corrMatrix is None: corrMatrix = Dist().corrcoeff(covMatrix=covMatrix) num_dims = corrMatrix.shape[0] nataf = NatafDensity() nataf.initialize_random_variable_types([GAUSSIAN] * num_dims) nataf.initialize_random_variable_parameters( [mean] * num_dims, [stddev] * num_dims, [[mean, stddev]] * num_dims) nataf.initialize_random_variable_correlations(corrMatrix) return cls(nataf, bounds=bounds, params={ "name": "normal", "mean": mean, "stddev": stddev, "corrMatrix": corrMatrix, "covMatrix": covMatrix })
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)
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)
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)
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()