Computation time

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

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

Hi Adil,
I had to change

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

into

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:

[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).

Cheers

Régis

Hi Regis,

I will try the last OpenTURS version.

Thanks
Adil

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

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