def _exec(self, Lambda): Y_lambda = ot.BoxCoxTransform(Lambda)(self.Y_i_) algo = ot.LinearLeastSquares(self.a_i_, Y_lambda) algo.run() beta0 = algo.getConstant()[0] beta1 = algo.getLinear()[0, 0] sigma2 = (self.N_ - 1.0) / (self.N_ - 2.0) * (Y_lambda - (self.a_i_ * [beta1] + [beta0])).computeVariance()[0] return [-0.5 * self.N_ * m.log(sigma2) + (Lambda[0] - 1) * self.sumLogY_i]
def build(self, dataX, dataY): logLikelihood = ot.NumericalMathFunction(ReducedLogLikelihood(dataX, dataY)) xlb = np.linspace(self.lambdaMin_,self.lambdaMax_,num=500) lambdax = [logLikelihood([x])[0] for x in xlb] algo = ot.TNC(logLikelihood) algo.setStartingPoint([xlb[np.array(lambdax).argmax()]]) algo.setBoundConstraints(ot.Interval(self.lambdaMin_, self.lambdaMax_)) algo.setOptimizationProblem(ot.BoundConstrainedAlgorithmImplementationResult.MAXIMIZATION) algo.run() optimalLambda = algo.getResult().getOptimizer()[0] # graph optimalLogLikelihood = algo.getResult().getOptimalValue() graph = logLikelihood.draw(0.01 * optimalLambda, 10.0 * optimalLambda) c = ot.Cloud([[optimalLambda, optimalLogLikelihood]]) c.setColor("red") c.setPointStyle("circle") graph.add(c) return ot.BoxCoxTransform([optimalLambda]), graph
def _run(self, inputSample, outputSample, detection, noiseThres, saturationThres, boxCox, censored): """ Run common preliminary analysis to all methods to build POD. """ #################### Filter censored data ############################## if censored: # Filter censored data inputSample, inputSampleNoise, inputSampleSat, signals = \ DataHandling.filterCensoredData(inputSample, outputSample, noiseThres, saturationThres) else: inputSample, signals = inputSample, outputSample inputSampleNoise, inputSampleSat = None, None ###################### Box Cox transformation ########################## # Compute Box Cox if enabled if boxCox: # optimization required, get optimal lambda without graph self._lambdaBoxCox, self._graphBoxCox = computeBoxCox( inputSample, signals) # Transformation of data boxCoxTransform = ot.BoxCoxTransform([self._lambdaBoxCox]) signals = boxCoxTransform(signals) if censored: if noiseThres is not None: noiseThres = boxCoxTransform([noiseThres])[0] if saturationThres is not None: saturationThres = boxCoxTransform([saturationThres])[0] detectionBoxCox = boxCoxTransform([detection])[0] else: detectionBoxCox = detection self._lambdaBoxCox = None boxCoxTransform = None return { 'inputSample': inputSample, 'signals': signals, 'detectionBoxCox': detectionBoxCox, 'boxCoxTransform': boxCoxTransform }
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View # Create a Box Cox transformation myLambda = ot.NumericalPoint([0.0, 0.1, 1.0, 1.5]) graph = ot.Graph() for i in range(myLambda.getDimension()): myBoxCox = ot.BoxCoxTransform(myLambda[i]) graph.add(myBoxCox.draw(0.1, 2.1)) graph.setColors(['red', 'blue', 'black', 'green']) graph.setLegends(['lambda = ' + str(lam) for lam in myLambda]) graph.setLegendPosition("bottomright") fig = plt.figure(figsize=(8, 4)) plt.suptitle("Box Cox transformations") axis = fig.add_subplot(111) axis.set_xlim(auto=True) View(graph, figure=fig, axes=[axis], add_legend=True)
def _run(self): """ Run the analysis : - Computes the Box Cox parameter if *boxCox* is True, - Computes the transformed signals if *boxCox* is True or a float, - Builds the univariate linear regression model on the data, - Computes the linear regression parameters for censored data if needed, - Computes the residuals, - Runs all hypothesis tests. """ #################### Filter censored data ############################## if self._censored: # Filter censored data # Returns: # defects in the non censored area # defectsNoise in the noisy area # defectsSat in the saturation area # signals in the non censored area # check if one the threshold is None defects, defectsNoise, defectsSat, signals = \ DataHandling.filterCensoredData(self._inputSample, self._outputSample, self._noiseThres, self._saturationThres) else: defects, signals = self._inputSample, self._outputSample defectsSize = defects.getSize() ###################### Box Cox transformation ########################## # Compute Box Cox if enabled if self._boxCox: if self._lambdaBoxCox is None: # optimization required, get optimal lambda and graph self._lambdaBoxCox, self._graphBoxCox = computeBoxCox( defects, signals) # Transformation of data boxCoxTransform = ot.BoxCoxTransform([self._lambdaBoxCox]) signals = boxCoxTransform(signals) if self._noiseThres is not None: noiseThres = boxCoxTransform([self._noiseThres])[0] else: noiseThres = self._noiseThres if self._saturationThres is not None: saturationThres = boxCoxTransform([self._saturationThres])[0] else: saturationThres = self._saturationThres else: noiseThres = self._noiseThres saturationThres = self._saturationThres ######################### Linear Regression model ###################### # Linear regression with statsmodels module # Create the X matrix : [1, inputSample] X = ot.NumericalSample(defectsSize, [1, 0]) X[:, 1] = defects self._algoLinear = OLS(np.array(signals), np.array(X)).fit() self._resultsUnc.intercept = self._algoLinear.params[0] self._resultsUnc.slope = self._algoLinear.params[1] # get standard error estimates (residuals standard deviation) self._resultsUnc.stderr = np.sqrt(self._algoLinear.scale) # get confidence interval at level 95% self._resultsUnc.confInt = self._algoLinear.conf_int(0.05) if self._censored: # define initial starting point for MLE optimization initialStartMLE = [ self._resultsUnc.intercept, self._resultsUnc.slope, self._resultsUnc.stderr ] # MLE optimization res = computeLinearParametersCensored(initialStartMLE, defects, defectsNoise, defectsSat, signals, noiseThres, saturationThres) self._resultsCens.intercept = res[0] self._resultsCens.slope = res[1] self._resultsCens.stderr = res[2] ############################ Residuals ################################# # get residuals from algoLinear self._resultsUnc.residuals = ot.NumericalSample( np.vstack(self._algoLinear.resid)) # compute residuals distribution self._resultsUnc.resDist = self._resDistFact.build( self._resultsUnc.residuals) if self._censored: # create linear model function for censored case def CensLinModel(x): return self._resultsCens.intercept + self._resultsCens.slope * x # compute the residuals for the censored case. self._resultsCens.fittedSignals = CensLinModel(defects) self._resultsCens.residuals = signals - self._resultsCens.fittedSignals # compute residuals distribution. self._resultsCens.resDist = self._resDistFact.build( self._resultsCens.residuals) ########################## Compute tests ############################### self._resultsUnc.testResults = \ self._computeTests(defects, signals, self._resultsUnc.residuals, self._resultsUnc.resDist) if self._censored: self._resultsCens.testResults = \ self._computeTests(defects, signals, self._resultsCens.residuals, self._resultsCens.resDist) ################ Build the result lists to be printed ################## self._buildPrintResults()
def _computeLinearModel(inputSample, outputSample, detection, noiseThres, saturationThres, boxCox, censored): """ Run filerCensoredData and build the linear regression model. It is defined as a simple function because it is also needed in a loop for the bootstrap based POD. """ #################### Filter censored data ############################## if censored: # Filter censored data defects, defectsNoise, defectsSat, signals = \ DataHandling.filterCensoredData(inputSample, outputSample, noiseThres, saturationThres) else: defects, signals = inputSample, outputSample defectsSize = defects.getSize() ###################### Box Cox transformation ########################## # Compute Box Cox if enabled if boxCox: if signals.getMin()[0] < 0: shift = -signals.getMin()[0] + 100 else: shift = 0. # optimization required, get optimal lambda without graph lambdaBoxCox, graphBoxCox = computeBoxCox(defects, signals, shift) # Transformation of data boxCoxTransform = ot.BoxCoxTransform([lambdaBoxCox]) signals = boxCoxTransform(signals + shift) if censored: if noiseThres is not None: noiseThres = boxCoxTransform([noiseThres + shift])[0] if saturationThres is not None: saturationThres = boxCoxTransform([saturationThres + shift])[0] detectionBoxCox = boxCoxTransform([detection + shift])[0] else: detectionBoxCox = detection lambdaBoxCox = None graphBoxCox = None ######################### Linear Regression model ###################### # Linear regression with statsmodels module # Create the X matrix : [1, inputSample] X = ot.Sample(defectsSize, [1, 0]) X[:, 1] = defects algoLinear = OLS(np.array(signals), np.array(X)).fit() intercept = algoLinear.params[0] slope = algoLinear.params[1] # get standard error estimates (residuals standard deviation) stderr = np.sqrt(algoLinear.scale) # get residuals from algoLinear residuals = ot.Sample(np.vstack(algoLinear.resid)) if censored: # define initial starting point for MLE optimization initialStartMLE = [intercept, slope, stderr] # MLE optimization res = computeLinearParametersCensored(initialStartMLE, defects, defectsNoise, defectsSat, signals, noiseThres, saturationThres) intercept = res[0] slope = res[1] stderr = res[2] residuals = signals - (intercept + slope * defects) return { 'defects': defects, 'signals': signals, 'intercept': intercept, 'slope': slope, 'stderr': stderr, 'residuals': residuals, 'detection': detectionBoxCox, 'lambdaBoxCox': lambdaBoxCox, 'graphBoxCox': graphBoxCox }
import openturns as ot from matplotlib import pyplot as plt from openturns.viewer import View # Create a Box Cox transformation lambdas = [0.0, 0.1, 1.0, 1.5] graph = ot.Graph("Box Cox transformations", 'x', 'y', True) for i in range(len(lambdas)): boxCoxT = ot.BoxCoxTransform(lambdas[i]) graph.add(boxCoxT.draw(0.1, 2.1)) graph.setColors(['red', 'blue', 'black', 'green']) graph.setLegends(['lambda = ' + str(lam) for lam in lambdas]) graph.setLegendPosition("bottomright") fig = plt.figure(figsize=(8, 4)) axis = fig.add_subplot(111) axis.set_xlim(auto=True) View(graph, figure=fig, axes=[axis], add_legend=True)