随机森林模型多因子选股:回测结果

Posted by YU on June 17, 2020

这个结果已经很久没改动了。

随机森林模型part3(滚动回测各项指标)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#导入需要用到的库
import numpy as np
import pandas as pd
from jqfactor import get_factor_values
from jqlib.technical_analysis import *
from jqfactor import neutralize
from jqfactor import winsorize_med
from jqfactor import standardlize
from jqfactor import winsorize
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import _validation
from sklearn.metrics import precision_score, recall_score, accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
import time
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import train_test_split
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
print 'Starting yudong-back-test, please wait ...........' 
start_time = time.time()
stocklist = get_index_stocks('000300.XSHG')   

sample_1 = pd.read_csv('data/data_train_2007to2010.csv')
sample_2 = pd.read_csv('data/data_train_2007to2011.csv')
sample_3 = pd.read_csv('data/data_train_2007to2012.csv')
sample_4 = pd.read_csv('data/data_train_2008to2013.csv')
sample_5 = pd.read_csv('data/data_train_2009to2014.csv')
sample_6 = pd.read_csv('data/data_train_2010to2015.csv')
sample_7 = pd.read_csv('data/data_train_2011to2016.csv')
sample_8 = pd.read_csv('data/data_train_2012to2017.csv')
sample2011to2017 = [sample_1, sample_2, sample_3, sample_4, sample_5, sample_6, sample_7]


year_start = pd.to_datetime(pd.date_range('2011-01-01','2017-12-31',freq = 'AS'))
year_end = pd.to_datetime(pd.date_range('2011-01-01','2017-12-31',freq = 'A'))

accuracy_test_list = [] 
precision_test_list = []
auc_test_list = []
recall_test_list = []
factor_score_list = []
gs_list = []
year = 2011
    
for i,j,k in zip(year_start, year_end,sample2011to2017):
    print '%s年回测开始:............'%year
    gs,a,b,c,d,factor_score = test_out(k, i,j,stocklist)
    gs_list.append(gs)
    factor_score_list.append(factor_score)
    accuracy_test_list += a
    precision_test_list += b
    auc_test_list += c
    recall_test_list += d
    print '%s年回测已经结束了............'%year
    print '\n'
    year += 1
    
1
2
3
4
5
6
7
8
gs_last, e,f,g,h,factor_score_last = test_out(sample_8, '2018-01-01', '2018-08-15',stocklist)
accuracy_test_list += e
precision_test_list += f
auc_test_list += g
recall_test_list += h
gs_list.append(gs_last)
factor_score_list.append(factor_score_last)
print 'Total cost time: %s'%(time.time() - start_time)
1
2
3
4
5
#打印每年因子重要性
factor_importance = factor_score_list[0]
for i in factor_score_list[1:]:
    factor_importance = pd.merge(factor_importance, i, left_on = 'features',right_on = 'features')
factor_importance
features importance_x importance_y importance_x importance_y importance_x importance_y importance_x importance_y
0 turnover_volatility 0.085290 0.070940 0.063438 0.060765 0.054880 0.056762 0.055559 0.058241
1 quick_ratio 0.080541 0.078048 0.078836 0.082299 0.066128 0.061665 0.063301 0.065500
2 VMACD 0.079592 0.073785 0.074901 0.071236 0.086967 0.086361 0.082764 0.077204
3 total_profit_growth_rate 0.074588 0.071198 0.074967 0.074053 0.089616 0.089956 0.087829 0.084951
4 total_asset_growth_rate 0.074156 0.068602 0.070751 0.071566 0.065317 0.062932 0.061112 0.065447
5 sharpe_ratio_20 0.073172 0.078520 0.072455 0.070199 0.065867 0.068128 0.071411 0.073998
6 AR 0.069965 0.077588 0.080356 0.089524 0.082555 0.075638 0.073560 0.067520
7 ACCA 0.058606 0.060009 0.057979 0.057339 0.053839 0.056908 0.056895 0.058328
8 net_asset_per_share 0.057264 0.059393 0.060932 0.056911 0.055650 0.058687 0.060041 0.063181
9 DAVOL20 0.055162 0.056412 0.055093 0.060552 0.056986 0.065627 0.077496 0.075411
10 WVAD 0.054794 0.059070 0.065648 0.064613 0.067596 0.071261 0.066566 0.063420
11 ocf_to_operating_profit 0.005169 0.008875 0.007189 0.006166 0.006301 0.006058 0.008223 0.007676
12 market_cap 0.004201 0.005158 0.006412 0.004656 0.005585 0.005569 0.004950 0.006358
13 pcf_ratio 0.004132 0.004365 0.003435 0.003133 0.003354 0.002892 0.003402 0.003481
14 pe_ratio_lyr 0.004000 0.002750 0.003271 0.003652 0.003199 0.002932 0.002802 0.002381
15 Skewness60 0.003794 0.002698 0.002795 0.002840 0.002915 0.002152 0.002464 0.002355
16 Kurtosis120 0.003760 0.002702 0.003176 0.002217 0.003039 0.002630 0.001842 0.001894
17 AR1 0.003692 0.004269 0.003722 0.002837 0.002903 0.003140 0.002957 0.002812
18 VOL20 0.003623 0.003058 0.002721 0.001964 0.002580 0.002921 0.002510 0.002612
19 AROON_XG2 0.003606 0.004343 0.002776 0.003067 0.002634 0.002366 0.002559 0.003905
20 net_asset_growth_rate 0.003587 0.002296 0.002526 0.003019 0.003617 0.002715 0.002124 0.001671
21 VDEA 0.003586 0.003024 0.002566 0.002793 0.002569 0.002817 0.002632 0.002326
22 total_profit_to_cost_ratio 0.003450 0.002984 0.002789 0.002092 0.002814 0.002894 0.002158 0.002221
23 VOL60 0.003445 0.004382 0.003412 0.005434 0.003197 0.002944 0.003335 0.003423
24 operating_profit_growth_rate 0.003438 0.002742 0.003469 0.002749 0.002803 0.002592 0.002484 0.002740
25 macd_dif 0.003429 0.002708 0.002920 0.002975 0.002931 0.002185 0.002007 0.003468
26 Variance20 0.003419 0.002238 0.002532 0.001944 0.002399 0.003168 0.003188 0.002618
27 net_profit_margin 0.003415 0.002758 0.002884 0.002608 0.003379 0.002427 0.002557 0.002207
28 Kurtosis20 0.003408 0.002553 0.003427 0.003140 0.002603 0.002849 0.002379 0.002217
29 macd_dea 0.003400 0.003268 0.002938 0.002593 0.002910 0.002272 0.001926 0.002207
... ... ... ... ... ... ... ... ... ...
60 Variance120 0.002876 0.002736 0.003672 0.002747 0.002746 0.002456 0.002755 0.002601
61 MAWVAD 0.002872 0.002652 0.003256 0.002446 0.002242 0.002311 0.002992 0.002602
62 eps 0.002869 0.003258 0.002645 0.002204 0.003214 0.002196 0.002879 0.002895
63 VDIFF 0.002867 0.002592 0.002582 0.002945 0.002495 0.002694 0.002759 0.002421
64 net_operate_cashflow_growth_rate 0.002861 0.002561 0.002974 0.002578 0.001833 0.002108 0.003370 0.002359
65 roe 0.002772 0.002706 0.002264 0.002134 0.003289 0.002531 0.002728 0.002940
66 RSI 0.002731 0.003573 0.003108 0.002558 0.002692 0.002597 0.002805 0.001715
67 account_receivable_turnover_rate 0.002727 0.002879 0.002520 0.002259 0.003187 0.002919 0.002604 0.002253
68 Skewness120 0.002722 0.002329 0.002489 0.003740 0.002212 0.003072 0.002442 0.002423
69 BR1 0.002722 0.002306 0.003055 0.002763 0.002997 0.002434 0.002435 0.002632
70 Skewness20 0.002704 0.003248 0.003351 0.002726 0.003273 0.002201 0.002898 0.002423
71 VEMA26 0.002694 0.003054 0.003021 0.002531 0.002649 0.002424 0.002637 0.002578
72 accounts_payable_turnover_rate 0.002607 0.002921 0.002579 0.002695 0.002523 0.002277 0.002798 0.002838
73 operating_expense_to_total_revenue 0.002600 0.002519 0.003272 0.002325 0.003461 0.002042 0.002234 0.002403
74 net_profit_ratio 0.002599 0.002975 0.003044 0.002991 0.002871 0.002635 0.002653 0.002149
75 inc_total_revenue_year_on_year 0.002592 0.003480 0.003481 0.003024 0.003128 0.003455 0.002507 0.002522
76 VOSC 0.002540 0.002643 0.002787 0.002654 0.002761 0.002763 0.002403 0.002814
77 sharpe_ratio_60 0.002521 0.003748 0.002111 0.002734 0.003017 0.002273 0.003309 0.001985
78 roa_ttm 0.002475 0.003537 0.002195 0.002310 0.002040 0.002288 0.002433 0.002880
79 net_profit_growth_rate 0.002443 0.002511 0.002768 0.002981 0.002387 0.002421 0.003119 0.003048
80 roa 0.002343 0.002610 0.002578 0.002428 0.002607 0.002280 0.002878 0.002097
81 operating_profit_ratio 0.002304 0.002773 0.002716 0.002300 0.002865 0.002920 0.002583 0.002520
82 VR 0.002212 0.002783 0.002601 0.002729 0.003086 0.003356 0.002529 0.003139
83 expense_to_total_revenue 0.002124 0.003212 0.003284 0.003223 0.002626 0.002537 0.003134 0.002232
84 VOL120 0.002084 0.003345 0.003025 0.002707 0.002448 0.002940 0.002355 0.002440
85 VSTD20 0.002067 0.003571 0.003242 0.002867 0.002633 0.002347 0.002692 0.002864
86 BR 0.001963 0.003575 0.002919 0.003044 0.002730 0.002352 0.001836 0.001775
87 operating_revenue_growth_rate 0.001846 0.003541 0.002374 0.003382 0.003180 0.002379 0.002839 0.002943
88 ps_ratio 0.001652 0.002018 0.002707 0.002765 0.002557 0.003585 0.001790 0.002555
89 adjusted_profit_to_profit 0.000000 0.000000 0.000000 0.008400 0.023708 0.028325 0.028444 0.034657

