def calculateInterpolatedAveragePrecision(
    recalls,
    precisions
):
    """
    Calculates interpolated average precision according to PASCAL VOC competition.

    :param recalls     list of all recalls, sorted ascendingly
    :param precisions  list of all precisions, with order matching corresponding recall values

    """
    recall_values = [0]
    recall_values += list(recalls)
    recall_values.append(1)

    interp_precision_values = [0.]
    interp_precision_values += list(precisions)
    interp_precision_values.append(0.)

    for i in range(len(interp_precision_values) - 1, 0, -1):
        interp_precision_values[i - 1] = max(interp_precision_values[i - 1], interp_precision_values[i])

    unique_recall_value_indices = []

    for recall_value_idx in range(len(recall_values) - 1):
        if recall_values[recall_value_idx + 1] != recall_values[recall_value_idx]:
            unique_recall_value_indices.append(recall_value_idx + 1)

    AP = 0
    for unique_recall_value_index in unique_recall_value_indices:
        recall_length = recall_values[unique_recall_value_index] - recall_values[unique_recall_value_index - 1]
        AP = AP + (recall_length * interp_precision_values[unique_recall_value_index])

    return (AP, recall_values, interp_precision_values)