# Problem with Sensitivity Analysis using FunctionalChaosResult

**URL:** <https://openturns.discourse.group/t/problem-with-sensitivity-analysis-using-functionalchaosresult/378>\
**Category:** Development\
**Created:** [September 3, 2024, 2:10am UTC](https://openturns.discourse.group/t/problem-with-sensitivity-analysis-using-functionalchaosresult/378 "2024-09-03T02:10:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles89](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@Charles89](https://openturns.discourse.group/u/Charles89)\
**Post date:** [September 3, 2024, 2:10am UTC](https://openturns.discourse.group/t/problem-with-sensitivity-analysis-using-functionalchaosresult/378/1 "2024-09-03T02:10:59Z")

</div>

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)_

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

```

_Residuals and relative errors - dummy initialization_

```auto
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._

```auto
print(len(function_collection))
print(len(chaos_coefficients))

```

_Create the FunctionalChaosResult object_

```auto
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:

```auto
---------------------------------------------------------------------------
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.

---

<div class="post-metadata">

**Author:** ![schueller](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/schueller/32/3_2.png) [@schueller](https://openturns.discourse.group/u/schueller)\
**Post date:** [October 17, 2024, 12:25pm UTC](https://openturns.discourse.group/t/problem-with-sensitivity-analysis-using-functionalchaosresult/378/2 "2024-10-17T12:25:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MichaelBaudin](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/michaelbaudin/32/9_2.png) [@MichaelBaudin](https://openturns.discourse.group/u/MichaelBaudin)\
**Post date:** [November 21, 2024, 11:32am UTC](https://openturns.discourse.group/t/problem-with-sensitivity-analysis-using-functionalchaosresult/378/3 "2024-11-21T11:32:36Z")

</div>

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 wrote this code, which creates the object using least squares:

> <https://gist.github.com/mbaudin47/08fb84cc755f3738a09ea1b7ae000d21>

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](https://github.com/openturns/openturns/pull/2283).
