# Dice Distance returning nan. Workaround?

Starting Point: I want to calculate the distances between nominal data. Furthermore, I want to see what difference it makes to only use important features (given some feature selection method). So I calculated the Hamming Distance with important nominal Features. I now want to discover what happens if I create dummy variables (one-hot encoding) and calculate the distance between only important dummy variables.

I tried out the dice distance as it yields the same distance for dummies as the hamming distance for nominal data when using all features.

The Dice distance is calculated by: $$D(x,y) =1- \frac{2a}{2a+b+c}$$,

where a: number of times x and y equal 1

b: number of times x = 1 and y = 0

c: number of times x = 0 and y = 1

Problem: When taking only the important dummy variables, it can happen that $$a = b = c = 0$$!. So the Dice Distance returns Nan.

So what is the distance between two observations whose important dummy variables are all equal to zero? Is it simply 1, because there is no match? Is it 0 because two observations are identical and it should be that $$D(x,y) = 0$$ if $$x = y$$?

Or are there binary distance measures that are more suitable for this task? I personally liked the dice distance for its comparability with the hamming distance.

## 1 Answer

When dealing with fractions that contain a denominator that may equal to $$0$$, it is always a good idea to add a very small value to the denominator to avoid dividing by $$0$$.

For example, in the case of the dice distance, it becomes:

$$D(x,y) =1- \frac{2a}{2a+b+c + 10^{-9}}$$

This means that when $$a=b=c=0$$, then the dice distance will equal $$1$$. $$10^{-9}$$ is an arbitrary value here. You can choose smaller values if you want.

• Nevertheless the question remains if that is suitable for the task. Two identical points would know have the maximum distance with does not seem right... – Malte Mar 7 at 10:54
• Your question has several sub-questions. I only answered the first one. What was wrong with using the Hamming distance? Cosine similarity may also be used here – mhdadk Mar 7 at 11:44