Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

alkaline-ml / statsmodels   python

Repository URL to install this package:

Version: 0.11.1 

/ discrete / tests / results / results_discrete.py

"""
Test Results for discrete models from Stata
"""
import os
import numpy as np


class Namespace(object):
    pass


# Discrete Model Tests
# Note that there is a slight refactor of the classes, so that one dataset
# might be used for more than one model

cur_dir = os.path.abspath(os.path.dirname(__file__))


class Anes(object):
    def __init__(self):
        """r
        Results are from Stata 11 (checked vs R nnet package).
        """
        self.nobs = 944

    def mnlogit_basezero():
        obj = Namespace()
        obj.nobs = 944
        params = [
            -.01153598, .29771435, -.024945, .08249144, .00519655,
            -.37340167, -.08875065, .39166864, -.02289784, .18104276,
            .04787398, -2.2509132, -.1059667, .57345051, -.01485121,
            -.00715242, .05757516, -3.6655835, -.0915567, 1.2787718,
            -.00868135, .19982796, .08449838, -7.6138431, -.0932846,
            1.3469616, -.01790407, .21693885, .08095841, -7.0604782,
            -.14088069, 2.0700801, -.00943265, .3219257, .10889408,
            -12.105751]
        obj.params = np.reshape(params, (6, -1), order='F')
        bse = [
            .0342823657, .093626795, .0065248584, .0735865799,
            .0176336937, .6298376313, .0391615553, .1082386919,
            .0079144618, .0852893563, .0222809297, .7631899491,
            .0570382292, .1585481337, .0113313133, .1262913234,
            .0336142088, 1.156541492, .0437902764, .1288965854,
            .0084187486, .0941250559, .0261963632, .9575809602,
            .0393516553, .1171860107, .0076110152, .0850070091,
            .0229760791, .8443638283, .042138047, .1434089089,
            .0081338625, .0910979921, .025300888, 1.059954821]
        obj.bse = np.reshape(bse, (6, -1), order='F')
        obj.yhat = np.loadtxt(os.path.join(cur_dir, 'yhat_mnlogit.csv'))
        obj.phat = np.loadtxt(os.path.join(cur_dir, 'phat_mnlogit.csv'))
        obj.cov_params = None
        obj.llf = -1461.922747312
        obj.llnull = -1750.34670999
        obj.llr = 576.8479253554
        obj.llr_pvalue = 1.8223179e-102
        obj.prsquared = .1647810465387
        obj.df_model = 30
        obj.df_resid = 944 - 36
        obj.J = 7
        obj.K = 6
        obj.aic = 2995.84549462
        obj.bic = 3170.45003661
        z = [
            -.3364988051, 3.179798597,  -3.823070772, 1.121012042,
            .2946945327, -.5928538661, -2.266269864, 3.618564069,
            -2.893164162, 2.122688754, 2.148652536, -2.949348555,
            -1.857818873, 3.616885888, -1.310634214, -.0566342868,
            1.712822091, -3.169435381, -2.090799808, 9.920912816,
            -1.031191864, 2.123004903, 3.225576554, -7.951122047,
            -2.370538224, 11.49421878, -2.352389066, 2.552011323,
            3.523595639, -8.361890935, -3.34331327, 14.43480847,
            -1.159676452, 3.533839715, 4.303962885, -11.42100649]
        obj.z = np.reshape(z, (6, -1), order='F')
        pvalues = [
            0.7364947525, 0.0014737744, 0.0001317999, 0.2622827367,
            0.7682272401, 0.5532789548, 0.0234348654, 0.0002962422,
            0.0038138191, 0.0337799420, 0.0316619538, 0.0031844460,
            0.0631947400, 0.0002981687, 0.1899813744, 0.9548365214,
            0.0867452747, 0.0015273542, 0.0365460134, 3.37654e-23,
            0.3024508550, 0.0337534410, 0.0012571921, 1.84830e-15,
            0.0177622072, 1.41051e-30, 0.0186532528, 0.0107103038,
            0.0004257334, 6.17209e-17, 0.0008278439, 3.12513e-47,
            0.2461805610, 0.0004095694, 0.0000167770, 3.28408e-30]
        obj.pvalues = np.reshape(pvalues, (6, -1), order='F')
        conf_int = [
            [[-0.0787282, 0.0556562],
             [0.1142092, 0.4812195],
             [-0.0377335, -0.0121565],
             [-0.0617356, 0.2267185],
             [-0.0293649, 0.0397580],
             [-1.6078610, 0.8610574]],
            [[-0.1655059, -0.0119954],
             [0.1795247, 0.6038126],
             [-0.0384099, -0.0073858],
             [0.0138787, 0.3482068],
             [0.0042042, 0.0915438],
             [-3.7467380, -0.7550884]],
            [[-0.2177596, 0.0058262],
             [0.2627019,   0.8841991],
             [-0.0370602, 0.0073578],
             [-0.2546789, 0.2403740],
             [-0.0083075, 0.1234578],
             [-5.9323630, -1.3988040]],
            [[-0.1773841, -0.0057293],
             [1.0261390, 1.5314040],
             [-0.0251818, 0.0078191],
             [0.0153462,    0.3843097],
             [0.0331544, 0.1358423],
             [-9.4906670, -5.7370190]],
            [[-0.1704124, -0.0161568],
             [1.1172810,    1.5766420],
             [-0.0328214, -0.0029868],
             [0.0503282, 0.3835495],
             [0.0359261, 0.1259907],
             [-8.7154010, -5.4055560]],
            [[-0.2234697, -0.0582916],
             [1.7890040, 2.3511560],
             [-0.0253747, 0.0065094],
             [0.1433769, 0.5004745],
             [0.0593053, 0.1584829],
             [-14.1832200, -10.0282800]]]
        obj.conf_int = np.asarray(conf_int)

        # margins, dydx(*) predict(outcome(#))
        obj.margeff_dydx_overall = np.array([
            [0.00868085993550, -0.09779854015456, 0.00272556969847,
             -0.01992376579372, -0.00603133322764],
            [0.00699386733148, -0.05022430802614, -0.00211003909752,
             -0.00536980000265, -0.00554366741814],
            [-0.00391040848820, -0.02824717135857, -0.00100551299310,
             0.00664337806861, 0.00097987356999],
            [-0.00182580888015, -0.00573744730031, -0.00004249256428,
             -0.00546669558488, 0.00054101121854],
            [-0.00098558129923, 0.01985550937033, 0.00047972250012,
             0.00172605778905, 0.00211291403209],
            [-0.00153469551647, 0.03755346502013, -0.00068531143399,
             0.00472471794347, 0.00254733486106],
            [-0.00741820702809, 0.12459834487569, 0.00063806819375,
             0.01766610701188, 0.00539385283759]
        ]).T
        obj.margeff_dydx_overall_se = np.array([
            [.0038581061, .0080471125, .0007068488, .0082318967, .0020261706],
            [.003904378, .0073600286, .000756431, .0084381578, .0020482238],
            [.003137126, .0056813182, .0006601377, .0068932588, .0018481806],
            [.0019427783, .0031904763, .0003865411, .004361789, .0011523221],
            [.0029863227, .0054076092, .0005886612, .0064426365, .0018886818],
            [.0035806552, .0069497362, .000722511, .0078287717, .0022352393],
            [.0033641608, .008376629, .0006774697, .0073505286, .0021660086]
        ]).T

        obj.margeff_dydx_mean = np.array([
            [0.01149887431225, -0.13784207091973, 0.00273313385873,
                -0.02542974260540, -0.00855346837482],
            [0.01114846831102, -0.09864273512889, -0.00222435063712,
                -0.01214617126321, -0.00903581444579],
            [-0.00381702868421, -0.05132297961269, -0.00116763216994,
                0.00624203027060, 0.00021912081810],
            [-0.00233455327258, -0.00928554037343, -0.00000206561214,
                -0.00775415690571, 0.00060004460394],
            [-0.00352579921274, 0.06412187169362, 0.00073938948643,
                0.00747778063206, 0.00459965010365],
            [-0.00574308219449, 0.11126535089794, -0.00057337915464,
                0.01467424346725, 0.00641760846097],
            [-0.00722687818452, 0.12170608820238, 0.00049490419675,
                0.01693601418978, 0.00575285798725]]).T
        obj.margeff_dydx_mean_se = np.array([
            [.0043729758, .0110343353, .0008149907, .0092551389, .0023752071],
            [.004875051, .0124746358, .0009613152, .0105665812, .0026524426],
            [.0040718954, .0103613938, .0008554615, .0089931297, .0024374625],
            [.0026430804, .0070845916, .0005364369, .0057654258, .0015988838],
            [.0037798151, .0103849291, .0007393481, .0082021938, .0023489261],
            [.0045654631, .0130329403, .0009128134, .0100053262, .0028048602],
            [.0027682389, .0113292677, .0005325113, .0061289353, .0017330763]
        ]).T

        obj.margeff_dydx_dummy_overall = np.array([
            [0.00549149574321, -0.05348235321783, 0.00298963549049,
             -0.01479461677951, -0.00332167981255, -0.26502967041815],
            [0.00345677928276, -0.00950322030929, -0.00189456107189,
             0.00033893662061, -0.00314690167350, -0.21040878091828],
            [-0.00645089013284, 0.00401746940204, -0.00083948249351,
             0.01114202556889, 0.00277069841472, -0.15967397659686],
            [-0.00215436802341, -0.00366545199370, -0.00000002297812,
             -0.00457368049644, 0.00065303026027, -0.00094772782001],
            [0.00058038428936, -0.00369080100124, 0.00035948233235,
             -0.00018863693013, 0.00079351293461, 0.12640653743480],
            [0.00217597030999, -0.01279456622853, -0.00091882392767,
             0.00001651192759, -0.00037998290789, 0.27175070356670],
            [-0.00309932483642, 0.07911868907484, 0.00030378521102,
             0.00805941631677, 0.00263129901425, 0.23790291475181]]).T
        obj.margeff_dydx_dummy_overall_se = np.array([
            [.0037314453, .0094102332, .000688838, .0079744554, .0019365971,
             .0243914836],
            [.0038215262, .0095938828, .0007410885, .008259353, .0019984087,
             .0317628806],
            [.0031045718, .00785814, .0006504353, .0067892866, .0018060332,
             0.0262803561],
            [.0019756086, .0051031194, .0003862449, .0043621673, .0011796953,
             .0219999601],
            [.0029714074, .0081732018, .0005715192, .0064742872, .0019130195,
             .0331694192],
            [.0034443743, .0097296187, .0006774867, .0075996454, .0021993881,
             .038600835],
            [.0032003518, .0098741227, .0006335772, .0070902078, .0021003227,
             .0255727127]]).T

        obj.margeff_eydx_dummy_overall = np.array([
            [.03939188, -.65758371, .01750922, -.12131806, -.03613241,
             -3.2132513],
            [.02752366, -.383165, -.00830021, -.03652935, -.03286046,
             -1.8741853],
            [-.05006681, -.2719659, -.00626481, .06525323, .01012554,
             -2.0058029],
            [-.05239558, -.22549142, .00025015, -.13104416, .01114517,
             -.27052009],
            [-.00296374, .25627809, .00140513, .03358712, .02296041,
             1.3302701],
            [.00328283, .2800168, -.0083912, .04332782, .01575863,
             1.8441023],
            [-.03257068, .98346111, -.00122118, .10847807, .0406456,
             2.9119099]]).T

        obj.margeff_eydx_dummy_overall_se = np.array([
            [.0272085605, .0777760394, .0052427952, .0584011446, .0148618012,
             .5796921383],
            [.0262290023, .0724479385, .005174736, .0567743614, .0144447083,
             .3015738731],
            [.0321415498, .0895589422, .0067480662, .0701460193, .0190451865,
             .3904138447],
            [.0511305319, .1420904068, .0102342163, .1129912244, .0308618233,
             .3693799595],
            [.0340186217, .0991711703, .0065812158, .0737441012, .0212966336,
             .2346982385],
            [.0289250212, .0840662279, .0056743561, .0631772185, .0177278895,
             .2089516714],
            [.0318251305, .1085637405, .0062400589, .0699123044, .0201045606,
             .3727166284]]).T

        # taken from gretl
        obj.resid = np.loadtxt(os.path.join(cur_dir, 'mnlogit_resid.csv'),
                               delimiter=",")
        return obj

    mnlogit_basezero = mnlogit_basezero()


