The aim is to calculate the similarity between two foods given the nutritional content of each. After some reading, it seems the most popular measure for this sort of problem is the cosine similarity measure.

My initial approach however was to use a quadratic function as an objective function. For $n$ categories $c = [Protein, Carbs]$ and positive vectors $x = [200, 100]$ and $y = [500, 50]$, where $x_0$ is the amount of protein in food $x$, we define the similarity function as:

\begin{equation} similarity = \frac{\sum_{i=0}^n \frac{-abs(x_i - y_i)^2}{max(x_i,y_i)^2} + 1}{n} \end{equation}

For example, for $c = 0$ (Protein), the absolute difference between the two foods is $500 - 200 = 300$ and the max is $500$. I feed this number into the quadratic function $y = \frac{-x^2}{500^2} + 1$, in essence calculating $\frac{-300^2}{500^2} + 1$. I repeat for the second category Carbs and average the two results to get the similarity measure as per the $similarity$ function above.

The objective function for the example above would look like the below:

Example quadratic objective function

If the difference is 0, we get a value of 1. If the difference is the maximum 500, we get a value of 0. More importantly, the convex nature of the quadratic function means the bigger the difference, the larger its effect on the similarity measure.

My question is, what is the problem with this approach, and why would you prefer to use cosine similarity?


1 Answer 1


Cosine similarity is best when the data is sparse (e.g. it contains a lot of zeros) and the magnitude of the values isn't extremely important.

When the data contains a lot of zeros, using functions that calculate the distance between points (ie. x - y ) end up with large deltas that skew the results which will likely give misleading results. Especially in the case where the value 0 means there is no data for that point.

Cosine Similarity uses the dot product, so any zero values are tossed away from the final result. Thus, you'll get vastly different similarity values when using sparse data with the Cosine Similarity metric versus something like Euclidean Distance.

In your case, I'm not sure but it seems like the magnitude of your values is important and there would be very few zero values, so Cosine Similarity may not be a good choice for your problem.

  • $\begingroup$ If cosine isn't ideal, what would be a better approach? A simple dot product? $\endgroup$ Sep 30, 2019 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.