Skip to content

ENH: models, distributions based on monotonic transformations #7241

Open

Description

@josef-pkt

see
#5409
#3224
#624

more flexible distributions and corresponding regression models can be created by monotonic, non-linear transformation of random variables.

My old code for creating distribution classes is in the sandbox. My version doesn't require monotonicity and works also for e.g. square transformation. (At the time I wrote that, I didn't have any real application for it.)

for example tensorflow has sinh-arcsinh as transformation model that can take base distribution as argument.
https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/SinhArcsinh

examples

I'm currently mainly interested in the first 3.

extra
composition of transformation, e.g.

  • log birnbaum-sanders is sinh transformation
  • birnbaum-sanders is sqrt transformation combined with base x - 1 / x transformation. There are other versions and generalizations for it.
  • plain logit 1 / (1+x) versus exp-logit 1 / (1 + exp(x)) where exp transforms R -> R+

implementation

  • generic: based on what I have in the sandbox, we create a new distribution that combines transformation and base distribution and takes both as argument
  • special cases:
    • some statistics like moments only have nice or analytical expressions in specific transformation-distribution combinations
      (Distributions have a generic expect method based on numerical integration, but that will not be efficient if we have to do it a few thousand or million times)
    • computational efficiency, for regression models we can use simpler expression for loglike and similar in some cases
  • tools, requirements
    • transformation and inverse transformation, domain and range of transformation
    • derivatives of the transformation, first and second should be enough
    • derivatives with respect to parameters of the transformation. I don't know yet what we need.

specific issue:

Clipping and enforcing domain for transformation or inverse transformation.
AFAICS, all transformation mapping real line to R+ or interval (0, 1) define open spaces, i.e. we get inf or zero division at boundaries. (similar to some cases with links in GLM or discrete)

implementation convention
Which is transform and which is inverse transform? e.g. Do we define transformation to normality or from normality?
Same question as link versus inverse link classes but more general use cases. Links are currently only used as non-linear mean functions.

edit The tradition might be reversed from what I want.
E,g Log transformation is to normality or other distribution, i.e. z = log(x) is normal. and x = exp(z) is log-normal distributed
(I always find this reversed, I would prefer that transformation is defined from R to restricted space, e.g. R+ or from R+ to (0, 1)
e.g. log-normal is the distribution of an exp transformed normal random variable exp-normal.
or Birnbaum-Sanders is the "bs" transformation of a normal distribution, and not: if we "bs" transform a BS random variable, then it is normally distributed.
)

