Computing the Mean Average Precision I've read this article about Mean Average Precision (MAP).

Now, in my C++ code I have an std::vector&lt;std::string&gt; queries, where queries[i] is the identifier of the i-th query.

In addition, I have a std::vector&lt;std::vector&lt;std::string&gt; truePositives where truePositives[i][z] is the z-th true positive correspondent to the i-th query. Since the actual order in truePositives[i] isn't important, I could have used std::vector&lt;std::set&lt;std::string&gt;&gt;, but whatever...

Finally, I have a std::vector&lt;std::string&gt; topkTest where topkTest[j] is the j-th element in the top-k ranked list returned by the system for the given query, where k = min(data set size, 10k) (following the suggestion in this question).

Here you can find my code to compute the MAP. I post it here because there is no actual way to say if the returned result (the map value) is correct or not.

    float map = 0;
    for(size_t i=0; i&lt;queries.size(); i++){
        std::vector&lt;std::string&gt; topkTest;
        //populate topkTest somehow using k
        float correct = 0;
        float ap = 0;
        for(size_t j=0; j&lt;topkTest.size(); j++){
            //if topkTest[j] belongs to the true positives, increment the number of correct images
            if(std::find(truePositives.begin(), truePositives.end(), topkTest[j]) != queries.end())
                ap += ++correct / (j+1);
        map += ap / topkTest.size();
    }
    map /= queries.size(),


What do you think about this?
The equation for computing the mean average precision (MAP) is shown below:

$$
\text{MAP}(Q) = \frac{1}{\lvert Q \rvert} \sum_{j=1}^{\lvert Q \rvert}\frac{1}{m_j} \sum_{k=1}^{m_j} \text{Precision}(R_{jk})
$$

The inner summation is something that we need to focus on. The outer summation is simply an average over the queries of the inner sum. The inner summation calculates point-wise precision values at the recall points. A recall point is defined as a position in the ranked list which retrieves a relevant document. Note that this quantity is divided by the total number of relevant documents for the jth query denoted by \$m_j\$, which adds the recall factor in an otherwise precision oriented metric.

Coming back to your code, you should thus change the following line

map += ap / topkTest.size();


to

map += ap / truePositives.size();


The rest of the code is okay.