If you ever attempted to forecast and/or used time series analysis, you are probably familiar, at least superficially, with the k-nearest neighbour (k-NN) method. If you are not, this tutorial will help you understand it and enable you to execute it in Excel.
By using the k-NN method, you are about to use one of the distance metrics, as this is the way to establish the similarity between time series. Most of the time people will use either simple absolute differences (L1 norm) or Euclidean distance (L2 norm). The speech recognition community adopted a method called Dynamic Time Warping (DTW), which is by far the most suitable for establishing similarities between the time series. We embedded DTW in the k-NN and this tutorial is all about that.
You will learn the logic behind the methods and how to embed the DTW in the k-NN method. We will use Excel to demonstrate how to manually do the calculations, followed by a VBA macro containing a more complex code. The VBA macro was rendered by the DeepSeek engine. We use the real stock exchange data (AAPL daily closing values) to demonstrate the method(s).
The spreadsheets with all the calculations (including the macro code) are placed here, while the paper containing the tutorial in PDF format can be downloaded or viewed here.