[ot-users] [External] Re: Custom distribution in FORM

regis lebrun regis_anne.lebrun_dutfoy at yahoo.fr
Sat Nov 11 10:22:12 CET 2017

 Is it possible to have a script reproducing the bug? As the distribution algebra involves quite different numerical algorithms it is a complex task to check everything. Any help is GREATLY appreciated on this part of the code.
Many thanks
    Le samedi 11 novembre 2017 à 00:31:02 UTC+1, Phil Fernandes <phil.fernandes at enbridge.com> a écrit :  
 I tried implementing via distribution algebra, but for some reason my program just hangs, so I decided to try implementing the distribution as a subclass of PythonDistribution as per the example https://github.com/openturns/openturns/blob/master/python/test/t_Distribution_python.py. Unfortunately there seem to be inconsistencies in the argument types that are accepted by the methods of Distribution objects and the example PythonDistribution. What are the required object types for the outputs of computeXXX(), e.g., computeQuantile() in order for the distribution to work in FORM? Would it suffice to output a list?

For example
    a = ot.Normal()
    x = np.linspace(0,1,5)[:,None]
    class=Sample name=Unnamed implementation=class=SampleImplementation name=Unnamed size=5 dimension=1 data=[[0.398942],[0.386668],[0.352065],[0.301137],[0.241971]]

returns 1.0. 

Thank you.

-----Original Message-----
From: regis lebrun [mailto:regis_anne.lebrun_dutfoy at yahoo.fr] 
Sent: Friday, November 10, 2017 12:12 PM
To: users at openturns.org; Phil Fernandes
Subject: [External] Re: [ot-users] Custom distribution in FORM


You can easily implement this distribution using OpenTURNS unique feature regarding distribution algebra (see https://en.wikipedia.org/wiki/Johnson%27s_SU-distribution):

import openturns as ot
lamb = 1.5
xi = 1.1
delta = 2.0
gamma = 1.0
distJU = ((ot.Normal() - gamma) / delta).sinh() * lamb + xi print("distJU=", distJU)

You will get:
distJU= RandomMixture(1.1 + 1.5 * CompositeDistribution=f(RandomMixture(Normal(mu = -0.5, sigma = 0.5))) with f=[x]->[sinh(x)])

and a graph similar to the one given on the wikipedia page.

Your script contains some bugs (computeQuantile, getMean, getStandardDeviation should return the result as a float sequence of size 1) and a missing method, namely getRange(). To get a running distribution you must implement getRange() and computeCDF(), all the other methods have a generic implementation, but these generic algorithms may be slow or inaccurate in difficult situations, so the more methods you provide the most efficient your distribution is.

The online documentation (http://secure-web.cisco.com/1_QlR51NTnci244KawP-NQBpuYV0mfhSTp4JXBwTpGJAqWfkDBxUY1JGCpr_XGFfQIZiEjVXqGphin3yoL6fV0Ro5q1JtmYip6xNs_iSbp14Iqwp3nL4GrrNfOhdGLYYpkwqSHmNCB0HmQ9TV4e4AAXGci3esSntXt8UjGeLuSqsWVIAgSfMQHC-yrAm6_JuU7HLDfmDuKjh0tAEtKH4Exm1PXrpvRwVwODuNBTOUej_7Q49C1pP-1sswRmGgOGm3NLSy3q3ZTJfNSogVMSuyZ4wcTzp0YH2CW-VW6edm4x21iG0omiNXB3pYDqQmrnNqz_uUj9AvsTr72Dh6iW8jGw/http%3A%2F%2Fopenturns.github.io%2Fopenturns%2Fmaster%2Fuser_manual%2F_generated%2Fopenturns.PythonDistribution.html%3Fhighlight%3Dpythondistribution) is very poor and will be updated. You can have a look at https://github.com/openturns/openturns/blob/master/python/test/t_Distribution_python.py for an example of a custom Python distribution.

You could also have used the SciPyDistribution wrapper of scipy distributions (see http://secure-web.cisco.com/1B_pKGvPkYtC86WGBDSnrF--4exrvoUqVoGHUYGIgi2rsg8OcFilo_WAXcF13h2kI6KqhmosBqsMMIjA_MZ9-V-t5GJ0wnk2hxZCqcogsNY4Q-1P7-gw4Jaj5Q1Y4l_n0WzDuI9V0YpSEvzmwpq65VeVL9VfSWO3Ec7QcmzW3i2NaK5oYsp4X5rrLp03MjlZiJscbJaKKstp9LrMGohAul3vBWC30HVxArmix5guZPggdaAavQcloR4ZVn0HF0ZGPR1LI6wJzM4LA4ZLfRh_EnO3yzEybS_kpyPYtyPUHVZY4xpUX8ojw-cDz54hEAWAx3a4pqsF5QN57pa9gdsppCw/http%3A%2F%2Fopenturns.github.io%2Fopenturns%2Fmaster%2Fuser_manual%2F_generated%2Fopenturns.SciPyDistribution.html and https://github.com/openturns/openturns/blob/master/python/test/t_Distribution_scipy.py):

import openturns as ot
import scipy.stats as st

lamb = 1.5
xi = 1.1
delta = 2.0
gamma = 1.0
distJU = ot.Distribution(ot.ScyPyDistribution(st.johnsonsu(gamma, delta, loc=xi, scale=lamb)))

but unfortunately this wrapper has a bug for unbounded distributions, resulting in a wrong range and a boggus computeQuantile() method.

Thanks for the question, it raised a lot of problems in OT!

Best regards,


Le vendredi 10 novembre 2017 à 18:42:19 UTC+1, Phil Fernandes <phil.fernandes at enbridge.com> a écrit : 



I am attempting to use a custom continuous probability distribution for a probability of failure calculation via FORM, but when I try to create a ComposedDistribution with my custom dist, the program fails with

NotImplementedError: Wrong number or type of arguments for overloaded function 'new_ComposedDistribution'.


The custom dist is defined as


class JohnsonSU(ot.PythonDistribution):

    def __init__(self, gamma=1, xi=0, delta=0.5, lam=1):

        super(JohnsonSU, self).__init__(1)

        if np.any(delta <= 0):

            raise ValueError('Delta must be >0.')

        if np.any(lam <= 0):

            raise ValueError('Lambda must be >0.')


        self.gamma = gamma  # shape 1

        self.xi = xi    # location

        self.delta = delta  # shape 2, >0

        self.lam = lam  # scale, >0

        self.scipy_dist = st.johnsonsu(self.gamma, self.delta, loc=self.xi, scale=self.lam)


    def computeCDF(self, x):

        return self.scipy_dist.cdf(x)


    def computePDF(self, x):

        return self.scipy_dist.pdf(x)


    def computeQuantile(self, p):

        return self.scipy_dist.ppf(p)


    def getMean(self):

        return self.scipy_dist.mean()


    def getStandardDeviation(self):

        return self.scipy_dist.std()



Are there additional functions that must be defined in order for the PythonDistribution to be compatible with existing OpenTurns Distributions? Or, is there a straightforward way that I could add arbitrary distributions to the OpenTurns source code? 


Many thanks!


Phil Fernandes   P.Eng, MASc
Engineer, Reliability Assessment


TEL: 780-420-8210  |  FAX: 780-420-5234
7045 Enbridge Centre, 10175 101 Street NW, Edmonton, AB, T5J 0H3

Integrity. Safety. Respect.


OpenTURNS users mailing list
users at openturns.org

OpenTURNS users mailing list
users at openturns.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://openturns.org/pipermail/users/attachments/20171111/6da1d76c/attachment.html>

More information about the users mailing list