<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Loïc Barbe on Medium]]></title>
        <description><![CDATA[Stories by Loïc Barbe on Medium]]></description>
        <link>https://medium.com/@loic.barbe?source=rss-7b37af76ad1f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*g2oHrl4BV1juLD0FdVl0TA.jpeg</url>
            <title>Stories by Loïc Barbe on Medium</title>
            <link>https://medium.com/@loic.barbe?source=rss-7b37af76ad1f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 01:35:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@loic.barbe/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Searching through time series databases using multivariate similarity
metrics]]></title>
            <link>https://medium.com/ml6team/searching-through-time-series-databases-using-multivariate-similarity-metrics-8831ff1e28fa?source=rss-7b37af76ad1f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8831ff1e28fa</guid>
            <category><![CDATA[multivariate-time-series]]></category>
            <category><![CDATA[structured-data]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[similarity-search]]></category>
            <dc:creator><![CDATA[Loïc Barbe]]></dc:creator>
            <pubDate>Mon, 30 May 2022 12:58:37 GMT</pubDate>
            <atom:updated>2022-06-23T11:50:42.873Z</atom:updated>
            <content:encoded><![CDATA[<p>As an intern at ML6, I was given the opportunity to study ways to measure similarities between multivariate time series. Measuring (dis)similarities between time series can be helpful for many tasks. The most important ones are similarity search, clustering, pattern detection and anomaly detection.</p><p>This blogpost will give you an intro to the relevance of this topic and an overview of the solution. We spend extra attention to the efficiency of the proposed solution: rapid calculation of similarity measures enables interesting applications such as detecting anomalies on a production line in real-time.</p><p>The goal of the work outlined in this blogpost was to come up with the best possible method to tackle the notion of similarity for multivariate time series without making assumptions about the data itself. Similar research is done in the context of human motion by <a href="https://www.researchgate.net/publication/221311125_Indexing_Large_Human-Motion_Databases">Keogh</a>, and comparative studies done by <a href="https://www.researchgate.net/publication/300007506_A_Time_Series_Classification_Approach_for_Motion_Analysis_Using_Ensembles_in_Ubiquitous_Healthcare_Systems">Salahelding</a> and <a href="https://iranjournals.nlai.ir/handle/123456789/336172">Salarpour</a>.</p><h3>Dataset and demo use case</h3><p>To show what the system is capable of, we created a <a href="https://level-clone-332910.ew.r.appspot.com/">demo</a> that shows the performance and speed of the model we&#39;ve come up with.</p><h3>Dataset</h3><p>The <a href="http://dreamdragon.github.io/PennAction/">Penn Action</a> dataset from the University of Pennsylvania consists of recordings of multiple human actions.</p><p>They provide 2326 samples with 13 key points manually annotated on the body. The samples are labelled by their action and poses, resulting in 54 different classes. These are only used for validation of the results.</p><h3>Demo use case</h3><p>The use case in the <a href="https://level-clone-332910.ew.r.appspot.com/">demo</a> is to find the five most similar actions for a given sample. At first sight, it looks like a computer vision job; but we can parse these videos into time series with a tiny memory footprint. We extract the keypoints of the moving subject and analyse their relative movement over time. These are 2D data (the X- and Y-coordinate), resulting in a multivariate time series of 26 dimensions.</p><p>We do not claim to compete with the current state of the art within computer vision when it comes to supervised classification performance (please refer to <a href="https://blog.ml6.eu/sports-video-analysis-in-the-real-world-realtime-tennis-action-recognition-using-movinet-stream-813200aa589f">this blogpost by our intern Timo Martens</a> if you want to have a view on that). We rather focused on creating an unsupervised similarity metric that works for multivariate time series</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*eKM5gH3eiAqTtlBa" /><figcaption>Figure 1: Example annotated sample of a baseball player</figcaption></figure><p>Figure 1 visualises the annotation of a baseball player, and in Figure 2, you can see the corresponding time series with the 13 key points.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qJrTvgucRP1XW1FpifR5LQ.gif" /><figcaption>Figure 2: Visualisation of the coordinates of all the key points.</figcaption></figure><h3>Summary system</h3><p>To get an overview of the whole implementation, Figure 3 illustrates the step by step graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jmjdnUAgXnyks_i1" /><figcaption>Figure 3: Summary of the system</figcaption></figure><p>It has two parts, the first being carried out in the background before the application starts and the second is run during inference.</p><p><strong>Pre compute:</strong></p><ol><li>Generate a distance matrix based on the similarity distance.</li><li>Construct a hierarchical tree with the help of a linkage method.</li></ol><p><strong>Inference:</strong></p><ol><li>Traverse the tree with Smart Selection.</li><li>Select the five most similar samples.</li></ol><h3>Similarity distance</h3><p>To find similar samples, we need to develop a method to determine the similarity between two samples. Given a single ‘query’ sample, we can rank all other samples based on this similarity measure.</p><p>The first thing that probably comes to your mind to fix this problem is to calculate the Euclidean distance between points at every time index.</p><p>If the two actions begin simultaneously, this method works, but if the camera starts rolling a few seconds later, there is a shift in time. The result is that similar points don&#39;t match in time anymore.</p><p>This problem is shown in Figure 4. Two people walk with the same style, but the plotted coordinates show a phase shift.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*vcwveELJCopSueaY" /><figcaption>Figure 4: 3D keypoint <a href="https://en.wikipedia.org/wiki/Dynamic_time_warping#/media/File:Two_repetitions_of_a_walking_sequence_of_an_individual_recorded_using_a_motion-capture_system.gif">visualisation </a>of two people</figcaption></figure><p>Aside from the slight difference in the start of the action, there is a big difference in pace. If the key points are plotted in time, they look more &#39;stretched&#39; out, resulting in a significant error with the Euclidean method.</p><p>Shazam engineers encountered a similar challenge in linking a part of a recorded song to a sample from the dataset, which has the metadata of that song. They ended up using the spectrogram of the song to create a fingerprint ID; this is well explained in a <a href="https://medium.com/@treycoopermusic/how-shazam-works-d97135fb4582">blogpost</a>.</p><p>The method is robust against noise during recording and the time shift due to starting recording later. But there is one scenario where it always fails. At a concert, Shazam will only succeed if the artist is lipsyncing. If the artist performs live, the spectrogram of the recorded part is not the same as the one in the dataset.</p><p>If we look at our use case, we conclude it has not the same task. We search for a ranking based similarity method to find multiple similar samples instead of the one identical. That aside, samples that are similar but vary in the pace of the action have a different spectrogram and would not be linked to each other.</p><p>That&#39;s why we opt for variants of Dynamic Time Warping (DTW), which is a method that searches for an optimal match between two given sequences that have a phase shift.</p><h3>Dynamic Time Warping</h3><p>Instead of comparing key points frame per frame, the algorithm also looks back and forward and matches the most similar points. Originally the algorithm was used for audio processing where there is a shift in time and speed.</p><p>To sketch a clearer view of the principle, Figure 5 visualises DTW on two key points (the left ankle and wrist) of the 13. The grey lines are the distance calculations with each point, and the highlighted red lines are the resulting matching point.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*c2nb8pqD2Kgy35zW" /><figcaption>Figure 5: Visualisation of DTW algorithm</figcaption></figure><p>The distance of each point with each point is used to construct the matrix that wraps the matching path, this path is then used to calculate similarity distance. Figure 6 is a simplified example of a path made from the distances with each point presented as a matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uXOdpQAau0dxcB5Id7W_8w.png" /><figcaption>Figure 6: Example of a path constructed from the distance matrix</figcaption></figure><p>In the context of multivariate, there are multiple time series. In Figure 5 is matching done for each time series individually. To apply DTW on multivariate samples the distance matrix is done for each time series and summed up afterwards. This results in one matrix to determine the path and the similarity distance for multivariate samples.</p><blockquote>The Euclidean distance is used in these examples to calculate the distances between points. But depending on the use case other distance metrics can perform better.</blockquote><h4>Tslearn</h4><p>We opted for Numba in combination with Numpy to implement DTW to improve the execution time. The package combination replaces in some parts the JIT compiler and a wrapper around a C implementation.</p><p>A widely used saying in software engineering is ‘Don’t reinvent the wheel’, so we searched for an open-source project that had already implemented DTW with Numba. We ended up using the <a href="https://tslearn.readthedocs.io/en/stable/">Tslearn</a> package.</p><h3>Search</h3><p>We finally have a suitable way to compare two (multivariate) time series. The next step is to find the N most similar samples in the whole dataset.</p><p>In our demo, we implement both the brute force method and an approximate technique. The latter is coined “Smart Search”.</p><h4>Brute Force</h4><p>The brute force method calculates all (N x (N-1)) distances for every sample in the dataset and the five most similar samples are returned based on the similarity distance.</p><h4>Smart search</h4><p>The average time needed to find the five most similar samples in the demo is around 5 seconds. The first thought that came to our minds was &#39;Is there a faster, less naive way to search the dataset?&#39;.</p><p>And there is! The basic idea is to use the similarities between the samples already in the dataset. If a new instance matches a sample from the dataset, the chances are high that the neighbour’s samples are similar.</p><p>The result is a trade-off between accuracy and search time (see Figure 12).</p><h4>Dendrogram</h4><p>The easiest way to know how close samples are to each other is by creating a distance matrix based on the similarity distance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*unzZECyhpjEy6k_oJIrqfQ.png" /><figcaption>Figure 7: Example of a distance matrix based on the similarity distance with the DTW algorithm</figcaption></figure><p>We can link multiple samples and groups of samples with a linkage method. If this process continues until there is one group, we have a hierarchical tree.</p><p>The tree is called a dendrogram, and figure 8 visualises the principle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XSQkZ3z-ZNfb2kgj" /><figcaption>Figure 8: Principle of a hierarchical tree</figcaption></figure><p>It starts with a root node containing all the samples and then splits into two nodes (A and B). This process continues until every node consists of only one sample. The result is a tree in which the samples are sorted by their similarity. To obtain the cluster of a particular sample, you can climb up the tree starting from the id of that sample. Figure 8 shows this phenomenon in more detail; sample ‘0’ is part of nodes ‘1’ and A.</p><p>This algorithm applied to the Penn action dataset results in the dendrogram shown in Figure 9.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YnZgtaP622kUOE2B" /><figcaption>Figure 9: The dendrogram of the dataset</figcaption></figure><h4>The problem</h4><p>We have a nicely constructed tree, but we can’t use it straight from the forest. It is not an option to start from the bottom because the sample is not presented there so we flip the strategy and start at the top. But there is still a problem in contrast to a binary tree; we have no decision method to traverse the tree. This problem is visualised in Figure 10.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cDRsllSoKGVBZf4X2sn1Nw.png" /><figcaption>Figure 10: The traverse problem explained</figcaption></figure><h4>Smart selection</h4><p>We introduce an approximation method called “Smart Selection” to establish a speed gain without compromising accuracy to solve the traversing problem.</p><p>A valuable feature of the dendrogram tree is that it can give us all the samples in each node. Figures 8 illustrate that with the colours A and B assigned to the array of samples.</p><p>The fastest solution would be to get the sample in the middle of the node that can be interpreted as the node&#39;s centroid. But this assumption is not very robust. The alternative is to pick a few of them to represent the whole node to minimise the error.</p><p>These are used to calculate a similarity distance with the given sample. Eventually, a decision is made by choosing which node holds the sample with the lowest distance. The left side of Figure 11 illustrates this process where node A is the chosen one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vJZyph7M2k_7WvZe" /><figcaption>Figure 11: Visualisation of the Smart Selection</figcaption></figure><p>Ok, now we have a way to decide, but how do we choose these samples representing the node?</p><p>To choose samples that will be used to represent the node we run over the ordered samples and reduce the selection by interleaving. The size of the steps is calculated by a new hyperparameter called <em>step ratio</em>. This parameter makes the strategy flexible, scalable for larger datasets and can be changed during inference. The ratio can also be interpreted as the trade-off between a fast and a more accurate result.</p><h3>Scalability</h3><h4>Parallelisation</h4><p>In the Smart Search strategy, many computation tasks can run in parallel. A disadvantage of parallelisation in Python is that there is a lot of overhead. After thorough testing, assigning nodes A and B (see Figure 8) to two workers/threads achieved the best results.</p><h3>Quantitative results</h3><p>The goal of the trade-off was to reduce the computation time; Figure 12 visualises this trade-off with on the Y-axis the accuracy and on the X-axis the computation time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5ZaE8fc_p0CY-zDXRnP7GA.png" /><figcaption>Figure 12: Results of the trade-off accuracy vs computation time.</figcaption></figure><blockquote>Note that the brute force method is used as the root for the accuracy and computation time.</blockquote><p>From the results in Figure 12, we can conclude that the<em> </em>Smart Search is a success; using a step ratio of 0.3, we can improve the brute force algorithm by three without losing any accuracy.</p><h3>Execution time results</h3><p>The Smart search was not the only approach to improving the computation time; the most significant improvement was optimising the JIT compiler. Figure 13 shows the average computation time for a search in pure Python (brute force), Tslearn (brute force), and Tslearn combined with Smart Search. The speed jump between the standard JIT compiler and the replacement with an optimised one provided by Tslearn which uses Numba underneath is the biggest one; that big that the y axis is a logarithmic scale.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*-FRJ9rKR46l5kOHvgQHfVg.png" /><figcaption>Figure 13: Comparison of the average computation time.</figcaption></figure><p>The average computation time with brute force and no improvements to the compiler is around 385 seconds. This takes a very long time compared to the accelerating methods, 5 and 2.5 seconds respectively.</p><h3>Conclusion</h3><p>We used state-of-the-art packages to improve upon the JIT compiler. This was a small step in code but a big one in terms of execution time. Nonetheless, inference times were still too long with brute force.</p><p>Using multidimensional DTW we create a search tree spanning our time series database. We build this hierarchical index in an offline fashion and leverage it during inference. This method has several advantages. It speeds up the search several times and lets the user have control of the inference speed/accuracy duality. It allows us to compare thousands of complex time series in the blink of an eye.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8831ff1e28fa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ml6team/searching-through-time-series-databases-using-multivariate-similarity-metrics-8831ff1e28fa">Searching through time series databases using multivariate similarity
metrics</a> was originally published in <a href="https://medium.com/ml6team">ML6team</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>