Ejemplo n.º 1
0
    def computeDetectionSize(self, probabilityLevel, confidenceLevel=None):
        defectMin = self._defects.getMin()[0]
        defectMax = self._defects.getMax()[0]
        # compute 'a90'
        model = self._buildModel(1. - probabilityLevel)
        try:
            detectionSize = ot.NumericalPointWithDescription(1, ot.Brent().solve(
                                        model, self._detectionBoxCox, defectMin, defectMax))
        except:
            raise Exception('The POD model does not contain, for the given ' + \
                             'defect interval, the wanted probability level.')
        description = ['a'+str(int(probabilityLevel*100))]

        # compute 'a90_95'
        if confidenceLevel is not None:
            modelCl = self.getPODCLModel(confidenceLevel)
            if not (modelCl([defectMin])[0] <= probabilityLevel <= modelCl([defectMax])[0]):
                raise Exception('The POD model at the confidence level does not '+\
                                'contain, for the given defect interval, the '+\
                                'wanted probability level.')
            detectionSize.add(ot.Brent().solve(modelCl,
                                               probabilityLevel,
                                               defectMin, defectMax))
            description.append('a'+str(int(probabilityLevel*100))+'/'\
                                                +str(int(confidenceLevel*100)))
        # add description to the NumericalPoint
        detectionSize.setDescription(description)
        return detectionSize
Ejemplo n.º 2
0
    def _computeDetectionSize(self,
                              model,
                              modelCl=None,
                              probabilityLevel=None,
                              confidenceLevel=None,
                              defectMin=None,
                              defectMax=None):
        """
        Compute the detection size for a given probability level.

        Parameters
        ----------
        probabilityLevel : float
            The probability level for which the defect size is computed.
        confidenceLevel : float
            The confidence level associated to the given probability level the
            defect size is computed. Default is None.

        Returns
        -------
        result : collection of :py:class:`openturns.NumericalPointWithDescription`
            A NumericalPointWithDescription containing the detection size
            computed at the given probability level and confidence level if provided.
        """

        if defectMin is None:
            defectMin = self._inputSample.getMin()[0]
        if defectMax is None:
            defectMax = self._inputSample.getMax()[0]

        # compute 'a90'
        if not (model([defectMin])[0] <= probabilityLevel <= model([defectMax
                                                                    ])[0]):
            raise Exception('The POD model does not contain, for the given ' + \
                             'defect interval, the wanted probability level.')
        detectionSize = ot.NumericalPointWithDescription(
            1,
            ot.Brent().solve(model, probabilityLevel, defectMin, defectMax))
        description = ['a' + str(int(probabilityLevel * 100))]

        # compute 'a90_95'
        if confidenceLevel is not None:
            if not (modelCl([defectMin])[0] <= probabilityLevel <= modelCl(
                [defectMax])[0]):
                raise Exception('The POD model at the confidence level does not '+\
                                'contain, for the given defect interval, the '+\
                                'wanted probability level.')
            detectionSize.add(ot.Brent().solve(modelCl, probabilityLevel,
                                               defectMin, defectMax))
            description.append('a'+str(int(probabilityLevel*100))+'/'\
                                                +str(int(confidenceLevel*100)))
        # add description to the NumericalPoint
        detectionSize.setDescription(description)
        return detectionSize
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
# We load the model giving the displacement at the end of the beam :
model = cb.model

# %%
# We create the event whose probability we want to estimate.

# %%
vect = ot.RandomVector(distribution)
G = ot.CompositeRandomVector(model, vect)
event = ot.ThresholdEvent(G, ot.Greater(), 0.30)

# %%
# Root finding algorithm.

# %%
solver = ot.Brent()
rootStrategy = ot.MediumSafe(solver)

# %%
# Direction sampling algorithm.

# %%
samplingStrategy = ot.OrthogonalDirection()

# %%
# Create a simulation algorithm.

# %%
algo = ot.DirectionalSampling(event, rootStrategy, samplingStrategy)
algo.setMaximumCoefficientOfVariation(0.1)
algo.setMaximumOuterSampling(40000)
Ejemplo n.º 5
0
    absEps = solver.getAbsoluteError()

    def test_f1(x):
        y = exp(x[0]) - 1.9151695967140057e-174
        return [y]

    f1 = ot.PythonFunction(1, 1, test_f1)
    root = solver.solve(f1, 0.0, -450.0, -350.0)
    ott.assert_almost_equal(root, -400.0, relEps, absEps)

    def test_f2(x):
        y = exp(x[0]) - 5.221469689764144e+173
        return [y]

    f2 = ot.PythonFunction(1, 1, test_f2)
    root = solver.solve(f2, 0.0, 350.0, 450.0)
    ott.assert_almost_equal(root, 400.0, relEps, absEps)


# 1) Bisection
algo = ot.Bisection()
test_solver(algo)

# 2) Brent
algo = ot.Brent()
test_solver(algo)

# 3) Secant
algo = ot.Secant()
test_solver(algo)
Ejemplo n.º 6
0
IS_convergence_graph = IS_algorithm.drawProbabilityConvergence(
    confidence_level)
_ = View(IS_convergence_graph)

# # Directional sampling

# In[36]:

g.clearHistory()

# In[37]:

root_strategy = ot.RiskyAndFast(
)  # Alternatives : ot.SafeAndSlow(), ot.MediumSafe(), ot.RiskyAndFast()
root_strategy.setSolver(
    ot.Brent())  # Alternatives : ot.Bisection(), ot.Secant(), ot.Brent()

# In[38]:

sampling_strategy = ot.RandomDirection(
)  # Alternatives : ot.RandomDirection(), ot.OrthogonalDirection()
sampling_strategy.setDimension(X_distribution.getDimension())

# In[39]:

ot.RandomGenerator.SetSeed(0)

# In[40]:

DS_algorithm = ot.DirectionalSampling(event)
DS_algorithm.setMaximumCoefficientOfVariation(.1)
Ejemplo n.º 7
0
    def run(self):
        """
        Build the POD models.

        Notes
        -----
        This method build the quantile regression model. First the censored data
        are filtered if needed. The Box Cox transformation is performed if it is
        enabled. Then it builds the POD model for given data and computes using
        bootstrap all the defects quantile needed to build the POD model at the
        confidence level.
        """

        # Run the preliminary run of the POD class
        result = self._run(self._inputSample, self._outputSample, self._detection,
                           self._noiseThres, self._saturationThres, self._boxCox,
                           self._censored)

        # get some results
        self._defects = result['inputSample']
        self._signals = result['signals']
        self._detectionBoxCox = result['detectionBoxCox']

        defectsSize = self._defects.getSize()

        # create the quantile regression object
        X = ot.NumericalSample(defectsSize, [1, 0])
        X[:, 1] = self._defects
        self._algoQuantReg = QuantReg(np.array(self._signals), np.array(X))

        # Compute the defect quantile
        defectMax = self._defects.getMax()[0]
        defectList = []
        for probLevel in self._quantile:
            # fit the quantile regression and return the NMF
            model = self._buildModel(1. - probLevel)
            # Solve the model == detectionBoxCox with defects 
            # boundaries = [0, defectMax]
            defectList.append(ot.Brent().solve(model, self._detectionBoxCox,
                                               0, defectMax))
        # create support of the interpolating function including
        # point (0, 0) and point (defectMax, max(quantile))
        xvalue = np.hstack([0, defectList, defectMax])
        yvalue = np.hstack([0., self._quantile, self._quantile.max()])
        interpModel = interp1d(xvalue, yvalue, kind='linear')
        self._PODmodel = ot.PythonFunction(1, 1, interpModel)


        ############ Confidence interval with bootstrap ########################
        # Compute a NsimulationSize defect sizes for all quantiles
        data = ot.NumericalSample(self._size, 2)
        data[:, 0] = self._inputSample
        data[:, 1] = self._outputSample
        # bootstrap of the data
        bootstrapExp = ot.BootstrapExperiment(data)
        # create a numerical sample which contains for all simulations the 
        # defect quantile value. The goal is to compute the QuantilePerComponent
        # of the simulation for each defect quantile (columns)
        self._defectsPerQuantile = ot.NumericalSample(self._simulationSize, self._quantile.size)
        for i in range(self._simulationSize):
            # generate a sample with replacement within data of the same size
            bootstrapData = bootstrapExp.generate()
            # run the preliminary analysis : censore checking and box cox
            result = self._run(bootstrapData[:,0], bootstrapData[:,1], self._detection,
                               self._noiseThres, self._saturationThres,
                               self._boxCox, self._censored)

            # get some results
            defects = result['inputSample']
            signals = result['signals']
            detectionBoxCox = result['detectionBoxCox']
            defectsSize = defects.getSize()

            # new quantile regression algorithm
            X = ot.NumericalSample(defectsSize, [1, 0])
            X[:, 1] = defects
            algoQuantReg = QuantReg(np.array(signals), np.array(X))

            # compute the quantile defects
            defectMax = defects.getMax()[0]
            defectList = []
            for probLevel in self._quantile:
                fit = algoQuantReg.fit(1. - probLevel, max_iter=300, p_tol=1e-2)
                def model(x):
                    X = ot.NumericalPoint([1, x[0]])
                    return ot.NumericalPoint(fit.predict(X))
                model = ot.PythonFunction(1, 1, model)
                # Solve the model == detectionBoxCox with defects 
                # boundaries = [-infinity, defectMax] : it allows negative defects
                # when for small prob level, there is no intersection with
                # the detection threshold for positive defects
                defectList.append(ot.Brent().solve(model, detectionBoxCox,
                                                   -ot.SpecFunc.MaxNumericalScalar,
                                                   defectMax))
            # add the quantile in the numerical sample as the ith simulation
            self._defectsPerQuantile[i, :] = defectList
            if self._verbose:
                updateProgress(i, self._simulationSize, 'Computing defect quantile')