def KSG_learning(data): print("Build KSG coefficients distribution") size = data.getSize() dimension = data.getDimension() t0 = time() marginals = [ ot.HistogramFactory().build(data.getMarginal(i)) for i in range(dimension) ] plot_marginals("KSG_marginals", marginals) copula = ot.NormalCopulaFactory().build(data) distribution = ot.ComposedDistribution(marginals, copula) print("t=", time() - t0, "s") return distribution
def BuildDistribution(X): #return ot.FunctionalChaosAlgorithm.BuildDistribution(X) input_dimension = X.shape[1] marginals = [] for j in range(input_dimension): marginals.append(ot.HistogramFactory().build(X[:,j].reshape(-1, 1))) isIndependent = True for j in range(input_dimension): marginalJ = X[:,j].reshape(-1, 1) for i in range(j + 1, input_dimension): marginalI = X[:,i].reshape(-1, 1) testResult = ot.HypothesisTest.Spearman(marginalI, marginalJ) isIndependent = isIndependent and testResult.getBinaryQualityMeasure() copula = ot.IndependentCopula(input_dimension) if not isIndependent: copula = ot.NormalCopulaFactory().build(X) distribution = ot.ComposedDistribution(marginals, copula) return distribution
ax.set_zlabel('$\\varphi_i(\mathbf{x})$') pl.savefig('2D_identification_eigensolution_%d.png' % i) pl.close() # Calculation of the KL coefficients by functional projection using # Gauss-Legendre quadrature xi = estimated_random_field.compute_coefficients(sample_paths) # Statistical inference of the KL coefficients' distribution kernel_smoothing = ot.KernelSmoothing(ot.Normal()) xi_marginal_distributions = ot.DistributionCollection([ kernel_smoothing.build(xi[:, i][:, np.newaxis]) for i in xrange(truncation_order) ]) try: xi_copula = ot.NormalCopulaFactory().build(xi) except RuntimeError: print('ERR: The normal copula correlation matrix built from the given\n' + 'Spearman correlation matrix is not definite positive.\n' + 'This would require expert judgement on the correlation\n' + 'coefficients significance (using e.g. Spearman test).\n' + 'Assuming an independent copula in the sequel...') xi_copula = ot.IndependentCopula(truncation_order) xi_estimated_distribution = ot.ComposedDistribution(xi_marginal_distributions, xi_copula) # Matrix plot of the empirical KL coefficients & their estimated distribution matrix_plot(xi, ot_distribution=xi_estimated_distribution, labels=[('$\\xi_{%d}$' % i) for i in xrange(truncation_order)]) pl.suptitle('Karhunen-Loeve coefficients ' +
def NC_marginalKS(data, marginals): # Third model: marginal KS + normal copula print("Normal copula") model = ot.ComposedDistribution(marginals, ot.NormalCopulaFactory().build(data)) return model
size = 300 sampleLearn = cbn.getSample(size) sample = cbn.getSample(size) sampleLearn.exportToCSVFile("samplelearn.csv", ",") sample.exportToCSVFile("sample.csv", ",") print("cbn sample=", sample) logL = 0.0 pdfSample = cbn.computePDF(sample) pdfSample.exportToCSVFile("pdfSample.csv", ",") for i in range(size): logL += m.log(pdfSample(i, 0)) logL /= size print("log-l=", logL) copula = ot.NormalCopulaFactory().buildAsNormalCopula(sampleLearn) print("copula=", copula) print("log-l=", copula.computeLogPDF(sample).computeMean()[0]) gr = ot.Graph() pairs = ot.Pairs(sample) pairs.setPointStyle("dot") gr.add(pairs) import openturns.viewer as otv view = otv.View(gr, (800, 820), square_axes=True) view.save("pairs.png") view.close() gr = ot.Graph() pairs = ot.Pairs(copula.getSample(size)) pairs.setPointStyle("dot") gr.add(pairs) view = otv.View(gr, (800, 820), square_axes=True)
# In this example we are going to estimate the parameters of a gaussian copula from a sample. # %% from __future__ import print_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # %% # Create data R = ot.CorrelationMatrix(2) R[1, 0] = 0.4 copula = ot.NormalCopula(R) sample = copula.getSample(500) # %% # Estimate a normal copula distribution = ot.NormalCopulaFactory().build(sample) print(distribution) # %% # The estimated parameters distribution.getParameter() # %% # Draw fitted distribution graph = distribution.drawPDF() view = viewer.View(graph) plt.show()
bic_curve = [] for alpha in alphas: ot.Log.Show(ot.Log.NONE) print("\tLearning with alpha={}".format(alpha)) learner = otagr.ContinuousMIIC( data_ref.select(train)) # Using CMIIC algorithm learner.setAlpha(alpha) cmiic_dag = learner.learnDAG() # Learning DAG ot.Log.Show(ot.Log.NONE) if True: cmiic_cbn = otagr.ContinuousBayesianNetworkFactory( ot.KernelSmoothing(ot.Normal()), ot.BernsteinCopulaFactory(), cmiic_dag, 0.05, 4, False).build(data_ref.select(train)) else: cmiic_cbn = otagr.ContinuousBayesianNetworkFactory( ot.NormalFactory(), ot.NormalCopulaFactory(), cmiic_dag, 0.05, 4, False).build(data_ref.select(train)) # sampled = cmiic_cbn.getSample(1000) # sampled = (sampled.rank() +1)/(sampled.getSize()+2) # pairs(sampled, figure_path.joinpath('pairs_test.pdf') ll = 0 s = 0 for point in data_ref.select(test): point_ll = cmiic_cbn.computeLogPDF(point) if np.abs(point_ll) <= 10e20: s += 1 ll += point_ll else: print("pb point=", point, "log pdf=", point_ll) ll /= s n_arc = cmiic_dag.getDAG().sizeArcs()
sobol.setReplicationSize(3) sobol.setBlockSize(1) sobol.setSeed(2) sobol.setInterestVariables(['y0', 'y1']) myStudy.add(sobol) # 4-b SRC ## src = persalys.SRCAnalysis('SRC', model1) src.setSimulationsNumber(20) src.setSeed(2) src.setInterestVariables(['y0', 'y1']) myStudy.add(src) # 7- data analysis ## dataAnalysis = persalys.DataAnalysis('DataAnalysis', model3) myStudy.add(dataAnalysis) # 8- Marginals inference ## inference = persalys.InferenceAnalysis('inference', model3) inference.setInterestVariables(['x_0', 'x_3']) factories = [ot.NormalFactory(), ot.GumbelFactory()] inference.setDistributionsFactories('x_3', factories) inference.setLevel(0.1) myStudy.add(inference) # 9- Copula inference ## copulaInference = persalys.CopulaInferenceAnalysis('copulaInference', model3) factories = [ot.NormalCopulaFactory(), ot.GumbelCopulaFactory()] copulaInference.setDistributionsFactories(['x_0', 'x_3'], factories) myStudy.add(copulaInference)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View ot.RandomGenerator.SetSeed(0) factory = ot.NormalCopulaFactory() ref = factory.build() dimension = ref.getDimension() if dimension <= 2: sample = ref.getSample(50) distribution = factory.build(sample) if dimension == 1: distribution.setDescription(['$t$']) pdf_graph = distribution.drawPDF(256) cloud = ot.Cloud(sample, ot.Sample(sample.getSize(), 1)) cloud.setColor('blue') cloud.setPointStyle('fcircle') pdf_graph.add(cloud) fig = plt.figure(figsize=(10, 4)) plt.suptitle(str(distribution)) pdf_axis = fig.add_subplot(111) View(pdf_graph, figure=fig, axes=[pdf_axis], add_legend=False) else: sample = ref.getSample(500) distribution.setDescription(['$t_0$', '$t_1$']) pdf_graph = distribution.drawPDF([256]*2) cloud = ot.Cloud(sample) cloud.setColor('red') cloud.setPointStyle('fcircle') pdf_graph.add(cloud) fig = plt.figure(figsize=(10, 4)) plt.suptitle(str(distribution))