def CBN_PC(data, result_structure_path): print("CBN with PC") skeleton_path = result_structure_path.joinpath("skeleton") skeleton_path.mkdir(parents=True, exist_ok=True) pdag_path = result_structure_path.joinpath("pdag") pdag_path.mkdir(parents=True, exist_ok=True) dag_path = result_structure_path.joinpath("dag") dag_path.mkdir(parents=True, exist_ok=True) skeleton_file_name = "skeleton_" + str(size).zfill(7) + ".dot" skeleton_done = skeleton_path.joinpath(skeleton_file_name).exists() pdag_file_name = "pdag_" + str(size).zfill(7) + ".dot" pdag_done = pdag_path.joinpath(pdag_file_name).exists() dag_file_name = "dag_" + str(size).zfill(7) + ".dot" dag_done = dag_path.joinpath(dag_file_name).exists() alpha = 0.01 conditioningSet = 4 learner = otagr.ContinuousPC(data, conditioningSet, alpha) learner.setVerbosity(True) if not skeleton_done: skel = learner.learnSkeleton() gu.write_graph( skel, skeleton_path.joinpath("skeleton_" + str(size).zfill(7) + ".dot")) if not pdag_done: pdag = learner.learnPDAG() gu.write_graph( pdag, pdag_path.joinpath("pdag_" + str(size).zfill(7) + ".dot")) if not dag_done: dag = learner.learnDAG() gu.write_graph(dag, dag_path.joinpath("dag_" + str(size).zfill(7) + ".dot")) else: dag, names = gu.read_graph( dag_path.joinpath("dag_" + str(size).zfill(7) + ".dot")) dag = otagr.NamedDAG(dag, names) print("Learning parameters") factories = [ ot.KernelSmoothing(ot.Epanechnikov()), ot.BernsteinCopulaFactory() ] ot.Log.SetFile("log") ot.Log.Show(ot.Log.INFO) model = otagr.ContinuousBayesianNetworkFactory(factories, dag, alpha, conditioningSet, False).build(data) ot.Log.Show(ot.Log.INFO) return model
def get_pdf(self, M, h): sample_ot = self.evaluate(np.asarray(self.jpdf.getSample(M))) sample = np.asarray(sample_ot).copy() #correct_pdf=np.where(sample > 1) ###sample[correct_pdf] = 1.0 # correct samples to 1 if samples are larger 1 (S-Parameter max 1!) #sample=np.delete(sample,correct_pdf) sample = ot.Sample(np.reshape(sample, sample.shape[0]), 1) return ot.KernelMixture(ot.Epanechnikov(), [h], sample)
def kernel_fit_distribution(sample): var = sample[:, 0] #Define the type of kernel kernel_distribution = ot.Epanechnikov() # Estimate Kernel Smoothing marginals kernel_function = ot.KernelSmoothing(kernel_distribution) kernel_distribution = kernel_function.build(var) return kernel_distribution
def ot_kernel_copula_fit(Pared): kernel_distribution = ot.Epanechnikov() sample_Pared = ot.Sample(Pared) marginals = ot_kernel_Marginals(sample_Pared) KernelSmoothing_copula_distribution = ot.KernelSmoothing( kernel_distribution).build(sample_Pared).getCopula() bivariate_distribution = ot.ComposedDistribution( marginals, KernelSmoothing_copula_distribution) return bivariate_distribution
# Dimension 2 (Fix https://github.com/openturns/openturns/issues/1485) # Indep copula : product of integrales distribution = ot.Normal(2) ott.assert_almost_equal(distribution.getRoughness(), compute_roughness_sampling(distribution)) # 2D Normal with scale & correlation # This allows checking that Normal::getRoughness is well implemented corr = ot.CorrelationMatrix(2) corr[1, 0] = 0.3 distribution = ot.Normal([0, 0], [1, 2], corr) ott.assert_almost_equal(distribution.getRoughness(), compute_roughness_sampling(distribution)) distribution = ot.Epanechnikov() ott.assert_almost_equal(distribution.getRoughness(), 3/5) distribution = ot.Triangular() ott.assert_almost_equal(distribution.getRoughness(), 2/3) distribution = ot.Distribution(Quartic()) ott.assert_almost_equal(distribution.getRoughness(), 5/7) # Testing Histogram ==> getSingularities distribution = ot.HistogramFactory().buildAsHistogram(ot.Uniform(0, 1).getSample(100000)) ott.assert_almost_equal(distribution.getRoughness(), 1.0, 5e-4, 1e-5) # Compute the roughness using width and height width = distribution.getWidth() height = distribution.getHeight() roughness = sum([width[i] * height[i]**2 for i in range(len(height))])
# Instantiate one distribution object dim = 2 meanPoint = [0.5, -0.5] sigma = [2.0, 3.0] R = ot.CorrelationMatrix(dim) for i in range(1, dim): R[i, i - 1] = 0.5 distribution = ot.Normal(meanPoint, sigma, R) discretization = 100 kernel = ot.KernelSmoothing() sample = distribution.getSample(discretization) kernels = ot.DistributionCollection(0) kernels.add(ot.Normal()) kernels.add(ot.Epanechnikov()) kernels.add(ot.Uniform()) kernels.add(ot.Triangular()) kernels.add(ot.Logistic()) kernels.add(ot.Beta(2.0, 2.0, -1.0, 1.0)) kernels.add(ot.Beta(3.0, 3.0, -1.0, 1.0)) meanExact = distribution.getMean() covarianceExact = distribution.getCovariance() for i in range(kernels.getSize()): kernel = kernels[i] print("kernel=", kernel.getName()) smoother = ot.KernelSmoothing(kernel) smoothed = smoother.build(sample) bw = smoother.getBandwidth() print("kernel bandwidth=[ %.6g" % bw[0], ", %.6g" % bw[1], "]") meanSmoothed = smoothed.getMean()
def fullKS(data): # First model: full KS print("Full KS") model = ot.KernelSmoothing(ot.Epanechnikov(), False, 0, False).build(data) return model
def get_KS_marginals(data): print("Marginal KS") dimension = data.getDimension() KS = ot.KernelSmoothing(ot.Epanechnikov(), False, 0, False) marginals = [KS.build(data.getMarginal(i)) for i in range(dimension)] return marginals
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if (ot.Epanechnikov().__class__.__name__ == 'ComposedDistribution'): correlation = ot.CorrelationMatrix(2) correlation[1, 0] = 0.25 aCopula = ot.NormalCopula(correlation) marginals = [ot.Normal(1.0, 2.0), ot.Normal(2.0, 3.0)] distribution = ot.ComposedDistribution(marginals, aCopula) elif (ot.Epanechnikov().__class__.__name__ == 'CumulativeDistributionNetwork'): distribution = ot.CumulativeDistributionNetwork( [ot.Normal(2), ot.Dirichlet([0.5, 1.0, 1.5])], ot.BipartiteGraph([[0, 1], [0, 1]])) elif (ot.Epanechnikov().__class__.__name__ == 'Histogram'): distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) else: distribution = ot.Epanechnikov() dimension = distribution.getDimension() if dimension <= 2: if distribution.getDimension() == 1: distribution.setDescription(['$x$']) pdf_graph = distribution.drawPDF() cdf_graph = distribution.drawCDF() fig = plt.figure(figsize=(10, 4)) plt.suptitle(str(distribution)) pdf_axis = fig.add_subplot(121) cdf_axis = fig.add_subplot(122) View(pdf_graph, figure=fig, axes=[pdf_axis], add_legend=False) View(cdf_graph, figure=fig, axes=[cdf_axis], add_legend=False) else: distribution.setDescription(['$x_1$', '$x_2$'])
print("Bernstein copula") marginals = get_KS_marginals(data_ref) KSBC = ot.ComposedDistribution(marginals, ot.BernsteinCopulaFactory().build(data_ref)) f = figure_path.joinpath("test_pairs_KSBC.pdf") pairs(KSBC.getSample(size_draw), f) print("Constructing CBN model") print("\tLearning structure") learner = otagr.ContinuousMIIC(data_ref) # Using CPC algorithm learner.setAlpha(-10) dag = learner.learnDAG() # Learning DAG write_graph(dag, result_path.joinpath("test_dag.dot")) print("\tLearning parameters") cbn = otagr.ContinuousBayesianNetworkFactory( ot.KernelSmoothing(ot.Epanechnikov()), ot.BernsteinCopulaFactory(), dag, 0.05, 4, False).build(data_ref) sample_draw = cbn.getSample(size_draw) # Sampling data from CBN model f = figure_path.joinpath("test_pairs_KSPC.pdf") pairs(cbn.getSample(size_draw), f) # marginal = cbn.getMarginal([1, 2]) # conditional = [[r.computeConditionalPDF(x, [y]) for x in np.linspace(-10, 10, 100)] for y in np.linspace(-10, 10, 100)] # print(conditional)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.Epanechnikov().__class__.__name__ == 'Bernoulli': distribution = ot.Bernoulli(0.7) elif ot.Epanechnikov().__class__.__name__ == 'Binomial': distribution = ot.Binomial(5, 0.2) elif ot.Epanechnikov().__class__.__name__ == 'ComposedDistribution': copula = ot.IndependentCopula(2) marginals = [ot.Uniform(1.0, 2.0), ot.Normal(2.0, 3.0)] distribution = ot.ComposedDistribution(marginals, copula) elif ot.Epanechnikov().__class__.__name__ == 'CumulativeDistributionNetwork': coll = [ot.Normal(2),ot.Dirichlet([0.5, 1.0, 1.5])] distribution = ot.CumulativeDistributionNetwork(coll, ot.BipartiteGraph([[0,1], [0,1]])) elif ot.Epanechnikov().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) elif ot.Epanechnikov().__class__.__name__ == 'KernelMixture': kernel = ot.Uniform() sample = ot.Normal().getSample(5) bandwith = [1.0] distribution = ot.KernelMixture(kernel, bandwith, sample) elif ot.Epanechnikov().__class__.__name__ == 'MaximumDistribution': coll = [ot.Uniform(2.5, 3.5), ot.LogUniform(1.0, 1.2), ot.Triangular(2.0, 3.0, 4.0)] distribution = ot.MaximumDistribution(coll) elif ot.Epanechnikov().__class__.__name__ == 'Multinomial': distribution = ot.Multinomial(5, [0.2]) elif ot.Epanechnikov().__class__.__name__ == 'RandomMixture': coll = [ot.Triangular(0.0, 1.0, 5.0), ot.Uniform(-2.0, 2.0)] weights = [0.8, 0.2] cst = 3.0 distribution = ot.RandomMixture(coll, weights, cst)
distribution = ot.Gamma(6.0, 1.0) sample = distribution.getSample(800) # %% # The definition of the Normal kernel : kernelNormal = ot.KernelSmoothing(ot.Normal()) estimatedNormal = kernelNormal.build(sample) # %% # The definition of the Triangular kernel : kernelTriangular = ot.KernelSmoothing(ot.Triangular()) estimatedTriangular = kernelTriangular.build(sample) # %% # The definition of the Epanechnikov kernel : kernelEpanechnikov = ot.KernelSmoothing(ot.Epanechnikov()) estimatedEpanechnikov = kernelEpanechnikov.build(sample) # %% # The definition of the Uniform kernel : kernelUniform = ot.KernelSmoothing(ot.Uniform()) estimatedUniform = kernelUniform.build(sample) # %% # We finally compare all the distributions : # graph = distribution.drawPDF() graph.setTitle("Different kernel smoothings vs original distribution") graph.setGrid(True) kernel_estimatedNormal_plot = estimatedNormal.drawPDF().getDrawable(0)