|
| 1 | + |
| 2 | +from sklearn import datasets |
| 3 | +from sklearn.utils import shuffle |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | +from pprint import pprint |
| 7 | + |
| 8 | +if __name__=='__main__': |
| 9 | + try: |
| 10 | + from lazypredict.Supervised import LazyRegressor |
| 11 | + except ImportError: |
| 12 | + raise Exception('pip install lazypredict') |
| 13 | + |
| 14 | + boston = datasets.load_boston() |
| 15 | + X, y = shuffle(boston.data, boston.target) |
| 16 | + X = X.astype(np.float32) |
| 17 | + n_train = 100 |
| 18 | + n_test = 50 |
| 19 | + X_train, y_train = X[:n_train], y[:n_train] |
| 20 | + X_test, y_test = X[n_train:(n_train+n_test)], y[n_train:(n_train+n_test)] |
| 21 | + X_val, y_val = X[(n_train+n_test):], y[(n_train+n_test):] |
| 22 | + X_train_and_test = X[:(n_train+n_test)] |
| 23 | + y_train_and_test = y[:(n_train+n_test)] |
| 24 | + |
| 25 | + # Train on some |
| 26 | + reg1 = LazyRegressor(verbose=0, ignore_warnings=False, custom_metric=None, predictions=True) |
| 27 | + models1, predictions1 = reg1.fit(np.copy(X_train), np.copy(X_test), np.copy(y_train), np.copy(y_test)) |
| 28 | + print(models1[:5]) |
| 29 | + |
| 30 | + # Train on some, predict validation |
| 31 | + reg2 = LazyRegressor(verbose=0, ignore_warnings=False, custom_metric=None, predictions=True) |
| 32 | + X_train_and_test_copy = np.copy(X_train_and_test) |
| 33 | + X_val_copy = np.copy(X_val) |
| 34 | + models2, predictions2 = reg2.fit(X_train_and_test_copy, X_val_copy, np.copy(y_train_and_test), np.copy(y_val)) |
| 35 | + yhat_val = predictions2.values |
| 36 | + print(models2[:5]) |
| 37 | + |
| 38 | + # In-sample performance on train |
| 39 | + reg3 = LazyRegressor(verbose=0, ignore_warnings=False, custom_metric=None, predictions=True) |
| 40 | + models3, predictions3 = reg3.fit(np.copy(X_train), np.copy(X_train), np.copy(y_train), np.copy(y_train)) |
| 41 | + |
| 42 | + # In-sample performance on train + test |
| 43 | + reg4 = LazyRegressor(verbose=0, ignore_warnings=False, custom_metric=None, predictions=True) |
| 44 | + models4, predictions4 = reg4.fit(np.copy(X_train_and_test), np.copy(X_train_and_test), np.copy(y_train_and_test), np.copy(y_train_and_test)) |
| 45 | + |
| 46 | + best_model_1 = models1.index[0] # <-- Best out of sample on test |
| 47 | + best_model_2 = models3.index[0] # <-- Best in sample on train |
| 48 | + best_model_3 = models4.index[0] # <-- Best in sample on train+test |
| 49 | + |
| 50 | + if True: |
| 51 | + # Train cov on out of sample prediction errors |
| 52 | + print('Creating portfolio ...') |
| 53 | + from precise.skaters.managers.ppomanagers import ppo_sk_glcv_pcov_d0_n100_t0_vol_long_manager as mgr |
| 54 | + s = {} |
| 55 | + yhat_train = np.copy(predictions1.values) |
| 56 | + n_train = len(yhat_train) |
| 57 | + es = [-1]*(n_train-1)+[1] |
| 58 | + for y, y_target,e in zip(yhat_train, y_train,es): |
| 59 | + y_error = np.copy(y-y_target) |
| 60 | + w, s = mgr(s=s, y=y_error, e=e) |
| 61 | + |
| 62 | + else: |
| 63 | + n_models = len(models1) |
| 64 | + w = np.ones(n_models)/n_models |
| 65 | + |
| 66 | + w_dict = sorted(zip(w, models1.index), reverse=True) |
| 67 | + pprint(w_dict) |
| 68 | + |
| 69 | + # Refit models using all the train+test data, and combine |
| 70 | + |
| 71 | + sum_w = sum(w) |
| 72 | + yhat_weighted = np.dot( yhat_val, w ) |
| 73 | + predictions2['weighted'] = yhat_weighted |
| 74 | + predictions2['best 1 (' + best_model_1 + ')'] = predictions2[best_model_1] |
| 75 | + predictions2['best 2 (' + best_model_2 + ')'] = predictions2[best_model_2] |
| 76 | + predictions2['best 3 (' + best_model_3 + ')'] = predictions2[best_model_3] |
| 77 | + |
| 78 | + val_errors = predictions2.copy() |
| 79 | + for col in predictions2.columns: |
| 80 | + val_errors[col] = predictions2[col] - y_val |
| 81 | + |
| 82 | + sq_errors = val_errors**2 |
| 83 | + print(sq_errors.mean().sort_values()) |
| 84 | + print('done') |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments