def find_pairs( i , n , min_price ): # 開啟matlab引擎 #eng=matlab.engine.start_matlab() # 選擇適合的 VECM model,並且檢定 formation period 是否有結構性斷裂,並刪除該配對,其餘配對則回傳共整合係數。 #rank = 1 #t1 = int(len(min_price)*3/4) # 一天的時間長度(偵測兩天中間是否有結構性斷裂) local_select_model = [] local_weight = [] local_name = [] local_pval = [] for j in range(i+1,n+1): stock1 = min_price.iloc[:,i] stock2 = min_price.iloc[:,j] stock1_name = min_price.columns.values[i] stock2_name = min_price.columns.values[j] z = ( np.vstack( [stock1 , stock2] ).T ) model = VAR(z) p = order_select(z,5) #p = int(model.select_order(5).bic) # VAR 至少需落後1期 if p < 1: continue # portmanteau test if model.fit(p).test_whiteness( nlags = 5 ).pvalue < 0.05: continue # Normality test if model.fit(p).test_normality().pvalue < 0.05: continue #r1 = eng.rank_jci( matlab.double(z.tolist()) , 'H2' , (p-1) ) #r2 = eng.rank_jci( matlab.double(z.tolist()) , 'H1*' , (p-1)) #r3 = eng.rank_jci( matlab.double(z.tolist()) , 'H1' , (p-1) ) r1 = rank( pd.DataFrame(z) , 'H2' , p ) r2 = rank( pd.DataFrame(z) , 'H1*' , p ) r3 = rank( pd.DataFrame(z) , 'H1' , p ) #r4 = rank( pd.DataFrame(z) , 'H*' , p ) if r3 > 0: # 在 model 3 上有 rank if r2 > 0: # 在 model 2 上有 rank if r1 > 0: # select model 1 and model 2 and model 3 #lambda_model2 = eng.eig_jci( matlab.double(z.tolist()) , 'H1*' , (p-1) , r2 ) #lambda_model3 = eng.eig_jci( matlab.double(z.tolist()) , 'H1' , (p-1) , r2 ) lambda_model2 = eig( pd.DataFrame(z) , 'H1*' , p , r2 ) lambda_model3 = eig( pd.DataFrame(z) , 'H1' , p , r2 ) test = np.log(lambda_model2/lambda_model3) * (len(min_price)-p) if test <= 0: raise ValueError('test value error') if test > 3.8414: #bp1 = chow_test( z , t1 , p , 'H1' , r3 ) #if bp1 == 0: local_select_model.append('model3') #weight.append( eng.coin_jci( matlab.double(z.tolist()) , 'H1' , (p-1) , r3 ) ) local_weight.append( weigh( pd.DataFrame(z) , 'H1' , p , r3 ) ) local_name.append([stock1_name,stock2_name]) local_pval.append( vecm_pvalue('model3', vecm( pd.DataFrame(z),'H1',p)[0][0] ) ) else: #lambda_model1 = eng.eig_jci( matlab.double(z.tolist()) , 'H2' , (p-1) , r1 ) lambda_model1 = eig( pd.DataFrame(z) , 'H2' , p , r1 ) test = np.log(lambda_model1/lambda_model2) * (len(min_price)-p) if test > 3.8414: #bp1 = chow_test( z , t1 , p , 'H1*' , r2 ) #if bp1 == 0: local_select_model.append('model2') #weight.append( eng.coin_jci( matlab.double(z.tolist()) , 'H1*' , (p-1) , r2 ) ) local_weight.append( weigh( pd.DataFrame(z) , 'H1*' , p , r2 ) ) local_name.append([stock1_name,stock2_name]) local_pval.append( vecm_pvalue('model2',vecm(pd.DataFrame(z),'H1*',p)[0][1] ) ) else: #bp1 = chow_test( z , t1 , p , 'H2' , r1 ) #if bp1 == 0: local_select_model.append('model1') #weight.append( eng.coin_jci( matlab.double(z.tolist()) , 'H2' , (p-1) , r1 ) ) local_weight.append( weigh( pd.DataFrame(z) , 'H2' , p , r1 ) ) local_name.append([stock1_name,stock2_name]) local_pval.append( vecm_pvalue('model1',vecm(pd.DataFrame(z),'H2',p)[0][0] ) ) else: # select model 2 and model 3 #lambda_model2 = eng.eig_jci( matlab.double(z.tolist()) , 'H1*' , (p-1) , r2 ) #lambda_model3 = eng.eig_jci( matlab.double(z.tolist()) , 'H1' , (p-1) , r2 ) lambda_model2 = eig( pd.DataFrame(z) , 'H1*' , p , r2 ) lambda_model3 = eig( pd.DataFrame(z) , 'H1' , p , r2 ) test = np.log(lambda_model2/lambda_model3) * (len(min_price)-p) if test <= 0: raise ValueError('test value error') if test > 3.8414: #bp1 = chow_test( z , t1 , p , 'H1' , r3 ) #if bp1 == 0: local_select_model.append('model3') #weight.append( eng.coin_jci( matlab.double(z.tolist()) , 'H1' , (p-1) , r3 ) ) local_weight.append( weigh( pd.DataFrame(z) , 'H1' , p , r3 ) ) local_name.append([stock1_name,stock2_name]) local_pval.append( vecm_pvalue('model3',vecm(pd.DataFrame(z),'H1',p)[0][0] ) ) else: #bp1 = chow_test( z , t1 , p , 'H1*' , r2 ) #if bp1 == 0: local_select_model.append('model2') #weight.append( eng.coin_jci( matlab.double(z.tolist()) , 'H1*' , (p-1) , r2 ) ) local_weight.append( weigh( pd.DataFrame(z) , 'H1*' , p , r2 ) ) local_name.append([stock1_name,stock2_name]) local_pval.append( vecm_pvalue('model2',vecm(pd.DataFrame(z),'H1*',p)[0][1] ) ) else : # 只在 model 3 上有rank #bp1 = chow_test( z , t1 , p , 'H1' , r3 ) #if bp1 == 0: local_select_model.append('model3') #weight.append( eng.coin_jci( matlab.double(z.tolist()) , 'H1' , (p-1) , r3 ) ) local_weight.append( weigh( pd.DataFrame(z) , 'H1' , p , r3 ) ) local_name.append([stock1_name,stock2_name]) local_pval.append( vecm_pvalue('model3',vecm(pd.DataFrame(z),'H1',p)[0][0] ) ) else: # 表示此配對無rank continue #local_pval.append(1) #if VECM residuals 不是常態則搜尋下一個配對 #if JB_VECM( stock1 , stock2 , local_select_model , p) == 1: #continue # 關閉 matlab 引擎 #eng.quit() return local_weight, local_name, local_select_model , local_pval
def find_pairs(i, n, min_price): local_select_model = [] local_weight = [] local_name = [] local_pval = [] for j in range(1): stock1 = min_price[[i]] stock2 = min_price[[n]] stock1_name = stock_number1 stock2_name = stock_number2 z = (np.hstack([stock1, stock2])) model = VAR(z) p = order_select(z, 5) if p < 1: continue # portmanteau test if model.fit(p).test_whiteness(nlags=5).pvalue < 0.05: continue # Normality test if model.fit(p).test_normality().pvalue < 0.05: continue r1 = rank(pd.DataFrame(z), 'H2', p) r2 = rank(pd.DataFrame(z), 'H1*', p) r3 = rank(pd.DataFrame(z), 'H1', p) if r3 > 0: # 在 model 3 上有 rank if r2 > 0: # 在 model 2 上有 rank if r1 > 0: # select model 1 and model 2 and model 3 lambda_model2 = eig(pd.DataFrame(z), 'H1*', p, r2) lambda_model3 = eig(pd.DataFrame(z), 'H1', p, r2) test = np.log( lambda_model2 / lambda_model3) * (len(min_price) - p) if test > 3.8414: local_select_model.append('model3') local_weight.append(weigh(pd.DataFrame(z), 'H1', p, r3)) local_name.append([stock1_name, stock2_name]) local_pval.append( vecm_pvalue('model3', vecm(pd.DataFrame(z), 'H1', p)[0][0])) else: lambda_model1 = eig(pd.DataFrame(z), 'H2', p, r1) test = np.log(lambda_model1 / lambda_model2) * (len(min_price) - p) if test > 3.8414: local_select_model.append('model2') local_weight.append( weigh(pd.DataFrame(z), 'H1*', p, r2)) local_name.append([stock1_name, stock2_name]) local_pval.append( vecm_pvalue( 'model2', vecm(pd.DataFrame(z), 'H1*', p)[0][1])) else: local_select_model.append('model1') local_weight.append( weigh(pd.DataFrame(z), 'H2', p, r1)) local_name.append([stock1_name, stock2_name]) local_pval.append( vecm_pvalue( 'model1', vecm(pd.DataFrame(z), 'H2', p)[0][0])) else: # select model 2 and model 3 lambda_model2 = eig(pd.DataFrame(z), 'H1*', p, r2) lambda_model3 = eig(pd.DataFrame(z), 'H1', p, r2) test = np.log( lambda_model2 / lambda_model3) * (len(min_price) - p) if test > 3.8414: local_select_model.append('model3') local_weight.append(weigh(pd.DataFrame(z), 'H1', p, r3)) local_name.append([stock1_name, stock2_name]) local_pval.append( vecm_pvalue('model3', vecm(pd.DataFrame(z), 'H1', p)[0][0])) else: local_select_model.append('model2') local_weight.append( weigh(pd.DataFrame(z), 'H1*', p, r2)) local_name.append([stock1_name, stock2_name]) local_pval.append( vecm_pvalue('model2', vecm(pd.DataFrame(z), 'H1*', p)[0][1])) else: # 只在 model 3 上有rank local_select_model.append('model3') local_weight.append(weigh(pd.DataFrame(z), 'H1', p, r3)) local_name.append([stock1_name, stock2_name]) local_pval.append( vecm_pvalue('model3', vecm(pd.DataFrame(z), 'H1', p)[0][0])) else: # 表示此配對無rank continue return local_weight, local_name, local_select_model, local_pval