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)

[((420, 452), 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'g'}), '(y, x, groups=g)\n', (436, 452), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((997, 1057), 'numpy.testing.assert_allclose', 'assert_allclose', (['result.params', 'np.r_[0.9272407]'], {'rtol': '(1e-05)'}), '(result.params, np.r_[0.9272407], rtol=1e-05)\n', (1012, 1057), False, 'from numpy.testing import assert_allclose\n'), ((1061, 1117), 'numpy.testing.assert_allclose', 'assert_allclose', (['result.bse', 'np.r_[1.295155]'], {'rtol': '(1e-05)'}), '(result.bse, np.r_[1.295155], rtol=1e-05)\n', (1076, 1117), False, 'from numpy.testing import assert_allclose\n'), ((1328, 1345), 'numpy.empty', 'np.empty', (['(10, 2)'], {}), '((10, 2))\n', (1336, 1345), True, 'import numpy as np\n'), ((1393, 1425), 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'g'}), '(y, x, groups=g)\n', (1409, 1425), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((2017, 2086), 'numpy.testing.assert_allclose', 'assert_allclose', (['result.params', 'np.r_[1.011074, 1.236758]'], {'rtol': '(0.001)'}), '(result.params, np.r_[1.011074, 1.236758], rtol=0.001)\n', (2032, 2086), False, 'from numpy.testing import assert_allclose\n'), ((2090, 2156), 'numpy.testing.assert_allclose', 'assert_allclose', (['result.bse', 'np.r_[1.420784, 1.361738]'], {'rtol': '(1e-05)'}), '(result.bse, np.r_[1.420784, 1.361738], rtol=1e-05)\n', (2105, 2156), False, 'from numpy.testing import assert_allclose\n'), ((3488, 3522), 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'g'}), '(y, x, groups=g)\n', (3506, 3522), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((3789, 3850), 'numpy.testing.assert_allclose', 'assert_allclose', (['result.params', 'np.r_[0.6466272]'], {'rtol': '(0.0001)'}), '(result.params, np.r_[0.6466272], rtol=0.0001)\n', (3804, 3850), False, 'from numpy.testing import assert_allclose\n'), ((3853, 3910), 'numpy.testing.assert_allclose', 'assert_allclose', (['result.bse', 'np.r_[0.4170918]'], {'rtol': '(1e-05)'}), '(result.bse, np.r_[0.4170918], rtol=1e-05)\n', (3868, 3910), False, 'from numpy.testing import assert_allclose\n'), ((4123, 4140), 'numpy.empty', 'np.empty', (['(10, 2)'], {}), '((10, 2))\n', (4131, 4140), True, 'import numpy as np\n'), ((4188, 4222), 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'g'}), '(y, x, groups=g)\n', (4206, 4222), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((4519, 4592), 'numpy.testing.assert_allclose', 'assert_allclose', (['result.params', 'np.r_[-0.9478957, -0.0134279]'], {'rtol': '(0.001)'}), '(result.params, np.r_[-0.9478957, -0.0134279], rtol=0.001)\n', (4534, 4592), False, 'from numpy.testing import assert_allclose\n'), ((4594, 4662), 'numpy.testing.assert_allclose', 'assert_allclose', (['result.bse', 'np.r_[0.3874942, 0.1686712]'], {'rtol': '(1e-05)'}), '(result.bse, np.r_[0.3874942, 0.1686712], rtol=1e-05)\n', (4609, 4662), False, 'from numpy.testing import assert_allclose\n'), ((4716, 4739), 'numpy.random.seed', 'np.random.seed', (['(3423948)'], {}), '(3423948)\n', (4730, 4739), True, 'import numpy as np\n'), ((4766, 4779), 'numpy.arange', 'np.arange', (['(10)'], {}), '(10)\n', (4775, 4779), True, 'import numpy as np\n'), ((4847, 4872), 'numpy.random.normal', 'np.random.normal', ([], {'size': '(10)'}), '(size=10)\n', (4863, 4872), True, 'import numpy as np\n'), ((4943, 4972), 'numpy.random.normal', 'np.random.normal', ([], {'size': '(n, 4)'}), '(size=(n, 4))\n', (4959, 4972), True, 'import numpy as np\n'), ((5165, 5202), 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'groups'}), '(y, x, groups=groups)\n', (5181, 5202), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((5279, 5316), 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'groups'}), '(y, x, groups=groups)\n', (5295, 5316), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((5377, 5436), 'numpy.testing.assert_allclose', 'assert_allclose', (['result0.params', 'result1.params'], {'rtol': '(0.001)'}), '(result0.params, result1.params, rtol=0.001)\n', (5392, 5436), False, 'from numpy.testing import assert_allclose\n'), ((5450, 5487), 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'groups'}), '(y, x, groups=groups)\n', (5466, 5487), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((5574, 5646), 'numpy.testing.assert_allclose', 'assert_allclose', (['result2.params', 'np.r_[0, 0, 0.55235152, 0]'], {'rtol': '(0.0001)'}), '(result2.params, np.r_[0, 0, 0.55235152, 0], rtol=0.0001)\n', (5589, 5646), False, 'from numpy.testing import assert_allclose\n'), ((5679, 5791), 'pandas.DataFrame', 'pd.DataFrame', (["{'y': y, 'x1': x[:, (0)], 'x2': x[:, (1)], 'x3': x[:, (2)], 'x4': x[:, (3)],\n    'groups': groups}"], {}), "({'y': y, 'x1': x[:, (0)], 'x2': x[:, (1)], 'x3': x[:, (2)],\n    'x4': x[:, (3)], 'groups': groups})\n", (5691, 5791), True, 'import pandas as pd\n'), ((5854, 5914), 'statsmodels.discrete.conditional_models.ConditionalLogit.from_formula', 'ConditionalLogit.from_formula', (['fml'], {'groups': '"""groups"""', 'data': 'df'}), "(fml, groups='groups', data=df)\n", (5883, 5914), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((5977, 6024), 'numpy.testing.assert_allclose', 'assert_allclose', (['result2.params', 'result3.params'], {}), '(result2.params, result3.params)\n', (5992, 6024), False, 'from numpy.testing import assert_allclose\n'), ((6058, 6080), 'numpy.random.seed', 'np.random.seed', (['(342394)'], {}), '(342394)\n', (6072, 6080), True, 'import numpy as np\n'), ((6107, 6120), 'numpy.arange', 'np.arange', (['(10)'], {}), '(10)\n', (6116, 6120), True, 'import numpy as np\n'), ((6188, 6213), 'numpy.random.normal', 'np.random.normal', ([], {'size': '(10)'}), '(size=10)\n', (6204, 6213), True, 'import numpy as np\n'), ((6284, 6313), 'numpy.random.normal', 'np.random.normal', ([], {'size': '(n, 4)'}), '(size=(n, 4))\n', (6300, 6313), True, 'import numpy as np\n'), ((6406, 6422), 'numpy.exp', 'np.exp', (['lin_pred'], {}), '(lin_pred)\n', (6412, 6422), True, 'import numpy as np\n'), ((6431, 6454), 'numpy.random.poisson', 'np.random.poisson', (['mean'], {}), '(mean)\n', (6448, 6454), True, 'import numpy as np\n'), ((6469, 6508), 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'groups'}), '(y, x, groups=groups)\n', (6487, 6508), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((6585, 6624), 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'groups'}), '(y, x, groups=groups)\n', (6603, 6624), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((6685, 6744), 'numpy.testing.assert_allclose', 'assert_allclose', (['result0.params', 'result1.params'], {'rtol': '(0.001)'}), '(result0.params, result1.params, rtol=0.001)\n', (6700, 6744), False, 'from numpy.testing import assert_allclose\n'), ((6758, 6797), 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'groups'}), '(y, x, groups=groups)\n', (6776, 6797), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((6882, 6954), 'numpy.testing.assert_allclose', 'assert_allclose', (['result2.params', 'np.r_[0, 0, 0.91697508, 0]'], {'rtol': '(0.0001)'}), '(result2.params, np.r_[0, 0, 0.91697508, 0], rtol=0.0001)\n', (6897, 6954), False, 'from numpy.testing import assert_allclose\n'), ((6987, 7099), 'pandas.DataFrame', 'pd.DataFrame', (["{'y': y, 'x1': x[:, (0)], 'x2': x[:, (1)], 'x3': x[:, (2)], 'x4': x[:, (3)],\n    'groups': groups}"], {}), "({'y': y, 'x1': x[:, (0)], 'x2': x[:, (1)], 'x3': x[:, (2)],\n    'x4': x[:, (3)], 'groups': groups})\n", (6999, 7099), True, 'import pandas as pd\n'), ((7162, 7224), 'statsmodels.discrete.conditional_models.ConditionalPoisson.from_formula', 'ConditionalPoisson.from_formula', (['fml'], {'groups': '"""groups"""', 'data': 'df'}), "(fml, groups='groups', data=df)\n", (7193, 7224), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((7286, 7333), 'numpy.testing.assert_allclose', 'assert_allclose', (['result2.params', 'result3.params'], {}), '(result2.params, result3.params)\n', (7301, 7333), False, 'from numpy.testing import assert_allclose\n'), ((702, 730), 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'ngrad'], {}), '(grad, ngrad)\n', (717, 730), False, 'from numpy.testing import assert_allclose\n'), ((820, 859), 'statsmodels.tools.numdiff.approx_fprime', 'approx_fprime', (['np.r_[x,]', 'model.loglike'], {}), '(np.r_[x,], model.loglike)\n', (833, 859), False, 'from statsmodels.tools.numdiff import approx_fprime\n'), ((909, 950), 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'score'], {'rtol': '(0.0001)'}), '(grad, score, rtol=0.0001)\n', (924, 950), False, 'from numpy.testing import assert_allclose\n'), ((1681, 1721), 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'ngrad'], {'rtol': '(1e-05)'}), '(grad, ngrad, rtol=1e-05)\n', (1696, 1721), False, 'from numpy.testing import assert_allclose\n'), ((1848, 1884), 'statsmodels.tools.numdiff.approx_fprime', 'approx_fprime', (['params', 'model.loglike'], {}), '(params, model.loglike)\n', (1861, 1884), False, 'from statsmodels.tools.numdiff import approx_fprime\n'), ((1929, 1970), 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'score'], {'rtol': '(0.0001)'}), '(grad, score, rtol=0.0001)\n', (1944, 1970), False, 'from numpy.testing import assert_allclose\n'), ((2229, 2250), 'numpy.random.seed', 'np.random.seed', (['(34234)'], {}), '(34234)\n', (2243, 2250), True, 'import numpy as np\n'), ((2279, 2310), 'numpy.random.randint', 'np.random.randint', (['(0)', '(2)'], {'size': 'n'}), '(0, 2, size=n)\n', (2296, 2310), True, 'import numpy as np\n'), ((2324, 2348), 'numpy.random.normal', 'np.random.normal', ([], {'size': 'n'}), '(size=n)\n', (2340, 2348), True, 'import numpy as np\n'), ((2362, 2386), 'numpy.random.normal', 'np.random.normal', ([], {'size': 'n'}), '(size=n)\n', (2378, 2386), True, 'import numpy as np\n'), ((2399, 2431), 'numpy.random.randint', 'np.random.randint', (['(0)', '(25)'], {'size': 'n'}), '(0, 25, size=n)\n', (2416, 2431), True, 'import numpy as np\n'), ((2445, 2486), 'numpy.hstack', 'np.hstack', (['(x1[:, (None)], x2[:, (None)])'], {}), '((x1[:, (None)], x2[:, (None)]))\n', (2454, 2486), True, 'import numpy as np\n'), ((2671, 2721), 'pandas.DataFrame', 'pd.DataFrame', (["{'y': y, 'x1': x1, 'x2': x2, 'g': g}"], {}), "({'y': y, 'x1': x1, 'x2': x2, 'g': g})\n", (2683, 2721), True, 'import pandas as pd\n'), ((3029, 3088), 'numpy.testing.assert_allclose', 'assert_allclose', (['result1.params', 'result2.params'], {'rtol': '(1e-05)'}), '(result1.params, result2.params, rtol=1e-05)\n', (3044, 3088), False, 'from numpy.testing import assert_allclose\n'), ((3096, 3149), 'numpy.testing.assert_allclose', 'assert_allclose', (['result1.bse', 'result2.bse'], {'rtol': '(1e-05)'}), '(result1.bse, result2.bse, rtol=1e-05)\n', (3111, 3149), False, 'from numpy.testing import assert_allclose\n'), ((3236, 3297), 'numpy.testing.assert_allclose', 'assert_allclose', (['result1.tvalues', 'result2.tvalues'], {'rtol': '(1e-05)'}), '(result1.tvalues, result2.tvalues, rtol=1e-05)\n', (3251, 3297), False, 'from numpy.testing import assert_allclose\n'), ((3612, 3651), 'statsmodels.tools.numdiff.approx_fprime', 'approx_fprime', (['np.r_[x,]', 'model.loglike'], {}), '(np.r_[x,], model.loglike)\n', (3625, 3651), False, 'from statsmodels.tools.numdiff import approx_fprime\n'), ((3701, 3742), 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'score'], {'rtol': '(0.0001)'}), '(grad, score, rtol=0.0001)\n', (3716, 3742), False, 'from numpy.testing import assert_allclose\n'), ((4350, 4386), 'statsmodels.tools.numdiff.approx_fprime', 'approx_fprime', (['params', 'model.loglike'], {}), '(params, model.loglike)\n', (4363, 4386), False, 'from statsmodels.tools.numdiff import approx_fprime\n'), ((4431, 4472), 'numpy.testing.assert_allclose', 'assert_allclose', (['grad', 'score'], {'rtol': '(0.0001)'}), '(grad, score, rtol=0.0001)\n', (4446, 4472), False, 'from numpy.testing import assert_allclose\n'), ((4809, 4825), 'numpy.ones', 'np.ones', (['(n // 10)'], {}), '(n // 10)\n', (4816, 4825), True, 'import numpy as np\n'), ((4916, 4932), 'numpy.ones', 'np.ones', (['(n // 10)'], {}), '(n // 10)\n', (4923, 4932), True, 'import numpy as np\n'), ((5019, 5036), 'numpy.dot', 'np.dot', (['x', 'params'], {}), '(x, params)\n', (5025, 5036), True, 'import numpy as np\n'), ((6150, 6166), 'numpy.ones', 'np.ones', (['(n // 10)'], {}), '(n // 10)\n', (6157, 6166), True, 'import numpy as np\n'), ((6257, 6273), 'numpy.ones', 'np.ones', (['(n // 10)'], {}), '(n // 10)\n', (6264, 6273), True, 'import numpy as np\n'), ((6360, 6377), 'numpy.dot', 'np.dot', (['x', 'params'], {}), '(x, params)\n', (6366, 6377), True, 'import numpy as np\n'), ((2523, 2555), 'statsmodels.discrete.conditional_models.ConditionalLogit', 'ConditionalLogit', (['y', 'x'], {'groups': 'g'}), '(y, x, groups=g)\n', (2539, 2555), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((2591, 2625), 'statsmodels.discrete.conditional_models.ConditionalPoisson', 'ConditionalPoisson', (['y', 'x'], {'groups': 'g'}), '(y, x, groups=g)\n', (2609, 2625), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((2762, 2831), 'statsmodels.discrete.conditional_models.ConditionalLogit.from_formula', 'ConditionalLogit.from_formula', (['"""y ~ 0 + x1 + x2"""'], {'groups': '"""g"""', 'data': 'df'}), "('y ~ 0 + x1 + x2', groups='g', data=df)\n", (2791, 2831), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((2892, 2963), 'statsmodels.discrete.conditional_models.ConditionalPoisson.from_formula', 'ConditionalPoisson.from_formula', (['"""y ~ 0 + x1 + x2"""'], {'groups': '"""g"""', 'data': 'df'}), "('y ~ 0 + x1 + x2', groups='g', data=df)\n", (2923, 2963), False, 'from statsmodels.discrete.conditional_models import ConditionalLogit, ConditionalPoisson\n'), ((5074, 5091), 'numpy.exp', 'np.exp', (['(-lin_pred)'], {}), '(-lin_pred)\n', (5080, 5091), True, 'import numpy as np\n'), ((5102, 5127), 'numpy.random.uniform', 'np.random.uniform', ([], {'size': 'n'}), '(size=n)\n', (5119, 5127), True, 'import numpy as np\n')]