pythonrandomstatisticsuncertaintyopenturns

Continuous and discrete variables with OpenTURNS


How to create a design of experiments with both continuous and discrete random variables with OpenTURNS?

I get that we can do:

X0 = ot.Normal()
X1 = ot.Normal()
distribution  = ot.ComposedDistribution([X0,X1])

But this creates only a continuous joint distribution, from which I can sample from. But how to create a joint distribution of a continuous and a discrete variable? Can I sample from it then?


Solution

  • Actually, in general, OpenTURNS does not make much difference between continuous and discrete distributions. So, once we have created a Distribution, all we have to do is to use the getSample method to get a simple Monte-Carlo sample. The following example shows that we can push the idea a little further by creating a LHS design of experiments.

    To create the first marginal of the distribution, we select a univariate discrete distribution. Many of them, like the Bernoulli or Geometric distributions, are implemented in the library. In this example we pick the UserDefined distribution that assigns equal weights to the values -2, -1, 1 and 2. Then we create a Monte-Carlo experiment first with the getSample method and then with the MonteCarloExperiment method. Any other type of design of experiments can be generated based on this distribution and this is why we finally show how to create a LHS (Latin Hypercube) experiment.

    import openturns as ot
    sample = ot.Sample([-2., -1., 1., 2.],1)
    X0 = ot.UserDefined(sample)
    X1 = ot.Normal()
    distribution = ot.ComposedDistribution([X0,X1])
    # Monte-Carlo experiment, simplest version
    sample = distribution.getSample(10)
    print(sample)
    # Monte-Carlo experiment
    size = 100
    experiment = ot.MonteCarloExperiment(distribution, size)
    sample = experiment.generate()
    

    The following script produces the associated graphics.

    graph = ot.Graph("MonteCarloExperiment", "x0", "x1", True, "")
    cloud = ot.Cloud(sample, "blue", "fsquare", "")
    graph.add(cloud)
    graph
    

    The previous script prints:

        [ v0         X0         ]
    0 : [  2         -0.0612243 ]
    1 : [  1          0.789099  ]
    2 : [ -1          0.583868  ]
    3 : [ -1          1.33198   ]
    4 : [ -2         -0.934389  ]
    5 : [  2          0.559401  ]
    6 : [ -1          0.860048  ]
    7 : [  1         -0.822009  ]
    8 : [  2         -0.548796  ]
    9 : [ -1          1.46505   ]
    

    and produces the following graphics:

    Mixed discrete and continuous sample

    It is straightforward to create a LHS on the same distribution.

    size = 100
    experiment = ot.LHSExperiment(distribution, size)
    sample = experiment.generate()