(experiment_id, general_hyperparameters, trading_hyperparameters, model_hyperparameters)
| 58 | |
| 59 | |
| 60 | def post_trading_analysis(experiment_id, general_hyperparameters, trading_hyperparameters, model_hyperparameters): |
| 61 | prediction = pd.read_pickle(f"{logger.find_save_path(experiment_id)}/prediction.pkl") |
| 62 | trading_simulation = pd.read_pickle(f"{logger.find_save_path(experiment_id)}/trading_simulation.pkl") |
| 63 | |
| 64 | training_stocks_string, test_stocks_string = get_training_test_stocks_as_string(general_hyperparameters) |
| 65 | |
| 66 | dataset = torch.load( |
| 67 | f"./torch_datasets/threshold_{model_hyperparameters['threshold']}/batch_size_{model_hyperparameters['batch_size']}/training_{training_stocks_string}_test_{test_stocks_string}/{model_hyperparameters['prediction_horizon']}/test_dataset_backtest.pt") |
| 68 | print(f"Reading test (backtest version) dataset...") |
| 69 | test_loader = DataLoader( |
| 70 | dataset, |
| 71 | batch_size=model_hyperparameters["batch_size"], |
| 72 | shuffle=False, |
| 73 | num_workers=model_hyperparameters["num_workers"], |
| 74 | ) |
| 75 | returns_labels_list = [] |
| 76 | for data, labels in tqdm(test_loader): |
| 77 | returns_labels_list.extend(labels.tolist()) |
| 78 | |
| 79 | targets = prediction[2].tolist() |
| 80 | predictions = prediction[3].tolist() |
| 81 | |
| 82 | print(classification_report(targets, predictions)) |
| 83 | |
| 84 | distributions_dataset = pd.DataFrame({"Predictions": predictions, "PCs": returns_labels_list}) |
| 85 | distribution_label_0 = distributions_dataset[distributions_dataset['Predictions'] == 0].PCs |
| 86 | distribution_label_1 = distributions_dataset[distributions_dataset['Predictions'] == 1].PCs |
| 87 | distribution_label_2 = distributions_dataset[distributions_dataset['Predictions'] == 2].PCs |
| 88 | |
| 89 | plt.hist(distribution_label_0, label='Label 0', alpha=0.5, bins=10) |
| 90 | plt.hist(distribution_label_1, label='Label 1', alpha=0.5, bins=10) |
| 91 | plt.hist(distribution_label_2, label='Label 2', alpha=0.5, bins=10) |
| 92 | |
| 93 | plt.title("Predictions' distribution") |
| 94 | plt.xlabel("PCs Values") |
| 95 | plt.ylabel("Frequency") |
| 96 | plt.legend(title="Labels") |
| 97 | plt.show() |
| 98 | |
| 99 | label_binarizer = LabelBinarizer().fit(targets) |
| 100 | y_onehot_test = label_binarizer.transform(targets) |
| 101 | colors = cycle(["aqua", "darkorange", "cornflowerblue"]) |
| 102 | fig, ax = plt.subplots(figsize=(10, 8)) |
| 103 | for class_id, color in zip(range(0, 3), colors): |
| 104 | RocCurveDisplay.from_predictions( |
| 105 | y_onehot_test[:, class_id], |
| 106 | prediction[-1][:, class_id], |
| 107 | name=f"ROC curve for class: {class_id}", |
| 108 | color=color, |
| 109 | ax=ax, |
| 110 | plot_chance_level=(class_id == 2), |
| 111 | ) |
| 112 | |
| 113 | plt.axis("square") |
| 114 | plt.xlabel("False Positive Rate") |
| 115 | plt.ylabel("True Positive Rate") |
| 116 | plt.title("Extension of Receiver Operating Characteristic\nto One-vs-Rest multiclass") |
| 117 | plt.legend() |
nothing calls this directly
no test coverage detected