ó
áp7]c           @   st   d  d l  Z  d  d l Z d  d l m Z d  d l m Z d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d d „ Z d S(	   iÿÿÿÿN(   t   pinv(   t	   utils_oldt   ContrastResultsc           B   s;   e  Z d  Z d d d d d d d „ Z d „  Z d „  Z RS(   s¸   
    Results from looking at a particular contrast of coefficients in
    a parametric model. The class does nothing, it is a container
    for the results from T and F contrasts.
    c         C   sR   | d  k	 r* | |  _ | |  _ | |  _ n$ | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   Nonet   Ft   df_denomt   df_numt   tt   sdt   effect(   t   selfR   R   R   R	   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyt   __init__   s    					c         C   s!   t  |  d ƒ r |  j S|  j Sd  S(   NR   (   t   hasattrR   R   (   R
   (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyt	   __array__   s    c         C   se   t  |  d ƒ r/ d t |  j ƒ |  j |  j f Sd t |  j ƒ t |  j ƒ t |  j ƒ |  j f Sd  S(   NR   s*   <F contrast: F=%s, df_denom=%d, df_num=%d>s1   <T contrast: effect=%s, sd=%s, t=%s, df_denom=%d>(   R   t   reprR   R   R   R	   R   R   (   R
   (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyt   __str__    s
    N(   t   __name__t
   __module__t   __doc__R   R   R   R   (    (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyR      s
   	t   Contrastc           B   sA   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z e e ƒ Z RS(   sh  
    This class is used to construct contrast matrices in regression models.
    They are specified by a (term, formula) pair.

    The term, T,  is a linear combination of columns of the design
    matrix D=formula(). The matrix attribute is
    a contrast matrix C so that

    colspan(dot(D, C)) = colspan(dot(D, dot(pinv(D), T)))

    where pinv(D) is the generalized inverse of D. Further, the matrix

    Tnew = dot(C, D)

    is full rank. The rank attribute is the rank of

    dot(D, dot(pinv(D), T))

    In a regression model, the contrast tests that E(dot(Tnew, Y)) = 0
    for each column of Tnew.

    t    c         C   s=   | |  _  | |  _ | d k r0 t | ƒ |  _ n	 | |  _ d  S(   NR   (   t   termt   formulat   strt   name(   R
   R   R   R   (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyR   A   s
    		c         C   s.   d t  i t |  j ƒ d 6t |  j ƒ d 6ƒ S(   Ns   <contrast:%s>R   R   (   R   R   R   R   (   R
   (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyR   I   s    c         O   sÚ   t  j  |  j ƒ } |  j j | _ t j t j | | | Ž  ƒ ƒ } | j d k rj | j d d f | _ n  t	 j
 | ƒ |  _ |  j j | | Ž  |  _ t |  j |  j ƒ |  _ y |  j j d |  _ Wn d |  _ n Xd S(   s   
        Construct a contrast matrix C so that

        colspan(dot(D, C)) = colspan(dot(D, dot(pinv(D), T)))

        where pinv(D) is the generalized inverse of D=self.D=self.formula().

        If the design, self.D is already set,
        then evaldesign can be set to False.
        i   i    N(   t   copyR   R   t	   namespacet   npt	   transposet   arrayt   ndimt   shapet   utilst   clean0t   Tt   designt   Dt   contrastfromcolst   _matrixt   matrixt   rank(   R
   t   argst   kwR   R"   (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyt   compute_matrixM   s    !c         C   s#   t  |  d ƒ s |  j ƒ  n  |  j S(   s`   
        This will fail if the formula needs arguments to construct
        the design.
        R&   (   R   R+   R&   (   R
   (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyt   _get_matrixj   s    (	   R   R   R   R   R   R+   R,   t   propertyR'   (    (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyR   )   s   			c         C   s^  t  j |  ƒ }  t  j | ƒ } | j \ } } |  j d | k rb |  j d | k rb t d ƒ ‚ n  | d k r} t | ƒ } n  |  j d | k r¨ t  j | |  ƒ j } n* |  } t  j | t  j | | j ƒ ƒ j } t  j | | j ƒ } t | j ƒ d k r| d f | _ n  t	 j
 | ƒ | j d k rQt	 j | ƒ } t  j | | ƒ j } n  t  j | ƒ S(   sV  
    From an n x p design matrix D and a matrix L, tries
    to determine a p x q contrast matrix C which
    determines a contrast of full rank, i.e. the
    n x q matrix

    dot(transpose(C), pinv(D))

    is full rank.

    L must satisfy either L.shape[0] == n or L.shape[1] == p.

    If L.shape[0] == n, then L is thought of as representing
    columns in the column space of D.

    If L.shape[1] == p, then L is thought of as what is known
    as a contrast matrix. In this case, this function returns an estimable
    contrast corresponding to the dot(D, L.T)

    Note that this always produces a meaningful contrast, not always
    with the intended properties because q is always non-zero unless
    L is identically 0. That is, it produces a contrast that spans
    the column space of L (after projection onto the column space of D).

    i    i   s   shape of L and D mismatchedN(   R   t   asarrayR   t
   ValueErrorR   R    t   dotR"   t   lenR    R(   t   fullrankt   squeeze(   t   LR$   t   pseudot   nt   pt   Ct   Lp(    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyR%   t   s$    &$(   R   t   numpyR   t   numpy.linalgR    t   statsmodels.sandboxR   R    t   objectR   R   R   R%   (    (    (    s?   lib/python2.7/site-packages/statsmodels/sandbox/contrast_old.pyt   <module>   s   "K