<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> class Bunch ( dict ) : <EOL> def __init__ ( self , ** kw ) : <EOL> dict . __init__ ( self , kw ) <EOL> self . __dict__ = self <EOL> def partial_project ( endog , exog ) : <EOL> '''<STR_LIT>''' <EOL> x1 , x2 = endog , exog <EOL> params = np . linalg . pinv ( x2 ) . dot ( x1 ) <EOL> predicted = x2 . dot ( params ) <EOL> residual = x1 - predicted <EOL> res = Bunch ( params = params , <EOL> fittedvalues = predicted , <EOL> resid = residual ) <EOL> return res <EOL> def cancorr ( x1 , x2 , demean = True , standardize = False ) : <EOL> '''<STR_LIT>''' <EOL> if demean or standardize : <EOL> x1 = ( x1 - x1 . mean ( <NUM_LIT:0> ) ) <EOL> x2 = ( x2 - x2 . mean ( <NUM_LIT:0> ) ) <EOL> if standardize : <EOL> x1 /= x1 . std ( <NUM_LIT:0> ) <EOL> x2 /= x2 . std ( <NUM_LIT:0> ) <EOL> t1 = np . linalg . pinv ( x1 ) . dot ( x2 ) <EOL> t2 = np . linalg . pinv ( x2 ) . dot ( x1 ) <EOL> m = t1 . dot ( t2 ) <EOL> cc = np . sqrt ( np . linalg . eigvals ( m ) ) <EOL> cc . sort ( ) <EOL> return cc [ : : - <NUM_LIT:1> ] <EOL> def cc_ranktest ( x1 , x2 , demean = True , fullrank = False ) : <EOL> '''<STR_LIT>''' <EOL> from scipy import stats <EOL> nobs1 , k1 = x1 . shape <EOL> nobs2 , k2 = x2 . shape <EOL> cc = cancorr ( x1 , x2 , demean = demean ) <EOL> cc2 = cc * cc <EOL> if fullrank : <EOL> df = np . abs ( k1 - k2 ) + <NUM_LIT:1> <EOL> value = nobs1 * cc2 [ - <NUM_LIT:1> ] <EOL> w_value = nobs1 * ( cc2 [ - <NUM_LIT:1> ] / ( <NUM_LIT:1.> - cc2 [ - <NUM_LIT:1> ] ) ) <EOL> return value , stats . chi2 . sf ( value , df ) , df , cc , w_value , stats . chi2 . sf ( w_value , df ) <EOL> else : <EOL> r = np . arange ( min ( k1 , k2 ) ) [ : : - <NUM_LIT:1> ] <EOL> df = ( k1 - r ) * ( k2 - r ) <EOL> values = nobs1 * cc2 [ : : - <NUM_LIT:1> ] . cumsum ( ) <EOL> w_values = nobs1 * ( cc2 / ( <NUM_LIT:1.> - cc2 ) ) [ : : - <NUM_LIT:1> ] . cumsum ( ) <EOL> return values , stats . chi2 . sf ( values , df ) , df , cc , w_values , stats . chi2 . sf ( w_values , df ) <EOL> def cc_stats ( x1 , x2 , demean = True ) : <EOL> '''<STR_LIT>''' <EOL> nobs1 , k1 = x1 . shape <EOL> nobs2 , k2 = x2 . shape <EOL> cc = cancorr ( x1 , x2 , demean = demean ) <EOL> cc2 = cc ** <NUM_LIT:2> <EOL> lam = ( cc2 / ( <NUM_LIT:1> - cc2 ) ) <EOL> df_model = k1 * k2 <EOL> df_resid = k1 * ( nobs1 - k2 - demean ) <EOL> s = min ( df_model , k1 ) <EOL> m = <NUM_LIT:0.5> * ( df_model - k1 ) <EOL> n = <NUM_LIT:0.5> * ( df_resid - k1 - <NUM_LIT:1> ) <EOL> df1 = k1 * df_model <EOL> df2 = k2 <EOL> pt_value = cc2 . sum ( ) <EOL> wl_value = np . product ( <NUM_LIT:1> / ( <NUM_LIT:1> + lam ) ) <EOL> ht_value = lam . sum ( ) <EOL> rm_value = lam . max ( ) <EOL> res = { } <EOL> res [ '<STR_LIT>' ] = cc <EOL> res [ '<STR_LIT>' ] = lam <EOL> res [ "<STR_LIT>" ] = pt_value <EOL> res [ "<STR_LIT>" ] = wl_value <EOL> res [ "<STR_LIT>" ] = ht_value <EOL> res [ "<STR_LIT>" ] = rm_value <EOL> res [ '<STR_LIT>' ] = df_resid <EOL> res [ '<STR_LIT>' ] = m <EOL> return res </s>
