# Computation time

**URL:** <https://openturns.discourse.group/t/computation-time/385>\
**Category:** Python usage\
**Created:** [October 3, 2024, 2:45pm UTC](https://openturns.discourse.group/t/computation-time/385 "2024-10-03T14:45:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asoubki](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/asoubki/32/305_2.png) [@asoubki](https://openturns.discourse.group/u/asoubki)\
**Post date:** [October 3, 2024, 2:45pm UTC](https://openturns.discourse.group/t/computation-time/385/1 "2024-10-03T14:45:21Z")

</div>

Hi Everyone,

I am new to OpenTURNS and I am wondering if it is possible to optimize this code ?  
The objective is to generate a sample over multiple variables.

Thanks in advance

```python
import numpy as np
import pandas as pd
import openturns as ot

distance_dist = ot.Uniform(300, 6000)
verticaldist = ot.Uniform(1.6, 4)
epsilon = 0.01
nsamples=1000
fbottomFunction = ot.SymbolicFunction(
    ["x0", "x1"], ["y0", "y1"], 
    f"y0 := max(min(((305+x0)*tan(x1*pi_/180)) + 152, 1990), 200) - {epsilon}; \
    y1 := 1990")

bottomDistribution = ot.BayesDistribution(
    ot.Uniform(), 
    ot.ComposedDistribution([distance_dist, verticaldist]), 
        fbottomFunction)

ftopFunction = ot.SymbolicFunction(["x0", "x1", "x2"], [f"x0+10", f"2000"])  
cDistribution= ot.BayesDistribution(ot.Uniform(), bottomDistribution, ftopFunction) 
cDistribution.setDescription(['top', 'base', 'dst', 'vdeg'])

fr = ot.SymbolicFunction(["xTop", "xBase", "xDst", "xvdeg"], ["y0", "y1"], "y0 := (-10*(xDst<=2500)) + (-15*(2500<xDst)*(xDst<=4500)) + (-30*(4500<xDst)); \
                                                                               y1 := (10*(xDst<=2500)) + (15*(2500<xDst)*(xDst<=4500)) + (30*(4500<xDst));")
rDistribution = ot.BayesDistribution(ot.Uniform(), cDistribution, fr)  
rDistribution.setDescription(['rdeg', 'top', 'base', 'dst', 'vdeg'])

fy = ot.SymbolicFunction(
    ["xR", "xTop", "xBase", "xDst", "xvdeg"], 
    ["y0", "y1"], "y0 := (-18.5*(xDst<=2500)) + (-24*(2500<xDst)); \
    y1 := (18.5*(xDst<=2500)) + (24*(2500<xDst));")
yDistribution = ot.BayesDistribution(ot.Uniform(), rDistribution, fy)
yDistribution.setDescription(['ydeg', 'rdeg', 'top', 'base', 'dst', 'vdeg'])

sample = yDistribution.getSample(10*nsamples)
yDistributionFinal = ot.UserDefined(
    sample, [1./w[0] for w in np.array(yDistribution.computePDF(sample))]) 
yDistributionFinal.setDescription(yDistribution.getDescription())

```

---

<div class="post-metadata">

**Author:** ![regislebrun](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/regislebrun/32/154_2.png) [@regislebrun](https://openturns.discourse.group/u/regislebrun)\
**Post date:** [October 4, 2024, 7:51am UTC](https://openturns.discourse.group/t/computation-time/385/2 "2024-10-04T07:51:36Z")

</div>

Hi Adil,  
I had to change

```python
ftopFunction = ot.SymbolicFunction(["x0", "x1", "x2"], [f"x0+10", f"2000"])

```

into

```python
ftopFunction = ot.SymbolicFunction(["x0", "x1", "x2"], [f"x0+10", f"7000"])

```

in order to get rid of an error saying that you try to build a Uniform distribution with a lower bound greater than its upper bound. You should double check the distribution of x0 here.

With this modification, I get:

```auto
[regis@localhost Adil]$ time python code_adil.py 
WRN - class BayesDistribution is deprecated in favor of JointByConditioningDistribution

real 0m5,099s
user 0m6,078s
sys 0m2,585s

```

on my (quite powerful) laptop using the current master of OpenTURNS. We worked quite a little bit optimizing things in `BayesDistribution` (btw it has been renamed `JointByConditioningDistribution`). You can install our weekly build to try it (see [Openturns | Anaconda.org](https://anaconda.org/openturns-wheels-nightly/openturns)).

Cheers

Régis

---

<div class="post-metadata">

**Author:** ![asoubki](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/asoubki/32/305_2.png) [@asoubki](https://openturns.discourse.group/u/asoubki)\
**Post date:** [October 4, 2024, 8:24am UTC](https://openturns.discourse.group/t/computation-time/385/3 "2024-10-04T08:24:19Z")

</div>

Hi Regis,

I will try the last OpenTURS version.

Thanks  
Adil

---

<div class="post-metadata">

**Author:** ![asoubki](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/asoubki/32/305_2.png) [@asoubki](https://openturns.discourse.group/u/asoubki)\
**Post date:** [October 7, 2024, 11:43am UTC](https://openturns.discourse.group/t/computation-time/385/4 "2024-10-07T11:43:55Z")

</div>

> [@regislebrun](#):
>
> `JointByConditioningDistribution`

Hi Régis,

with the version 1.23, the computation is faster. However, It seems that the variables order in the `BayesDistribution` output has changed between the 1.21 and 1.23. is it or did i missed something? How to get something compatible both versions?

Thanks  
Regards  
Adil

---

<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 9, 2024, 9:59am UTC](https://openturns.discourse.group/t/computation-time/385/5 "2024-10-09T09:59:26Z")

</div>

hello,  
indeed, the conditioned/conditioning components were swapped in 1.23 for more efficiency  
it seems missing from the changelog  
you could use getMarginal with indices to reorder the components depending on the version

j
