Skip to content

Instantly share code, notes, and snippets.

@calebdre
Last active June 14, 2024 00:56
Show Gist options
  • Save calebdre/7fd83603d9b3a251bfe6c83038d72381 to your computer and use it in GitHub Desktop.
Save calebdre/7fd83603d9b3a251bfe6c83038d72381 to your computer and use it in GitHub Desktop.
export const calculateAveragePrecision = (
sampleQuery: SampleQuery,
retrievedResults: QueryResult[],
k: number
) => {
// get the function names from the validation set
const relevantFunctions = sampleQuery.expectedFunctions.map((func) => func.functionName)
// get the function names from the retrieved results
const retrievedFunctionNames = retrievedResults.map((result) => result.funcName)
let relevantCount = 0
let precisionSum = 0
for (let i = 0; i < k; i++) {
// check if the function from the retrieved results is relevant
if (relevantFunctions.includes(retrievedFunctionNames[i])) {
// if so, increment the relevant count and calculate the precision
relevantCount++
precisionSum += relevantCount / (i + 1)
}
}
return precisionSum / Math.min(k, relevantFunctions.length)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment