def _PODbootstrapModelCl(self): class buildPODModel(): def __init__(self, inputSample, outputSample, detection, noiseThres, saturationThres, resDistFact, boxCox, censored): results = _computeLinearModel(inputSample, outputSample, detection, noiseThres, saturationThres, boxCox, censored) self.intercept = results['intercept'] self.slope = results['slope'] self.residuals = results['residuals'] self.detectionBoxCox = results['detection'] self.resDist = resDistFact.build(self.residuals) def PODmodel(self, x): defectThres = self.detectionBoxCox - (self.intercept + self.slope * x) return self.resDist.computeComplementaryCDF(defectThres) data = ot.Sample(self._size, 2) data[:, 0] = self._inputSample data[:, 1] = self._outputSample # bootstrap of the data bootstrapExp = ot.BootstrapExperiment(data) PODcoll = [] for i in range(self._simulationSize): # generate a sample with replacement within data of the same size bootstrapData = bootstrapExp.generate() # compute the linear models model = buildPODModel(bootstrapData[:, 0], bootstrapData[:, 1], self._detection, self._noiseThres, self._saturationThres, self._resDistFact, self._boxCox, self._censored) PODcoll.append(model.PODmodel) if self._verbose: updateProgress(i, self._simulationSize, 'Computing POD (bootstrap)') return PODcoll
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View ot.RandomGenerator.SetSeed(0) # Generate sample with the given plane size = 20 dim = 2 refSample = ot.Sample(size, dim) for i in range(size): p = ot.Point(dim) for j in range(dim): p[j] = i + j refSample[i] = p myPlane = ot.BootstrapExperiment(refSample) sample = myPlane.generate() # Create an empty graph graph = ot.Graph("Bootstrap experiment", "x1", "x2", True, "") # Create the cloud cloud = ot.Cloud(sample, "blue", "fsquare", "") # Then, draw it graph.add(cloud) fig = plt.figure(figsize=(4, 4)) axis = fig.add_subplot(111) axis.set_xlim(auto=True) View(graph, figure=fig, axes=[axis], add_legend=False)
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View ot.RandomGenerator.SetSeed(0) # Generate sample with the given plane size = 20 dim = 2 refSample = ot.Sample(size, dim) for i in range(size): p = ot.Point(dim) for j in range(dim): p[j] = i + j refSample[i] = p experiment = ot.BootstrapExperiment(refSample) sample = experiment.generate() # Create an empty graph graph = ot.Graph("Bootstrap experiment", "x1", "x2", True, "") # Create the cloud cloud = ot.Cloud(sample, "blue", "fsquare", "") # Then, draw it graph.add(cloud) fig = plt.figure(figsize=(4, 4)) axis = fig.add_subplot(111) axis.set_xlim(auto=True) View(graph, figure=fig, axes=[axis], add_legend=False)
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')