
    M/Ph                         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dS )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/
    :0yE>noneNc                 n     t                      j        ||f||d| |                     |           d S )N)missinghasconst)super__init___fit)selfendogexog	tolerancer   r   kwargs	__class__s          `/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/multivariate/cancorr.pyr   zCanCorr.__init__.   sW     	Cg/7	C 	C;A	C 	C 	C		)    c                 h   | j         j        \  }}| j        j        \  }}t          j        ||g          }t          j        | j                  }||                    d          z
  }t          j        | j                   }||                    d          z
  }t          |d          \  }}	}
|
j        }|	|k    }|	                                t          |          k     rt          d          |dd|fxx         |	|         z  cc<   t          |d          \  }}}|j        }||k    }|	                                t          |          k     rt          d          |dd|fxx         ||         z  cc<   t          |j                            |          d          \  }}t          j        fdt          t                              D                       | _        |                    |ddd|f                   | _        |                    |j        ddd|f                   | _        dS )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.c           	      X    g | ]&}t          d t          |         d                    'S )r   r   )maxmin).0iss     r   
<listcomp>z CanCorr._fit.<locals>.<listcomp>X   s/     M M M!QAaD!!5!5 M M Mr   )r   shaper   npr   arraymeanr   Tsumlen
ValueErrordotrange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vr    s                      @r   r   zCanCorr._fit3   s    z'fyfFFF#$$HTYq		MHTZ  q		MAYY
BI~88::D		!!1222aaag"T("AYY
BI~88::D		!!2333aaag"T("bdhhrllA&&1a x M M M MuSVV}} M M MNN1QQQU8,,13qqq"1"u:..r   c           
      4   | 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 ]*}|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k    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        |df<   ,|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   rB   rC   rD   rE   rF   rG   N)r   r"   r   r#   powerr,   pd	DataFramelistr+   r(   sqrtlocscipystatsfsfrJ   valuesr   CanCorrTestResults)r   r/   r0   r1   	eigenvalsrT   prodr   pqrr>   df1tdf2lmdFpvalindstats_mvs                       r   	corr_testzCanCorr.corr_test]   s    z'fyfHT\1--	 &M &M &M#'c)nnq.@"b(I(I#J#JL L L s9~~)2r22 	 	AA	!$$D
A
A"q1uqyAo5AQaAa%CAvQ"Q&&Ga!e\A-!q&16/A2EFGGa%!a%-C(4Q''CSC#%+A48LOEIa001,0EIa()%(EIak"%(EIak"&'EIal#;=##AsC00D%)EIak"	 k 2&	#qqq&! &i&,fdVma6GI I!%222r   )r
   r   N)r
   )__name__
__module____qualname____doc__r   r   rf   __classcell__)r   s   @r   r	   r	      sk         <     
(/ (/ (/ (/T73 73 73 73 73 73 73r   r	   c                   $    e Zd ZdZd Zd Zd ZdS )rX   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                 "    || _         || _        d S N)rT   re   )r   rT   re   s      r   r   zCanCorrTestResults.__init__   s    
 r   c                 N    |                                                                  S rn   )summary__str__)r   s    r   rq   zCanCorrTestResults.__str__   s    ||~~%%'''r   c                    t          j                    }|                    d           |                    | j                   |                    ddi           |                    ddi           |                    | j                   |S )NzCancorr results z,Multivariate Statistics and F Approximations)r   Summary	add_titleadd_dfrT   add_dictre   )r   summs     r   rp   zCanCorrTestResults.summary   s~    !!()))DJr2hErJKKKDM"""r   N)rg   rh   ri   rj   r   rq   rp    r   r   rX   rX      sK        	 	! ! !( ( (    r   rX   )rj   numpyr#   numpy.linalgr   rS   pandasrN   statsmodels.base.modelr   statsmodels.iolibr   multivariate_olsr   r	   rX   ry   r   r   <module>r      s                    ( ( ( ( ( ( & & & & & & 0 0 0 0 0 0E3 E3 E3 E3 E3e E3 E3 E3P         r   