import numpy as np
from statsmodels.discrete.conditional_models import (
      ConditionalLogit, ConditionalPoisson)
from statsmodels.tools.numdiff import approx_fprime
from numpy.testing import assert_allclose
import pandas as pd


def test_logit_1d():

    y = np.r_[0, 1, 0, 1, 0, 1, 0, 1, 1, 1]
    g = np.r_[0, 0, 0, 1, 1, 1, 2, 2, 2, 2]

    x = np.r_[0, 1, 0, 0, 1, 1, 0, 0, 1, 0]
    x = x[:, None]

    model = ConditionalLogit(y, x, groups=g)

    # Check the gradient for the denominator of the partial likelihood
    for x in -1, 0, 1, 2:
        params = np.r_[x, ]
        _, grad = model._denom_grad(0, params)
        ngrad = approx_fprime(params, lambda x: model._denom(0, x))
        assert_allclose(grad, ngrad)

    # Check the gradient for the loglikelihood
    for x in -1, 0, 1, 2:
        grad = approx_fprime(np.r_[x, ], model.loglike)
        score = model.score(np.r_[x, ])
        assert_allclose(grad, score, rtol=1e-4)

    result = model.fit()

    # From Stata
    assert_allclose(result.params, np.r_[0.9272407], rtol=1e-5)
    assert_allclose(result.bse, np.r_[1.295155], rtol=1e-5)


def test_logit_2d():

    y = np.r_[0, 1, 0, 1, 0, 1, 0, 1, 1, 1]
    g = np.r_[0, 0, 0, 1, 1, 1, 2, 2, 2, 2]

    x1 = np.r_[0, 1, 0, 0, 1, 1, 0, 0, 1, 0]
    x2 = np.r_[0, 0, 1, 0, 0, 1, 0, 1, 1, 1]
    x = np.empty((10, 2))
    x[:, 0] = x1
    x[:, 1] = x2

    model = ConditionalLogit(y, x, groups=g)

    # Check the gradient for the denominator of the partial likelihood
    for x in -1, 0, 1, 2:
        params = np.r_[x, -1.5*x]
        _, grad = model._denom_grad(0, params)
        ngrad = approx_fprime(params, lambda x: model._denom(0, x))
        assert_allclose(grad, ngrad, rtol=1e-5)

    # Check the gradient for the loglikelihood
    for x in -1, 0, 1, 2:
        params = np.r_[-0.5*x, 0.5*x]
        grad = approx_fprime(params, model.loglike)
        score = model.score(params)
        assert_allclose(grad, score, rtol=1e-4)

    result = model.fit()

    # From Stata
    assert_allclose(result.params, np.r_[1.011074, 1.236758], rtol=1e-3)
    assert_allclose(result.bse, np.r_[1.420784, 1.361738], rtol=1e-5)

    result.summary()


def test_formula():

    for j in 0, 1:

        np.random.seed(34234)
        n = 200
        y = np.random.randint(0, 2, size=n)
        x1 = np.random.normal(size=n)
        x2 = np.random.normal(size=n)
        g = np.random.randint(0, 25, size=n)

        x = np.hstack((x1[:, None], x2[:, None]))
        if j == 0:
            model1 = ConditionalLogit(y, x, groups=g)
        else:
            model1 = ConditionalPoisson(y, x, groups=g)
        result1 = model1.fit()

        df = pd.DataFrame({"y": y, "x1": x1, "x2": x2, "g": g})
        if j == 0:
            model2 = ConditionalLogit.from_formula(
                        "y ~ 0 + x1 + x2", groups="g", data=df)
        else:
            model2 = ConditionalPoisson.from_formula(
                        "y ~ 0 + x1 + x2", groups="g", data=df)
        result2 = model2.fit()

        assert_allclose(result1.params, result2.params, rtol=1e-5)
        assert_allclose(result1.bse, result2.bse, rtol=1e-5)
        assert_allclose(result1.cov_params(), result2.cov_params(), rtol=1e-5)
        assert_allclose(result1.tvalues, result2.tvalues, rtol=1e-5)


def test_poisson_1d():

    y = np.r_[3, 1, 1, 4, 5, 2, 0, 1, 6, 2]
    g = np.r_[0, 0, 0, 0, 1, 1, 1, 1, 1, 1]

    x = np.r_[0, 1, 0, 0, 1, 1, 0, 0, 1, 0]
    x = x[:, None]

    model = ConditionalPoisson(y, x, groups=g)

    # Check the gradient for the loglikelihood
    for x in -1, 0, 1, 2:
        grad = approx_fprime(np.r_[x, ], model.loglike)
        score = model.score(np.r_[x, ])
        assert_allclose(grad, score, rtol=1e-4)

    result = model.fit()

    # From Stata
    assert_allclose(result.params, np.r_[0.6466272], rtol=1e-4)
    assert_allclose(result.bse, np.r_[0.4170918], rtol=1e-5)


