def interpret_complete_cdf( cdfs_p: List[Union[list, np.ndarray]], cdfs_v: List[Union[list, np.ndarray]], distribution: str = None, ) -> Union[List[Union[list, np.ndarray]], Tuple[List[Union[list, np.ndarray]], List[Union[list, np.ndarray]]], ot.DistributionImplementation, ]: """Interpret the given points on the cumulative distribution function to represent a complete CDF. The default policy is to assume discrete probabilities. If a distribution name is specified, the CDF is returned as an openturns distribution object. Supported openturns distributions are the following: - discrete: all residual probability is attributed to the highest given value - normal or gaussian: derived from the first two point only - uniform: interpolates linearly between points, with residual probability attributed to the min and max values """ # Todo: refactor, currently too many possible types of output if distribution is None: for cdf_p in cdfs_p: cdf_p[-1] = 1 # Last value is the highest return cdfs_p, cdfs_v cdfs = [] if distribution == "discrete": for cdf_p, cdf_v in zip(cdfs_p, cdfs_v): cdf_p[-1] = 1 # Last value is the highest cdfs.append(ot.UserDefined([[v] for v in cdf_v], cp_to_p(cdf_p))) elif distribution in ["normal", "gaussian"]: for cdf_p, cdf_v in zip(cdfs_p, cdfs_v): if len(cdf_v) > 1: x1 = cdf_v[0] x2 = cdf_v[1] y1 = cdf_p[0] y2 = cdf_p[1] mu = (x1 * pyerf.erfinv(1 - 2 * y2) - x2 * pyerf.erfinv(1 - 2 * y1)) / ( pyerf.erfinv(1 - 2 * y2) - pyerf.erfinv(1 - 2 * y1)) sigma = (2**0.5 * x1 - 2**0.5 * x2) / (2 * pyerf.erfinv(1 - 2 * y2) - 2 * pyerf.erfinv(1 - 2 * y1)) cdfs.append(ot.Normal(mu, sigma)) else: cdfs.append(ot.UserDefined([[v] for v in cdf_v], cdf_p)) elif distribution == "uniform": for cdf_p, cdf_v in zip(cdfs_p, cdfs_v): if len(cdf_v) == 1: cdfs.append(ot.UserDefined([cdf_v])) elif len(cdf_v) > 1: coll = ([ot.UserDefined([[cdf_v[0]]])] + [ ot.Uniform(float(cdf_v[i]), float(cdf_v[i + 1])) for i in range(len(cdf_v) - 1) ] + [ot.UserDefined([[cdf_v[-1]]])]) weights = np.append(cp_to_p(cdf_p), 1 - cdf_p[-1]) cdfs.append(ot.Mixture(coll, weights)) else: return NotImplementedError return cdfs
def drawKSDistance(sample, distribution, observation, D, distFactory): graph = ot.Graph("KS Distance = %.4f" % (D), "X", "CDF", True, "topleft") # Thick vertical line at point x ECDF_x_plus = sample.computeEmpiricalCDF(observation) ECDF_x_minus = ECDF_x_plus - 1.0 / sample.getSize() CDF_index = distribution.computeCDF(observation) curve = ot.Curve( [observation[0], observation[0], observation[0]], [ECDF_x_plus, ECDF_x_minus, CDF_index], ) curve.setLegend("KS Statistics") curve.setLineWidth(4.0 * curve.getLineWidth()) graph.add(curve) # Empirical CDF empiricalCDF = ot.UserDefined(sample).drawCDF() empiricalCDF.setLegends(["Empirical DF"]) graph.add(empiricalCDF) # distname = distFactory.getClassName() distribution = distFactory.build(sample) cdf = distribution.drawCDF() cdf.setLegends([distname]) graph.add(cdf) graph.setColors(ot.Drawable.BuildDefaultPalette(3)) return graph
def drawKSDistance(sample, distribution, observation, D, distFactory, delta_x=ot.Point([1.0e-6])): graph = ot.Graph("KS Distance = %.4f" % (D), "X", "CDF", True, "topleft") # Vertical line at point x ECDF_index = sample.computeEmpiricalCDF(observation) ECDF_index_shifted = sample.computeEmpiricalCDF(observation - delta_x) CDF_index = distribution.computeCDF(observation) curve = ot.Curve( [observation[0], observation[0], observation[0]], [ECDF_index, ECDF_index_shifted, CDF_index], ) curve.setColor("green") curve.setLegend("KS Statistics") curve.setLineWidth(4.0 * curve.getLineWidth()) graph.add(curve) # Empirical CDF empiricalCDF = ot.UserDefined(sample).drawCDF() empiricalCDF.setColors(["blue"]) empiricalCDF.setLegends(["Empirical DF"]) graph.add(empiricalCDF) # distname = distFactory.getClassName() distribution = distFactory.build(sample) cdf = distribution.drawCDF() cdf.setLegends([distname]) graph.add(cdf) return graph
def _PODbinomialModel(self, residuals, linearModel): empiricalDist = ot.UserDefined(residuals) # function to compute the POD(defect) def PODmodel(x): def_threshold = self._detectionBoxCox - linearModel(x[0]) # Nb of residuals > threshold(defect) / N return [empiricalDist.computeComplementaryCDF(def_threshold)] return PODmodel
def interpret_complete_cdf( cdfs_p: List[Union[list, np.ndarray]], cdfs_v: List[Union[list, np.ndarray]], distribution: str = None, ) -> Union[List[Union[list, np.ndarray]], Tuple[List[Union[list, np.ndarray]], List[Union[list, np.ndarray]]], ot.DistributionImplementation, ]: """Interpret the given points on the cumulative distribution function to represent a complete CDF. The default policy is to assume discrete probabilities. If a distribution name is specified, the CDF is returned as an openturns distribution object. Supported openturns distributions are the following: - discrete: all residual probability is attributed to the highest given value - normal or gaussian: derived from the first two point only - uniform: derived from the first and last points and extended to range from cp=0 to cp=1 """ # Todo: refactor, currently too many possible types of output if distribution is None: for cdf_p in cdfs_p: cdf_p[-1] = 1 # Last value is the highest return cdfs_p, cdfs_v cdfs = [] if distribution == "discrete": for cdf_p, cdf_v in zip(cdfs_p, cdfs_v): cdf_p[-1] = 1 # Last value is the highest cdfs.append(ot.UserDefined([[v] for v in cdf_v], cdf_p)) elif distribution in ["normal", "gaussian"]: for cdf_p, cdf_v in zip(cdfs_p, cdfs_v): x1 = cdf_v[0] x2 = cdf_v[1] y1 = cdf_p[0] y2 = cdf_p[1] mu = (x1 * pyerf.erfinv(1 - 2 * y2) - x2 * pyerf.erfinv(1 - 2 * y1) ) / (pyerf.erfinv(1 - 2 * y2) - pyerf.erfinv(1 - 2 * y1)) sigma = (2**0.5 * x1 - 2**0.5 * x2) / ( 2 * pyerf.erfinv(1 - 2 * y2) - 2 * pyerf.erfinv(1 - 2 * y1)) cdfs.append(ot.Normal(mu, sigma)) elif distribution is "uniform": for cdf_p, cdf_v in zip(cdfs_p, cdfs_v): x1 = cdf_v[0] x2 = cdf_v[-1] y1 = cdf_p[0] y2 = cdf_p[-1] dydx = (y2 - y1) / (x2 - x1) a = x1 - y1 / dydx b = x2 + (1 - y2) / dydx cdfs.append(ot.Uniform(a, b)) else: return NotImplementedError return cdfs
def _PODbinomialModelCl(self, residuals, linearModel, confLevel): empiricalDist = ot.UserDefined(residuals) sizeResiduals = residuals.getSize() def PODmodelCl(x): # Nb of residuals > threshold - linModel(defect) def_threshold = self._detectionBoxCox - linearModel(x[0]) NbDepDef = m.trunc(sizeResiduals * empiricalDist.computeComplementaryCDF(def_threshold)) # Particular case : NbDepDef == sizeResiduals if NbDepDef == sizeResiduals: pod = confLevel**(1. / sizeResiduals) else: # 1 - quantile(confLevel) of distribution Beta(r, s) pod = 1-ot.DistFunc.qBeta(sizeResiduals - NbDepDef, NbDepDef + 1, confLevel) return [pod] return PODmodelCl
def drawIFS(f_i, skip=100, iterations=1000, batch_size=1, name="IFS", color="blue"): # Any set of initial points should work in theory initialPoints = ot.Normal(2).getSample(batch_size) # Compute the contraction factor of each function all_r = [m.sqrt(abs(f[1].computeDeterminant())) for f in f_i] # Find the box counting dimension, ie the value s such that r_1^s+...+r_n^s-1=0 equation = "-1.0" for r in all_r: equation += "+" + str(r) + "^s" dim = len(f_i) s = ot.Brent().solve(ot.SymbolicFunction("s", equation), 0.0, 0.0, -m.log(dim) / m.log(max(all_r))) # Add a small perturbation to sample even the degenerated transforms probabilities = [r**s + 1e-2 for r in all_r] # Build the sampling distribution support = [[i] for i in range(dim)] choice = ot.UserDefined(support, probabilities) currentPoints = initialPoints points = ot.Sample(0, 2) # Convert the f_i into LinearEvaluation to benefit from the evaluation over # a Sample phi_i = [ot.LinearEvaluation([0.0] * 2, f[0], f[1]) for f in f_i] # Burning phase for i in range(skip): index = int(round(choice.getRealization()[0])) currentPoints = phi_i[index](currentPoints) # Iteration phase for i in range(iterations): index = int(round(choice.getRealization()[0])) currentPoints = phi_i[index](currentPoints) points.add(currentPoints) # Draw the IFS graph = ot.Graph() graph.setTitle(name) graph.setXTitle("x") graph.setYTitle("y") graph.setGrid(True) cloud = ot.Cloud(points) cloud.setColor(color) cloud.setPointStyle("dot") graph.add(cloud) return graph, s
def drawKSDistance(sample, distribution, x, D, distFactory): graph = ot.Graph("KS Distance = %.4f" % (D), "X", "CDF", True, "topleft") # Vertical line at point x ECDF_index = sample.computeEmpiricalCDF([x]) CDF_index = distribution.computeCDF(x) curve = ot.Curve([x, x], [ECDF_index, CDF_index]) curve.setColor("green") curve.setLegend("KS Statistics") curve.setLineWidth(4. * curve.getLineWidth()) graph.add(curve) # Empirical CDF empiricalCDF = ot.UserDefined(sample).drawCDF() empiricalCDF.setColors(["blue"]) empiricalCDF.setLegends(["Empirical DF"]) graph.add(empiricalCDF) # distname = distFactory.getClassName() distribution = distFactory.build(sample) cdf = distribution.drawCDF() cdf.setLegends([distname]) graph.add(cdf) return graph
""" Sort a sample ============= """ # %% # In this example we present useful methods of the `Sample` object such as marginals extraction and various sorting strategies. # %% from __future__ import print_function import openturns as ot ot.Log.Show(ot.Log.NONE) ot.RandomGenerator.SetSeed(0) # %% # We start by defining the distribution of a regular non-biased dice. dice_distribution = ot.UserDefined([[i] for i in range(1, 7)]) # %% # We consider now an experiment with two independent dices and build the corresponding random vector : two_dices_distribution = ot.ComposedDistribution([dice_distribution] * 2) # %% # We now build a sample of size :math:`n=5` from this distribution : n = 5 sample = two_dices_distribution.getSample(n) print(sample) # %% # Useful methods # --------------
import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # %% # Then create a sample from a gaussian distribution. # %% size = 100 normal = ot.Normal(1) sample = normal.getSample(size) # %% # We draw the empirical CDF based on the `UserDefined` distribution. By default, the `drawCDF` method requires no input argument. # %% distribution = ot.UserDefined(sample) graph = distribution.drawCDF() view = viewer.View(graph) # %% # If required, we can specify the interval that we want to draw. In the following example, these bounds are computed from the minimum and the maximum of the sample. # %% xmin = sample.getMin()[0] - 2.0 xmax = sample.getMax()[0] + 2.0 graph = ot.UserDefined(sample).drawCDF(xmin, xmax) view = viewer.View(graph) plt.show()
import openturns.viewer as viewer from matplotlib import pylab as plt import math as m ot.Log.Show(ot.Log.NONE) # %% # Define the origin origin = [0.0] # %% # Define an 1-d mesh tgrid = ot.RegularGrid(0.0, 1.0, 500) # %% # 1-d random walk and discrete distribution dist = ot.UserDefined([[-1], [10]], [0.9, 0.1]) process = ot.RandomWalk(origin, dist, tgrid) sample = process.getSample(5) graph = sample.drawMarginal(0) graph.setTitle('1D Random Walk with discrete steps') view = viewer.View(graph) # %% # 1-d random walk and continuous distribution dist = ot.Normal(0.0, 1.0) process = ot.RandomWalk(origin, dist, tgrid) sample = process.getSample(5) graph = sample.drawMarginal(0) graph.setTitle('1D Random Walk with continuous steps') view = viewer.View(graph)
estimatedDistribution = factory.build() print("Default distribution=", estimatedDistribution) estimatedDistribution = factory.build(distribution.getParameter()) print("Distribution from parameters=", estimatedDistribution) estimatedBurr = factory.buildAsBurr(sample) print("Burr =", distribution) print("Estimated burr=", estimatedBurr) estimatedBurr = factory.buildAsBurr() print("Default burr=", estimatedBurr) estimatedBurr = factory.buildAsBurr(distribution.getParameter()) print("Burr from parameters=", estimatedBurr) try: estimatedBurr = factory.build(ot.Normal(1e-3, 1e-5).getSample(100)) print('Estimated burr=', estimatedBurr) except: pass ot.RandomGenerator.SetSeed(0) try: estimatedBurr = factory.build( ot.UserDefined(ot.LogNormal(7.71, 1.0056).getSample(500)).getSupport()) #print('Estimated burr=', estimatedBurr) except: pass except: import sys print("t_BurrFactory_std.py", sys.exc_info()[0], sys.exc_info()[1])
# We first generate random points in the square. distUniform2 = ot.ComposedDistribution([ot.Uniform(-1.0, 1.0)] * 2) N = 100 sample = distUniform2.getSample(N) # %% # We then build the points and weights for the `UserDefined` distribution. points = [] weights = [] for i in range(N): points.append(sample[i, :]) weights.append((sample[i, 0]**2 + sample[i, 1]**2)**2) # %% # We build the distribution : distribution = ot.UserDefined(points, weights) graph = distribution.drawPDF() graph.setTitle("User defined PDF") # %% # We can draw a sample from this distribution with the `getSample` method : omega = distribution.getSample(100) cloud = ot.Cloud(omega, 'black', 'fdiamond', 'Sample from UserDefined distribution') graph.add(cloud) view = otv.View(graph) # %% # As expected most values are near the edge of the square where the PDF is the higher. # %%
def multivariate_marginal_to_univariate_joint_cdf( # noqa: C901 marginal_cdfs_p: Union[List[Union[List[float], np.ndarray, ot.DistributionImplementation]], np.ndarray], marginal_cdfs_v: Union[List[Union[List[float], np.ndarray]], np.ndarray] = None, a: float = 0, b: float = 1, copula: ot.CopulaImplementation = None, agg_function: Callable[[np.ndarray], np.ndarray] = None, simplify: bool = True, n_draws: int = 100, empirical: bool = False, ) -> Tuple[np.array, np.array]: """Calculate univariate joint CDF given a list of multivariate marginal CDFs and a copula, returning both the cumulative probabilities and the aggregated outcome of the random variables. :param: marginal_cdfs_p: Each marginal CDF is a list (or 2darray) with cumulative probabilities up to cp=1. If a cdf does not go up to cp=1 and there are few cdfs (low dimension), we can still evaluate possible combinations for each marginal cp given. That is, the remaining probability is attributed to some higher (but unknown) outcome. However, the empirical method can't be used. :param: marginal_cdfs_v: Values of possible outcomes for each random variable, i.e. the bins of the marginal CDFs. If just one set of bins is given, we assume the CDFs share the same set of bins. If no bins are specified (the default), we assume the CDFs share a set of equal-sized bins between a and b. "All bins are equal, but some bins are more equal than others." (because they have a higher probability) :param: a: The lowest outcome (0 by default, and ignored if CDF values are given explicitly) :param: b: The highest outcome (1 by default, and ignored if CDF values are given explicitly) :param: copula: The default copula is the independence copula (i.e. we assume independent random variables). :param: agg_function: The default aggregation function is to take the sum of the outcomes of the random variables. :param: simplify: Simplify the resulting cdf by removing possible outcomes with zero probability (True by default) :param: n_draws: Number of draws (sample size) to compute the empirical CDF when aggregating >3 random variables. :param: empirical: Compute the empirical CDF regardless of number of random variables (default is False) """ dim = len(marginal_cdfs_p) n_outcomes = ( 99 ) # Todo: refactor to avoid having to set this above our threshold for computing exact probabilities # Set up marginal distributions empirical_method_possible = True if isinstance(marginal_cdfs_p[0], ot.DistributionImplementation): marginals = marginal_cdfs_p shared_bins = False empirical = True else: # Set up marginal cdf values n_outcomes = len(marginal_cdfs_p[0]) shared_bins = True if marginal_cdfs_v is None: values = np.linspace(a, b, n_outcomes) elif isinstance(marginal_cdfs_v[0], (list, np.ndarray)): shared_bins = False values = marginal_cdfs_v else: values = marginal_cdfs_v marginals = [] for i in range(dim): marginal_cdf = marginal_cdfs_p[i] if shared_bins is True: values_for_cdf = values else: values_for_cdf = marginal_cdfs_v[i] if not math.isclose(marginal_cdf[-1], 1, rel_tol=1e-7): empirical_method_possible = False # We can assume some higher outcome exists with cp=1 values_for_cdf = np.append( values_for_cdf, values_for_cdf[-1] + 1) # Add a higher outcome (+1 suffices) marginal_pdf = np.clip( np.concatenate(( [marginal_cdf[0]], np.diff(marginal_cdf), [1.0 - marginal_cdf[-1]], )), 0, 1, ) marginals.append( ot.UserDefined([[v] for v in values_for_cdf], marginal_pdf)) else: marginal_pdf = np.clip(cp_to_p(marginal_cdf), 0, 1) marginals.append( ot.UserDefined([[v] for v in values_for_cdf], marginal_pdf)) # If not specified, pick the independent copula as a default (i.e. assume independent random variables) if copula is None: copula = ot.IndependentCopula(dim) # If not specified, pick the sum function as a default for joining values if agg_function is None: agg_function = np.sum # Evaluate exact probabilities only for small bivariate and tri-variate joint distributions if dim <= 3 and n_outcomes <= 10 and empirical is False: # Determine joint distribution (too slow for high dimensions) d = ot.ComposedDistribution(marginals, copula) # Compute acceptable margin to prevent floating point errors (we'll evaluate a little on the right side of each marginal point) if shared_bins is True: smallest_marginal_point_distance = (np.diff(values).min() if n_outcomes > 1 else 1) elif dim > 1: smallest_marginal_point_distance = (np.diff(values, axis=1).min() if n_outcomes > 1 else 1) else: smallest_marginal_point_distance = ( 1 ) # With just 1 point, an arbitrary positive distance suffices (e.g. 1) margin = smallest_marginal_point_distance / 2 # Construct an n-dimensional matrix with all possible points (i.e. combinations of outcomes of our random variables) if shared_bins is True: marginal_points = list(product(values, repeat=dim)) shape = (n_outcomes, ) * dim # Marginal points for the cdf evaluation are slightly higher to ensure we are on the right side of the discrete jump in cumulative probability marginal_points_for_cdf_evaluation = list( product([v + margin for v in values], repeat=dim)) else: marginal_points = list(product(*marginal_cdfs_v)) shape = [len(m) for m in marginal_cdfs_v] # Marginal points for the cdf evaluation marginal_points_for_cdf_evaluation = list( product(*[v + margin for v in marginal_cdfs_v])) # Evaluate exact probabilities at each point (too slow for high dimensions) joint_multivariate_cdf = np.reshape( d.computeCDF(marginal_points_for_cdf_evaluation), shape) joint_multivariate_pdf = joint_cdf_to_pdf(joint_multivariate_cdf) # Sort the probabilities ascending, keeping track of the corresponding values p, v = zip(*sorted( zip(joint_multivariate_pdf.flatten(), agg_function(marginal_points, 1)))) # Calculate total probability of each unique value (by adding probability of cases that yield the same value) cdf_v = np.unique(v) pdf_p = np.array( [sum(np.array(p)[np.where(v == i)[0]]) for i in cdf_v]) elif ( empirical_method_possible is True ): # Otherwise, compute the empirical cdf from a sample generated directly from the copula uniform_points = np.array(copula.getSample( n_draws)) # Much faster than sampling from the joint cdf aggregated_points = np.zeros(n_draws) for i, point in enumerate(uniform_points): aggregated_points[i] = agg_function( list( marginal_cdf.computeQuantile(marginal_cdf_p)[0] for marginal_cdf_p, marginal_cdf in zip(point, marginals))) empirical_cdf = ot.UserDefined([[v] for v in aggregated_points]) pdf_p = np.array(empirical_cdf.getP()) cdf_v = np.array(empirical_cdf.getX()).flatten() else: raise ValueError( "Empirical method not possible given incomplete marginal CDF. Make sure all CDFs go up to 1." ) # Simplify resulting pdf if simplify is True: cdf_v = cdf_v[np.nonzero(pdf_p)] pdf_p = pdf_p[np.nonzero(pdf_p)] # Return the univariate joint cumulative probability function cdf_p = pdf_p.cumsum() return cdf_p, cdf_v
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.UserDefined().__class__.__name__ == 'Bernoulli': distribution = ot.Bernoulli(0.7) elif ot.UserDefined().__class__.__name__ == 'Binomial': distribution = ot.Binomial(5, 0.2) elif ot.UserDefined().__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.UserDefined().__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.UserDefined().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) elif ot.UserDefined().__class__.__name__ == 'KernelMixture': kernel = ot.Uniform() sample = ot.Normal().getSample(5) bandwith = [1.0] distribution = ot.KernelMixture(kernel, bandwith, sample) elif ot.UserDefined().__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.UserDefined().__class__.__name__ == 'Multinomial': distribution = ot.Multinomial(5, [0.2])
def __init__(self, n_samples, bounds, kind, dists=None, discrete=None): """Initialize the DOE generation. In case of :attr:`kind` is ``uniform``, :attr:`n_samples` is decimated in order to have the same number of points in all dimensions. If :attr:`kind` is ``discrete``, a join distribution between a discrete uniform distribution is made with continuous distributions. Another possibility is to set a list of PDF to sample from. Thus one can do: `dists=['Uniform(15., 60.)', 'Normal(4035., 400.)']`. If not set, uniform distributions are used. :param int n_samples: number of samples. :param array_like bounds: Space's corners [[min, n dim], [max, n dim]] :param str kind: Sampling Method if string can be one of ['halton', 'sobol', 'faure', '[o]lhs[c]', 'sobolscramble', 'uniform', 'discrete'] otherwize can be a list of openturns distributions. :param lst(str) dists: List of valid openturns distributions as string. :param int discrete: Position of the discrete variable. """ self.n_samples = n_samples self.bounds = np.asarray(bounds) self.kind = kind self.dim = self.bounds.shape[1] self.scaler = preprocessing.MinMaxScaler() self.scaler.fit(self.bounds) if dists is None: dists = [ot.Uniform(float(self.bounds[0][i]), float(self.bounds[1][i])) for i in range(self.dim)] else: dists = bat.space.dists_to_ot(dists) if discrete is not None: # Creating uniform discrete distribution for OT disc_list = [[i] for i in range(int(self.bounds[0, discrete]), int(self.bounds[1, discrete] + 1))] disc_dist = ot.UserDefined(disc_list) dists.pop(discrete) dists.insert(discrete, disc_dist) # Join distribution self.distribution = ot.ComposedDistribution(dists) if self.kind == 'halton': self.sequence_type = ot.LowDiscrepancyExperiment(ot.HaltonSequence(), self.distribution, self.n_samples) elif self.kind == 'sobol': self.sequence_type = ot.LowDiscrepancyExperiment(ot.SobolSequence(), self.distribution, self.n_samples) elif self.kind == 'faure': self.sequence_type = ot.LowDiscrepancyExperiment(ot.FaureSequence(), self.distribution, self.n_samples) elif (self.kind == 'lhs') or (self.kind == 'lhsc'): self.sequence_type = ot.LHSExperiment(self.distribution, self.n_samples) elif self.kind == 'olhs': lhs = ot.LHSExperiment(self.distribution, self.n_samples) self.sequence_type = ot.SimulatedAnnealingLHS(lhs, ot.GeometricProfile(), ot.SpaceFillingC2()) elif self.kind == 'saltelli': # Only relevant for computation of Sobol' indices size = self.n_samples // (2 * self.dim + 2) # N(2*dim + 2) self.sequence_type = ot.SobolIndicesExperiment(self.distribution, size, True).generate()
# %% import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) # %% # To create the first marginal of the distribution, we select a univariate discrete distribution. Some of them, like the `Bernoulli` or `Geometric` distributions, are implemented in the library as classes. In this example however, we pick the `UserDefined` distribution that assigns equal weights to the values -2, -1, 1 and 2. # %% sample = ot.Sample([[-2.], [-1.], [1.], [2.]]) sample # %% X0 = ot.UserDefined(sample) # %% # For the second marginal, we pick a Gaussian distribution. # %% X1 = ot.Normal() # %% # Create the multivariate distribution from its marginals and an independent copula. # %% distribution = ot.ComposedDistribution([X0, X1]) # %% # Create the design.
binomial = ot.Binomial(10, 0.25) distributionCollection.add(binomial) discreteDistributionCollection.add(binomial) zipf = ot.ZipfMandelbrot(20, 5.25, 2.5) distributionCollection.add(zipf) discreteDistributionCollection.add(zipf) poisson = ot.Poisson(5.0) distributionCollection.add(poisson) discreteDistributionCollection.add(poisson) x = [[1.0], [2.0], [3.0]] p = [0.3, 0.2, 0.5] userdefined = ot.UserDefined(x, p) distributionCollection.add(userdefined) discreteDistributionCollection.add(userdefined) size = 100 # Number of continuous distributions continuousDistributionNumber = continuousDistributionCollection.getSize() # Number of discrete distributions discreteDistributionNumber = discreteDistributionCollection.getSize() # Number of distributions distributionNumber = continuousDistributionNumber + \ discreteDistributionNumber # We create a collection of Sample of size "size" and of # dimension 1 (scalar values) : the collection has distributionNumber
#! /usr/bin/env python import openturns as ot ot.TESTPREAMBLE() # Instantiate one distribution object x = [[1.0], [2.0], [3.0], [3.0]] p = [0.3, 0.1, 0.6, 0.6] distribution = ot.UserDefined(x, p) print("Distribution ", repr(distribution)) print("Distribution ", distribution) # Is this distribution elliptical ? print("Elliptical = ", distribution.isElliptical()) # Is this distribution continuous ? print("Continuous = ", distribution.isContinuous()) # Has this distribution an independent copula ? print("Has independent copula = ", distribution.hasIndependentCopula()) # Test for realization of distribution oneRealization = distribution.getRealization() print("oneRealization=", repr(oneRealization)) # Test for sampling size = 10 oneSample = distribution.getSample(size) print("oneSample=Ok", repr(oneSample))
ce3 = 8*x[2]*(x[2]**2-x[1])-2*(1-x[2])+4*(x[2]-x[3])**2 + \ x[1]**2-x[0]+x[3]-x[4]**2+x[0]**2+x[4]-x[5]**2 ce4 = 8*x[3]*(x[3]**2-x[2])-2*(1-x[3])+4*(x[3]-x[4])**2 + \ x[2]**2-x[1]+x[4]-x[5]**2+x[1]**2+x[5]-x[0] ci1 = 8*x[4]*(x[4]**2-x[3])-2*(1-x[4])+4*(x[4]-x[5])**2 + \ x[3]**2-x[2]+x[5]+x[2]**2-x[1] ci2 = -(8 * x[5] * (x[5]**2 - x[4]) - 2 * (1 - x[5]) + x[4]**2 - x[3] + x[3]**2 - x[4]) return [-ce1, -ce2, -ce3, -ce4, -ci1, -ci2] f = ot.PythonFunction(6, 1, minlp_obj) bounds = ot.Interval([-5.0] * 6, [5.0] * 6) ineq = ot.PythonFunction(6, 6, minlp_cstr) pop0 = ot.ComposedDistribution([ot.Uniform(-5.0, 5.0)] * 4 + [ot.UserDefined([[i - 5] for i in range(11)])] * 2).getSample(100) problem = ot.OptimizationProblem(f) problem.setBounds(bounds) problem.setInequalityConstraint(ineq) problem.setVariablesType([ot.OptimizationProblemImplementation.CONTINUOUS] * 4 + [ot.OptimizationProblemImplementation.INTEGER] * 2) for name in ["gaco", "ihs", "sga"]: algo = ot.Pagmo(problem, name, pop0) algo.setBlockSize(8) algo.run() result = algo.getResult() x = result.getOptimalPoint() y = result.getOptimalValue() print(name, x, y)
#! /usr/bin/env python from __future__ import print_function import openturns as ot ot.TESTPREAMBLE() # Instanciate one distribution object x = [[1.0], [2.0], [3.0], [3.0]] p = [0.3, 0.1, 0.6, 0.6] distribution = ot.UserDefined(x, p) 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)) # Test for sampling size = 10 oneSample = distribution.getSample(size) print("oneSample=Ok", repr(oneSample)) # Define a point point = ot.Point(distribution.getDimension(), 2.0)
# %% # Create a discrete mixture # ------------------------- # # In this paragraph we build the distribution of the value of the sum of 20 dice rolls. # # .. math:: # Y = \sum_{i=1}^{20} X_i # # where :math:`X_i \sim U(1,2,3,4,5,6)` # # %% # We create the distribution associated to the dice roll : X = ot.UserDefined([[i] for i in range(1, 7)]) # %% # Let's roll the dice a few times ! sample = X.getSample(10) print(sample) # %% N = 20 # %% # We create a collection of identically distributed Xi : coll = [X] * N # %% # We create the weights and an affine combination :
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if (ot.UserDefined().__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.UserDefined().__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.UserDefined() 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))
# %% p1 + p2 # %% # Draw the survival of a sample # ----------------------------- # %% # In order to draw the empirical functions of a `Sample`, we use the `UserDefined` class. # # * The `drawCDF` method plots the CDF. # * The `drawSurvivalFunction` method plots the survival function. # %% userdefined = ot.UserDefined(sample) graph = userdefined.drawCDF() graph.setTitle("CDF of a sample") view = viewer.View(graph) # graph # %% graph = userdefined.drawSurvivalFunction() graph.setTitle("Empirical survival function of a sample") view = viewer.View(graph) # graph # %% # As previously, the `drawSurvivalFunction` method of a distribution has an option to set the X axis in logarithmic scale. # %%
# Create the output random vector Y=model(X) output = ot.CompositeRandomVector(model, inputVector) # %% # Quantile level alpha = 0.95 # Confidence level of the estimation beta = 0.90 # %% # Get a sample of the variable N = 10**4 sample = output.getSample(N) graph = ot.UserDefined(sample).drawCDF() view = viewer.View(graph) # %% # Empirical Quantile Estimator empiricalQuantile = sample.computeQuantile(alpha) # Get the indices of the confidence interval bounds aAlpha = ot.Normal(1).computeQuantile((1.0+beta)/2.0)[0] min_i = int(N*alpha - aAlpha*m.sqrt(N*alpha*(1.0-alpha))) max_i = int(N*alpha + aAlpha*m.sqrt(N*alpha*(1.0-alpha))) #print(min_i, max_i) # Get the sorted sample sortedSample = sample.sort()
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) elif ot.LogNormal().__class__.__name__ == 'TruncatedDistribution': distribution = ot.TruncatedDistribution(ot.Normal(2.0, 1.5), 1.0, 4.0) elif ot.LogNormal().__class__.__name__ == 'UserDefined': distribution = ot.UserDefined([[0.0], [1.0], [2.0]], [0.2, 0.7, 0.1]) elif ot.LogNormal().__class__.__name__ == 'ZipfMandelbrot': distribution = ot.ZipfMandelbrot(10, 2.5, 0.3) else: distribution = ot.LogNormal() dimension = distribution.getDimension() title = str(distribution)[:100].split('\n')[0] if dimension == 1: distribution.setDescription(['$x$']) pdf_graph = distribution.drawPDF() cdf_graph = distribution.drawCDF() fig = plt.figure(figsize=(10, 4)) 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)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View if ot.UserDefined().__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.UserDefined().__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.UserDefined().__class__.__name__ == 'Histogram': distribution = ot.Histogram([-1.0, 0.5, 1.0, 2.0], [0.45, 0.4, 0.15]) else: distribution = ot.UserDefined() 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()