def mySaltelliSobolEstimator(distribution, size, model):
    inputDesign = ot.SobolIndicesExperiment(distribution, size,
                                            True).generate()
    outputDesign = model(inputDesign)
    sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm(
        inputDesign, outputDesign, size)
    return sensitivityAnalysis
Exemple #2
0
    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 mySaltelliSamplingMethodLHSIndicesExperiment(distribution, size, model):
    ot.ResourceMap.SetAsString('SobolIndicesExperiment-SamplingMethod', 'LHS')
    inputDesign = ot.SobolIndicesExperiment(distribution, size,
                                            True).generate()
    outputDesign = model(inputDesign)
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(
        inputDesign, outputDesign, size)
    return sensitivity_algorithm
Exemple #4
0
    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 myOptimalLHSExperiment(distribution, size, model):
    # Build standard randomized LHS algorithm
    lhs = ot.LHSExperiment(distribution, size)
    #lhs.setAlwaysShuffle(False) # randomized
    # Defining space fillings
    spaceFilling = ot.SpaceFillingC2()
    # RandomBruteForce MonteCarlo with N designs (LHS with C2 optimization)
    N = 10000
    optimalLHSAlgorithm = ot.MonteCarloLHS(lhs, N, spaceFilling)
    experiment = optimalLHSAlgorithm.getLHS()
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
    return sensitivity_algorithm
    def test_GaussianSum2(self):
        mu = [0.0] * 4
        sigma = [1.0, 2.0, 3.0, 4.0]
        a = [0.0, 1.0, 1.0, 1.0, 1.0]
        problem = otb.GaussianSumSensitivity(a, mu, sigma)
        distribution = problem.getInputDistribution()
        model = problem.getFunction()

        # Create X/Y data
        ot.RandomGenerator.SetSeed(0)
        size = 10000
        inputDesign = ot.SobolIndicesExperiment(distribution, size,
                                                True).generate()
        outputDesign = model(inputDesign)

        # Compute first order indices using the Saltelli estimator
        sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm(
            inputDesign, outputDesign, size)
        computed_first_order = sensitivityAnalysis.getFirstOrderIndices()
        computed_total_order = sensitivityAnalysis.getTotalOrderIndices()

        # Exact first and total order
        exact_first_order = problem.getFirstOrderIndices()
        exact_total_order = problem.getTotalOrderIndices()

        # Check exact results
        Sexact = [1.0 / 30.0, 4.0 / 30, 9.0 / 30, 16.0 / 30]
        Texact = [1.0 / 30.0, 4.0 / 30, 9.0 / 30, 16.0 / 30]
        np.testing.assert_allclose(Sexact, exact_first_order)
        np.testing.assert_allclose(Texact, exact_total_order)

        # Compare with exact results
        print("Sample size : ", size)
        atol = 5.0 / np.sqrt(size)
        print("Absolute Tolerance = ", atol)
        # First order
        # Compute absolute error (the LRE cannot be computed,
        # because S can be zero)
        print("Computed first order = ", computed_first_order)
        print("Exact first order = ", exact_first_order)
        np.testing.assert_allclose(computed_first_order,
                                   exact_first_order,
                                   atol=atol)
        # Total order
        print("Computed total order = ", computed_total_order)
        print("Exact total order = ", exact_total_order)
        np.testing.assert_allclose(computed_total_order,
                                   exact_total_order,
                                   atol=atol)
Exemple #7
0
 def __init__(self,
              inputDesign=None,
              outputDesign=None,
              N=0,
              estimator=ot.SaltelliSensitivityAlgorithm(),
              computeSecondOrder=False):
     self.inputDesign = inputDesign
     self.outputDesign = atLeastList(outputDesign)
     self.N = int(N)
     self.size = None
     self.__nOutputs__ = 0
     self.computeSecondOrder = computeSecondOrder
     self.__visibility__ = True
     self.__shadowedId__ = 0
     self.__name__ = 'Unnamed'
     self.inputDescription = None
     self.__nSobolIndices__ = None
     self.__Meshes__ = list()
     self.__Classes__ = list()
     self.__BootstrapSize__ = None
     self.flatOutputDesign = list()
     self.__centeredOutputDesign__ = list()
     self.__results__ = list()
     self.estimator = estimator
     if len(self.outputDesign) > 0 and self.outputDesign[0] is not None:
         assert all_same([
             len(self.outputDesign[i])
             for i in range(len(self.outputDesign))
         ])
     if inputDesign is not None and outputDesign is not None:
         try:
             assert isinstance(
                 inputDesign,
                 ot.Sample), 'The input design can only be a Sample'
             assert any([
                 isinstance(outputDesign[i], (ot.Sample, ot.ProcessSample))
                 for i in range(len(outputDesign))
             ])
         except AssertionError:
             print('\n\n\n\n\n\n\nThe error\n\n\n\n\n\n\n')
             return None
     self.__setDefaultState__()
Exemple #8
0
    def test_Ishigami(self):
        problem = otb.IshigamiSensitivity()
        print(problem)
        distribution = problem.getInputDistribution()
        model = problem.getFunction()

        # Create X/Y data
        ot.RandomGenerator.SetSeed(0)
        size = 10000
        inputDesign = ot.SobolIndicesExperiment(distribution, size,
                                                True).generate()
        outputDesign = model(inputDesign)

        # Compute first order indices using the Saltelli estimator
        sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm(
            inputDesign, outputDesign, size)
        computed_first_order = sensitivityAnalysis.getFirstOrderIndices()
        computed_total_order = sensitivityAnalysis.getTotalOrderIndices()

        # Exact first and total order
        exact_first_order = problem.getFirstOrderIndices()
        exact_total_order = problem.getTotalOrderIndices()

        # Compare with exact results
        print("Sample size : ", size)
        atol = 10.0 / np.sqrt(size)
        print("Absolute Tolerance = ", atol)
        # First order
        # Compute absolute error (the LRE cannot be computed,
        # because S can be zero)
        print("Computed first order = ", computed_first_order)
        print("Exact first order = ", exact_first_order)
        np.testing.assert_allclose(computed_first_order,
                                   exact_first_order,
                                   atol=atol)
        # Total order
        print("Computed total order = ", computed_total_order)
        print("Exact total order = ", exact_total_order)
        np.testing.assert_allclose(computed_total_order,
                                   exact_total_order,
                                   atol=atol)
def CostSobol(MyModel, p, m, lower, upper, distribution, indexNumber,
              indexChoice, NSobol, MINMAX):
    '''
    Return the associated sobol index to the measure recovered from the canonical moment sequences
    '''
    dim = len(lower)
    # We concatenate p per block of variable
    if len(m) == dim:
        pp = []
        t = 0
        for i in range(dim):
            pp.append(p[t:t + len(m[i]) + 1])
            t = t + len(m[i]) + 1
    else:
        print('error size of moment vector')

    if indexChoice == 1:
        P = list(
            QD_Algorithm(
                Affine_Transformation(lower[indexNumber], upper[indexNumber],
                                      m[indexNumber]))) + list(pp[indexNumber])
        Position, Weight = Canonical_to_Position([lower[indexNumber]],
                                                 [upper[indexNumber]], P)

        distribution[indexNumber] = ot.Mixture(
            [ot.Dirac(Position[i]) for i in range(len(Position))], Weight)
        composedDistribution = ot.ComposedDistribution(distribution)
        ot.RandomGenerator.SetSeed(0)
        inputDesign = ot.SobolIndicesExperiment(composedDistribution, NSobol,
                                                True).generate()
        outputDesign = MyModel(inputDesign)

        sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm(
            inputDesign, outputDesign, NSobol)
        firstOrder = sensitivityAnalysis.getFirstOrderIndices()
        return MINMAX * firstOrder[indexNumber]

    elif indexChoice == 0:
        t = 0
        P = [[]] * (dim - 1)
        Position = [[]] * (dim - 1)
        Weight = [[]] * (dim - 1)
        for i in range(dim):
            if i != indexNumber:
                P[t] = list(
                    QD_Algorithm(
                        Affine_Transformation(lower[i], upper[i],
                                              m[i]))) + list(pp[i])
                Position[t], Weight[t] = Canonical_to_Position([lower[i]],
                                                               [upper[i]],
                                                               P[t])
                distribution[i] = ot.Mixture([
                    ot.Dirac(Position[t][j]) for j in range(len(Position[t]))
                ], Weight[t])
                t += 1
        composedDistribution = ot.ComposedDistribution(distribution)
        ot.RandomGenerator.SetSeed(0)
        inputDesign = ot.SobolIndicesExperiment(composedDistribution, NSobol,
                                                True).generate()
        outputDesign = MyModel(inputDesign)

        sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm(
            inputDesign, outputDesign, NSobol)
        totalOrder = sensitivityAnalysis.getTotalOrderIndices()
        return MINMAX * totalOrder[indexNumber]
# 6. Estimate expectation with algorithm
algo = ot.ExpectationSimulationAlgorithm(Y)
algo.setMaximumOuterSampling(1000)
algo.setBlockSize(1)
algo.setCoefficientOfVariationCriterionType('NONE')
algo.run()
result = algo.getResult()
expectation = result.getExpectationEstimate()
print("Mean by ESA = %f " % expectation[0])
expectationDistribution = result.getExpectationDistribution()

View(expectationDistribution.drawPDF())
pl.savefig("MeanDistribution.pdf")

# Sensitivity analysis
estimator = ot.SaltelliSensitivityAlgorithm()
estimator.setUseAsymptoticDistribution(True)
algo = ot.SobolSimulationAlgorithm(X, g, estimator)
algo.setMaximumOuterSampling(25)  # number of iterations
algo.setBlockSize(100)  # size of Sobol experiment at each iteration
algo.setBatchSize(4)  # number of points evaluated simultaneously
algo.setIndexQuantileLevel(0.05)  # alpha
algo.setIndexQuantileEpsilon(1e-2)  # epsilon
algo.run()
result = algo.getResult()
fo = result.getFirstOrderIndicesEstimate()
print("First order:")
print(fo)
to = result.getTotalOrderIndicesEstimate()
print("Total order:")
print(to)
Exemple #11
0
        interval_to_asymptotic = sensitivity_algorithm.getTotalOrderIndicesInterval(
        )
        print("asymptotic intervals:")
        print("First order indices distribution = ",
              sensitivity_algorithm.getFirstOrderIndicesDistribution())
        print("Total order indices distribution = ",
              sensitivity_algorithm.getTotalOrderIndicesDistribution())
        print("First order indices interval = ", interval_fo_asymptotic)
        print("Total order indices interval = ", interval_to_asymptotic)

# with experiment
sequence = ot.SobolSequence(input_dimension)
experiment = ot.LowDiscrepancyExperiment(
    sequence,
    ot.ComposedDistribution([ot.Uniform(0.0, 1.0)] * input_dimension), size)
sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
print(sensitivity_algorithm.getFirstOrderIndices())

# multi variate model
model_aggregated = ot.SymbolicFunction(
    ['X1', 'X2', 'X3'],
    ['2*X1 + X2 - 3*X3 + 0.3*X1*X2', '-5*X1 + 4*X2 - 0.8*X2*X3 + 2*X3'])
distribution_aggregated = ot.ComposedDistribution([ot.Uniform()] * 3)
inputDesign = ot.SobolIndicesExperiment(distribution_aggregated,
                                        size).generate()
outputDesign = model_aggregated(inputDesign)
# Case 1 : Estimation of sensitivity using estimator and no bootstrap
for method in methods:
    sensitivity_algorithm = eval(
        'ot.' + method +
        "SensitivityAlgorithm(inputDesign, outputDesign, size)")
# Taille du plan d'expérience de base pour estimer S et ST
sampleSize = 1000

# Nombre de répétition de l'expérience
nrepetitions = 500

# Estimations des indices du premier ordre
sampleFirstMartinez = ot.Sample(nrepetitions, 3)

# Estimations des indices totaux
sampleTotalMartinez = ot.Sample(nrepetitions, 3)
for i in range(nrepetitions):
    inputDesign = ot.SobolIndicesExperiment(distribution,
                                            sampleSize).generate()
    outputDesign = gsobol(inputDesign, a)
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(
        inputDesign, outputDesign, sampleSize)
    fo = sensitivity_algorithm.getFirstOrderIndices()
    to = sensitivity_algorithm.getTotalOrderIndices()
    for j in range(d):
        sampleFirstMartinez[i, j] = fo[j]
    for j in range(d):
        sampleTotalMartinez[i, j] = to[j]

fig = pl.figure(figsize=(12, 8))
for j in range(d):
    ax = fig.add_subplot(2, 3, 1 + j)
    graph = ot.HistogramFactory().build(sampleFirstMartinez[:, j]).drawPDF()
    graph.setXTitle("S%d" % (d))
    graph.setLegends([""])
    _ = otv.View(graph, figure=fig, axes=[ax])
    ax = fig.add_subplot(2, 3, 4 + j)
Exemple #13
0
input_names = inputDistribution.getDescription()
inputDesign.setDescription(input_names)
print("Sample size: ", inputDesign.getSize())

# %%
# We see that 7000 function evaluations are required to estimate the first order and total Sobol' indices.
# Then we evaluate the outputs corresponding to this design of experiments.

# %%
outputDesign = function(inputDesign)

# %%
# Then we estimate the Sobol' indices with the `SaltelliSensitivityAlgorithm`.

# %%
sensitivityAnalysis = ot.SaltelliSensitivityAlgorithm(
    inputDesign, outputDesign, size)

# %%
# The `getFirstOrderIndices` and `getTotalOrderIndices` method respectively return estimates of first order and total Sobol' indices with a given output.
# Since these depend on the output marginal, the index of the output must
# be specified (the default is to return the index for the first output).

# %%
output_dimension = function.getOutputDimension()
for i in range(output_dimension):
    print("Output #", i)
    first_order = sensitivityAnalysis.getFirstOrderIndices(i)
    total_order = sensitivityAnalysis.getTotalOrderIndices(i)
    print("    First order indices: ", first_order)
    print("    Total order indices: ", total_order)
Exemple #14
0
        interval_to_asymptotic = sensitivity_algorithm.getTotalOrderIndicesInterval(
        )
        print("asymptotic intervals:")
        print("First order indices distribution = ",
              sensitivity_algorithm.getFirstOrderIndicesDistribution())
        print("Total order indices distribution = ",
              sensitivity_algorithm.getTotalOrderIndicesDistribution())
        print("First order indices interval = ", interval_fo_asymptotic)
        print("Total order indices interval = ", interval_to_asymptotic)

# with experiment
sequence = ot.SobolSequence(input_dimension)
experiment = ot.LowDiscrepancyExperiment(
    sequence,
    ot.ComposedDistribution([ot.Uniform(0.0, 1.0)] * input_dimension), size)
sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
print(sensitivity_algorithm.getFirstOrderIndices())

# multi variate model
model_aggregated = ot.SymbolicFunction(
    ['X1', 'X2', 'X3'],
    ['2*X1 + X2 - 3*X3 + 0.3*X1*X2', '-5*X1 + 4*X2 - 0.8*X2*X3 + 2*X3'])
distribution_aggregated = ot.ComposedDistribution([ot.Uniform()] * 3)
inputDesign = ot.SobolIndicesExperiment(distribution_aggregated,
                                        size).generate()
outputDesign = model_aggregated(inputDesign)
# Case 1 : Estimation of sensitivity using estimator and no bootstrap
for method in methods:
    sensitivity_algorithm = eval(
        'ot.' + method +
        "SensitivityAlgorithm(inputDesign, outputDesign, size)")
def myLowDiscrepancyExperiment(distribution, size,model):
    inputDesign = generateByLowDiscrepancy(distribution, size)
    outputDesign = model(inputDesign)
    salg = ot.SaltelliSensitivityAlgorithm(inputDesign, outputDesign, size)
    return salg
def myMonteCarloExperiment(distribution, size,model):
    inputDesign = generateByMonteCarlo(distribution, size)
    outputDesign = model(inputDesign)
    salg = ot.SaltelliSensitivityAlgorithm(inputDesign, outputDesign, size)
    return salg
    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
    # transform and get an asymptotic confidence interval
    ot.ResourceMap.SetAsBool(
        "MartinezSensitivityAlgorithm-UseAsymptoticInterval", True)
    interval_fo_asymptotic = sensitivity_algorithm.getFirstOrderIndicesInterval(
    )
    interval_to_asymptotic = sensitivity_algorithm.getTotalOrderIndicesInterval(
    )
    print("Martinez method : asymptotic intervals")
    print("First order indices interval = ", interval_fo_asymptotic)
    print("Total order indices interval = ", interval_to_asymptotic)

    # with experiment
    sequence = ot.SobolSequence(input_dimension)
    experiment = ot.LowDiscrepancyExperiment(sequence, ot.ComposedDistribution(
        [ot.Uniform(0.0, 1.0)] * input_dimension), size)
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
    print(sensitivity_algorithm.getFirstOrderIndices())
except:
    import sys
    print("t_SensitivityAlgorithm_std.py",
          sys.exc_info()[0], sys.exc_info()[1])
