I'm using multiprocessing to calculate the correlation between factor and returns month-by-month. But there is an error
TypeError: cannot pickle 'staticmethod' object
However, this is one of the solutions I searched to run multiprocess in class.
How should I correct this?
I'm using Python 3.9.
global calc_ic_help
@staticmethod
def calc_ic_help(month, df_factor, df_ret_1):
print(month)
month_filter = pd.Series(df_factor.index).apply(lambda x: x[:6] == month)
month_filter = month_filter.values
arr1 = df_factor[month_filter].values.flatten()
arr2 = df_ret_1[month_filter].values.flatten()
df1 = pd.DataFrame([arr1, arr2],
index=['factor', 'future_ret']).T
df1.dropna(how='any', inplace=True)
ic = df1.corr().iloc[0, 1]
return ic
def calc_ic(self, df_factor):
df_open = pd.read_csv(os.path.join(self.field_path, 'open.csv'),
index_col=0, header=0)
df_open.index = df_open.index.astype('str')
df_factor.index = df_factor.index.astype('str')
df_ret = (df_open.shift(-1) / df_open) - 1
df_ret_1 = df_ret.shift(-1)
month_list = pd.Series(df_ret_1.index).apply(lambda x: x[:6]).unique()
with concurrent.futures.ProcessPoolExecutor() as executor:
results = [executor.submit(calc_ic_help, (i, df_factor, df_ret_1)) for i in month_list]
for f in concurrent.futures.as_completed(results):
print(f.result())