Test.check failing for property "Euclidian distance between any two normalized vectors are bounded to [0-2]

Im trying to experiment with test.check and Clojure, however im not an expert in either, or math. However it is my understanding that the following code and test should always succeed. Can someone help to clarify if it is my math understanding or my impl that is wrong.

(defn minmax-norm
  [xs]
  (let [min (apply min xs)
        max (apply max xs)]
    (map (fn [x] (/ (- x min) (+ (- max min) (math/pow 10 -100)))) xs)))

(defn euclidean-distance
  [fr fc]
  (->> (minmax-norm fc)
       (map - (minmax-norm fr))
       (map #(math/pow % 2))
       (reduce +)
       math/sqrt))

(defspec preference-fn
  (prop/for-all [[xs ys] (gen/bind (gen/such-that pos? gen/nat) (fn [x]
                                       (gen/tuple
                                        (gen/not-empty (gen/vector gen/large-integer x))
                                        (gen/not-empty (gen/vector gen/large-integer x)))))]
                (let [s (sut/euclidean-distance xs ys)]
                  (t/testing "For any two non-empty vectors of equal length s is bounded between 0-2"
                    (and (<= 0 s) (>= 2 s))))))

1 Like
user> (euclidean-distance [0 0 0] [1 0 0])
1.0
user> (euclidean-distance [0 0 0] [-1 0 0])
1.4142135623730951

:thinking:

Is it that I misunderstand how I should normalize the two vectors? I normalize them independently…

After switching the normalization function to

(defn norm
  [xs]
  (let [magnitude (->> xs
                       (map #(math/pow % 2))
                       (reduce +)
                       math/sqrt)]
    (->> xs (map #(/ % magnitude)))))

The test are passing, might be that the property does not hold true for min-max normalization?

However its not the case that (= (euclidean-distance [10 1]) (euclidean-distance [1 10])) however since the formula involve subtraction I dont think that commutativity is a property of euclidean-distance?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.