import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.LogNormal().__class__.__name__ == 'Bernoulli': distribution = ot.Bernoulli(0.7) elif ot.LogNormal().__class__.__name__ == 'Binomial': distribution = ot.Binomial(5, 0.2) elif ot.LogNormal().__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.LogNormal().__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.LogNormal().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) elif ot.LogNormal().__class__.__name__ == 'KernelMixture': kernel = ot.Uniform() sample = ot.Normal().getSample(5) bandwith = [1.0] distribution = ot.KernelMixture(kernel, bandwith, sample) elif ot.LogNormal().__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.LogNormal().__class__.__name__ == 'Multinomial': distribution = ot.Multinomial(5, [0.2]) elif ot.LogNormal().__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)
#! /usr/bin/env python import openturns as ot import openturns.testing as ott ot.TESTPREAMBLE() # Instantiate one distribution object distribution = ot.DiscreteCompoundDistribution( ot.Bernoulli(0.5), ot.Poisson(20.0)) upper_bound = int(distribution.getRange().getUpperBound()[0]) print("Upper bound : {!r}".format(upper_bound)) # Compare with mathematically equal distribution poisson_distribution = ot.Poisson(10.0) for i in range(upper_bound): ott.assert_almost_equal(distribution.computePDF( [i]), poisson_distribution.computePDF([i])) print("Distribution ", repr(distribution)) print("Distribution ", distribution) # Is this distribution elliptical ? print("Elliptical = ", distribution.isElliptical()) # Is this distribution continuous ? print("Continuous = ", distribution.isContinuous()) # Test for realization of distribution oneRealization = distribution.getRealization()
res = term1 / (term1 + term0) # output must be a 1d list or array in order to create a PythonFunction return res.reshape(-1) nor0posterior = ot.PythonFunction(2 + N, 2, nor0post) nor1posterior = ot.PythonFunction(2 + N, 2, nor1post) zposterior = ot.PythonFunction(2 + N, N, zpost) # We can now construct the Gibbs algorithm initialState = [0.0] * (N + 2) sampler0 = ot.RandomVectorMetropolisHastings(ot.RandomVector(ot.Normal()), initialState, [0], nor0posterior) sampler1 = ot.RandomVectorMetropolisHastings(ot.RandomVector(ot.Normal()), initialState, [1], nor1posterior) big_bernoulli = ot.ComposedDistribution([ot.Bernoulli()] * N) sampler2 = ot.RandomVectorMetropolisHastings(ot.RandomVector(big_bernoulli), initialState, range(2, N + 2), zposterior) gibbs = ot.Gibbs([sampler0, sampler1, sampler2]) # Run the Gibbs algorithm s = gibbs.getSample(10000) # Extract the relevant marginals: the first (:math:`mu_0`) and the second (:math:`\mu_1`). posterior_sample = s[:, 0:2] mean = posterior_sample.computeMean() stddev = posterior_sample.computeStandardDeviation() print(mean, stddev) ott.assert_almost_equal(mean, [-0.0788226, 2.80322]) ott.assert_almost_equal(stddev, [0.0306272, 0.0591087])
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if (ot.Bernoulli().__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.Bernoulli().__class__.__name__ == 'CumulativeDistributionNetwork'): distribution = ot.CumulativeDistributionNetwork( [ot.Normal(2), ot.Dirichlet([0.5, 1.0, 1.5])], ot.BipartiteGraph([[0, 1], [0, 1]])) else: distribution = ot.Bernoulli() 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$']) pdf_graph = distribution.drawPDF() fig = plt.figure(figsize=(10, 5))
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.Bernoulli().__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.Bernoulli().__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.Bernoulli().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) else: distribution = ot.Bernoulli() 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$']) pdf_graph = distribution.drawPDF()
1], [70, 1], [72, 0], [73, 0], [75, 0], [75, 1], [76, 0], [76, 0], [78, 0], [79, 0], [81, 0]]) data.setDescription(['Temp. (°F)', 'Failure']) print(data) fun = ot.SymbolicFunction( ["alpha", "beta", "x"], ["exp(alpha + beta * x) / (1 + exp(alpha + beta * x))"]) linkFunction = ot.ParametricFunction(fun, [2], [0.0]) instrumental = ot.Normal([0.0] * 2, [0.5, 0.05], ot.IdentityMatrix(2)) target = ot.ComposedDistribution([ot.Uniform(-100.0, 100.0)] * 2) rwmh = ot.RandomWalkMetropolisHastings(target, [0.0] * 2, instrumental) conditional = ot.Bernoulli() observations = data[:, 1] covariates = data[:, 0] rwmh.setLikelihood(conditional, observations, linkFunction, covariates) # try to generate a sample sample = rwmh.getSample(10000) mu = sample.computeMean() sigma = sample.computeStandardDeviation() print('mu=', mu, 'sigma=', sigma) ott.assert_almost_equal(mu, [14.8747, -0.230384]) ott.assert_almost_equal(sigma, [7.3662, 0.108103]) print('acceptance rate=', rwmh.getAcceptanceRate()) ott.assert_almost_equal(rwmh.getAcceptanceRate(), 0.1999)
"round": 2, }, "nb_provider_rating": { "marg": ot.Geometric(0.001), # mean is 1000 "corr": 0.08, # Weak positive correlation "bounds": None, "round": 0, # Already an integer (casted as double) }, "avg_provider_rating": { "marg": ot.Triangular(2.5, 4.0, 4.8), # mode is 4.0 "corr": 0.01, # Weak positive correlation "bounds": None, "round": 2, }, "has_multipayment": { "marg": ot.Bernoulli(0.4), # assume 40% of yes "corr": 0.0, # Assume no correlation "bounds": None, "round": 0, }, "conversion_rate": { "marg": ot.Exponential(15.0, 0.05), # mean is ~0.12 (0.05 + 1/15) "corr": None, # Corr not relevant for conversion_rate, because # it is the target variable. "bounds": None, "round": 4, }, } CORR_CONF_TRAIN = { # Conf file for adding correlations between variables ("price", "ratio_shipping"): -0.15, # The more expensive, the less costly
import smartGrid as sg import uncertain.boolean as ubool import openturns as ot if __name__ == '__main__': fuse1 = sg.Fuse(ubool.UBoolean(ot.Bernoulli(1))) fuse2 = sg.Fuse(ubool.UBoolean(ot.Bernoulli(1))) fuse3 = sg.Fuse(ubool.UBoolean(ot.Bernoulli(1))) cable1 = sg.Cable(ot.Normal(10, 0.8)) fuse1.cable = cable1 cable2 = sg.Cable(ot.Normal(10, 0.8)) fuse2.cable = cable2 cable3 = sg.Cable(ot.Normal(10, 0.8)) fuse3.cable = cable3 substation = sg.Substation(fuse1, fuse2, fuse3) sg.compute_load_no_cable(substation) print(substation)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.Bernoulli().__class__.__name__ == 'Bernoulli': distribution = ot.Bernoulli(0.7) elif ot.Bernoulli().__class__.__name__ == 'Binomial': distribution = ot.Binomial(5, 0.2) elif ot.Bernoulli().__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.Bernoulli().__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.Bernoulli().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) elif ot.Bernoulli().__class__.__name__ == 'KernelMixture': kernel = ot.Uniform() sample = ot.Normal().getSample(5) bandwith = [1.0] distribution = ot.KernelMixture(kernel, bandwith, sample) elif ot.Bernoulli().__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.Bernoulli().__class__.__name__ == 'Multinomial': distribution = ot.Multinomial(5, [0.2])
def __init__(self, confidence=ot.Bernoulli()): self.confidence = confidence
def not_op(self): return UBoolean(ot.Bernoulli(1 - self.confidence.getP()))
def __or__(self, other): if isinstance(other, UBoolean): return UBoolean( ot.Bernoulli(self.confidence.getP() * other.confidence.getP())) return NotImplemented
#! /usr/bin/env python import openturns as ot import openturns.testing as ott ot.TESTPREAMBLE() # Instantiate one distribution object distribution = ot.DiscreteCompoundDistribution(ot.Bernoulli(0.5), ot.Poisson(20.0)) upper_bound = int(distribution.getRange().getUpperBound()[0]) print("Upper bound : {!r}".format(upper_bound)) # Compare with mathematically equal distribution poisson_distribution = ot.Poisson(10.0) for i in range(upper_bound): ott.assert_almost_equal(distribution.computePDF([i]), poisson_distribution.computePDF([i])) print("Distribution ", repr(distribution)) print("Distribution ", distribution) # Is this distribution elliptical ? print("Elliptical = ", distribution.isElliptical()) # Is this distribution continuous ? print("Continuous = ", distribution.isContinuous()) # Test for realization of distribution oneRealization = distribution.getRealization() print("oneRealization=", repr(oneRealization))
def __init__(self, cable=None, is_closed=ubool.UBoolean(ot.Bernoulli(1))): self.cable = cable self.isClosed = is_closed