import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if (ot.ConditionalDistribution().__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.ConditionalDistribution().__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.ConditionalDistribution().__class__.__name__ == 'Histogram'): distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) else: distribution = ot.ConditionalDistribution() 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:
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.ConditionalDistribution().__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.ConditionalDistribution( ).__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.ConditionalDistribution().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) else: distribution = ot.ConditionalDistribution() dimension = distribution.getDimension() if dimension == 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) elif dimension == 2: distribution.setDescription(['$x_1$', '$x_2$'])
# choose the initial state within the prior initialState = prior.getRealization() # conditional distribution conditional = ot.Normal() # create a metropolis-hastings sampler sampler = ot.RandomWalkMetropolisHastings( prior, conditional, data, initialState, proposalColl) sampler.setVerbose(True) sampler.setThinning(2) sampler.setBurnIn(500) sampler.setCalibrationStrategyPerComponent(calibrationColl) realization = sampler.getRealization() sigmay = ot.ConditionalDistribution( ot.Normal(), prior).getStandardDeviation()[0] w = size * sigma0 ** 2. / (size * sigma0 ** 2. + sigmay ** 2.0) print("prior variance= %.12g" % (sigma0 ** 2.)) print(" realization=", realization) print(" w= %.12g" % w) # the posterior for mu is analytical print(" expected posterior ~N( %.12g" % (w * data.computeMean() [0] + (1. - w) * mu0), ", %.12g" % ((w * sigmay ** 2.0 / size) ** 0.5), ")") # try to generate a sample sample = sampler.getSample(50) print(" obtained posterior ~N( %.12g" % sample.computeMean()[
atoms = [ot.Uniform(0.0, 1.0), ot.Uniform(1.0, 2.0)] conditioningDistributionCollection.append(ot.ComposedDistribution(atoms)) # Second conditioning distribution: discrete/continuous atoms = [ot.Binomial(3, 0.5), ot.Uniform(1.0, 2.0)] #conditioningDistributionCollection.append(ot.ComposedDistribution(atoms)) # Third conditioning distribution: dirac/continuous atoms = [ot.Dirac(0.0), ot.Uniform(1.0, 2.0)] conditioningDistributionCollection.append(ot.ComposedDistribution(atoms)) for conditioning in conditioningDistributionCollection: print("conditioning distribution=", conditioning) observationsDistribution = ot.Distribution(conditionedDistribution) observationsDistribution.setParameter(conditioning.getMean()) observations = observationsDistribution.getSample(observationsSize) distribution = ot.PosteriorDistribution(ot.ConditionalDistribution(conditionedDistribution, conditioning), observations) dim = distribution.getDimension() print("Distribution ", distribution) print("Distribution ", distribution) print("range=", distribution.getRange()) mean = distribution.getMean() print("Mean ", mean) print("Covariance ", distribution.getCovariance()) # Is this distribution an elliptical distribution? print("Elliptical distribution= ", distribution.isElliptical()) # Has this distribution an elliptical copula? print("Elliptical copula= ", distribution.hasEllipticalCopula()) # Has this distribution an independent copula? print("Independent copula= ", distribution.hasIndependentCopula())
import openturns.viewer as viewer from matplotlib import pylab as plt # %% # create the Y distribution YDist = ot.Uniform(-1.0, 1.0) # %% # create Theta=f(y) f = ot.SymbolicFunction(['y'], ['y', '1+y^2']) # %% # create the X|Theta distribution XgivenThetaDist = ot.Uniform() # %% # create the distribution XDist = ot.ConditionalDistribution(XgivenThetaDist, YDist, f) XDist.setDescription(['X|Theta=f(y)']) XDist # %% # Get a sample XDist.getSample(5) # %% # draw PDF graph = XDist.drawPDF() view = viewer.View(graph) plt.show()
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.ConditionalDistribution().__class__.__name__ == 'Bernoulli': distribution = ot.Bernoulli(0.7) elif ot.ConditionalDistribution().__class__.__name__ == 'Binomial': distribution = ot.Binomial(5, 0.2) elif ot.ConditionalDistribution().__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.ConditionalDistribution( ).__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.ConditionalDistribution().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) elif ot.ConditionalDistribution().__class__.__name__ == 'KernelMixture': kernel = ot.Uniform() sample = ot.Normal().getSample(5) bandwith = [1.0] distribution = ot.KernelMixture(kernel, bandwith, sample) elif ot.ConditionalDistribution().__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.ConditionalDistribution().__class__.__name__ == 'Multinomial':
atoms = [ot.Uniform(0.0, 1.0), ot.Uniform(1.0, 2.0)] conditioningDistributionCollection.append(ot.ComposedDistribution(atoms)) # Second conditioning distribution: discrete/continuous atoms = [ot.Binomial(3, 0.5), ot.Uniform(1.0, 2.0)] # conditioningDistributionCollection.append(ot.ComposedDistribution(atoms)) # Third conditioning distribution: dirac/continuous atoms = [ot.Dirac(0.0), ot.Uniform(1.0, 2.0)] conditioningDistributionCollection.append(ot.ComposedDistribution(atoms)) for conditioning in conditioningDistributionCollection: print("conditioning distribution=", conditioning) observationsDistribution = ot.Distribution(conditionedDistribution) observationsDistribution.setParameter(conditioning.getMean()) observations = observationsDistribution.getSample(observationsSize) distribution = ot.PosteriorDistribution( ot.ConditionalDistribution(conditionedDistribution, conditioning), observations) dim = distribution.getDimension() print("Distribution ", distribution) print("Distribution ", distribution) print("range=", distribution.getRange()) mean = distribution.getMean() print("Mean ", mean) print("Covariance ", distribution.getCovariance()) # Is this distribution an elliptical distribution? print("Elliptical distribution= ", distribution.isElliptical()) # Has this distribution an elliptical copula? print("Elliptical copula= ", distribution.hasEllipticalCopula()) # Has this distribution an independent copula?