How to code a K Nearest Neighbour trading strategy in sklearn

This tutorial covers the following:

  • How to use K Nearest Neighbour to predict whether stock prices will close above 99.5%*(today’s close)
  • How to get the confusion matrix, classification report and ROC curve
  • How to do hyperparameter tuning using a grid search
  • How to do forward chaining with time series (using TimeSeriesSplit)
  • How to code a simple trading strategy using K Nearest Neighbour

Understanding the confusion matrix and classification report

Positive = Predicted Positive
Negative = Predicted Negative

TP = True Positive = Instances that are correctly predicted as positive
FP = False Positive = Instances that are incorrectly predicted as positive

TN = True Negative = Instances that are correctly predicted as negative
FN = False Negative = Instances that are incorrectly predicted as negative

Precision = Out of all the instances whose predicted values are positive, how many are true positives
Recall = Out of all the instances whose actual values are positive, how many are true positives

Accuracy=TP+TNTP+TN+FP+FNPrecision=TPTP+FPRecall=TPTP+FNF1=2recallprecisionrecall+precision\begin{aligned} Accuracy &= \frac{TP+TN}{TP+TN+FP+FN}\\\\ Precision &= \frac{TP}{TP+FP}\\\\ Recall &= \frac{TP}{TP+FN}\\\\ F_1 &= \frac{2*recall*precision}{recall+precision}\\\\ \end{aligned}

Link to Code (as HTML file)

KNN.html


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *