Problem with Sensitivity Analysis using FunctionalChaosResult

Hello Folks. I am trying to conduct sensitivity analysis of LSTM predictions for my Master’s Thesis and wrote myself part of the code generating it (metamodel was created in earlier part of the code):

import openturns as ot

Assuming pce_chebyshev_result has been generated from a PCE method:
pce_chebyshev_result = pce_chebyshev_results[0]

Get the chaos coefficients from the PCE model
pce_chebyshev_results[0]

Get the multi-indices from the PCE model
multi_indices = pce_chebyshev_result.getIndices()

Define the dimension (should match the input dimension of a model)
dimension = 4 # Example dimension

Create the LinearEnumerateFunction
enumerate_function = ot.LinearEnumerateFunction(dimension)

Generate the multi-indices using enumerate_function
multi_indices_list = [enumerate_function(i) for i in range(len(chaos_coefficients))]

Convert the list of multi-indices to an ot.IndicesCollection

multi_indices_ot = ot.IndicesCollection([ot.Indices(indices) for indices in multi_indices_list])

Create the necessary objects for FunctionalChaosResult

The orthogonal basis (assuming it has been defined correctly)
pce_basis = pce_chebyshev_result.getOrthogonalBasis()

Placeholder for the forward model (optional, if available)
forward_model = pce_chebyshev_result.getModel()

Distribution used for the input variables (can use the same as in PCE)
input_distribution = pce_chebyshev_result.getDistribution()

Placeholder for the transformations (typically derived from the PCE analysis)

transformation = pce_chebyshev_result.getTransformation()
inverse_transformation = pce_chebyshev_result.getInverseTransformation()

Residuals and relative errors - dummy initialization

n_terms = 83  # As per the number of chaos coefficients

residuals = ot.Point(n_terms, 0.0)        # Initialize with zeros for now
relative_errors = ot.Point(n_terms, 0.0)  # Initialize with zeros for now

Adjust the Function Collection: If function_collection only contains forward_model, it might not match the length of chaos_coefficients

function_collection = ot.FunctionCollection([forward_model] * len(chaos_coefficients))

Check the Lengths: Verifying that function_collection and chaos_coefficients have the same length.

print(len(function_collection))
print(len(chaos_coefficients))

Create the FunctionalChaosResult object

functional_chaos_result = ot.FunctionalChaosResult(
    forward_model,               
    input_distribution,           
    transformation,               #Isoprobabilistic transformation*
    inverse_transformation,       #*Inverse transformation*
    forward_model,                #*Placeholder for output distribution*
    pce_basis,                    #*The orthogonal basis*
    multi_indices_ot,             #*Multi-indices as ot.IndicesCollection*
    ot.Sample(),                  #*Placeholder for output samples (empty for now)*
    function_collection,      #*Placeholder for function collection (empty for now)*
    chaos_coefficients,           #*The chaos coefficients*
    relative_errors               #*The relative errors*

)

It throws that error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_373/3551658331.py in <module>
     81     function_collection,     # Placeholder for function collection (empty for now)
     82     chaos_coefficients,          # The chaos coefficients
---> 83     relative_errors              # The relative errors
     84 )
     85 

~/conda/envs/python/lib/python3.7/site-packages/openturns/metamodel.py in __init__(self, *args)
   3573 
   3574     def __init__(self, *args):
-> 3575         _metamodel.FunctionalChaosResult_swiginit(self, _metamodel.new_FunctionalChaosResult(*args))
   3576     __swig_destroy__ = _metamodel.delete_FunctionalChaosResult
   3577 

TypeError: Wrong number or type of arguments for overloaded function 'new_FunctionalChaosResult'.
  Possible C/C++ prototypes are:
    OT::FunctionalChaosResult::FunctionalChaosResult()
    OT::FunctionalChaosResult::FunctionalChaosResult(OT::Function const &,OT::Distribution const &,OT::Function const &,OT::Function const &,OT::Function const &,OT::OrthogonalBasis const &,OT::Indices const &,OT::Sample const &,OT::FunctionalChaosResult::FunctionCollection const &,OT::Point const &,OT::Point const &)
    OT::FunctionalChaosResult::FunctionalChaosResult(OT::FunctionalChaosResult const &)

I’m a beginner in openTURNS and began using it few days ago. Can someone enlighten me what is exactly wrong with my code? I used all objects according to the OT documentation.

1 Like

Hello,
First it looks like you’re using a previous version (<=1.19), you should consider updating to 1.23 before anything else (the arguments will differ though).
Maybe one of the arguments is not exactly of the expected type.
You can make sure forward_model, transformation and inverse_transformation can be converted to ot.Function, that input_distribution can be converted to ot.Distribution, pce_basis to ot.OrthogonalBasis and chaos_coefficients and relative_errors to ot.Point.

Hi Charles and welcome on this forum!
Sorry: I did not see your message until yesterday. I always struggle to create a FunctionalChaosResult from scratch. Actually, there is no example of this in the documentation.
It is not easy to see the bug, because you do not provide a code that is easy to copy and paste. In order to help you, I creates this code, which creates the object using least squares:

Using this code, I think that you can find the bug in your script. Does it help?
Best regards,
Michaël

PS
The original authors were Sofiane Haddad and Régis Lebrun, in order to create the new classes that were finally integrated in New chaos by regislebrun · Pull Request #2283 · openturns/openturns · GitHub.