ó
áp7]c           @   sª   d  Z  d d l m Z d d l Z d d l m Z d d l Z d d l Z	 d d l
 m Z d d l m Z d d l m Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   s4   Canonical correlation analysis

author: Yichuan Liu
iÿÿÿÿ(   t   divisionN(   t   svd(   t   Model(   t   summary2i   (   t   multivariate_statst   CanCorrc           B   s5   e  Z d  Z d d d d „ Z d d „ Z d „  Z RS(   s‘  
    Canonical correlation analysis using singluar value decomposition

    For matrices exog=x and endog=y, find projections x_cancoef and y_cancoef
    such that:

        x1 = x * x_cancoef, x1' * x1 is identity matrix
        y1 = y * y_cancoef, y1' * y1 is identity matrix

    and the correlation between x1 and y1 is maximized.

    Attributes
    ----------
    endog : array
        See Parameters.
    exog : array
        See Parameters.
    cancorr : array
        The canonical correlation values
    y_cancoeff: array
        The canonical coeefficients for endog
    x_cancoeff: array
        The canonical coefficients for exog

    References
    ----------
    .. [*] http://numerical.recipes/whp/notes/CanonCorrBySVD.pdf
    .. [*] http://www.csun.edu/~ata20315/psy524/docs/Psy524%20Lecture%208%20CC.pdf
    .. [*] http://www.mathematica-journal.com/2014/06/canonical-correlation-analysis/
    g:Œ0âŽyE>t   nonec         K   s9   t  t |  ƒ j | | d | d | | |  j | ƒ d  S(   Nt   missingt   hasconst(   t   superR   t   __init__t   _fit(   t   selft   endogt   exogt	   toleranceR   R   t   kwargs(    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyR
   2   s    
c         C   s-  |  j  j \ } } |  j j \ } } t j | | g ƒ } t j |  j ƒ } | | j d ƒ } t j |  j  ƒ } | | j d ƒ } t | d ƒ \ } }	 }
 |
 j } |	 | k } | j	 ƒ  t
 | ƒ k  r× t d ƒ ‚ n  | d d … | f c |	 | <t | d ƒ \ } } } | j } | | k } | j	 ƒ  t
 | ƒ k  rKt d ƒ ‚ n  | d d … | f c | | <t | j j | ƒ d ƒ \ } } } t j g  t t
 | ƒ ƒ D]" } t d t | | d ƒ ƒ ^ q¨ƒ |  _ | j | d d … d | … f ƒ |  _ | j | j d d … d | … f ƒ |  _ d S(   s=  Fit the model

        A ValueError is raised if there are singular values smaller than the
        tolerance. The treatment of singular arrays might change in future.

        Parameters
        ----------
        tolerance : float
            eigenvalue tolerance, values smaller than which is considered 0
        i    s   exog is collinear.Ns   endog is collinear.i   (   R   t   shapeR   t   npt   mint   arrayt   meanR   t   Tt   sumt   lent
   ValueErrort   dott   ranget   maxt   cancorrt	   x_cancoeft	   y_cancoef(   R   R   t   nobst   k_yvart   k_xvart   kt   xt   yt   uxt   sxt   vxt   vx_dst   maskt   uyt   syt   vyt   vy_dst   ut   st   vt   i(    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyR   7   s.    	 	 $G(c      	   C   sŽ  |  j  j \ } } |  j j \ } } t j |  j d ƒ } t j d d d d d d d g d	 t t	 t
 | ƒ d
 d d ƒ ƒ ƒ } d
 } xªt	 t
 | ƒ d
 d d ƒ D]Œ} | d
 | | 9} | | } | | }	 | | d
 | |	 d
 d }
 | |	 d d } | |	 } | d |	 d d d k rVt j | |	 d d | d |	 d d ƒ } n d
 } |
 | d | } t j | d
 | ƒ } d
 | | | | } |  j | | j | d f <| | j | d f <| | j | d f <| | j | d f <| | j | d f <t j j j | | | ƒ } | | j | d f <q¢ W| j j d d d … } | j | d d … f } t | | | | | d
 ƒ } t | | ƒ S(   sJ  Approximate F test
        Perform multivariate statistical tests of the hypothesis that
        there is no canonical correlation between endog and exog.
        For each canonical correlation, testing its significance based on
        Wilks' lambda.

        Returns
        -------
        CanCorrTestResults instance

        i   t   columnss   Canonical Correlations   Wilks' lambdas   Num DFs   Den DFs   F Values   Pr > Ft   indexi   iÿÿÿÿi   i   i    N(   R   R   R   R   t   powerR   t   pdt	   DataFramet   listR   R   t   sqrtt   loct   scipyt   statst   ft   sfR4   t   valuesR   t   CanCorrTestResults(   R   R    R!   R"   t	   eigenvalsR<   t   prodR2   t   pt   qt   rR/   t   df1t   tt   df2t   lmdt   Ft   pvalt   indt   stats_mv(    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyt	   corr_testa   sB    %#


2
N(   t   __name__t
   __module__t   __doc__t   NoneR
   R   RN   (    (    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyR      s   *R@   c           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sÿ   
    Canonical correlation results class

    Attributes
    ----------
    stats : DataFrame
        Contain statistical tests results for each canonical correlation
    stats_mv : DataFrame
        Contain the multivariate statistical tests results
    c         C   s   | |  _  | |  _ d  S(   N(   R<   RM   (   R   R<   RM   (    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyR
   §   s    	c         C   s   |  j  ƒ  j ƒ  S(   N(   t   summaryt   __str__(   R   (    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyRT   «   s    c         C   se   t  j ƒ  } | j d ƒ | j |  j ƒ | j i d d 6ƒ | j i d d 6ƒ | j |  j ƒ | S(   Ns   Cancorr resultst    s,   Multivariate Statistics and F Approximations(   R   t   Summaryt	   add_titlet   add_dfR<   t   add_dictRM   (   R   t   summ(    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyRS   ®   s    (   RO   RP   RQ   R
   RT   RS   (    (    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyR@   œ   s   
		(   RQ   t
   __future__R    t   numpyR   t   numpy.linalgR   R;   t   pandasR6   t   statsmodels.base.modelR   t   statsmodels.iolibR   t   multivariate_olsR   R   t   objectR@   (    (    (    s?   lib/python2.7/site-packages/statsmodels/multivariate/cancorr.pyt   <module>   s   ‰