Exemple #18
0
    def sobol(self):
        """Compute Sobol' indices.

        It returns the second, first and total order indices of Sobol'.
        Two methods are possible for the indices:

        - `sobol`
        - `FAST`

        .. warning:: The second order indices are only available with the sobol
          method. Also, when there is no surrogate (ensemble mode), FAST is not
          available and the DoE must have been generated with `saltelli`.

        And two types of computation are availlable for the global indices:

        - `block`
        - `aggregated`

        If *aggregated*, *map* indices are computed. In case of a scalar value,
        all types returns the same values. *block* indices are written
        within `sensitivity.dat` and aggregated indices within
        `sensitivity_aggregated.dat`.

        Finally, it calls :func:`error_pod` in order to compare the indices
        with their analytical values.

        :return: Sobol' indices.
        :rtype: array_like.
        """
        indices = [[], [], []]
        aggregated = [[], [], []]
        indices_conf = [[], []]

        if self.type_indices == 'block':
            sobol_model = self.int_func
            sobol_len = 1
        else:
            sobol_model = self.func
            sobol_len = self.output_len

        if self.method_sobol == 'sobol':
            self.logger.info("\n----- Sobol' indices -----")

            if self.surrogate is not None:
                size = self.points_sample
                input_design = ot.SobolIndicesExperiment(self.distribution,
                                                         size, True).generate()
                output_design = sobol_model(input_design)
                self.logger.info("Created {} samples for Sobol'"
                                 .format(len(output_design)))
            else:
                input_design = self.space
                output_design = self.output
                size = len(self.space) // (2 * self.p_len + 2)
            # Martinez, Saltelli, MauntzKucherenko, Jansen
            ot.ResourceMap.SetAsBool('SobolIndicesAlgorithm-DefaultUseAsymptoticDistribution', True)
            sobol = ot.SaltelliSensitivityAlgorithm(input_design,
                                                    output_design, size)

            for i in range(sobol_len):
                try:
                    indices[0].append(np.array(sobol.getSecondOrderIndices(i)))
                except TypeError:
                    indices[0].append(np.zeros((self.p_len, self.p_len)))
            self.logger.debug("-> Second order:\n{}\n".format(indices[0]))

        elif self.method_sobol == 'FAST':
            self.logger.info("\n----- FAST indices -----")
            if self.output_len > 1:
                wrap_fun = sobol_model
            else:
                def wrap_fun(x):
                    return [sobol_model(x)]

            fast_model = ot.PythonFunction(self.p_len, self.output_len, wrap_fun)
            sobol = ot.FAST(ot.Function(fast_model),
                            self.distribution, self.points_sample)
            output_design = sobol_model(self.sample)
            self.logger.warning("No Second order indices with FAST")

        # try block used to handle boundary conditions with fixed values
        for i in range(sobol_len):
            try:
                indices[1].append(np.array(sobol.getFirstOrderIndices(i)))
            except TypeError:
                indices[1].append(np.zeros(self.p_len))
            try:
                indices[2].append(np.array(sobol.getTotalOrderIndices(i)))
            except TypeError:
                indices[2].append(np.zeros(self.p_len))

        self.logger.debug("-> First order:\n{}\n"
                          "-> Total:\n{}\n"
                          .format(*indices[1:]))

        # Write Sobol' indices to file: block or map
        if self.fname is not None:
            i1 = np.reshape(indices[1], (sobol_len, self.p_len))
            i2 = np.reshape(indices[2], (sobol_len, self.p_len))
            data = np.append(i1, i2, axis=1)

            names = ['S_{}'.format(p) for p in self.plabels]
            names += ['S_T_{}'.format(p) for p in self.plabels]

            if (self.output_len > 1) and (self.type_indices != 'block'):
                names = ['x'] + names
                data = np.append(np.reshape(self.xdata, (sobol_len, 1)), data, axis=1)
            sizes = [1] * len(names)

            self.io.write(os.path.join(self.fname, 'sensitivity.json'), data, names, sizes)
        else:
            self.logger.debug("No output folder to write indices in")

        # Aggregated Indices
        if self.type_indices == 'aggregated':
            self.logger.info("\n----- Aggregated Sensitivity Indices -----")

            output_var = output_design.var(axis=0)
            sum_var_indices = [np.zeros((self.p_len, self.p_len)),
                               np.zeros((self.p_len)), np.zeros((self.p_len))]

            # Compute manually for FAST and second order, otherwise OT
            if self.method_sobol == 'FAST':
                agg_range = [0, 1, 2]
            else:
                agg_range = [0]
            for i, j in itertools.product(range(self.output_len), agg_range):
                try:
                    indices[:][j][i] = np.nan_to_num(indices[:][j][i])
                    sum_var_indices[j] += float(output_var[i]) * indices[:][j][i]
                except IndexError:
                    sum_var_indices[j] = np.inf
            sum_var = np.sum(output_var)
            for i in range(3):
                aggregated[i] = sum_var_indices[i] / sum_var

            if self.method_sobol != 'FAST':
                aggregated[1] = np.array(sobol.getAggregatedFirstOrderIndices())
                aggregated[2] = np.array(sobol.getAggregatedTotalOrderIndices())
                indices_conf[0] = sobol.getFirstOrderIndicesInterval()
                indices_conf[1] = sobol.getTotalOrderIndicesInterval()

                self.logger.info("-> First order confidence:\n{}\n"
                                 "-> Total order confidence:\n{}\n"
                                 .format(*indices_conf))

            self.logger.info("Aggregated_indices:\n"
                             "-> Second order:\n{}\n"
                             "-> First order:\n{}\n"
                             "-> Total order:\n{}\n"
                             .format(*aggregated))

            # Write aggregated indices to file
            if self.fname is not None:
                i1 = np.array(aggregated[1])
                i2 = np.array(aggregated[2])
                if self.method_sobol != 'FAST':
                    i1_min = np.array(indices_conf[0].getLowerBound())
                    i1_max = np.array(indices_conf[0].getUpperBound())
                    i2_min = np.array(indices_conf[1].getLowerBound())
                    i2_max = np.array(indices_conf[1].getUpperBound())

                    # layout: [S_min_P1, S_min_P2, ..., S_P1, S_p2, ...]
                    data = np.array([i1_min, i1, i1_max, i2_min, i2, i2_max]).flatten()

                    names = [i + str(p) for i, p in
                             itertools.product(['S_min_', 'S_', 'S_max_',
                                                'S_T_min_', 'S_T_', 'S_T_max_'],
                                               self.plabels)]

                    conf = [(i1_max - i1_min) / 2, (i2_max - i2_min) / 2]
                else:
                    conf = None
                    names = [i + str(p) for i, p in
                             itertools.product(['S_', 'S_T_'], self.plabels)]
                    data = np.append(i1, i2)

                self.io.write(os.path.join(self.fname, 'sensitivity_aggregated.json'), data, names)
            else:
                self.logger.debug("No output folder to write aggregated indices in")

            full_indices = [aggregated[1], aggregated[2], indices[1], indices[2]]
        else:
            full_indices = [indices[1][0], indices[2][0]]
            aggregated = [indices[0][0], indices[1][0], indices[2][0]]
            conf = None
            self.xdata = None

        # Plot
        if self.fname:
            path = os.path.join(self.fname, 'sensitivity.pdf')
            plabels = [re.sub(r'(_)(.*)', r'\1{\2}', label)
                       for label in self.plabels]
            visualization.sensitivity_indices(full_indices, plabels=plabels,
                                              conf=conf, xdata=self.xdata,
                                              fname=path)
            path = os.path.join(self.fname, 'sensitivity-polar.pdf')
            visualization.sensitivity_indices(full_indices, plabels=plabels,
                                              conf=conf, polar=True,
                                              xdata=self.xdata, fname=path)
            if self.mesh_kwargs.get('fname'):
                path = os.path.join(self.fname, '1st_order_Sobol_map.pdf')
                visualization.mesh_2D(var=full_indices[2], flabels=plabels,
                                      output_path=path, **self.mesh_kwargs)
                path = os.path.join(self.fname, 'Total_order_Sobol_map.pdf')
                visualization.mesh_2D(var=full_indices[3], flabels=plabels,
                                      output_path=path, **self.mesh_kwargs)

        # Compute error of the POD with a known function
        if (self.type_indices in ['aggregated', 'block'])\
                and (self.test) and (self.surrogate):
            self.error_model(aggregated, self.test)

        return aggregated
def myHaltonLowDiscrepancyExperiment(distribution, size, model):
    sequence = ot.HaltonSequence(distribution.getDimension())
    experiment = ot.LowDiscrepancyExperiment(sequence, distribution, size)
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
    return sensitivity_algorithm
def mySaltelliSobolRandomizedSequence(distribution, size, model):
    sequence = ot.SobolSequence(distribution.getDimension())
    experiment = ot.LowDiscrepancyExperiment(sequence, distribution, size)
    experiment.setRandomize(True)
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
    return sensitivity_algorithm
def myLHSExperiment(distribution, size, model):
    experiment = ot.LHSExperiment(distribution, size)
    # lhs.setAlwaysShuffle(True) # randomized = works
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
    return sensitivity_algorithm
def myMonteCarloExperiment(distribution, size, model):
    experiment = ot.MonteCarloExperiment(distribution, size)
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
    return sensitivity_algorithm
def myImportanceSamplingExperiment(distribution, size, model):
    experiment = ot.ImportanceSamplingExperiment(distribution, distribution,
                                                 size)
    sensitivity_algorithm = ot.SaltelliSensitivityAlgorithm(experiment, model)
    # Fails : this is good
    return sensitivity_algorithm