class DiscreteL1(object):
    def __init__(self):
        """
        Special results for L1 models
        Uses the Spector data and a script to generate the baseline results
        """
        pass

    def logit():
        """
        Results generated with:
            data = sm.datasets.spector.load(as_pandas=False)
            data.exog = sm.add_constant(data.exog, prepend=True)
            alpha = 3 * np.array([0, 1, 1, 1])
            res2 = sm.Logit(data.endog, data.exog).fit_regularized(
                method="l1", alpha=alpha, disp=0, trim_mode='size',
                size_trim_tol=1e-5, acc=1e-10, maxiter=1000)
        """
        obj = Namespace()
        nan = np.nan
        obj.params = [-4.10271595,  0., 0.15493781, 0.]
        obj.conf_int = [
            [-9.15205122,  0.94661932],
            [nan, nan],
            [-0.06539482,  0.37527044],
            [nan, nan]]
        obj.bse = [2.5762388,          nan,  0.11241668,         nan]
        obj.nnz_params = 2
        obj.aic = 42.091439368583671
        obj.bic = 45.022911174183122
        obj.cov_params = [
            [6.63700638, nan, -0.28636261, nan],
            [nan, nan, nan, nan],
            [-0.28636261, nan,  0.01263751, nan],
            [nan, nan, nan, nan]]
        return obj

    logit = logit()

    def sweep():
        """
        Results generated with
            params = np.zeros((3, 4))
            alphas = np.array(
                    [[0.1, 0.1, 0.1, 0.1],
                        [0.4, 0.4, 0.5, 0.5], [0.5, 0.5, 1, 1]])
            model = sm.Logit(data.endog, data.exog)
            for i in range(3):
                alpha = alphas[i, :]
                res2 = model.fit_regularized(method="l1", alpha=alpha,
                                             disp=0, acc=1e-10,
                                             maxiter=1000, trim_mode='off')
                params[i, :] = res2.params
            print(params)
        """
        obj = Namespace()
        obj.params = [
            [-10.37593611,   2.27080968,   0.06670638,   2.05723691],
            [-5.32670811,   1.18216019,   0.01402395,   1.45178712],
            [-3.92630318,   0.90126958,  -0.,           1.09498178]]
        return obj

    sweep = sweep()

    def probit():
        """
        Results generated with
            data = sm.datasets.spector.load(as_pandas=False)
            data.exog = sm.add_constant(data.exog, prepend=True)
            alpha = np.array([0.1, 0.2, 0.3, 10])
            res2 = sm.Probit(data.endog, data.exog).fit_regularized(
                method="l1", alpha=alpha, disp=0, trim_mode='auto',
                auto_trim_tol=0.02, acc=1e-10, maxiter=1000)
        """
        obj = Namespace()
        nan = np.nan
        obj.params = [-5.40476992,  1.25018458,  0.04744558,  0.]
        obj.conf_int = [
            [-9.44077951, -1.36876033],
            [0.03716721,  2.46320194],
            [-0.09727571,  0.19216687],
            [np.nan,         np.nan]]
        obj.bse = [2.05922641,  0.61889778,  0.07383875,         np.nan]
        obj.nnz_params = 3
        obj.aic = 38.399773877542927
        obj.bic = 42.796981585942106
        obj.cov_params = [
            [4.24041339, -0.83432592, -0.06827915, nan],
            [-0.83432592,  0.38303447, -0.01700249,         nan],
            [-0.06827915, -0.01700249,  0.00545216,         nan],
            [nan,         nan,         nan,         nan]]
        return obj

    probit = probit()

    def mnlogit():
        """
        Results generated with
Loading ...