Note
Go to the end to download the full example code.
Create a deconditioned random vector¶
In this example we are going to build the random vector defined as the
deconditioned distribution of:
with respect to parameter random vector following the distribution
.
This example creates a DeconditionedRandomVector
. Remember that a DeconditionedRandomVector
(and more generally a RandomVector
) can only be sampled.
There is no restriction on the random vector . It can be created from any multivariate distribution or
as the output of a function
applied to an input random vector
:
.
Note that in some restricted cases, it is possible to create the
distribution of using the class
DeconditionedDistribution
.
The DeconditionedDistribution
offers a lot of methods attached to the distribution, in particular the
computation of the PDF, CDF, the moments if any, . The advantage of the
DeconditionedRandomVector
relies
on the fact that it is fast to build and can be built in all cases. But it only offers the sampling capacity.
We consider the following cases:
Case 1: the parameter random vector has continuous marginals,
Case 2: the parameter random vector has dependent continuous and discrete marginals,
Case 3: the parameter random vector has any dependent marginals.
import openturns as ot
import openturns.viewer as otv
Case 1: the parameter random vector has continuous marginals¶
We consider the case where is of dimension 1 and follows an exponential distribution
defined by:
Variable |
Distribution |
Parameter |
---|---|---|
|
( |
|
|
||
|
||
Copula ( |
Create the parameter random vector :
lambdaDist = ot.Uniform(0.0, 1.0)
gammaDist = ot.Uniform(1.0, 2.0)
thetaDist = ot.JointDistribution([lambdaDist, gammaDist], ot.ClaytonCopula(2))
thetaRV = ot.RandomVector(thetaDist)
Create the distribution: as the parameters have no importance, we
create the default distribution.
XgivenThetaDist = ot.Exponential()
Create the random vector.
X_RV = ot.DeconditionedRandomVector(XgivenThetaDist, thetaRV)
Draw a sample
X_RV.getSample(5)
If we generate a large sample of the random vector, we can fit its distribution with non-parametric techniques such as a kernel smoothing.
sample_large = X_RV.getSample(10000)
dist_KS = ot.KernelSmoothing().build(sample_large)
g_PDF = dist_KS.drawPDF()
g_PDF.setTitle("Case 1: PDF of X by kernel smoothing")
g_PDF.setXTitle("x")
g_PDF.setLegendPosition("")
view = otv.View(g_PDF)
Case 2: the parameter random vector has dependent continuous and discrete marginals¶
We consider the case where is of dimension 1 and follows an exponential distribution
defined by:
Variable |
Distribution |
Parameter |
---|---|---|
|
( |
|
1 + |
||
|
||
Copula ( |
Create the parameter random vector . We shift the Poisson distribution to
get positive values for
.
lambdaDist = 1 + ot.Poisson(1)
gammaDist = ot.Uniform(1.0, 2.0)
thetaDist = ot.JointDistribution([lambdaDist, gammaDist], ot.ClaytonCopula(2))
thetaRV = ot.RandomVector(thetaDist)
Create the random vector.
XgivenThetaDist = ot.Exponential()
X_RV = ot.DeconditionedRandomVector(XgivenThetaDist, thetaRV)
If we generate a large sample of the random vector, we can fit its distribution with non-parametric techniques such as a kernel smoothing.
sample_large = X_RV.getSample(10000)
dist_KS = ot.KernelSmoothing().build(sample_large)
g_PDF = dist_KS.drawPDF()
g_PDF.setTitle("Case 2: PDF of X by kernel smoothing")
g_PDF.setXTitle("x")
g_PDF.setLegendPosition("")
view = otv.View(g_PDF)
Case 3: the parameter random vector has any dependent marginals¶
We consider the case where is of dimension 1 and follows an exponential distribution
defined by:
Variable Distribution |
Parameter |
|
---|---|---|
|
( |
|
see below |
||
|
||
Copula ( |
where the mixture is built from the Exponential
() with
and the
Geometric
() with
, with equal weights. In this case, the distribution of
is
not discrete nor continuous.
Create the parameter random vector :
lambdaDist = ot.Mixture([ot.Exponential(1.0), ot.Geometric(0.1)])
gammaDist = ot.Uniform(1.0, 2.0)
thetaDist = ot.JointDistribution([lambdaDist, gammaDist], ot.ClaytonCopula(2))
thetaRV = ot.RandomVector(thetaDist)
Create the random vector.
XgivenThetaDist = ot.Exponential()
X_RV = ot.DeconditionedRandomVector(XgivenThetaDist, thetaRV)
If we generate a large sample of the random vector, we can fit its distribution with non-parametric techniques such as a kernel smoothing.
sample_large = X_RV.getSample(10000)
dist_KS = ot.KernelSmoothing().build(sample_large)
g_PDF = dist_KS.drawPDF()
g_PDF.setTitle("Case 3: PDF of X by kernel smoothing")
g_PDF.setXTitle("x")
g_PDF.setLegendPosition("")
view = otv.View(g_PDF)
view.ShowAll()