def getSensitivityAnalysisResultsMetamodel(self, metamodel, doe_sobol, size, N_LHS, N_KL_Young, N_KL_Diam, R2): dim = doe_sobol.getDimension() try : meta_response = sequentialFunctionEvaulation(metamodel.__kriging_metamodel__, doe_sobol) except Exception as e: print('Caugt exception:\n',e) print('filling response with zeros...') meta_response = ot.Sample(np.zeros((doe_sobol.getSize(),1))) sensitivity_analysis = klfs.SobolKarhunenLoeveFieldSensitivityAlgorithm() sensitivity_analysis.setDesign(doe_sobol, meta_response, size) sensitivity_analysis.setEstimator(ot.MartinezSensitivityAlgorithm()) FO_indices = sensitivity_analysis.getFirstOrderIndices()[0] conf_level = sensitivity_analysis.getFirstOrderIndicesInterval()[0] lb = conf_level.getLowerBound() ub = conf_level.getUpperBound() result_point = ot.PointWithDescription([('meta',1.),('N_LHS',N_LHS),('size',size),('kl_dimension',dim), ('N_KL_Young',N_KL_Young), ('N_KL_Diam',N_KL_Diam), ('R2',R2), ('SI_E',FO_indices[0][0]),('SI_E_lb',lb[0]),('SI_E_ub',ub[0]), ('SI_D',FO_indices[1][0]),('SI_D_lb',lb[1]),('SI_D_ub',ub[1]), ('SI_FP',FO_indices[2][0]),('SI_FP_lb',lb[2]),('SI_FP_ub',ub[2]), ('SI_FN',FO_indices[3][0]),('SI_FN_lb',lb[3]),('SI_FN_ub',ub[3])]) print('------------- RESULTS ------------') print('------------ META MODEL ----------') print('-----------SIZE LHS : {} ---------'.format(int(N_LHS))) print(result_point) return result_point
def run(self): """ Compute the Sobol indices with the chosen algorithm. """ # create the NumericalMathFunction which computes the POD for a given # realization and for all defect sizes. if self._podType == "kriging": self._PODaggr = ot.NumericalMathFunction( PODaggrKriging(self._POD, self._dim, self._defectSizes, self._detectionBoxCox)) elif self._podType == "chaos": self._PODaggr = ot.NumericalMathFunction( PODaggrChaos(self._POD, self._dim, self._defectSizes, self._detectionBoxCox, self._simulationSize)) if self._method == "Saltelli": self._sa = ot.SaltelliSensitivityAlgorithm(self._distribution, self._N, self._PODaggr, False) elif self._method == "Martinez": self._sa = ot.MartinezSensitivityAlgorithm(self._distribution, self._N, self._PODaggr, False) elif self._method == "Jansen": self._sa = ot.JansenSensitivityAlgorithm(self._distribution, self._N, self._PODaggr, False) elif self._method == "MauntzKucherenko": self._sa = ot.MauntzKucherenkoSensitivityAlgorithm( self._distribution, self._N, self._PODaggr, False)
def myMartinezSobolEstimator(distribution, size, model): inputDesign = ot.SobolIndicesExperiment(distribution, size, True).generate() outputDesign = model(inputDesign) sensitivityAnalysis = ot.MartinezSensitivityAlgorithm( inputDesign, outputDesign, size) return sensitivityAnalysis
def run(self): """ Compute the Sobol indices with the chosen algorithm. """ # create the Function which computes the POD for a given # realization and for all defect sizes. if self._podType == "kriging": self._PODaggr = ot.Function( PODaggrKriging(self._POD, self._dim, self._defectSizes, self._detectionBoxCox)) elif self._podType == "chaos": self._PODaggr = ot.Function( PODaggrChaos(self._POD, self._dim, self._defectSizes, self._detectionBoxCox, self._simulationSize)) input_design = ot.SobolIndicesExperiment(self._distribution, self._N, False).generate() output_design = self._PODaggr(input_design) # remove the output marginal when the variance is null because it causes # a failure. Must remove also the associated defect sizes. selected_marginal_index = [] for index_output in range(output_design.getDimension()): if output_design.getMarginal( index_output)[:self._N].computeCovariance()[0, 0] != 0: selected_marginal_index.append(index_output) if len(selected_marginal_index) != output_design.getDimension(): self.setDefectSizes(self._defectSizes[selected_marginal_index]) selected_output_design = np.array( output_design)[:, selected_marginal_index] logging.warning("Warning : some output variances are null. " + \ "Only the following defect sizes are taken into " + \ "account : {}".format(self._defectSizes)) else: selected_output_design = output_design if self._method == "Saltelli": self._sa = ot.SaltelliSensitivityAlgorithm(input_design, selected_output_design, self._N) elif self._method == "Martinez": self._sa = ot.MartinezSensitivityAlgorithm(input_design, selected_output_design, self._N) elif self._method == "Jansen": self._sa = ot.JansenSensitivityAlgorithm(input_design, selected_output_design, self._N) elif self._method == "MauntzKucherenko": self._sa = ot.MauntzKucherenkoSensitivityAlgorithm( input_design, selected_output_design, self._N) self._sa.setUseAsymptoticDistribution(True)
def getSensitivityAnalysisResults(self, sample_in, sample_out, size, N_KL_Young, N_KL_Diam): dim = sample_in.getDimension() # Dimensoin of the KL input vector sensitivity_analysis = klfs.SobolKarhunenLoeveFieldSensitivityAlgorithm() sensitivity_analysis.setDesign(sample_in, sample_out, size) sensitivity_analysis.setEstimator(ot.MartinezSensitivityAlgorithm()) FO_indices = sensitivity_analysis.getFirstOrderIndices()[0] conf_level = sensitivity_analysis.getFirstOrderIndicesInterval()[0] lb = conf_level.getLowerBound() ub = conf_level.getUpperBound() result_point = ot.PointWithDescription([('meta',0.),('N_LHS',-1.),('size',size),('kl_dimension',dim), ('N_KL_Young',N_KL_Young), ('N_KL_Diam',N_KL_Diam), ('R2',-1), ('SI_E',round(FO_indices[0][0],5)),('SI_E_lb',round(lb[0],5)),('SI_E_ub',round(ub[0],5)), ('SI_D',round(FO_indices[1][0],5)),('SI_D_lb',round(lb[1],5)),('SI_D_ub',round(ub[1],5)), ('SI_FP',round(FO_indices[2][0],5)),('SI_FP_lb',round(lb[2],5)),('SI_FP_ub',round(ub[2],5)), ('SI_FN',round(FO_indices[3][0],5)),('SI_FN_lb',round(lb[3],5)),('SI_FN_ub',round(ub[3],5))]) print('------------- RESULTS ------------') print('------------ REAL MODEL ----------') print('----------- SIZE DOE = {} -----------'.format(str(int(size)))) print(result_point) return result_point
alpha = (Zm - Zv)/L H = (Q/(Ks*B*alpha**0.5))**0.6 Zc = H + Zv S = Zc - Zd return [S] myFunction = ot.PythonFunction(4, 1, flooding) myParam = ot.GumbelAB(1013.0, 558.0) Q = ot.ParametrizedDistribution(myParam) Q = ot.TruncatedDistribution(Q, 0.0, ot.SpecFunc.MaxScalar) Ks = ot.Normal(30.0, 7.5) Ks = ot.TruncatedDistribution(Ks, 0.0, ot.SpecFunc.MaxScalar) Zv = ot.Uniform(49.0, 51.0) Zm = ot.Uniform(54.0, 56.0) inputX = ot.ComposedDistribution([Q, Ks, Zv, Zm]) inputX.setDescription(["Q","Ks", "Zv", "Zm"]) size = 5000 computeSO = True inputDesign = ot.SobolIndicesExperiment(inputX, size, computeSO).generate() outputDesign = myFunction(inputDesign) sensitivityAnalysis = ot.MartinezSensitivityAlgorithm(inputDesign, outputDesign, size) graph = sensitivityAnalysis.draw() fig = plt.figure(figsize=(8, 4)) plt.suptitle(graph.getTitle()) axis = fig.add_subplot(111) axis.set_xlim(auto=True) View(graph, figure=fig, axes=[axis], add_legend=True)
# Case 1 : Estimation of sensitivity using estimator and no bootstrap for method in methods: sensitivity_algorithm = eval( 'ot.' + method + "SensitivityAlgorithm(inputDesign, outputDesign, size)") # Get first order indices fo = sensitivity_algorithm.getFirstOrderIndices() print("Method of evaluation=", method) print("First order indices = ", fo) # Get total order indices to = sensitivity_algorithm.getTotalOrderIndices() print("Total order indices = ", to) # Case 2 : Estimation of sensitivity using Martinez estimator and bootstrap nr_bootstrap = 100 confidence_level = 0.95 sensitivity_algorithm = ot.MartinezSensitivityAlgorithm( inputDesign, outputDesign, size) sensitivity_algorithm.setBootstrapSize(nr_bootstrap) sensitivity_algorithm.setBootstrapConfidenceLevel(confidence_level) # Get first order indices fo = sensitivity_algorithm.getFirstOrderIndices() to = sensitivity_algorithm.getTotalOrderIndices() print("Martinez method : indices") print("First order indices = ", fo) print("Total order indices = ", to) # Get the confidence interval thanks to Bootstrap interval_fo = sensitivity_algorithm.getFirstOrderIndicesInterval() interval_to = sensitivity_algorithm.getTotalOrderIndicesInterval() print("Martinez method : bootstrap intervals") print("First order indices interval = ", interval_fo) print("Total order indices interval = ", interval_to) # In the case of Martinez, if output is Gaussian, we may use the Fisher