Repository navigation
Expand file tree
/
Copy pathchoice_sample.py
More file actions
116 lines (106 loc) · 4.33 KB
/
Copy pathchoice_sample.py
File metadata and controls
116 lines (106 loc) · 4.33 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import numpy as np
import pandas as pd
import random
import statsmodels.api as sm
import warnings
warnings.filterwarnings('ignore')
model_dict = {'线性回归':sm.OLS,'Logit':sm.Logit,'Probit':sm.Probit}
B_dict = {'线性回归':'t','Logit':'z','Probit':'z'}
global issuccess
issuccess = False
global issuccess_all
issuccess_all = False
def sig_t(X,y,model_reg,method_reg,p_alpha_list,t_above_list,t_below_list,alpha=0.05):
MyReg = model_dict[model_reg]
regt = MyReg(y,X)
try:
result = regt.fit(method=method_reg, disp=False).summary2().tables[1]
except:
return False
else:
for t in p_alpha_list:
if result.iloc[t]['P>|%s|'%B_dict[model_reg]]>=alpha:
return False
for t in t_above_list:
if result.iloc[t][B_dict[model_reg]]<=0:
return False
for t in t_below_list:
if result.iloc[t][B_dict[model_reg]]>=0:
return False
return True
#运行choice_step2前必须运行choice_step1。
#运行choice_step3前不一定先运行choice_step1和choice_step2,因为choice_step3里会从头开始计算
def choice_step1(X,y,model_reg,method_reg,p_alpha_list,t_above_list,t_below_list,alpha=0.05,iteration=10000,lr=0.2,rr=0.6):
l = len(X)
l1 = int(l*lr)
l2 = int(l*rr)
s = random.randint(l1, l2)
index1 = random.sample(list(X.index),s)
XN = X.loc[index1]
yN = y.loc[index1]
t=1
global issuccess
issuccess = False
while (not issuccess) and t<=iteration:
t+=1
if t%(iteration//10)==0:
print("\t进行了%d次尝试" %t)
s = random.randint(l1, l2)
index1 = random.sample(list(X.index),s)
XN = X.loc[index1]
yN = y.loc[index1]
if sig_t(XN,yN,model_reg,method_reg,p_alpha_list,t_above_list,t_below_list,alpha):
flag = True
#print('当前批次共%d个样本,该样本符合要求。共进行了%d次迭代'%(len(index1),t))
print('共进行了%d次迭代,找到了一个可行的样本'%t)
issuccess=True
global issuccess_all
issuccess_all=True
if t>iteration and issuccess==False:
print('当前批次迭代次数超过上限。共进行了%d次迭代。\n这批样本尝试未成功,不可用'%iteration)
break
return index1
def choice_step2(X,y,index1,model_reg,method_reg,p_alpha_list,t_above_list,t_below_list,alpha=0.05,whether_print=False):
index2 = X.index.drop(index1)
#print('初始子样本包含%d个样本' % len(index1))
for j in index2:
indexj = index1.copy()
indexj.append(j)
XN = X.loc[indexj]
yN = y.loc[indexj]
if sig_t(XN,yN,model_reg,method_reg,p_alpha_list,t_above_list,t_below_list,alpha):
index1 = indexj
if whether_print:
print('\t加入了序号为%d的样本' % j)
print('原始数据中,样本量为%d'%len(y))
print('这批样本中,最终保留下来的样本量为%d'%len(index1))
return index1
def choice_step3(X,y,model_reg,method_reg,p_alpha_list,t_above_list,t_below_list,alpha=0.05,lr=0.2,rr=0.6,iteration=10000,N=5):
index_final = []
index_len = 0
j = 0
for k in range(N):
print('\n开始寻找第%d批子样本:'%(k+1))
global issuccess
issuccess = False
index1 = choice_step1(X,y,model_reg,method_reg,p_alpha_list,t_above_list,t_below_list,alpha,iteration,lr,rr)
if issuccess:
index2 = choice_step2(X,y,index1,model_reg,method_reg,p_alpha_list,t_above_list,t_below_list,alpha)
if len(index2)>index_len:
if k>0:
print('第%d批子样本比之前的样本量大\n' % (k+1))
j = k+1
index_final = index2
index_len = len(index2)
else:
if k>0:
print('第%d批子样本并不比之前的样本量大\n' % (k+1))
else:
print('第%d批的%d次尝试均不成功,结果不可用'%(k+1,iteration))
global issuccess_all
if issuccess_all:
print('第%d批子样本的样本量最大,共%d个样本' % (j,index_len))
return index_final
else:
print('%d批尝试均不成功,可以增加迭代次数再试,或者修改模型' %N)
return None