ó
áp7]c           @   s  d  Z  d d l m Z d d l m Z m Z m Z m Z m Z d d l	 Z	 d d l
 Z d Z i  Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ e d „ Z d d „ Z e d d e ƒZ d e _  d d g d „ Z d „  Z d S(   s©   
Provides the basic classes needed to specify statistical models.



namespace : dictionary
   mapping from names to data, used to associate data to a formula or term


iÿÿÿÿ(   t   OrderedDict(   t   iterkeyst   lranget   string_typest
   itervaluest   rangeNt   restructuredtextt   Termc           B   s€   e  Z d  Z d „  Z d d d „ Z d „  Z d „  Z d „  Z e	 e e e ƒ Z
 d „  Z d „  Z d „  Z d	 „  Z d
 „  Z RS(   sˆ  
    This class is very simple: it is just a named term in a model formula.

    It is also callable: by default it namespace[self.name], where namespace
    defaults to formula.default_namespace.
    When called in an instance of formula,
    the namespace used is that formula's namespace.

    Inheritance of the namespace under +,*,- operations:
    ----------------------------------------------------

    By default, the namespace is empty, which means it must be
    specified before evaluating the design matrix.

    When it is unambiguous, the namespaces of objects are derived from the
    context.

    Rules:
    ------

    i) "X * I", "X + I", "X**i": these inherit X's namespace
    ii) "F.main_effect()": this inherits the Factor F's namespace
    iii) "A-B": this inherits A's namespace
    iv) if A.namespace == B.namespace, then A+B inherits this namespace
    v) if A.namespace == B.namespace, then A*B inherits this namespace

    Equality of namespaces:
    -----------------------

    This is done by comparing the namespaces directly, if
    an exception is raised in the check of equality, they are
    assumed not to be equal.
    c            s¡   y t  ˆ  ƒ ‰  Wn t d ƒ ‚ n Xˆ  t ˆ  ƒ k rT d |  j t ˆ  ƒ f } n d |  j ˆ  f } t | d |  d ‡  f d †  ƒ} ˆ  | _ |  j | _ | S(   sd   
        Raise the quantitative term's values to an integer power, i.e.
        polynomial.
        s   expecting a floats   %s^%ds   %s^%0.2ft   funct	   transformc            s   t  j |  ˆ  ƒ S(   N(   t   npt   power(   t   x(   R   (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   <lambda>H   t    (   t   floatt
   ValueErrort   intt   namet   QuantitativeR   t	   namespace(   t   selfR   R   t   value(    (   R   s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   __pow__8   s    !	c         C   sj   | |  _  d  |  _ | d  k r* | |  _ n	 | |  _ t |  j t ƒ sT t d ƒ ‚ n  | rf | |  _ n  d  S(   Ns   expecting a string for termname(   R   t   Nonet   _Term__namespacet   termnamet
   isinstanceR   R   R   (   R   R   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   __init__M   s    			c         C   s-   t  |  j t j ƒ r |  j S|  j p( t Sd  S(   N(   R   R   R
   t   ndarrayt   default_namespace(   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   _get_namespace]   s    c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   _set_namespaceb   R   c         C   s
   |  `  d  S(   N(   R   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   _del_namespacec   R   c         C   s   d |  j  S(   s.   
        '<term: %s>' % self.termname
        s
   <term: %s>(   R   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   __str__f   s    c         C   sG   t  | d | j ƒ} | |  } t | j |  j ƒ rC |  j | _ n  | S(   s0   
        Formula(self) + Formula(other)
        R   (   t   FormulaR   t   _namespace_equal(   R   t   othert   fothert   f(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   __add__l   s
    
c         C   s¤   t  | t ƒ r6 | j d k r6 t |  d |  j ƒ} nF |  j d k r] t | d | j ƒ} n t | d | j ƒ} | |  } t | j |  j ƒ r  |  j | _ n  | S(   s0   
        Formula(self) * Formula(other)
        t	   interceptR   (   R   R   R   R#   R   R$   (   R   R%   R'   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   __mul__v   s    
c         C   s-   t  |  j t ƒ r |  j g St |  j ƒ Sd S(   s†   
        Return the names of the columns in design associated to the terms,
        i.e. len(self.names()) = self().shape[0].
        N(   R   R   R   t   list(   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   names†   s    
c         O   s˜   t  |  d ƒ s" |  j |  j } n	 |  j } t | ƒ r| t | t t f ƒ rj t j | ƒ } |  j | _ n  | | | Ž  } n  t	 j
 | ƒ } t	 j | ƒ S(   sé   
        Return the columns associated to self in a design matrix.
        If the term has no 'func' attribute, it returns
        ``self.namespace[self.termname]``
        else, it returns
        ``self.func(*args, **kw)``
        R   (   t   hasattrR   R   R   t   callableR   R   R#   t   copyR
   t   asarrayt   squeeze(   R   t   argst   kwt   val(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   __call__   s    		N(   t   __name__t
   __module__t   __doc__R   R   R   R   R    R!   t   propertyR   R"   R(   R*   R,   R5   (    (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR      s   !						
		
t   Factorc           B   sS   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d d „ Z
 d „  Z RS(	   s   A categorical factor.c         C   sì   | s+ t  t | ƒ ƒ |  _ |  j j ƒ  n< | |  _ t t | ƒ ƒ t t  | ƒ ƒ k rg t d ƒ ‚ n  | |  _ | |  _ | |  _ |  j r— |  j } n/ g  |  j D] } d |  j t	 | ƒ f ^ q¡ } t
 j |  | d |  j d |  j ƒd S(   s&  
        Factor is initialized with keys, representing all valid
        levels of the factor.

        If ordinal is False, keys can have repeats: set(keys) is what is
        used.

        If ordinal is True, the order is taken from the keys, and
        there should be no repeats.
        s=   keys for ordinal Factor should be unique, in increasing orders   (%s==%s)R   R   N(   R+   t   sett   keyst   sortt   lenR   t   _nameR   t   ordinalt   strR   R   t   get_columns(   R   R   R<   R@   R   t   key(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR   ©   s    	$				/c   	      O   s-  |  j  |  j } x\ t rn t | ƒ rj t | t t f ƒ rX t j | ƒ } |  j  | _  n  | | | Ž  } q Pq Wt | ƒ } |  j	 rÆ g  t
 | ƒ D]" } t |  j j | | ƒ ƒ ^ q‘ } t j | ƒ Sg  } xM |  j D]B } g  t
 | ƒ D] } t | | | k ƒ ^ qé } | j | ƒ qÖ Wt j | ƒ Sd S(   s7   
        Calling function for factor instance.
        N(   R   R?   t   TrueR.   R   R   R#   R/   R>   R@   R   R   R<   t   indexR
   t   arrayt   append(	   R   R2   R3   t   vt   nt   it   colR   RC   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyRB   Ç   s"    		5/c         O   s%   |  `  |  | | Ž  } |  j |  _  | S(   sf   
        Return the keys of the factor, rather than the columns of the design
        matrix.
        (   R   RB   (   R   R2   R3   R4   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   valuesâ   s    c         C   s1   t  | ƒ } | j |  j ƒ s- t d ƒ ‚ n  d S(   sJ   
        Verify that all values correspond to valid keys in self.
        s   unknown keys in valuesN(   R;   t   issubsetR<   R   (   R   RL   t   s(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   verifyí   s    c         C   sE   t  | t ƒ r1 | j d k r1 t |  d |  j ƒSt j |  | ƒ Sd S(   s¢   
        Formula(self) + Formula(other)

        When adding 'intercept' to a factor, this just returns

        Formula(self, namespace=self.namespace)

        R)   R   N(   R   R   R   R#   R   R(   (   R   R%   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR(   õ   s    
c   	      C   s  |  j  ƒ  } | d k r! d } n6 y |  j j | ƒ } Wn t k
 rV t | ƒ } n X| d „ } t t |  j  ƒ  ƒ ƒ } | j | ƒ |  j  ƒ  } g  t	 t | ƒ ƒ D]" } d | | | | | f ^ q§ } t
 | d |  d d |  j d | ƒ} |  j | _ | S(	   s  
        Return the 'main effect' columns of a factor, choosing
        an optional reference key.

        The reference key can be one of the keys of the Factor,
        or an integer, representing which column to remove.
        It defaults to 0.

        i    c         S   sm   g  } t  |  j d ƒ } | j | ƒ x7 t t | ƒ ƒ D]# } | j |  | | |  | ƒ q9 Wt j | ƒ S(   Ni    (   R   t   shapet   popR   R>   RG   R
   RF   (   R   t	   referencet   rvaluet   keepRJ   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   maineffect_func  s    !s   %s-%sR   R   s   %s:maineffectR	   N(   R,   R   R<   RE   R   R   R   R>   RQ   R   R   R   R   (	   R   RR   R,   RU   RT   t   _Factor__namesRJ   t   _namesR   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   main_effect  s"    	;	c         C   s‚   |  j  s< |  j ƒ  j d |  j t | ƒ f ƒ } |  ƒ  | S|  j |  j } t j g  | D] } | | k ^ qY ƒ j	 t j
 ƒ Sd S(   s  
        Retrieve the column corresponding to key in a Formula.

        Parameters
        ----------
        key : Factor key
            one of the Factor's keys

        Returns
        -------
        ndarray corresponding to key, when evaluated in current namespace
        s   (%s==%s)N(   R@   R,   RE   R   RA   R   R?   R
   RF   t   astypeR   (   R   RC   RJ   RH   t   vv(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   __getitem__+  s
    	(N(   R6   R7   R8   t   FalseR   RB   RL   RO   R(   R   RX   R[   (    (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR:   ¦   s   				'R   c           B   s,   e  Z d  Z d d d „  d „ Z d „  Z RS(   s
  
    A subclass of term that can be used to apply point transformations
    of another term, i.e. to take powers:

    >>> import numpy as np
    >>> from nipy.fixes.scipy.stats.models import formula
    >>> X = np.linspace(0,10,101)
    >>> x = formula.Term('X')
    >>> x.namespace={'X':X}
    >>> x2 = x**2
    >>> print np.allclose(x()**2, x2())
    True
    >>> x3 = formula.Quantitative('x2', func=x, transform=lambda x: x**2)
    >>> x3.namespace = x.namespace
    >>> print np.allclose(x()**2, x3())
    True

    c         C   s   |  S(   N(    (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR   T  R   c         C   s)   | |  _  t j |  | d | d | ƒd  S(   NR   R   (   R	   R   R   (   R   R   R   R   R	   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR   T  s    	c         O   s   |  j  t j |  | | Ž ƒ S(   sz   
        A quantitative is just like term, except there is an additional
        transformation: self.transform.

        (   R	   R   R5   (   R   R2   R3   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR5   X  s    N(   R6   R7   R8   R   R   R5   (    (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR   @  s   R#   c           B   s¹   e  Z d  Z d „  Z d „  Z d „  Z e e e e ƒ Z d „  Z e	 d „ Z
 d „  Z d „  Z d „  Z d	 „  Z e d
 „ Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z RS(   s8  
    A formula object for manipulating design matrices in regression models,
    essentially consisting of a list of term instances.

    The object supports addition and multiplication which correspond
    to concatenation and pairwise multiplication, respectively,
    of the columns of the two formulas.

    c         C   s-   t  |  j t j ƒ r |  j S|  j p( t Sd  S(   N(   R   t   _Formula__namespaceR
   R   R   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR   k  s    c         C   s   | |  _  d  S(   N(   R]   (   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR    p  R   c         C   s
   |  `  d  S(   N(   R]   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR!   q  R   c         C   s"   |  j  ƒ  |  _ |  j ƒ  |  _ d  S(   N(   R,   RW   t	   termnamest
   _termnames(   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   _terms_changedt  s    c         C   sƒ   | |  _  t | t ƒ r6 t j t | j ƒ ƒ |  _ n? t | t ƒ rQ | |  _ n$ t | t ƒ ro | g |  _ n t ‚ |  j ƒ  d S(   s“   
        Create a formula from either:
         i. a `formula` object
         ii. a sequence of `term` instances
         iii. one `term`
        N(	   R]   R   R#   R/   R+   t   termsR   R   R`   (   R   t   termlistR   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR   x  s    		c         C   s;   g  } x! |  j  D] } | | j g 7} q Wd d j | ƒ S(   sJ   
        String representation of list of termnames of a formula.
        s   <formula: %s>s    + (   Ra   R   t   join(   R   R   t   term(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR"     s    c         O   s  d | k r | d } n	 |  j  } g  } t } d } xÝ |  j D]Ò } t j | ƒ } | | _  | | | Ž  } t }	 t | d ƒ r´ | j d k r´ t } t }	 | }
 | j d	 ƒ q´ n  | j	 d k rð |	 rð d | j
 d f | _
 | j | ƒ n |	 s| j | ƒ n  | d 7} q> W| s:y t j | ƒ } WqôqôXnº | j d d ƒ } | g  k rº|
 d k rx| d j
 d } n | d j
 d } t j d | f t j ƒ | |
 <t j | ƒ } n: | d k rÕt d ƒ ‚ n t d | ƒ } d
 | j
 | _
 t j | ƒ S(   s  
        Create (transpose) of the design matrix of the formula within
        namespace. Extra arguments are passed to each term instance. If
        the formula just contains an intercept, then the keyword
        argument 'nrow' indicates the number of rows (observations).
        R   i    R   R)   i   t   nrowiÿÿÿÿs>   with only intercept in formula, keyword 'nrow' argument neededN(   i   (   R   R\   Ra   R/   R-   R   RD   RG   R   t   ndimRP   R
   t   concatenatet   gett   onest   float64R   t   IR1   (   R   R2   R3   R   t   allvalsR)   t   iindext   tR4   t   isinterceptt   interceptindexRe   RI   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR5   –  sP    			c         C   s·   t  | t ƒ sr t  | t ƒ rM y! |  | } | j |  j ƒ  k SWqo t SXq³ t  | t ƒ r³ | j |  j ƒ  k SnA t | j ƒ d k r§ | j d } | j |  j ƒ  k St	 d ƒ ‚ d S(   sA   
        Determine whether a given term is in a formula.
        i   i    s$   more than one term passed to hastermN(
   R   R#   R   R   R^   R\   R   R>   Ra   R   (   R   t
   query_termt   query(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   hastermÑ  s    
c         C   sF   |  j  ƒ  } | | k r, |  j | j | ƒ St d t | ƒ ƒ ‚ d  S(   Ns   formula has no such term: %s(   R^   Ra   RE   t   KeyErrort   repr(   R   R   Rn   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR[   å  s    c         C   sx   |  j  | ƒ rN | j ƒ  } t ƒ  } x3 | D] } |  j j | ƒ | | <q+ Wn t d ƒ ‚ | rd | St t | ƒ ƒ Sd S(   sa   
        Return a list of the indices of all columns associated
        to a given term.
        s   term not in formulaN(   Rs   R,   R    RW   RE   R   R+   R   (   R   Rq   t   dictR,   R   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   termcolumnsì  s    	c         C   s.   g  } x! |  j  D] } | | j ƒ  7} q W| S(   s£   
        Return a list of the names in the formula. The order of the
        names corresponds to the order of the columns when self
        is evaluated.
        (   Ra   R,   (   R   t   allnamesRd   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR,   þ  s    c         C   s.   g  } x! |  j  D] } | | j g 7} q W| S(   s|   
        Return a list of the term names in the formula. These
        are the names of each term instance in self.
        (   Ra   R   (   R   R,   Rd   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR^   
  s    c         O   s   |  | | Ž  j  S(   s2   
        ``transpose(self(*args, **kw))``
        (   t   T(   R   R2   R3   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   design  s    c      
   C   s£  t  | ƒ } |  j ƒ  } | j ƒ  } t | ƒ } t | ƒ } g  } g  } xNt | ƒ D]@}	 x7t | ƒ D])}
 d t | |	 ƒ t | |
 ƒ f } t | j d ƒ ƒ } d j | ƒ } | j | ƒ |  j	 |	 j
 ƒ  } | j	 |
 j
 ƒ  } |  j	 |	 j d k r| j	 |
 } | j | _ ni| j	 |
 j d k rM|  j	 |	 } |  j | _ n7g  } t | ƒ } t | ƒ } x€ t | ƒ D]r } xi t | ƒ D][ } d t | | ƒ t | | ƒ f } t | j d ƒ ƒ } d j | ƒ } | j | ƒ q‹WqxW| | d „ } t j |  j	 |	 ƒ } t j | j	 |
 ƒ } | | } | | g | _	 t | d | d | d | ƒ} t |  j | j ƒ r„|  j | _ n  | j | ƒ qh WqU Wt  | ƒ S(   sÆ   
        This returns a formula whose columns are the pairwise
        product of the columns of self and other.

        TO DO: check for nesting relationship. Should not be too difficult.
        s   %s*%st   *R)   c         S   s^   g  } xH t  | ƒ D]: } x1 t  | ƒ D]# } | j |  | |  | | ƒ q& Wq Wt j | ƒ S(   N(   R   RG   R
   RF   (   R   t   d1t   d2t   outt   rRN   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   product_funcK  s
    %R   R   R	   (   R#   R^   R>   R   RA   t   sortedt   splitRc   RG   Ra   R,   R   R   R/   R   R$   (   R   R%   t   nestedt   selftermnamest   othertermnamest   n_term_namest   JRa   R^   RJ   t   jR   t   piecest	   selfnamest
   othernamest   _termR,   R|   R}   R   RN   R   R€   t   cselft   cothert   sumterms(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR*     sT    $$
	c         C   s­   d „  } t  | ƒ } |  j | j } t g  | D] } | | j ƒ | f ^ q/ d d „  ƒ} g  | D] } | d ^ qc } t  | ƒ } t |  j | j ƒ r© |  j | _ n  | S(   s©   
        Return a formula whose columns are the
        concatenation of the columns of self and other.

        terms in the formula are sorted alphabetically.
        c         S   s   t  |  t ƒ r |  d S|  Sd  S(   Ni    (   R   R+   (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   _delistl  s    RC   c         S   s   |  d S(   Ni    (    (   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR   t  R   i   (   R#   Ra   R   R   R$   R   (   R   R%   R   Ra   Rd   R‰   t   pieceR'   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR(   c  s    		7c         C   s’   t  | ƒ } t j |  j ƒ } xU | j D]J } xA t t | ƒ ƒ D]- } | | j | j k rA | j | ƒ PqA qA Wq( Wt  | ƒ } |  j | _ | S(   s¹   
        Return a formula with all terms in other removed from self.
        If other contains term instances not in formula, this
        function does not raise an exception.
        (   R#   R/   Ra   R   R>   R   RQ   R   (   R   R%   Ra   Rd   RJ   R'   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   __sub__{  s    (   R6   R7   R8   R   R    R!   R9   R   R`   R   R   R"   R5   Rs   R[   R\   Rw   R,   R^   Rz   R*   R(   R’   (    (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR#   `  s$   								;					H	c         C   sC  | d k	 r? t j |  ƒ }  | |  _ t j | ƒ } | | _ n  |  d t ƒ d } | d t ƒ d } t | ƒ t | ƒ k rŒ t d ƒ ‚ n  t t | ƒ ƒ } t t | ƒ ƒ } t | | ƒ } g  t t | ƒ ƒ D] } | | | | f ^ qÒ }	 t t |	 ƒ ƒ }
 |
 | k r5| | k r%|  } n | } t | f St	 d f Sd S(   só   
    Is factor B nested within factor A or vice versa: a very crude test
    which depends on the namespace.

    If they are nested, returns (True, F) where F is the finest
    level of the relationship. Otherwise, returns (False, None)

    RL   i    s2   A() and B() should be sequences of the same lengthN(
   R   R/   R   RD   R>   R   R;   t   maxR   R\   (   t   At   BR   t   at   bt   nAt   nBRI   RJ   t   ABt   nABt   F(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   isnested  s(    
	3	
i   c         K   s   t  j d |  f ƒ S(   Ni   (   R
   Ri   (   Re   t   extra(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   _intercept_fnµ  s    R)   R   s1  
Intercept term in a formula. If intercept is the
only term in the formula, then a keyword argument
'nrow' is needed.

>>> from nipy.fixes.scipy.stats.models.formula import Formula, I
>>> I()
array(1.0)
>>> I(nrow=5)
array([ 1.,  1.,  1.,  1.,  1.])
>>> f=Formula(I)
>>> f(nrow=5)
array([1, 1, 1, 1, 1])

i   c         C   s	  t  |  ƒ } i  } t j | ƒ j d k rF t d t | ƒ d ƒ } n  xt| D]l} t j | f | ƒ } | j d t j | j d ƒ f | _ x't | j d ƒ D]} t j	 | d d … | f ƒ j | d d … | f j k r£ t j
 t j t j | d d … | f ƒ | d d … | f ƒ ƒ r£ g  | d d … | f D] } |  | ^ q?} | d }	 x0 t t  | ƒ d ƒ D] }
 |	 | |
 d 9}	 qvW|	 | t | d d … | f ƒ <q£ q£ WqM Wt t | ƒ ƒ d } | | } | | =x t | ƒ D] }	 | |	 7} qñW| S(   s
  
    Output all pairwise interactions of given order of a
    sequence of terms.

    The argument order is a sequence specifying which order
    of interactions should be generated -- the default
    creates main effects and two-way interactions. If order
    is an integer, it is changed to range(1,order+1), so
    order=3 is equivalent to order=[1,2,3], generating
    all one, two and three-way interactions.

    If any entry of order is greater than len(terms), it is
    effectively treated as len(terms).

    >>> print interactions([Term(l) for l in ['a', 'b', 'c']])
    <formula: a*b + a*c + b*c + a + b + c>
    >>>
    >>> print interactions([Term(l) for l in ['a', 'b', 'c']], order=list(range(5)))
    <formula: a*b + a*b*c + a*c + b*c + a + b + c>
    >>>

    i   i    N(    (   R>   R
   R0   RP   R   R   t   indicest   productR   t   uniquet   alltruet   equalR=   t   tupleR+   R   R   (   Ra   t   ordert   n_termsRL   t   oR    t   mRˆ   t   llRH   t   iiRC   R   (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   interactionsÉ  s*    &;D-
+
c         C   s
   |  | k S(   N(    (   t   space1t   space2(    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyR$      s    (   R8   t   collectionsR    t   statsmodels.compat.pythonR   R   R   R   R   R/   t   numpyR
   t   __docformat__R   t   objectR   R:   R   R#   R   R   RŸ   Rk   R¬   R$   (    (    (    s:   lib/python2.7/site-packages/statsmodels/sandbox/formula.pyt   <module>
   s"   (‘š ÿ 0&	7