
    "g                     z    d 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  G d d	e	      Z G d
 d      Zy)z4Canonical correlation analysis

author: Yichuan Liu
    N)svd)Model)summary2   )multivariate_statsc                   2     e Zd ZdZd fd	ZddZd Z xZS )CanCorra  
    Canonical correlation analysis using singular 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 : ndarray
        See Parameters.
    exog : ndarray
        See Parameters.
    cancorr : ndarray
        The canonical correlation values
    y_cancoef : ndarray
        The canonical coefficients for endog
    x_cancoef : ndarray
        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/
    c                 P    t        |   ||f||d| | j                  |       y )N)missinghasconst)super__init___fit)selfendogexog	tolerancer   r   kwargs	__class__s          ]/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/multivariate/cancorr.pyr   zCanCorr.__init__.   s5     	Cg/7	C;A	C		)    c                 `   | j                   j                  \  }}| j                  j                  \  }}t        j                  ||g      }t        j
                  | j                        }||j                  d      z
  }t        j
                  | j                         }||j                  d      z
  }t        |d      \  }}	}
|
j                  }|	|kD  }|j                         t        |      k  rt        d      |dd|fxx   |	|   z  cc<   t        |d      \  }}}|j                  }||kD  }|j                         t        |      k  rt        d      |dd|fxx   ||   z  cc<   t        |j                  j                  |      d      \  }}}t        j
                  t        t        |            D cg c]  }t        dt	        ||   d             c}      | _        |j                  |ddd|f         | _        |j                  |j                  ddd|f         | _        yc c}w )a=  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
        r   zexog is collinear.Nzendog is collinear.r   )r   shaper   npminarraymeanr   Tsumlen
ValueErrordotrangemaxcancorr	x_cancoef	y_cancoef)r   r   nobsk_yvark_xvarkxyuxsxvxvx_dsmaskuysyvyvy_dsusvis                        r   r   zCanCorr._fit3   s    zz''fyyfFFFF#$HHTYYq	MHHTZZ q	MAY
BI~88:D	!122ag"T("AY
BI~88:D	!233ag"T("bddhhrlA&1a xxuSV} M!QAaD!!5 MN1QU8,133q"1"u:. !Ns   6 H+c           
         | j                   j                  \  }}| j                  j                  \  }}t        j                  | j
                  d      }t        j                  g dt        t        t        |      dz
  dd                  }d}t        t        |      dz
  dd      D ]M  }|d||   z
  z  }||z
  }||z
  }	||z
  dz
  ||	z
  dz   dz  z
  }
||	z  dz
  dz  }||	z  }|dz  |	dz  z   dz
  dkD  r.t        j                  ||	z  dz  dz
  |dz  |	dz  z   dz
  z        }nd}|
|z  d|z  z
  }t        j                  |d|z        }d|z
  |z  |z  |z  }| j
                  |   |j                  |d	f<   ||j                  |d
f<   ||j                  |df<   ||j                  |df<   ||j                  |df<   t        j                  j                  j!                  |||      }||j                  |df<   P |j"                  j$                  ddd   }|j                  |ddf   }t'        |||||z
  dz
        }t)        ||      S )aI  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
           )Canonical CorrelationWilks' lambdaNum DFDen DFF ValuePr > Fr   )columnsindex      r   r=   r>   r?   r@   rA   rB   N)r   r   r   r   powerr%   pd	DataFramelistr#   r    sqrtlocscipystatsfsfrE   valuesr   CanCorrTestResults)r   r(   r)   r*   	eigenvalsrO   prodr:   pqrr7   df1tdf2lmdFpvalindstats_mvs                       r   	corr_testzCanCorr.corr_test]   sl    zz''fyyfHHT\\1-	 &M#'c)nq.@"b(I#JL s9~)2r2 	AA	!$$D
A
A"q1uqyAo5AQaAa%CAvQ"Q&GGa!e\A-!q&16/A2EFGa%!a%-C((4Q'CSC#%+A48LLOEIIa001,0EIIa()%(EIIak"%(EIIak"&'EIIal#;;==##AsC0D%)EIIak"	+	> kk  2&		#q&! &i&,fdVma6GI!%22r   ):0yE>noneN)rb   )__name__
__module____qualname____doc__r   r   ra   __classcell__)r   s   @r   r	   r	      s    <
(/T73r   r	   c                   "    e Zd ZdZd Zd Zd Zy)rS   z
    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                      || _         || _        y N)rO   r`   )r   rO   r`   s      r   r   zCanCorrTestResults.__init__   s    
 r   c                 >    | j                         j                         S rk   )summary__str__)r   s    r   rn   zCanCorrTestResults.__str__   s    ||~%%''r   c                    t        j                         }|j                  d       |j                  | j                         |j                  ddi       |j                  ddi       |j                  | j                         |S )NzCancorr results z,Multivariate Statistics and F Approximations)r   Summary	add_titleadd_dfrO   add_dictr`   )r   summs     r   rm   zCanCorrTestResults.summary   sd    !()DJJr2hErJKDMM"r   N)rd   re   rf   rg   r   rn   rm    r   r   rS   rS      s    	!(r   rS   )rg   numpyr   numpy.linalgr   rN   pandasrI   statsmodels.base.modelr   statsmodels.iolibr   multivariate_olsr   r	   rS   rv   r   r   <module>r}      s:        ( & 0E3e E3P r   