def form_table(self): # 將共整合係數標準化,此權重為資金權重,因此必須依股價高低轉為張數權重。 for i in range(len(self.name)): total = abs(self.weight.w1[i]) + abs(self.weight.w2[i]) self.weight.w2[i] = (self.weight.w2[i] / total) self.weight.w1[i] = (self.weight.w1[i] / total) table = pd.concat([self.name, self.select_model, self.weight], axis=1) #print("共整合係數標準化 done in " + str(end - start)) #------------------------------------------------------------------------------ #計算spread序列,做單根檢定,並刪除非定態spread序列 spread = np.zeros((len(self.data), len(table))) for i in range(len(table)): spread[:, i] = table.w1[i] * self.data[ table.stock1[i]] + table.w2[i] * self.data[table.stock2[i]] self.spread = pd.DataFrame(spread) #print("刪除非定態spread序列 done in " + str(end - start)) #------------------------------------------------------------------------------ # 計算信噪比 ( spread )------------------------------ for i in range(len(self.spread.T)): y = self.spread.iloc[:, i] self.s_n_r.append(snr(y, 100)) self.s_n_r = pd.DataFrame(self.s_n_r) self.s_n_r.columns = ["snr"] #print("計算信噪比 done in " + str(end - start)) #------------------------------------------------------------------------------ # 計算過零率 ( spread )------------------------------ #pool = Pool(processes=16) Boot = 500 for j in range(len(self.spread.T)): y = self.spread.iloc[:, j] #t = pool.apply_async(zcr, (y,Boot,con.stock1[j],con.stock2[j],),callback=self.append_zcr_result) self.z_c_r.append(zcr(y, Boot)) #pool.close() #pool.join() #self.stock1_name = pd.DataFrame(self.stock1_name) ; self.stock1_name.columns = ["stock1"] #self.stock2_name = pd.DataFrame(self.stock2_name) ; self.stock2_name.columns = ["stock2"] self.z_c_r = pd.DataFrame(self.z_c_r) self.z_c_r.columns = ["zcr"] #mix = pd.concat([self.stock1_name,self.stock2_name,self.z_c_r],axis=1) #print("計算過零率 done in " + str(end - start)) #------------------------------------------------------------------------------ # 開倉門檻and平倉門檻and偏度-------------------------------------- for i in range(len(self.spread.T)): y = self.spread.iloc[:, i] # 有時間趨勢項的模型必須分開計算 if table.model_type[i] == 'model4': x = np.arange(0, len(y)) b1, b0 = np.polyfit(x, y, 1) trend_line = x * b1 + b0 y = y - trend_line self.ave.append(np.mean(y)) self.std.append(np.std(y)) self.ske.append(skew(y)) self.ave = pd.DataFrame(self.ave) self.ave.columns = ["mu"] self.std = pd.DataFrame(self.std) self.std.columns = ["stdev"] self.ske = pd.DataFrame(self.ske) self.ske.columns = ["skewness"] #print("開倉門檻and平倉門檻 done in " + str(end - start)) #------------------------------------------------------------------------------ # 整理表格 #start = datetime.now() #con = pd.concat([con,self.s_n_r],axis=1) #con = pd.merge( con , mix , on=["stock1","stock2"] , how="outer" ) table = pd.concat( [table, self.s_n_r, self.z_c_r, self.ave, self.std, self.ske], axis=1) print(table) #end = datetime.now() del self.s_n_r del self.z_c_r del self.ave del self.std del self.ske del self.select_model del self.weight del self.name stock1_name = table.stock1.astype('str', copy=False) stock2_name = table.stock2.astype('str', copy=False) test_stock1 = np.array(self.data[stock1_name].T) test_stock2 = np.array(self.data[stock2_name].T) mean = np.zeros(len(table)) std = np.zeros(len(table)) for i in range(len(table)): spread_m, spread = spread_mean(test_stock1, test_stock2, i, table) mean[i] = np.mean(spread_m[-1:]) #std[i] = np.sqrt(np.mean(np.square(spread_m-spread))) std[i] = get_Estd(test_stock1, test_stock2, i, table) table['e_mu'] = mean table['e_stdev'] = std return table
def formation_period_single(day1): # 合併二日資料 #min_price = np.log(pd.concat([day1,day2])) min_price = day1 min_price = min_price.dropna(axis=1) min_price.index = np.arange(0, len(min_price), 1) unit_stock = np.where( min_price.apply(lambda x: adfuller(x)[1] > 0.05, axis=0) == True) # 找出單跟的股票 min_price.drop(min_price.columns[unit_stock], axis=1, inplace=True) # 刪除單跟股票,剩餘為定態序列。 spread = min_price #------------------------------------------------------------------------------ # 無母數開倉門檻-------------------------------------- ave = [] std = [] for i in range(len(spread.T)): y = spread.iloc[:, i] ave.append(np.mean(y)) std.append(np.std(y)) ave = pd.DataFrame(ave) ave.columns = ["mu"] std = pd.DataFrame(std) std.columns = ["stdev"] # 程式防呆 a = np.array(np.where(std < 0.0000001)) if a.size > 1: a = int(np.delete(a, -1, axis=0)) spread.drop(spread.columns[a], axis=1, inplace=True) ave.drop(ave.index[a], axis=0, inplace=True) ave.index = np.arange(0, len(ave), 1) std.drop(std.index[a], axis=0, inplace=True) std.index = np.arange(0, len(std), 1) #------------------------------------------------------------------------------ # 計算過零率 ( spread )------------------------------ Boot = 500 z_c_r = [] for j in range(len(spread.T)): y = spread.iloc[:, j] z_c_r.append(zcr(y, Boot)) z_c_r = pd.DataFrame(z_c_r) z_c_r.columns = ["zcr"] # ----------------------------------------------------------------------------- stock_name = pd.DataFrame(spread.columns) stock_name.columns = ["stock"] con = pd.concat([stock_name, z_c_r, ave, std], axis=1) return con
def form_table(self): # 將共整合係數標準化,此權重為資金權重,因此必須依股價高低轉為張數權重。 for i in range(len(self.name)): total = abs(self.weight.w1[i]) + abs(self.weight.w2[i]) self.weight.w2[i] = (self.weight.w2[i] / total) self.weight.w1[i] = (self.weight.w1[i] / total) con = pd.concat([self.name, self.select_model, self.weight, self.pval], axis=1) #print("共整合係數標準化 done in " + str(end - start)) #------------------------------------------------------------------------------ #計算spread序列 spread = np.zeros((len(self.data), len(con))) for i in range(len(con)): spread[:, i] = con.w1[i] * self.data[ con.stock1[i]] + con.w2[i] * self.data[con.stock2[i]] self.spread = pd.DataFrame(spread) #print("刪除非定態spread序列 done in " + str(end - start)) #------------------------------------------------------------------------------ # 計算信噪比 ( spread )------------------------------ for i in range(len(self.spread.T)): y = self.spread.iloc[:, i] self.s_n_r.append(snr(y, 100)) self.s_n_r = pd.DataFrame(self.s_n_r) self.s_n_r.columns = ["snr"] #print("計算信噪比 done in " + str(end - start)) #------------------------------------------------------------------------------ # 計算過零率 ( spread )------------------------------ #pool = Pool(processes=16) Boot = 500 for j in range(len(self.spread.T)): y = self.spread.iloc[:, j] #t = pool.apply_async(zcr, (y,Boot,con.stock1[j],con.stock2[j],),callback=self.append_zcr_result) self.z_c_r.append(zcr(y, Boot)) #pool.close() #pool.join() #self.stock1_name = pd.DataFrame(self.stock1_name) ; self.stock1_name.columns = ["stock1"] #self.stock2_name = pd.DataFrame(self.stock2_name) ; self.stock2_name.columns = ["stock2"] # self.z_c_r = np.zeros(len(self.spread.T)) self.z_c_r = pd.DataFrame(self.z_c_r) self.z_c_r.columns = ["zcr"] #mix = pd.concat([self.stock1_name,self.stock2_name,self.z_c_r],axis=1) #print("計算過零率 done in " + str(end - start)) #------------------------------------------------------------------------------ # 開倉門檻and平倉門檻and偏度-------------------------------------- for i in range(len(self.spread.T)): y = self.spread.iloc[:, i] # 有時間趨勢項的模型必須分開計算 if con.model_type[i] == 'model4': x = np.arange(0, len(y)) b1, b0 = np.polyfit(x, y, 1) trend_line = x * b1 + b0 y = y - trend_line # 將spread消除趨勢項後,計算mu與std self.ave.append(np.mean(y)) self.std.append(np.std(y)) self.ske.append(skew(y)) else: self.ave.append(np.mean(y)) self.std.append(np.std(y)) self.ske.append(skew(y)) self.ave = pd.DataFrame(self.ave) self.ave.columns = ["mu"] self.std = pd.DataFrame(self.std) self.std.columns = ["stdev"] self.ske = pd.DataFrame(self.ske) self.ske.columns = ["skewness"] #print("開倉門檻and平倉門檻 done in " + str(end - start)) #------------------------------------------------------------------------------ # 整理表格 #start = datetime.now() #con = pd.concat([con,self.s_n_r],axis=1) #con = pd.merge( con , mix , on=["stock1","stock2"] , how="outer" ) con = pd.concat( [con, self.s_n_r, self.z_c_r, self.ave, self.std, self.ske], axis=1) #end = datetime.now() del self.s_n_r del self.z_c_r del self.ave del self.std del self.ske del self.select_model del self.weight del self.name #print("整理表格 done in " + str(end - start)) #print(con) return con