Activity

  1. josef-pkt commented on Jan 5, 2021

    @josef-pkt
    MemberAuthor

    example first draft of sinh-arcsinh transformation
    (derivation based on Wolfram alpha verified by numerical derivatives. Not simplified yet for common terms

    d controls tail heaviness, kurtosis
    e controls skew

    base distribution is (d, e) = (1, 0) which is identity transformation

    def transform(x, params):
        d, e = params
        z = np.sinh(d * np.arcsinh(x) - e)
        return z
    
    def inverse(z, params):
        d, e = params
        xrev = np.sinh(1 / d * (np.arcsinh(z) + e))
        return xrev
    
    def deriv(x, params):
        d, e = params
        d_dx = (d * np.cosh(e - d * np.arcsinh(x))) / np.sqrt(x**2 + 1)
        return d_dx
    
    def deriv_inverse(z, params):
        d, e = params
        d_dz = np.cosh((np.arcsinh(z) + e) / d) / (d * np.sqrt(z**2 + 1))
        return d_dz
    
    def deriv2(x, params):
        d, e = params
        d2_dx = ((d**2 * (-np.sqrt(x**2 + 1)) * np.sinh(e - d * np.arcsinh(x)) - 
                 d * x * np.cosh(e - d * np.arcsinh(x)))/(x**2 + 1)**(3/2))
        return d2_dx
    
    def deriv2_inverse(z, params):
        d, e = params
        d2_dz = ((np.sqrt(z**2 + 1) * np.sinh((np.arcsinh(z) + e)/d) - 
               d * z * np.cosh((np.arcsinh(z) + e)/d)) / 
              (d**2 * (z**2 + 1)**(3/2)))
        return d2_dz
    

    immediate use case, create random variables

    example: light tailed, symmetric rv based on normal distribution

    xrand = np.random.randn(5000)
    zrand = transform(xrand, [0.5, 0])
    

    aside

    special property of sinh-arcsinh: same functional form of transform and inverse transform.
    I didn't use it in the above

    z is transformed variable

    transform(z, [1 / d, -e / d]), inverse(z, [d, e])
    (array([-5. , -2.5,  0. ,  2.5,  5. ]), array([-5. , -2.5,  0. ,  2.5,  5. ]))
    
    
    deriv(z, [1 / d, -e / d]), deriv_inverse(z, [d, e])
    (array([27.43313272, 15.31162031,  6.48054274, 19.65957832, 38.91384889]),
     array([27.43313272, 15.31162031,  6.48054274, 19.65957832, 38.91384889]))
    
    deriv2(z, [1 / d, -e / d]), deriv2_inverse(z, [d, e])
    (array([-132.28514823,  -73.68112766,    3.19850004,  143.08374758,
             310.39993159]),
     array([-132.28514823,  -73.68112766,    3.19850004,  143.08374758,
             310.39993159]))
    

    However, derivatives with respect to params will differ, or require an extra term for the [1 / d, -e / d] params transformation

  2. josef-pkt commented on Jan 5, 2021

    @josef-pkt
    MemberAuthor

    first draft of Birnbaum-Saunders transformation, not verified yet

    class BirnbaumSaunders(object):
        """Birnbaum-Saunders Transformation
        """
        # Note: we could use static methods, without self, 
        # currently self is not used, but we might want to store something in an instance
        
        def transform(self, x, params):
            a, b = params
            z = b * (a / 2 * x + np.sqrt((a / 2 * x)**2 + 1))**2
            return z
    
        def inverse(self, z, params):
            a, b = params
            xrev = 1 / a * (np.sqrt(z / b) - np.sqrt(b / z))
            return xrev
    
        def deriv(self, x, params):
            a, b = params
            t = a * x / 2
            corr_fact = (a / 2)
            d_dx = (2 * b * (np.sqrt(t**2 + 1) + t)**2) / np.sqrt(t**2 + 1) * corr_fact
            return d_dx
    
        def deriv_inverse(self, z, params):
            a, b = params
            d_dz = (z + b) / (2 * a * np.sqrt(b) * z**(1.5))
            return d_dz
    
        def deriv2(self, x, params):
            a, b = params
            t = a * x / 2
            corr_fact = (a / 2)**2
            d2_dx = (2 * b * (np.sqrt(t**2 + 1) + t)**2 * (2 * np.sqrt(t**2 + 1) - t)) / (t**2 + 1)**(3/2) * corr_fact
            return d2_dx
    
        def deriv2_inverse(self, z, params):
            a, b = params
            d2_dz = -(3 * b + z) / (4 * a * np.sqrt(b) * z**(5/2))
            return d2_dz
    
  3. josef-pkt commented on Jan 5, 2021

    @josef-pkt
    MemberAuthor

    and the last of the three: sinh, Johnson's SU transformation

    class Sinh(object):
        """(Arc)Sinh Transformation, Johnson's SU transformation
        
        The two parameters are loc and scale transformation of base standard distributions.
        
        changing loc from zero introduces skewness
        """
        # Note: we could use static methods, without self, 
        # currently self is not used, but we might want to store something in an instance
        
        def transform(self, x, params):
            m, s = params
            z = np.sinh((x - m)/s)
            return z
    
        def inverse(self, z, params):
            m, s = params
            xrev = m + s * np.arcsinh(z)
            return xrev
    
        def deriv(self, x, params):
            m, s = params
            t = (x - m) / s
            corr_fact = 1 / s
            d_dx = np.cosh(t) / s # * corr_fact
            return d_dx
    
        def deriv_inverse(self, z, params):
            m, s = params
            d_dz = s / np.sqrt(z**2 + 1)
            return d_dz
    
        def deriv2(self, x, params):
            m, s = params
            t = (x - m) / s
            corr_fact = 1 / s**2
            d2_dx = np.sinh(t) / s**2 # * corr_fact
            return d2_dx
    
        def deriv2_inverse(self, z, params):
            m, s = params
            d2_dz = - s * z / (z**2 + 1)**(3/2)
            return d2_dz
    

    aside consistency checks based on code that we use in Link classes for second derivatives

    sh = Sinh()
    m, s = -0.1, 1
    
    sh.deriv2(x, [m, s]), -sh.deriv2_inverse(z, [m, s]) / sh.deriv_inverse(z, [m, s])**3
    (array([-67.14116655,  -5.46622921,   0.10016675,   6.69473223,
             82.00790528]),
     array([-67.14116655,  -5.46622921,   0.10016675,   6.69473223,
             82.00790528]))
    
    sh.deriv2_inverse(z, [m, s]), -sh.deriv2(x, [m, s]) / sh.deriv(x, [m, s])**3
    (array([ 0.00022176,  0.0318551 , -0.09867792, -0.02158532, -0.00014866]),
     array([ 0.00022176,  0.0318551 , -0.09867792, -0.02158532, -0.00014866]))
    
  4. josef-pkt commented on Jan 5, 2021

    @josef-pkt
    MemberAuthor

    looking at my ancient code (before 2011 when I got pissed off in scipy.stats and stopped working on distributions)

    This only defines cdf and ppf
    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/distributions/transformed.py#L53
    I don't know why pdf is missing, the unit tests for it include pdf

    I had written a chain transformation class to chain transformation
    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/distributions/transform_functions.py#L147
    AFAIR, I didn't try and do much with it.

    The non-monotonic (u- or hump shaped) version includes pdf
    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/distributions/transformed.py#L326

    The old code still runs with current scipy, it has some basic unit test. However, some of the "frameworky" things link numargs and names might be easier now.

    I think we can add something like this mainly targeted to our own application and not support it as a generic extension to scipy distributions.

    We don't need parameter support for the base distributions like normal and logistic, but we need to support shape parameters like df in t-distribution.

    The main candidates for base distributions are normal, t and logistic, and we want generic support for others for experimentation or quick analysis.

    3 transformations times 3 distributions already makes 9 new distributions and models.

  5. josef-pkt commented on Jan 6, 2021

    @josef-pkt
    MemberAuthor

    xrand is a Gaussian random sample

    the following is for t minus inverse transform, similar to BS without the sqrt

    kls = stats.norm
    tr = tmi
    bounds = 7.5
    zz = np.linspace(1e-5, bounds, 200)
    pdf = np.abs(tr.deriv_inverse(zz, [0, 1])) * kls._pdf(tr.inverse(zz, [0, 1]))
    plt.plot(zz, pdf)
    r = plt.hist(tmi.transform(xrand * 1 + 0, [-1, s]), bins=np.linspace(0, bounds, 30), density=True)
    

    image

    but I need to check which is which, I'm not sure what I did in the sandbox, part of the pdf method of TransfTwo_gen uses
    (self.derivplus(x)*self.kls._pdf(self.funcinvplus(x),*args, **kwargs)

    but now I needed to use deriv_inverse for the jacobian of the transformation

    I'm using naming convention from GLM Link classes, (which is more recent than my old code)

  6. josef-pkt commented on Jan 7, 2021

    @josef-pkt
    MemberAuthor

    another class of transformations are CDFLinks

    in the context here, they convert to and from uniform distribution, i.e. probability integral transform PIT
    In GLM we use them to convert R to (0,1)-Interval, or from any other space of a non-uniform distribution to (0,1)

    cdf: R -> (0,1)
    ppf: (0, 1) -> R

    It's not really used to create new flexible distribution, PIT usage is "trivial" because we don't need derivatives or other properties.
    The only more complex usecase seems to be as CDFLink, i.e. sigmoid shaped transformation for mean function or params.

    aside:
    Usage of cdf links in Binomial/Bernoulli requires only standardized form, i.e. fixed loc-scale because those are not separately identified in the models. We can use scale for heteroscedasticity, i.e. variation in variance as long as we pin down the location, i.e. one reference point with fixed scale. We don't have that yet, and I'm not sure whether we need it in CDFLinks or handle that as model.scale parameter.

    I guess I will keep them as links for now.

    What I would like to get are parameterized CDFLink based on flexible distributions,
    e.g. SinhArcsinhLogistic CDFLink, i.e. combine SinhArcsinh transformation with a CDFLink.

  7. josef-pkt commented on Mar 5, 2021

    @josef-pkt
    MemberAuthor

    another group of references unit-xxx distributions - transformation to unit interval (0, 1)

    one transformation that I haven't seen before y = exp(-x) for x in R+

    I only skimmed a few of these articles,

    Ghitany, M. E., J. Mazucheli, A. F. B. Menezes, and F. Alqallaf. 2019. “The Unit-Inverse Gaussian Distribution: A New Alternative to Two-Parameter Distributions on the Unit Interval.” Communications in Statistics - Theory and Methods 48 (14): 3423–38. https://doi.org/10.1080/03610926.2018.1476717.

    Guerra, Renata Rojas, Fernando A. Peña-Ramírez, and Marcelo Bourguignon. 2020. “The Unit Extended Weibull Families of Distributions and Its Applications.” Journal of Applied Statistics 0 (0): 1–19. https://doi.org/10.1080/02664763.2020.1796936.

    Gündüz, Selim, and Mustafa Ç Korkmaz. 2020. “A New Unit Distribution Based On The Unbounded Johnson Distribution Rule: The Unit Johnson SU Distribution.” Pakistan Journal of Statistics and Operation Research, August, 471–90. https://doi.org/10.18187/pjsor.v16i3.3421.

    Haq, Muhammad Ahsan ul, Sharqa Hashmi, Khaoula Aidi, Pedro Luiz Ramos, and Francisco Louzada. 2020. “Unit Modified Burr-III Distribution: Estimation, Characterizations and Validation Test.” Annals of Data Science, June. https://doi.org/10.1007/s40745-020-00298-6.

    Mazucheli, J., A. F. B. Menezes, L. B. Fernandes, R. P. de Oliveira, and M. E. Ghitany. 2020. “The Unit-Weibull Distribution as an Alternative to the Kumaraswamy Distribution for the Modeling of Quantiles Conditional on Covariates.” Journal of Applied Statistics 47 (6): 954–74. https://doi.org/10.1080/02664763.2019.1657813.

    Mazucheli, J, AFB Menezes, and ME Ghitany. 2018. “The Unit-Weibull Distribution and Associated Inference.”

    Mazucheli, Josmar, AF Menezes, and Sanku Dey. n.d. “The Unit-Birnbaum-Saunders Distribution with Applications.”
    Chilean Journal of Statistics Vol. 9, No. 1, April 2018, 47-57

    Mazucheli, Josmar, André Felipe Berdusco Menezes, and Subrata Chakraborty. 2018. “On the One Parameter Unit-Lindley Distribution and Its Associated Regression Model for Proportion Data.” Journal of Applied Statistics, August. https://www.tandfonline.com/doi/abs/10.1080/02664763.2018.1511774.

    Mazucheli, Josmar, André Felipe Menezes, and Sanku Dey. 2019. “Unit-Gompertz Distribution with Applications.” Statistica 79 (1): 25–43. https://doi.org/10.6092/issn.1973-2201/8497.

    Menezes, André Felipe Berdusco, Josmar Mazucheli, Sanku Dey, André Felipe Berdusco Menezes, Josmar Mazucheli, and Sanku Dey. 2018. “THE UNIT-LOGISTIC DISTRIBUTION: DIFFERENT METHODS OF ESTIMATION.” Pesquisa Operacional 38 (3): 555–78. https://doi.org/10.1590/0101-7438.2018.038.03.0555.

    Nadarajah, Saralees, and Stephen Chan. 2020. “On Moments of the Unit Lindley Distribution.” Journal of Applied Statistics 47 (5): 947–49. https://doi.org/10.1080/02664763.2019.1675607.


    Mazucheli, Josmar, AF Menezes, and Sanku Dey. “The Unit-Birnbaum-Saunders Distribution with Applications.”
    They combine -log or exp(-x) with BS transformation.
    That might be a test case for chaining transformations.

  8. josef-pkt commented on Mar 5, 2021

    @josef-pkt
    MemberAuthor

    distributions on R+, mainly for lifetime analysis and different shapes of hazard function
    mainly extensions to Weibull

    I only skimmed a few parts.
    The advantage is that many have closed form expressions based on exp in weibull cdf,
    In one group of transformations, the extension corresponds to cumulative hazard function as generator.

    GURVICH, M. R., A. T. DiBENEDETTO, and S. V. RANADE. 1997. “A New Statistical Distribution for Characterizing the Random Strength of Brittle Materials.” Journal of Materials Science 32 (10): 2559–64. https://doi.org/10.1023/A:1018594215963.

    Ahmad, Zubair, and Zawar Hussain. 2017. “Flexible Weibull Distribution.” Journal of Computer and Mathematical Sciences 8 (6): 251–60.

    Lai, C. D. 2013. “Constructions and Applications of Lifetime Distributions.” Applied Stochastic Models in Business and Industry 29 (2): 127–40. https://doi.org/10.1002/asmb.948.

    Lai, C. D., D. N. P. Murthy, and M. Xie. 2011. “Weibull Distributions.” WIREs Computational Statistics 3 (3): 282–87. https://doi.org/10.1002/wics.157.

    Lai, C. D., M. Xie, and D. N. P. Murthy. 2005. “Reply to ‘On Some Recent Modifications of Weibull Distribution.’” IEEE Transactions on Reliability 54 (4): 563–563. https://doi.org/10.1109/TR.2005.858812.

    Murthy, D. N. P, M Xie, and Renyan Jiang. 2004. Weibull Models. Hoboken, N.J.: Wiley-InterScience. http://www.123library.org/book_details/?id=25807.

    Nadarajah, S., and S. Kotz. 2005. “On Some Recent Modifications of Weibull Distribution.” IEEE Transactions on Reliability 54 (4): 561–62. https://doi.org/10.1109/TR.2005.858811.

    Pham, H., and C. Lai. 2007. “On Recent Generalizations of the Weibull Distribution.” IEEE Transactions on Reliability 56 (3): 454–58. https://doi.org/10.1109/TR.2007.903352.

    Raqab, Mohammad Z., Shafiqah A. Al-Awadhi, and Debasis Kundu. 2018. “Discriminating among Weibull, Log-Normal, and Log-Logistic Distributions.” Communications in Statistics - Simulation and Computation 47 (5): 1397–1419. https://doi.org/10.1080/03610918.2017.1315729.

  9. josef-pkt commented on Dec 3, 2021

    @josef-pkt
    MemberAuthor

    Desousa, Mário F., Helton Saulo, Víctor Leiva, and Paulo Scalco. 2018. “On a Tobit–Birnbaum–Saunders Model with an Application to Medical Data.” Journal of Applied Statistics 45 (5): 932–55. https://doi.org/10.1080/02664763.2017.1322559.

    parameterization of BS
    AFAIR BS, they have an extra alpha in there to parameterize, e.g. 0.5 in standard BS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions