How to use Zero-skewness log transform in Python

Viewed 189

How to do zero-skewness log transform in Python?

For example in Stata it is implemented in lnskew0 (see https://www.stata.com/manuals13/rlnskew0.pdf).

I didn't find an implementation in Python. Is anyone aware of an implementation?

Otherwise, a first try would be:

from scipy.stats import skew
import numpy as np
from scipy.optimize import root_scalar


def lnskew0(x):
    def skew_ln(k):
        return skew(np.log(x - k))
    res = root_scalar(
        skew_ln,
        bracket=[-x.min(), x.max()*0.99999],
        method='bisect'
    )
    return np.log(x - res.root)

Works fine on numpy arrays with only positive numbers. How is Stata's lnskew0 implemented that it works with negative numbers as well?

1 Answers

I gave it another try so that it works with negative numbers as well:

from scipy.stats import skew
import numpy as np
from scipy.optimize import root_scalar


def lnskew0(x):

    x0 = x + 1

    def skew_ln_pos(k):
        with np.errstate(all='ignore'):
            return skew(np.log(x0 - k))
    res_pos = root_scalar(
        skew_ln_pos,
        bracket=[-150, 150],
        method='bisect'
    )

    def skew_ln_neg(k):
        with np.errstate(all='ignore'):
            return skew(np.log(-x0 - k))
    res_neg = root_scalar(
        skew_ln_neg,
        bracket=[-150, 150],
        method='bisect'
    )

    res = (res_pos.root - 1, res_neg.root + 1)

    lnskew0_res = (
        np.log(x - res[0]),
        np.log(-x - res[1])
    )

    whichmin = np.nanargmin([abs(skew(x)) for x in lnskew0_res])
    return lnskew0_res[whichmin]

Note: It still has one issue. The bracket of the root_scalar needs to be choosen manually.

Related