ó
áp7]c        	   @  s’  d  Z  d d l m Z d d l m Z d d l Z d d l j j	 Z
 d d l m Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 Z d e f d „  ƒ  YZ e j d d d d d j ƒ  ƒZ e d j d d ƒ j ƒ  Z e j \ Z Z e j e ƒ j d d ƒ Z e d d … d d … f j d d e ƒ j ƒ  Z e e j  d
 ƒ ƒ e j! j" Z# e e# d ƒ d d d d g Z$ e e j% d
 ƒ j e d ƒ ƒ d d g Z& e j' g  e& D] Z( e e( ^ q¬ƒ Z) e) j d e* e& ƒ e ƒ Z) e j+ e e) f Z) e d d d … d f Z- e j. e j d
 ƒ e Z/ e d e/ d
 k Z0 e
 j1 e d g e0 g d e2 ƒZ3 g  Z4 x+ e5 d ƒ D] Z6 e4 j7 e e/ e6 k ƒ qpWe
 j1 e d g e j8 e j d
 ƒ g d e2 ƒZ9 d d d d g d d d g d d d g d d g g Z: g  Z4 x3 e5 d ƒ D]% Z6 e4 j7 e9 e: e6 e/ e6 k ƒ qWd  Z; g  e5 d ƒ D]  Z6 e* e4 e6 j! j" ƒ e; ^ qGZ< e j= e; g e; e j> e< ƒ j? ƒ  f Z@ e j. d ƒ ZA g  e5 e* e@ ƒ d ƒ D]; Z6 e j= e j> d
 d g ƒ eA e@ e6 e@ e6 d !f ^ q¼ZB e j. d ƒ ZC g  eB D] ZD eC eD ^ qZE g  e4 D]! ZF eF j e ƒ j e d ƒ ^ q0ZG e e eG d  ƒ ZH d
 ZI eI r–e jJ eH jK e j8 d ƒ ƒ ZL n  d! d" d# d$ d% d& g ZM eI rÌe jJ eH jK eM ƒ ZN n  e jJ eH jK e jO d ƒ d' d( ƒZP eP d d
 d  d) d d* g ZQ eQ d
 c d+ 9<e eQ eM ƒ e eH jR ƒ  ƒ d, d- d g f d. d d d g f g f ZS eT e d d d d g g  e5 d ƒ D] ZU eG eU ^ q†ƒ ƒ ZV eT e d d d d g d/ d0 d1 d2 g ƒ ƒ ZV eV jW i g  d, 6g  d- 6g  d. 6ƒ i g  d, 6g  d- 6g  d. 6d d d3 d g d 6d d d4 g d 6d d d5 g d 6d d g d 6ZX e e eV eS eX ƒ ZY e eY jZ eY j[ ƒ ƒ e d6 ƒ e eY j\ ƒ d S(7   s.  conditional logit and nested conditional logit

nested conditional logit is supposed to be the random utility version
(RU2 and maybe RU1)

References:
-----------
currently based on:
Greene, Econometric Analysis, 5th edition and draft (?)
Hess, Florian, 2002, Structural Choice analysis with nested logit models,
    The Stats Journal 2(3) pp 227-252

not yet used:
Silberhorn Nadja, Yasemin Boztug, Lutz Hildebrandt, 2008, Estimation with the
    nested logit model: specifications and software particularities,
    OR Spectrum
Koppelman, Frank S., and Chandra Bhat with technical support from Vaneet Sethi,
    Sriram Subramanian, Vincent Bernardin and Jian Zhang, 2006,
    A Self Instructing Course in Mode Choice Modeling: Multinomial and
    Nested Logit Models

Author: josef-pktd
License: BSD (simplified)
iÿÿÿÿ(   t   print_function(   t   zipN(   t   optimizet	   TryCLogitc           B  s5   e  Z d  Z d „  Z d „  Z d „  Z d d „ Z RS(   s|  
    Conditional Logit, data handling test

    Parameters
    ----------

    endog : array (nobs,nchoices)
        dummy encoding of realized choices
    exog_bychoices : list of arrays
        explanatory variables, one array of exog for each choice. Variables
        with common coefficients have to be first in each array
    ncommon : int
        number of explanatory variables with common coefficients

    Notes
    -----

    Utility for choice j is given by

        $V_j = X_j * beta + Z * gamma_j$

    where X_j contains generic variables (terminology Hess) that have the same
    coefficient across choices, and Z are variables, like individual-specific
    variables that have different coefficients across variables.

    If there are choice specific constants, then they should be contained in Z.
    For identification, the constant of one choice should be dropped.


    c      	   C  s'  | |  _  | |  _ | |  _ | j \ |  _ |  _ t | ƒ |  _ g  t d ƒ D] } | | j d | ^ qL } t j	 | g | t j
 | ƒ j ƒ  f } g  t t | ƒ d ƒ D]; } t j	 t j
 d d g ƒ t | | | | d !f ^ q­ } | |  _ t j d ƒ } g  | D] }	 | |	 ^ q}
 d  S(   Ni   i   i    i   (   t   endogt   exog_bychoicest   ncommont   shapet   nobst   nchoicest   lent   ranget   npt   r_t   arrayt   cumsumt   zt   beta_indicest   arange(   t   selfR   R   R   t   iit   betaindt   ziR   t   betat   idxt   betaidx_bychoices(    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyt   __init__A   s    			.)U	c         C  sm   t  j |  j |  j f ƒ } xK t |  j ƒ D]: } t  j |  j | | |  j | ƒ | d d … | f <q+ W| S(   s   these are the V_i
        N(   R   t   emptyR   R	   R   t   dotR   R   (   R   t   paramst   rest	   choiceind(    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyt   xbetasT   s
    (c         C  sz   |  j  | ƒ } t j | ƒ } | j d ƒ } | | j d ƒ d  d  … d  f } |  j t j | ƒ j d ƒ } | j ƒ  S(   Ni   (   R    R   t   expt   sumt   NoneR   t   log(   R   R   t   xbt   expxbt   sumexpxbt   probst   loglike(    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyR)   ^   s    #c         C  s7   | d  k r t j d ƒ } n  t j |  j | d d ƒS(   Ni   t   maxfuni'  (   R#   R   t   zerosR   t   fminR)   (   R   t   start_params(    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyt   fitj   s    N(   t   __name__t
   __module__t   __doc__R   R    R)   R#   R.   (    (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyR   !   s
   		
	t
   TryNCLogitc           B  s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   si   
    Nested Conditional Logit (RUNMNL), data handling test

    unfinished, doesn't do anything yet

    c      	   C  s'  | |  _  | |  _ | |  _ | j \ |  _ |  _ t | ƒ |  _ g  t d ƒ D] } | | j d | ^ qL } t j	 | g | t j
 | ƒ j ƒ  f } g  t t | ƒ d ƒ D]; } t j	 t j
 d d g ƒ t | | | | d !f ^ q­ } | |  _ t j d ƒ } g  | D] }	 | |	 ^ q}
 d  S(   Ni   i   i    i   (   R   R   R   R   R   R	   R
   R   R   R   R   R   R   R   R   (   R   R   R   R   R   R   R   R   R   R   R   (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyR   x   s    			.)U	c         C  sm   t  j |  j |  j f ƒ } xK t |  j ƒ D]: } t  j |  j | | |  j | ƒ | d d … | f <q+ W| S(   s   these are the V_i
        N(   R   R   R   R	   R   R   R   R   (   R   R   R   R   (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyR    Œ   s
    (c         C  sd   |  j  | ƒ } t j | | ƒ } | j d ƒ } t j | ƒ } | | d  d  … d  f } | t f S(   Ni   (   R    R   R!   R"   R$   R#   t   logsumexpxp(   R   R   t   tauR%   R&   R'   t   logsumexpxbR(   (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyt   loglike_leafbranch–   s    c   
      C  sš   g  } x3 t  D]+ } |  j | | ƒ \ } } | j | ƒ q Wt j | ƒ } t j | | ƒ } | j d ƒ } t j t ƒ }	 | | d  d  … d  f } d  S(   Ni   (
   t   branchesR6   t   appendR   t   column_stackR!   R"   R$   R'   R#   (
   R   R   R4   t   ivst   bR(   t   ivt   exptivt	   sumexptivR5   (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyt   loglike_branch£   s    (   R/   R0   R1   R   R    R6   R?   (    (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyR2   p   s
   		
	i    t   RU2NMNLc           B  s#   e  Z d  Z d „  Z d d „ Z RS(   sE   Nested Multinomial Logit with Random Utility 2 parameterization

    c         C  s:   | |  _  | |  _ | |  _ | |  _ d |  _ i  |  _ d  S(   Nt    (   R   t   datadictt   treet	   paramsindt	   branchsumR(   (   R   R   t   exogRC   RD   (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyR   »   s    					c           s  |  j  } |  j ‰  |  j } |  j } t | t ƒ rô | \ } } t | ˆ  | ƒ t d | ƒ g  } t rv ˆ  | } n | } x. | D]& } t | ƒ | |  j | | ƒ } qƒ Wt d | | ƒ x¼ | D]) }	 |  j	 |	 d | d g |  j	 |	 <qÄ Wnˆ | j
 | ƒ | d d d j |  j | ƒ g |  j	 | <t rot ‡  f d †  | Dƒ ƒ }
 t d | d	 j | ƒ |
 ƒ |
 Sd	 j | ƒ St d
 | | ƒ | S(   s5   walking a tree bottom-up based on dictionary
        t   subtreeRE   t   *s   -probs   (%s)s   , c         3  s   |  ] } ˆ  | Vq d  S(   N(    (   t   .0t   bi(   RB   (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pys	   <genexpr>ä   s    s   final branch withRA   s   working on branch(   R   RB   RD   RE   t
   isinstancet   tuplet   printt   testxbt	   calc_probR(   R8   t   joinR"   (   R   RC   t   keysR   RD   RE   t   nameRG   R;   t   kt	   leavessum(    (   RB   sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyRO   Å   s8    				
*%N(   R/   R0   R1   R   R#   RO   (    (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyR@   ¶   s   	
s   TableF23-2.txtt   skip_headeri   t   namess4   Mode   Ttme   Invc    Invt      GC     Hinc    PSizet   Modei   i   i   t   Airt   Traint   Bust   Cart   GCt   Ttmet   Hinct   Hincat   usemaskt   Consti   geÇF ^×Ã¿gAÔ} R›¸¿gò$éšÉ7‹?gøÂdª`Ô@gÁÊ¡E¶ó@gX¨5Í;N	@R*   i'  i   i   i
   t   topt   Flyt   Groundt   Airdatat	   Traindatat   Busdatat   Cardatat   ConstAt   ConstTt   ConstBs   
modru.probs(]   R1   t
   __future__R    t   statsmodels.compat.pythonR   t   numpyR   t   numpy.lib.recfunctionst   libt   recfunctionst   recft   scipyR   t   objectR   R2   RN   R@   t
   genfromtxtt   splitt   dtat   reshapet   copyR   R   R   R	   t   viewt   floatt	   datafloatRF   RM   R"   t   dtypeRV   t   varnamest   modest   meant   exog_choice_namesR9   RR   t   exog_choiceR
   t   c_R#   t   exog_individualR   t   choice_indext   hincat   append_fieldst   Falset   dta2t   xiR   R   R8   t   onest   dta1t   xivarR   R   R   R   R   R   R   t   betaindicesR   R   t   betait   xxt   xifloatt   clogitt   debugR,   R)   R   t   tab2324t   res2R+   t   res3t   res3corrR.   t   tree0t   dictt   iRB   t   updateRD   t   modruRO   RC   R(   (    (    (    sD   lib/python2.7/site-packages/statsmodels/sandbox/regression/runmnl.pyt   <module>   s¢   OE=2&!1#3)U
.!$)

