Giter VIP home page Giter VIP logo

Comments (1)

tommedema avatar tommedema commented on September 26, 2024

I've now implemented this after the end of the inner loop while keeping track of the smallest distance found in the entire range:

export const getDTWDistance = (
  ser1: number[],
  ser2: number[],
  distFunc: (a: number, b: number) => number,
  earlyAbandonBoundary: number
): number => {
  const matrix: number[][] = []
  for (let i = 0; i < ser1.length; i++) {
    matrix[i] = []
    let minDist = Infinity
    for (let j = 0; j < ser2.length; j++) {
      let cost = Infinity
      if (i > 0) {
        cost = Math.min(cost, matrix[i - 1][j])
        if (j > 0) {
          cost = Math.min(cost, matrix[i - 1][j - 1])
          cost = Math.min(cost, matrix[i][j - 1])
        }
      } else {
        if (j > 0) {
          cost = Math.min(cost, matrix[i][j - 1])
        } else {
          cost = 0
        }
      }

      matrix[i][j] = cost + distFunc(ser1[i], ser2[j])
      if (matrix[i][j] < minDist) {
        minDist = matrix[i][j]
      }
    }

    // Note: early abandoning might be further improved
    // See: https://github.com/GordonLesti/dynamic-time-warping/issues/1
    if (minDist > earlyAbandonBoundary) {
      return minDist
    }
  }

  return matrix[ser1.length - 1][ser2.length - 1]
}

This seems to give the desired effect but of course is not as fast as I would like, probably because it has to finish the inner loop first. Is there a reason why the comparison cannot be made inside the inner loop? @GordonLesti

from dynamic-time-warping.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.