"""
Misc sigmoid related code
"""
import argparse
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt


def probit_for_sigmoid():
    """
    Probit approximation of sigmoid.
    Nice property with this approximation is that now you can
    integrate sigmoid with gaussian pdf, by approximating sigmoid
    with probit (cdf of gaussian).
    g = ∫ sigmoid(x) N(x; mu, sigma) dx ≈ sigmoid(mu/(1+pi*sigma^2/8)**.5)
    """
    x = np.linspace(-10, 10, 10000)
    scale = (np.pi/8)**.5

    sigmoid = (1+np.exp(-x))**-1
    approx = norm.cdf(scale*x)

    plt.figure()
    plt.plot(x, sigmoid, label='Sigmoid')
    plt.plot(x, approx, label='Approx')
    plt.legend()
    plt.show()


def monoton_transformation(n1, n2):
    """
    let the convex monotonically decreasing logistic loss be
    ll(x) = log(1+exp(-x))
    let g(x) be a monotonically increasing function
    is ll(g(x)) convex?
    """

    x = np.linspace(n1, n2, 10**5)
    ll = np.log(1+np.exp(-x))  # logistic loss
    # logistic loss with sigmoid
    lls = np.log(1+np.exp(-(1+np.exp(-x))**-1))
    # logistig loss with log sigmoid
    llls = np.log(1+np.exp(-np.log((1+np.exp(-x))**-1)))

    plt.figure()
    plt.plot(x, ll, label='ll')
    plt.plot(x, lls, label='lls')
    plt.plot(x, llls, label='llls')
    plt.legend()
    plt.grid()
    plt.show()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('n1', type=float)
    parser.add_argument('n2', type=float)
    args = parser.parse_args()
    monoton_transformation(args.n1, args.n2)
