title: Cosine distance of RandomAccessSparseVectors in Mahout
body: <p>I am new to Mahout, and have been lately transforming a lot of my previous machine learning code to this framework. In many places, I am using cosine similarity between vectors for clustering, classification, etc. Investigating Mahout's <a href="https://builds.apache.org/job/Mahout-Quality/javadoc/org/apache/mahout/common/distance/CosineDistanceMeasure.html#distance%28double,%20org.apache.mahout.math.Vector,%20org.apache.mahout.math.Vector%29" rel="nofollow">distance method</a>, however, gave me quite a surprise. In the following code snippet, the dimension and the float values are taken from an actual output of one of my programs (not that it matters here):</p>
  
  <pre><code>import org.apache.mahout.math.RandomAccessSparseVector;
  import org.apache.mahout.common.distance.CosineDistanceMeasure;
  
  public static void main(String[] args) {
      RandomAccessSparseVector u = new RandomAccessSparseVector(373);
      RandomAccessSparseVector v = new RandomAccessSparseVector(373);
      u.set(24, 0.4526985183337534);
      u.set(55, 0.5333219834564495);
      u.set(54, 0.5333219834564495);
      u.set(53, 0.4756042214095471);
  
      v.set(57, 0.6653016370845252);
      v.set(56, 0.6653016370845252);
      v.set(11, 0.3387439495921685);
  
      CosineDistanceMeasure cosineDistanceMeasure = new CosineDistanceMeasure();
      System.out.println(cosineDistanceMeasure.distance(u, v));
  }
  </code></pre>
  
  <p>The output is <code>1.0</code>. Shouldn't it be <code>0.0</code>?</p>
  
  <p>Comaring this with the output of <code>cosineDistanceMeasure.distance(u, u)</code>, I realize that what I am looking for is <code>1 - cosineDistanceMeasure.distance(u, v)</code>. But this <em>reversal</em> just doesn't make sense to me. Any idea why it was implemented this way? Or am I missing something very obvious?</p>