def test_const_linear_model(self): features = ['c', 'b', 'a'] weights = dict(c=3., b=2., a=1.) model = ConstLinearModel(features=features, weights=weights) calculated_y = model.predict(self.predict_x) expected_y = self.predict_x[features] @ np.array([weights[f] for f in features]) np.testing.assert_array_almost_equal(calculated_y, expected_y)
def test_const_linear_model_persistence(self): weights = dict(c=3., b=2., a=1.) model = ConstLinearModel(features=['a', 'b', 'c'], weights=weights) desc = model.save() new_model = load_model(desc) self.assertEqual(model.features, new_model.features) np.testing.assert_array_almost_equal(model.weights, new_model.weights)
def test_const_linear_model(self): weights = np.array([1., 2., 3.]) model = ConstLinearModel(features=['a', 'b', 'c'], weights=weights) calculated_y = model.predict(self.predict_x) expected_y = self.predict_x @ weights np.testing.assert_array_almost_equal(calculated_y, expected_y)
def test_const_linear_model_persistence(self): weights = np.array([1., 2., 3.]) model = ConstLinearModel(features=['a', 'b', 'c'], weights=weights) desc = model.save() new_model = ConstLinearModel.load(desc) self.assertEqual(model.features, new_model.features) np.testing.assert_array_almost_equal(model.weights, new_model.weights)
def test_const_linear_model_score(self): model = LinearRegression(['a', 'b', 'c'], fit_intercept=False) model.fit(self.train_x, self.train_y) expected_score = model.score(self.train_x, self.train_y) const_model = ConstLinearModel(features=['a', 'b', 'c'], weights=dict(zip(model.features, model.weights))) calculated_score = const_model.score(self.train_x, self.train_y) self.assertAlmostEqual(expected_score, calculated_score)
def load_model(model_desc: dict) -> ModelBase: model_name = model_desc['model_name'] model_name_parts = set(model_name.split('.')) if 'ConstLinearModel' in model_name_parts: return ConstLinearModel.load(model_desc) elif 'LinearRegression' in model_name_parts: return LinearRegression.load(model_desc) elif 'LassoRegression' in model_name_parts: return LassoRegression.load(model_desc) elif 'LogisticRegression' in model_name_parts: return LogisticRegression.load(model_desc) elif 'RandomForestRegressor' in model_name_parts: return RandomForestRegressor.load(model_desc) elif 'RandomForestClassifier' in model_name_parts: return RandomForestClassifier.load(model_desc) elif 'XGBRegressor' in model_name_parts: return XGBRegressor.load(model_desc) elif 'XGBClassifier' in model_name_parts: return XGBClassifier.load(model_desc) elif 'XGBTrainer' in model_name_parts: return XGBTrainer.load(model_desc) elif 'NvSVR' in model_name_parts: return NvSVRModel.load(model_desc) else: raise ValueError('{0} is not currently supported in model loader.'.format(model_name))
def load_model(model_desc: dict) -> ModelBase: model_name = model_desc['model_name'] model_name_parts = set(model_name.split('.')) if 'ConstLinearModel' in model_name_parts: return ConstLinearModel.load(model_desc) elif 'LinearRegression' in model_name_parts: return LinearRegression.load(model_desc) else: raise ValueError( '{0} is not currently supported in model loader.'.format( model_name))
def test_simple_model_features(self): model = ConstLinearModel(features=['c', 'b', 'a']) self.assertListEqual(['a', 'b', 'c'], model.features)
@classmethod def load(cls, comp_desc): alpha_model = load_model(comp_desc['alpha_model']) data_meta = DataMeta.load(comp_desc['data_meta']) return cls(alpha_model, data_meta) if __name__ == '__main__': import numpy as np from alphamind.data.standardize import standardize from alphamind.data.winsorize import winsorize_normal from alphamind.data.engines.sqlengine import industry_styles from alphamind.model.linearmodel import ConstLinearModel data_source = "postgres+psycopg2://postgres:we083826@localhost/alpha" alpha_model = ConstLinearModel(['EPS'], np.array([1.])) alpha_factors = ['EPS'] freq = '1w' universe = Universe('zz500', ['zz500']) batch = 4 neutralized_risk = ['SIZE'] + industry_styles risk_model = 'short' pre_process = [winsorize_normal, standardize] pos_process = [winsorize_normal, standardize] data_meta = DataMeta(freq, universe, batch, neutralized_risk, risk_model, pre_process,