How do I implement this similarity measure in Python?
Question: I tried implementing the distance measure shown in the image, in Python as
such:

    
    
    import numpy as np
    
    A = [1, 2, 3, 4, 5, 6, 7, 8, 1]
    B = [1, 2, 3, 2, 4, 6, 7, 8, 2]
    
    A = np.asarray(A).flatten()
    B = np.asarray(B).flatten()
    
    x = np.sum(1 - np.divide((1 + np.minimum(A, B)), (1 + np.maximum(A, B))))
    print("Distance: {}".format(x))
    

but after testing, it doesn't seem to be the right approach. The maximum value
returned if there's no similarity at all between the given vectors should be
1, with 0 as perfect similiarity. A and B in the image are both vectors with
size m.

Edit: forgot to add that I ignored the part for min(A, B) < 0 as that wont
ever happen for my intentions

[![enter image description
here](http://i.stack.imgur.com/QH48e.png)](http://i.stack.imgur.com/QH48e.png)


Answer: This should work. First, we create a matrix `AB` by stacking the columns and
calculate the minimum vector `AB_min` and maximum vector `AB_max` out of that.
Then, we compute `D` as you defined it, making use of `numpy.where` to specify
the two conditions. After that, we sum the elements to get the `D_proposed` as
you defined it. It gives a value of `0.9` for this example.

    
    
    import numpy as np
    
    A = [1, 2, 3, 4, 5, 6, 7, 8, 1]
    B = [1, 2, 3, 2, 4, 6, 7, 8, 2]
    
    AB = np.column_stack((A,B))
    
    AB_min = np.min(AB,1)
    AB_max = np.max(AB,1)
    print AB_min
    print AB_max
    
    D = np.where(AB_min >= 0.,\
            1. - (1. + AB_min) / (1. + AB_max),\
            1. - (1. + AB_min + abs(AB_min)) / (1. + AB_max + abs(AB_min)))
    print D
    
    D_proposed = np.sum(D)
    print D_proposed
    