90 rows × 9 columns

1
2
factor_importance.columns = ['features']+range(2011,2019)
factor_importance
features 2011 2012 2013 2014 2015 2016 2017 2018
0 turnover_volatility 0.085290 0.070940 0.063438 0.060765 0.054880 0.056762 0.055559 0.058241
1 quick_ratio 0.080541 0.078048 0.078836 0.082299 0.066128 0.061665 0.063301 0.065500
2 VMACD 0.079592 0.073785 0.074901 0.071236 0.086967 0.086361 0.082764 0.077204
3 total_profit_growth_rate 0.074588 0.071198 0.074967 0.074053 0.089616 0.089956 0.087829 0.084951
4 total_asset_growth_rate 0.074156 0.068602 0.070751 0.071566 0.065317 0.062932 0.061112 0.065447
5 sharpe_ratio_20 0.073172 0.078520 0.072455 0.070199 0.065867 0.068128 0.071411 0.073998
6 AR 0.069965 0.077588 0.080356 0.089524 0.082555 0.075638 0.073560 0.067520
7 ACCA 0.058606 0.060009 0.057979 0.057339 0.053839 0.056908 0.056895 0.058328
8 net_asset_per_share 0.057264 0.059393 0.060932 0.056911 0.055650 0.058687 0.060041 0.063181
9 DAVOL20 0.055162 0.056412 0.055093 0.060552 0.056986 0.065627 0.077496 0.075411
10 WVAD 0.054794 0.059070 0.065648 0.064613 0.067596 0.071261 0.066566 0.063420
11 ocf_to_operating_profit 0.005169 0.008875 0.007189 0.006166 0.006301 0.006058 0.008223 0.007676
12 market_cap 0.004201 0.005158 0.006412 0.004656 0.005585 0.005569 0.004950 0.006358
13 pcf_ratio 0.004132 0.004365 0.003435 0.003133 0.003354 0.002892 0.003402 0.003481
14 pe_ratio_lyr 0.004000 0.002750 0.003271 0.003652 0.003199 0.002932 0.002802 0.002381
15 Skewness60 0.003794 0.002698 0.002795 0.002840 0.002915 0.002152 0.002464 0.002355
16 Kurtosis120 0.003760 0.002702 0.003176 0.002217 0.003039 0.002630 0.001842 0.001894
17 AR1 0.003692 0.004269 0.003722 0.002837 0.002903 0.003140 0.002957 0.002812
18 VOL20 0.003623 0.003058 0.002721 0.001964 0.002580 0.002921 0.002510 0.002612
19 AROON_XG2 0.003606 0.004343 0.002776 0.003067 0.002634 0.002366 0.002559 0.003905
20 net_asset_growth_rate 0.003587 0.002296 0.002526 0.003019 0.003617 0.002715 0.002124 0.001671
21 VDEA 0.003586 0.003024 0.002566 0.002793 0.002569 0.002817 0.002632 0.002326
22 total_profit_to_cost_ratio 0.003450 0.002984 0.002789 0.002092 0.002814 0.002894 0.002158 0.002221
23 VOL60 0.003445 0.004382 0.003412 0.005434 0.003197 0.002944 0.003335 0.003423
24 operating_profit_growth_rate 0.003438 0.002742 0.003469 0.002749 0.002803 0.002592 0.002484 0.002740
25 macd_dif 0.003429 0.002708 0.002920 0.002975 0.002931 0.002185 0.002007 0.003468
26 Variance20 0.003419 0.002238 0.002532 0.001944 0.002399 0.003168 0.003188 0.002618
27 net_profit_margin 0.003415 0.002758 0.002884 0.002608 0.003379 0.002427 0.002557 0.002207
28 Kurtosis20 0.003408 0.002553 0.003427 0.003140 0.002603 0.002849 0.002379 0.002217
29 macd_dea 0.003400 0.003268 0.002938 0.002593 0.002910 0.002272 0.001926 0.002207
... ... ... ... ... ... ... ... ... ...
60 Variance120 0.002876 0.002736 0.003672 0.002747 0.002746 0.002456 0.002755 0.002601
61 MAWVAD 0.002872 0.002652 0.003256 0.002446 0.002242 0.002311 0.002992 0.002602
62 eps 0.002869 0.003258 0.002645 0.002204 0.003214 0.002196 0.002879 0.002895
63 VDIFF 0.002867 0.002592 0.002582 0.002945 0.002495 0.002694 0.002759 0.002421
64 net_operate_cashflow_growth_rate 0.002861 0.002561 0.002974 0.002578 0.001833 0.002108 0.003370 0.002359
65 roe 0.002772 0.002706 0.002264 0.002134 0.003289 0.002531 0.002728 0.002940
66 RSI 0.002731 0.003573 0.003108 0.002558 0.002692 0.002597 0.002805 0.001715
67 account_receivable_turnover_rate 0.002727 0.002879 0.002520 0.002259 0.003187 0.002919 0.002604 0.002253
68 Skewness120 0.002722 0.002329 0.002489 0.003740 0.002212 0.003072 0.002442 0.002423
69 BR1 0.002722 0.002306 0.003055 0.002763 0.002997 0.002434 0.002435 0.002632
70 Skewness20 0.002704 0.003248 0.003351 0.002726 0.003273 0.002201 0.002898 0.002423
71 VEMA26 0.002694 0.003054 0.003021 0.002531 0.002649 0.002424 0.002637 0.002578
72 accounts_payable_turnover_rate 0.002607 0.002921 0.002579 0.002695 0.002523 0.002277 0.002798 0.002838
73 operating_expense_to_total_revenue 0.002600 0.002519 0.003272 0.002325 0.003461 0.002042 0.002234 0.002403
74 net_profit_ratio 0.002599 0.002975 0.003044 0.002991 0.002871 0.002635 0.002653 0.002149
75 inc_total_revenue_year_on_year 0.002592 0.003480 0.003481 0.003024 0.003128 0.003455 0.002507 0.002522
76 VOSC 0.002540 0.002643 0.002787 0.002654 0.002761 0.002763 0.002403 0.002814
77 sharpe_ratio_60 0.002521 0.003748 0.002111 0.002734 0.003017 0.002273 0.003309 0.001985
78 roa_ttm 0.002475 0.003537 0.002195 0.002310 0.002040 0.002288 0.002433 0.002880
79 net_profit_growth_rate 0.002443 0.002511 0.002768 0.002981 0.002387 0.002421 0.003119 0.003048
80 roa 0.002343 0.002610 0.002578 0.002428 0.002607 0.002280 0.002878 0.002097
81 operating_profit_ratio 0.002304 0.002773 0.002716 0.002300 0.002865 0.002920 0.002583 0.002520
82 VR 0.002212 0.002783 0.002601 0.002729 0.003086 0.003356 0.002529 0.003139
83 expense_to_total_revenue 0.002124 0.003212 0.003284 0.003223 0.002626 0.002537 0.003134 0.002232
84 VOL120 0.002084 0.003345 0.003025 0.002707 0.002448 0.002940 0.002355 0.002440
85 VSTD20 0.002067 0.003571 0.003242 0.002867 0.002633 0.002347 0.002692 0.002864
86 BR 0.001963 0.003575 0.002919 0.003044 0.002730 0.002352 0.001836 0.001775
87 operating_revenue_growth_rate 0.001846 0.003541 0.002374 0.003382 0.003180 0.002379 0.002839 0.002943
88 ps_ratio 0.001652 0.002018 0.002707 0.002765 0.002557 0.003585 0.001790 0.002555
89 adjusted_profit_to_profit 0.000000 0.000000 0.000000 0.008400 0.023708 0.028325 0.028444 0.034657

90 rows × 9 columns

1
2
3
4
average(auc_test_list)
average(precision_test_list)
average(accuracy_test_list)
average(recall_test_list)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0.52902307309989738






0.52722596200396266






0.5272712720966195






0.50976269590833212
1
2
3
4
draw(accuracy_test_list,'Accuracy','b')
draw(precision_test_list,'Precision','g')
draw(auc_test_list,'AUC','r')
draw(recall_test_list,'Recall','c')

本文采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议(CC BY-NC-ND 4.0)进行许可,转载请注明出处,请勿用于任何商业用途采用。

☛决定关注我了吗☚