示例#1
0
文件: main.py 项目: jobex/larbitrage
#----------------------------------
#     NNLS fitting
#----------------------------------
from modeling import nnls_parser

obj_nnls = nnls_parser.BASIC_NNLS(ts_matrix, ts_matrix_title)

fout = open('result_r2.dat', 'a')
fout.write("%s\t%.6f\n" %(settings.TIME_SLOT, obj_nnls.fitted_r2)) 
fout.close()
np.save('../tmp_data/' + settings.TIME_SLOT+'_obj.npy', obj_nnls)

if pred_folders is not None:
	# now have the new selected_symbols
	selected_symbols = ts_matrix_title[1:-1]

	# use the selected_symbols to get the prediction matrix
	pr_stock_matrix = prep_matrix.parser_for_prediction(pred_folders, 
							selected_symbols, start=settings.Time_Slot, end=settings.E_PRED_DATE)
	pr_hs300_matrix = prep_matrix.parser_for_hs300(start=settings.Time_Slot, end=settings.E_PRED_DATE)
	pr_matrix = ts_format.ts2npy(logic.merge(pr_stock_matrix, pr_hs300_matrix))

	np.save('../tmp_data/'+settings.TIME_SLOT+'_pred.npy', pr_matrix)
	obj_nnls.predict(pr_matrix)

def test():
	#part2()
	import cProfile
	cProfile.run('part2()')
示例#2
0
def make_integrity(ts_matrix, matrix_title, integrity_type=BIND_TYPE, eliminate_rule=None):
	''' convert the ts_matrix to integrity one, use integrity_type
		to set up different merge type
		------------------------------
		integrity_type = 0 : merge all
		integrity_type = 1 : merge by eliminate partial columns first, then rows
		integrity_type = 2 : merge by eliminate partial rows first, then columns
		integrity_type = 3 : merge by eliminate partial columns only
		integrity_type = 4 : merge by eliminate partial rows only
		------------------------------
	'''
	matrix = ts_format.ts2npy(ts_matrix) #convert ts matrix to npy
	nrows, ncols = matrix.shape
	
	if eliminate_rule is None:
		eliminate_or_not = lambda x: x.count('NA') > (len(x)/NA_N_PART)
	else:
		eliminate_or_not = eliminate_rule

	# the function to eliminate the corresponding titles from checker
	eliminate_matrix_title = lambda checker: [matrix_title[i] for i in checker]

	if integrity_type == 0:
		matrix = matrix[~(matrix=='NA').any(axis=1), :] # merge all
		eliminated_matrix_title = matrix_title[:]
		if 0 in matrix.shape:
			print 'Error: Empty integral matrix, change another BIND_TYPE'
			exit()

	elif integrity_type == 1:
		# start from cols, first column is the timeline
		checker = [0]    # don't eliminate first column
		for i in range(1, ncols-1):
			if not eliminate_or_not(matrix[:,i].tolist()): 
				checker.append(i) 
			else: 
				pass
			pass
		checker.append(ncols-1)  # don't eliminate the last column
		matrix = matrix[:, checker] # remove columns
		matrix = matrix[~(matrix=='NA').any(axis=1), :] # remove rows

		eliminated_matrix_title = eliminate_matrix_title(checker)

		if matrix.shape[0] < 3 or matrix.shape[1] < 3:
			print 'Error: Empty integral matrix, change another BIND_TYPE'
			exit()

		pass

	elif integrity_type == 2:
		checker = []
		for i in range(nrows):
			if not eliminate_or_not(matrix[i, :].tolist()): checker.append(i) 
		#print checker
		matrix = matrix[checker, :]
		matrix = matrix[:, ~(matrix=='NA').any(axis=0)]
		eliminated_matrix_title = \
			eliminate_matrix_title(~(matrix=='NA').any(axis=0))

		if matrix.shape[0] < 3 or matrix.shape[1] < 3:
			print 'Error: Nearly Empty integral matrix, change another BIND_TYPE'
			exit()

		pass

	elif integrity_type == 3:
		checker = [0]
		for i in range(1, ncols-1):
			if not eliminate_or_not(matrix[:,i].tolist()): checker.append(i) 
		checker.append(ncols-1)
		#print checker
		matrix = matrix[:, checker] # remove columns only

		eliminated_matrix_title = eliminate_matrix_title(checker)
		pass

	elif integrity_type == 4:
		checker = []
		for i in range(nrows):
			if not eliminate_or_not(matrix[i, :].tolist()): checker.append(i) 
		#print checker
		matrix = matrix[checker, :]

		eliminated_matrix_title = matrix_title[:]
		pass
		
			
	return matrix, eliminated_matrix_title