-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_tests.py
173 lines (142 loc) · 6.5 KB
/
project_tests.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from collections import OrderedDict
import numpy as np
import pandas as pd
from sklearn.ensemble import BaggingClassifier, RandomForestRegressor
from sklearn.tree import DecisionTreeClassifier
from unittest.mock import patch
from zipline.data import bundles
from tests import assert_output, project_test, generate_random_dates, assert_structure
def get_assets(ticker_count):
bundle = bundles.load('eod-quotemedia')
return bundle.asset_finder.retrieve_all(bundle.asset_finder.sids[:ticker_count])
@project_test
def test_train_valid_test_split(fn):
columns = ['test column 1', 'test column 2', 'test column 3']
dates = generate_random_dates(10)
assets = get_assets(3)
index = pd.MultiIndex.from_product([dates, assets])
values = np.arange(len(index) * len(columns)).reshape([len(columns), len(index)]).T
targets = np.arange(len(index))
fn_inputs = {
'all_x': pd.DataFrame(values, index, columns),
'all_y': pd.Series(targets, index, name='target'),
'train_size': 0.6,
'valid_size': 0.2,
'test_size': 0.2}
fn_correct_outputs = OrderedDict([
('X_train', pd.DataFrame(values[:18], index[:18], columns=columns)),
('X_valid', pd.DataFrame(values[18:24], index[18:24], columns=columns)),
('X_test', pd.DataFrame(values[24:], index[24:], columns=columns)),
('y_train', pd.Series(targets[:18], index[:18])),
('y_valid', pd.Series(targets[18:24], index[18:24])),
('y_test', pd.Series(targets[24:], index[24:]))])
assert_output(fn, fn_inputs, fn_correct_outputs, check_parameter_changes=False)
@project_test
def test_non_overlapping_samples(fn):
columns = ['test column 1', 'test column 2']
dates = generate_random_dates(8)
assets = get_assets(3)
index = pd.MultiIndex.from_product([dates, assets])
values = np.arange(len(index) * len(columns)).reshape([len(columns), len(index)]).T
targets = np.arange(len(index))
fn_inputs = {
'x': pd.DataFrame(values, index, columns),
'y': pd.Series(targets, index),
'n_skip_samples': 2,
'start_i': 1}
new_index = pd.MultiIndex.from_product([dates[fn_inputs['start_i']::fn_inputs['n_skip_samples'] + 1], assets])
fn_correct_outputs = OrderedDict([
(
'non_overlapping_x',
pd.DataFrame(
[
[3, 27],
[4, 28],
[5, 29],
[12, 36],
[13, 37],
[14, 38],
[21, 45],
[22, 46],
[23, 47]],
new_index, columns)),
(
'non_overlapping_y',
pd.Series([3, 4, 5, 12, 13, 14, 21, 22, 23], new_index))])
assert_output(fn, fn_inputs, fn_correct_outputs, check_parameter_changes=False)
@project_test
def test_bagging_classifier(fn):
n_estimators = 200
parameters = {
'criterion': 'entropy',
'min_samples_leaf': 2500,
'oob_score': True,
'n_jobs': -1,
'random_state': 0}
fn_inputs = {
'n_estimators': n_estimators,
'max_samples': 0.2,
'max_features': 1.0,
'parameters': parameters}
return_value = fn(**fn_inputs)
assert isinstance(return_value, BaggingClassifier),\
'Returned object is wrong. It should be a BaggingClassifier.'
assert return_value.max_samples == fn_inputs['max_samples'],\
'BaggingClassifier\'s max_samples is the wrong value.'
assert return_value.max_features == fn_inputs['max_features'],\
'BaggingClassifier\'s max_features is the wrong value.'
assert return_value.oob_score == parameters['oob_score'],\
'BaggingClassifier\'s oob_score is the wrong value.'
assert return_value.n_jobs == parameters['n_jobs'],\
'BaggingClassifier\'s n_jobs is the wrong value.'
assert return_value.random_state == parameters['random_state'],\
'BaggingClassifier\'s random_state is the wrong value.'
assert isinstance(return_value.base_estimator, DecisionTreeClassifier),\
'BaggingClassifier\'s base estimator is the wrong value type. It should be a DecisionTreeClassifier.'
assert return_value.base_estimator.criterion == parameters['criterion'],\
'The base estimator\'s criterion is the wrong value.'
assert return_value.base_estimator.min_samples_leaf == parameters['min_samples_leaf'],\
'The base estimator\'s min_samples_leaf is the wrong value.'
@project_test
def test_calculate_oob_score(fn):
n_estimators = 3
n_features = 2
n_samples = 1000
noise = np.random.RandomState(0).random_sample([3, n_samples]) * n_samples
x = np.arange(n_estimators * n_samples * n_features).reshape([n_estimators, n_samples, n_features])
y = np.sum(x, axis=-1) + noise
estimators = [
RandomForestRegressor(300, oob_score=True, n_jobs=-1, random_state=101).fit(x[estimator_i], y[estimator_i])
for estimator_i in range(n_estimators)]
fn_inputs = {
'classifiers': estimators}
fn_correct_outputs = OrderedDict([('oob_score', 0.911755651666)])
assert_output(fn, fn_inputs, fn_correct_outputs, check_parameter_changes=False)
@project_test
def test_non_overlapping_estimators(fn):
n_estimators = 3
columns = ['test column 1', 'test column 2']
dates = generate_random_dates(8)
assets = get_assets(3)
index = pd.MultiIndex.from_product([dates, assets])
noise = np.random.RandomState(0).random_sample([len(index)]) * len(index)
values = np.arange(len(index) * len(columns)).reshape([len(columns), len(index)]).T
targets = np.sum(values, axis=-1) + noise
classifiers = [
RandomForestRegressor(300, oob_score=True, n_jobs=-1, random_state=101)
for _ in range(n_estimators)]
fn_inputs = {
'x': pd.DataFrame(values, index, columns),
'y': pd.Series(targets, index),
'classifiers': classifiers,
'n_skip_samples': 3}
random_forest_regressor_fit = RandomForestRegressor.fit
with patch.object(RandomForestRegressor, 'fit', autospec=True) as mock_fit:
mock_fit.side_effect = random_forest_regressor_fit
fn_return_value = fn(**fn_inputs)
assert_structure(fn_return_value, [RandomForestRegressor for _ in range(n_estimators)], 'PCA')
for classifier in fn_return_value:
try:
classifier.fit.assert_called()
except AssertionError:
raise Exception('Test Failure: RandomForestRegressor.fit not called on all classifiers')