def test_poisson_2d():

    y = np.r_[3, 1, 4, 8, 2, 5, 4, 7, 2, 6]
    g = np.r_[0, 0, 0, 1, 1, 1, 2, 2, 2, 2]

    x1 = np.r_[0, 1, 0, 0, 1, 1, 0, 0, 1, 0]
    x2 = np.r_[2, 1, 0, 0, 1, 2, 3, 2, 0, 1]
    x = np.empty((10, 2))
    x[:, 0] = x1
    x[:, 1] = x2

    model = ConditionalPoisson(y, x, groups=g)

    # Check the gradient for the loglikelihood
    for x in -1, 0, 1, 2:
        params = np.r_[-0.5*x, 0.5*x]
        grad = approx_fprime(params, model.loglike)
        score = model.score(params)
        assert_allclose(grad, score, rtol=1e-4)

    result = model.fit()

    # From Stata
    assert_allclose(result.params, np.r_[-.9478957, -.0134279], rtol=1e-3)
    assert_allclose(result.bse, np.r_[.3874942, .1686712], rtol=1e-5)

    result.summary()


def test_lasso_logistic():

    np.random.seed(3423948)

    n = 200
    groups = np.arange(10)
    groups = np.kron(groups, np.ones(n // 10))
    group_effects = np.random.normal(size=10)
    group_effects = np.kron(group_effects, np.ones(n // 10))

    x = np.random.normal(size=(n, 4))
    params = np.r_[0, 0, 1, 0]
    lin_pred = np.dot(x, params) + group_effects

    mean = 1 / (1 + np.exp(-lin_pred))
    y = (np.random.uniform(size=n) < mean).astype(np.int)

    model0 = ConditionalLogit(y, x, groups=groups)
    result0 = model0.fit()

    # Should be the same as model0
    model1 = ConditionalLogit(y, x, groups=groups)
    result1 = model1.fit_regularized(L1_wt=0, alpha=0)

    assert_allclose(result0.params, result1.params, rtol=1e-3)

    model2 = ConditionalLogit(y, x, groups=groups)
    result2 = model2.fit_regularized(L1_wt=1, alpha=0.05)

    # Rxegression test
    assert_allclose(result2.params, np.r_[0, 0, 0.55235152, 0], rtol=1e-4)

    # Test with formula
    df = pd.DataFrame({"y": y, "x1": x[:, 0], "x2": x[:, 1], "x3": x[:, 2],
                       "x4": x[:, 3], "groups": groups})
    fml = "y ~ 0 + x1 + x2 + x3 + x4"
    model3 = ConditionalLogit.from_formula(fml, groups="groups", data=df)
    result3 = model3.fit_regularized(L1_wt=1, alpha=0.05)
    assert_allclose(result2.params, result3.params)


def test_lasso_poisson():

    np.random.seed(342394)

    n = 200
    groups = np.arange(10)
    groups = np.kron(groups, np.ones(n // 10))
    group_effects = np.random.normal(size=10)
    group_effects = np.kron(group_effects, np.ones(n // 10))

    x = np.random.normal(size=(n, 4))
    params = np.r_[0, 0, 1, 0]
    lin_pred = np.dot(x, params) + group_effects

    mean = np.exp(lin_pred)
    y = np.random.poisson(mean)

    model0 = ConditionalPoisson(y, x, groups=groups)
    result0 = model0.fit()

    # Should be the same as model0
    model1 = ConditionalPoisson(y, x, groups=groups)
    result1 = model1.fit_regularized(L1_wt=0, alpha=0)

    assert_allclose(result0.params, result1.params, rtol=1e-3)

    model2 = ConditionalPoisson(y, x, groups=groups)
    result2 = model2.fit_regularized(L1_wt=1, alpha=0.2)

    # Regression test
    assert_allclose(result2.params, np.r_[0, 0, 0.91697508, 0], rtol=1e-4)

    # Test with formula
    df = pd.DataFrame({"y": y, "x1": x[:, 0], "x2": x[:, 1], "x3": x[:, 2],
                       "x4": x[:, 3], "groups": groups})
    fml = "y ~ 0 + x1 + x2 + x3 + x4"
    model3 = ConditionalPoisson.from_formula(fml, groups="groups", data=df)
    result3 = model3.fit_regularized(L1_wt=1, alpha=0.2)
    assert_allclose(result2.params, result3.params)

[(17, 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'g'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (35, 'numpy.testing.assert_allclose', 'assert_allclose', (['result.params', 'np.r_[0.9272407]'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (36, 'numpy.testing.assert_allclose', 'assert_allclose', (['result.bse', 'np.r_[1.295155]'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (46, 'numpy.empty', 'np.empty', (['(10, 2)'], {}), True, 'import numpy as np\n'), (50, 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'g'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (69, 'numpy.testing.assert_allclose', 'assert_allclose', (['result.params', 'np.r_[1.011074, 1.236758]'], {'rtol': '(0.001)'}), False, 'from numpy.testing import assert_allclose\n'), (70, 'numpy.testing.assert_allclose', 'assert_allclose', (['result.bse', 'np.r_[1.420784, 1.361738]'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (116, 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'g'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (127, 'numpy.testing.assert_allclose', 'assert_allclose', (['result.params', 'np.r_[0.6466272]'], {'rtol': '(0.0001)'}), False, 'from numpy.testing import assert_allclose\n'), (128, 'numpy.testing.assert_allclose', 'assert_allclose', (['result.bse', 'np.r_[0.4170918]'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (138, 'numpy.empty', 'np.empty', (['(10, 2)'], {}), True, 'import numpy as np\n'), (142, 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'g'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (154, 'numpy.testing.assert_allclose', 'assert_allclose', (['result.params', 'np.r_[-0.9478957, -0.0134279]'], {'rtol': '(0.001)'}), False, 'from numpy.testing import assert_allclose\n'), (155, 'numpy.testing.assert_allclose', 'assert_allclose', (['result.bse', 'np.r_[0.3874942, 0.1686712]'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (162, 'numpy.random.seed', 'np.random.seed', (['(3423948)'], {}), True, 'import numpy as np\n'), (165, 'numpy.arange', 'np.arange', (['(10)'], {}), True, 'import numpy as np\n'), (167, 'numpy.random.normal', 'np.random.normal', ([], {'size': '(10)'}), True, 'import numpy as np\n'), (170, 'numpy.random.normal', 'np.random.normal', ([], {'size': '(n, 4)'}), True, 'import numpy as np\n'), (177, 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'groups'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (181, 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'groups'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (184, 'numpy.testing.assert_allclose', 'assert_allclose', (['result0.params', 'result1.params'], {'rtol': '(0.001)'}), False, 'from numpy.testing import assert_allclose\n'), (186, 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'groups'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (190, 'numpy.testing.assert_allclose', 'assert_allclose', (['result2.params', 'np.r_[0, 0, 0.55235152, 0]'], {'rtol': '(0.0001)'}), False, 'from numpy.testing import assert_allclose\n'), (193, 'pandas.DataFrame', 'pd.DataFrame', (["{'y': y, 'x1': x[:, (0)], 'x2': x[:, (1)], 'x3': x[:, (2)], 'x4': x[:, (3)],\n    'groups': groups}"], {}), True, 'import pandas as pd\n'), (196, 'statsmodels.discrete.conditional_models.ConditionalLogit.from_formula', 'ConditionalLogit.from_formula', (['fml'], {'groups': '"""groups"""', 'data': 'df'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (198, 'numpy.testing.assert_allclose', 'assert_allclose', (['result2.params', 'result3.params'], {}), False, 'from numpy.testing import assert_allclose\n'), (203, 'numpy.random.seed', 'np.random.seed', (['(342394)'], {}), True, 'import numpy as np\n'), (206, 'numpy.arange', 'np.arange', (['(10)'], {}), True, 'import numpy as np\n'), (208, 'numpy.random.normal', 'np.random.normal', ([], {'size': '(10)'}), True, 'import numpy as np\n'), (211, 'numpy.random.normal', 'np.random.normal', ([], {'size': '(n, 4)'}), True, 'import numpy as np\n'), (215, 'numpy.exp', 'np.exp', (['lin_pred'], {}), True, 'import numpy as np\n'), (216, 'numpy.random.poisson', 'np.random.poisson', (['mean'], {}), True, 'import numpy as np\n'), (218, 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'groups'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (222, 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'groups'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (225, 'numpy.testing.assert_allclose', 'assert_allclose', (['result0.params', 'result1.params'], {'rtol': '(0.001)'}), False, 'from numpy.testing import assert_allclose\n'), (227, 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'groups'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (231, 'numpy.testing.assert_allclose', 'assert_allclose', (['result2.params', 'np.r_[0, 0, 0.91697508, 0]'], {'rtol': '(0.0001)'}), False, 'from numpy.testing import assert_allclose\n'), (234, 'pandas.DataFrame', 'pd.DataFrame', (["{'y': y, 'x1': x[:, (0)], 'x2': x[:, (1)], 'x3': x[:, (2)], 'x4': x[:, (3)],\n    'groups': groups}"], {}), True, 'import pandas as pd\n'), (237, 'statsmodels.discrete.conditional_models.ConditionalPoisson.from_formula', 'ConditionalPoisson.from_formula', (['fml'], {'groups': '"""groups"""', 'data': 'df'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (239, 'numpy.testing.assert_allclose', 'assert_allclose', (['result2.params', 'result3.params'], {}), False, 'from numpy.testing import assert_allclose\n'), (24, 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'ngrad'], {}), False, 'from numpy.testing import assert_allclose\n'), (28, 'statsmodels.tools.numdiff.approx_fprime', 'approx_fprime', (['np.r_[x,]', 'model.loglike'], {}), False, 'from statsmodels.tools.numdiff import approx_fprime\n'), (30, 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'score'], {'rtol': '(0.0001)'}), False, 'from numpy.testing import assert_allclose\n'), (57, 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'ngrad'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (62, 'statsmodels.tools.numdiff.approx_fprime', 'approx_fprime', (['params', 'model.loglike'], {}), False, 'from statsmodels.tools.numdiff import approx_fprime\n'), (64, 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'score'], {'rtol': '(0.0001)'}), False, 'from numpy.testing import assert_allclose\n'), (79, 'numpy.random.seed', 'np.random.seed', (['(34234)'], {}), True, 'import numpy as np\n'), (81, 'numpy.random.randint', 'np.random.randint', (['(0)', '(2)'], {'size': 'n'}), True, 'import numpy as np\n'), (82, 'numpy.random.normal', 'np.random.normal', ([], {'size': 'n'}), True, 'import numpy as np\n'), (83, 'numpy.random.normal', 'np.random.normal', ([], {'size': 'n'}), True, 'import numpy as np\n'), (84, 'numpy.random.randint', 'np.random.randint', (['(0)', '(25)'], {'size': 'n'}), True, 'import numpy as np\n'), (86, 'numpy.hstack', 'np.hstack', (['(x1[:, (None)], x2[:, (None)])'], {}), True, 'import numpy as np\n'), (93, 'pandas.DataFrame', 'pd.DataFrame', (["{'y': y, 'x1': x1, 'x2': x2, 'g': g}"], {}), True, 'import pandas as pd\n'), (102, 'numpy.testing.assert_allclose', 'assert_allclose', (['result1.params', 'result2.params'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (103, 'numpy.testing.assert_allclose', 'assert_allclose', (['result1.bse', 'result2.bse'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (105, 'numpy.testing.assert_allclose', 'assert_allclose', (['result1.tvalues', 'result2.tvalues'], {'rtol': '(1e-05)'}), False, 'from numpy.testing import assert_allclose\n'), (120, 'statsmodels.tools.numdiff.approx_fprime', 'approx_fprime', (['np.r_[x,]', 'model.loglike'], {}), False, 'from statsmodels.tools.numdiff import approx_fprime\n'), (122, 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'score'], {'rtol': '(0.0001)'}), False, 'from numpy.testing import assert_allclose\n'), (147, 'statsmodels.tools.numdiff.approx_fprime', 'approx_fprime', (['params', 'model.loglike'], {}), False, 'from statsmodels.tools.numdiff import approx_fprime\n'), (149, 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'score'], {'rtol': '(0.0001)'}), False, 'from numpy.testing import assert_allclose\n'), (166, 'numpy.ones', 'np.ones', (['(n // 10)'], {}), True, 'import numpy as np\n'), (168, 'numpy.ones', 'np.ones', (['(n // 10)'], {}), True, 'import numpy as np\n'), (172, 'numpy.dot', 'np.dot', (['x', 'params'], {}), True, 'import numpy as np\n'), (207, 'numpy.ones', 'np.ones', (['(n // 10)'], {}), True, 'import numpy as np\n'), (209, 'numpy.ones', 'np.ones', (['(n // 10)'], {}), True, 'import numpy as np\n'), (213, 'numpy.dot', 'np.dot', (['x', 'params'], {}), True, 'import numpy as np\n'), (88, 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'g'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (90, 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'g'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (95, 'statsmodels.discrete.conditional_models.ConditionalLogit.from_formula', 'ConditionalLogit.from_formula', (['"""y ~ 0 + x1 + x2"""'], {'groups': '"""g"""', 'data': 'df'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (98, 'statsmodels.discrete.conditional_models.ConditionalPoisson.from_formula', 'ConditionalPoisson.from_formula', (['"""y ~ 0 + x1 + x2"""'], {'groups': '"""g"""', 'data': 'df'}), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), (174, 'numpy.exp', 'np.exp', (['(-lin_pred)'], {}), True, 'import numpy as np\n'), (175, 'numpy.random.uniform', 'np.random.uniform', ([], {'size': 'n'}), True, 'import numpy as np\n')]