Trapezoidal distribution using truncature and mixture

# this is a script to create a trapezoid like distribution
# ========================================================
# version OT 0.12.3


from openturns import *
from openturns_viewer import *
from math import *


# The four next lines are the only one the user has to fill in. (change the values)
# warning : the values must respect the following ordre : a<b<c<d

a=1.0 # lower bound
b=2.0 # lower bound of the uniform distribution 
c=5.0 # upper bound of the uniform distribution 
d=7.0 # upper bound

######################################################
####      Trapezoid shape distribution       #########
######################################################

#Creation of the distributions:
triang=Triangular(a,(a+d)/2,d) # regular triangular distribution
myTruncatedTriang1 = TruncatedDistribution(Distribution(triang),b,TruncatedDistribution.UPPER) # truncated dist. on the right
myTruncatedTriang2=TruncatedDistribution(Distribution(triang),c,TruncatedDistribution.LOWER) # truncated dist. on the left
unif=Uniform(b,c) # uniform distribution

aCollection=DistributionCollection(3)  # mixture of these distribution : see UseCasesGuide manual from OT documentation

aCollection[0]=Distribution(myTruncatedTriang1)
aCollection[1]=Distribution(unif)
aCollection[2]=Distribution(myTruncatedTriang2)


aCollection[0].setWeight((b-a)/2) # weight of each distribution in order to have a continuous final distribution
aCollection[1].setWeight(c-b)
aCollection[2].setWeight((d-c)/2)

myTrapezoid=Mixture(aCollection) #final distribution

OneHundredrealisations=myTrapezoid.getNumericalSample(1000000) # graph of the obtained distribution
sampleHistOpt=VisualTest.DrawHistogram(OneHundredrealisations)
sampleHistOpt.draw("sampleHistOpt")
Show(sampleHistOpt)
ViewImage(sampleHistOpt.getBitmap())

Attachments (1)

Download all attachments as: .zip

Comments

1. souchaud@… -- 2011-04-22 15:20

myTrapezoid is a distribution so you can use all the methods related to distribution and particularly the method drawPDF to draw the probability density function of the distribution (without creating any sample)...

PDF=myTrapezoid.drawPDF()
PDF.draw("real_pdf")
2. souchaud@… -- 2011-04-22 15:20

I put an new module named 'trapezoidal' on the Open TURNS repository for extension modules (https://svn.openturns.org/openturns-modules/trapezoidal) that implements the Trapezoidal distribution directly in C++.

Here is the link to the Trapezoidal module.

After downloading the module you should use the openturns-module script to make it usable by Open TURNS :

<openturns_install_dir>/bin/openturns-module --install=trapezoidal-0.1.0.tar.bz2 --prefix=user

And now you can use it in your studies :

  from openturns import *
  from trapezoidal import *
  tr = Trapezoidal( 1., 2., 3., 4. )
  tr.drawPDF()

Ivan