Пример #1
0
                                                         keep_value_list].copy(
                                                         )
    for factor in change_factor_list:
        this_factor_df = this_date_components_df.loc[:, [factor, 'Size']]
        #去空
        this_factor_df = xyk_common_data_processing.delete_none(this_factor_df)
        #做回归
        size_sr = this_factor_df.loc[:, 'Size']
        this_factor_sr = this_factor_df.loc[:, factor]
        X_np = sm.add_constant(size_sr)
        ols_model = sm.OLS(this_factor_sr, X_np)
        results = ols_model.fit()
        this_factor_np = results.resid
        this_factor_df.loc[:, factor] = this_factor_np
        #储存
        output_data_dict[date] = pd.concat(
            [output_data_dict[date], this_factor_df.loc[:, factor]],
            axis=1,
            join='outer')

for i, date in enumerate(daily_date_list):
    print date, 'inserting...'
    temp_insert_data = output_data_dict[date]
    temp_insert_data['curr_date'] = date
    temp_insert_data.index.name = 'stock_id'
    temp_insert_data = temp_insert_data.set_index(
        [temp_insert_data['curr_date'],
         temp_insert_data.index]).drop(['curr_date'], axis=1)
    db_interaction.insert_df_append(Output_Table_Name,
                                    temp_insert_data,
                                    index_name_list=["curr_date", "stock_id"])
                X = sm.add_constant(this_index_ROR_list)
                Y = this_minus_list
                wls_model = sm.WLS(Y, X, weights=half_life_list)
                results = wls_model.fit()
                this_beta = float(results.params[1])
                resid_list = results.resid
                this_resid_mean = sum(resid_list) / float(len(resid_list))
                this_treated_list = []
                for resid_data in resid_list:
                    this_treated_list.append((resid_data - this_resid_mean) *
                                             (resid_data - this_resid_mean))
                this_HSIGMA = math.sqrt(
                    xyk_common_data_processing.weighted_mean(this_treated_list,
                                                             half_life_list,
                                                             use_df=1))
                result_list.append([stock, data[0], this_beta, this_HSIGMA])
'''
***输出至DB***
'''
result_pd = xyk_common_data_processing.change_data_format_with_df(
    "list of lists",
    "DataFrame",
    result_list,
    columns_name_list=["stock_id", "curr_date", "Beta", "HSIGMA"])
result_pd.set_index(["stock_id", "curr_date"], inplace=True)
print "Begin inserting..."
table_name = "daily_stock_descriptors_" + Now_Index + "_unique"
db_interaction.insert_df_append(table_name,
                                result_pd,
                                index_name_list=["stock_id", "curr_date"])