QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
metric.h
Go to the documentation of this file.
1 /*
2  * QuickRank - A C++ suite of Learning to Rank algorithms
3  * Webpage: http://quickrank.isti.cnr.it/
4  * Contact: quickrank@isti.cnr.it
5  *
6  * Unless explicitly acquired and licensed from Licensor under another
7  * license, the contents of this file are subject to the Reciprocal Public
8  * License ("RPL") Version 1.5, or subsequent versions as allowed by the RPL,
9  * and You may not copy or use this file in either source code or executable
10  * form, except in compliance with the terms and conditions of the RPL.
11  *
12  * All software distributed under the RPL is provided strictly on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND
14  * LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
15  * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16  * PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific
17  * language governing rights and limitations under the RPL.
18  *
19  * Contributor:
20  * HPC. Laboratory - ISTI - CNR - http://hpc.isti.cnr.it/
21  */
22 #pragma once
23 
24 #include <iostream>
25 #include <climits>
26 #include <memory>
27 
28 #include <stdint.h>
29 
30 #include "data/queryresults.h"
31 #include "data/rankedresults.h"
32 #include "data/dataset.h"
33 #include "data/vertical_dataset.h"
34 #include "types.h"
35 
36 namespace quickrank {
37 namespace metric {
38 namespace ir {
39 
40 /**
41  * This class implements the basic functionalities of an IR evaluation metric.
42  */
43 class Metric {
44  public:
45  /// This should be used when no cut-off on the results list is required.
46  static const size_t NO_CUTOFF = SIZE_MAX;
47 
48  /// Creates a new metric with the specified cut-off threshold.
49  ///
50  /// \param k The cut-off threshold.
51  explicit Metric(size_t k = NO_CUTOFF) {
52  set_cutoff(k);
53  }
54  virtual ~Metric() {
55  }
56 
57  /// Returns the name of the metric.
58  virtual std::string name() const = 0;
59 
60  /// Returns the current cut-off of the Metric.
61  size_t cutoff() const {
62  return cutoff_;
63  }
64  /// Updates the cut-off of the Metric.
65  void set_cutoff(size_t k) {
66  cutoff_ = k == 0 ? NO_CUTOFF : k;
67  }
68 
69  /// Measures the quality of the given results list according to the Metric.
70  ///
71  /// \param rl A results list.
72  /// \param scores a list of scores
73  /// \return The quality score of the result list.
75  const quickrank::data::QueryResults *rl, const Score *scores) const = 0;
76 
78  const std::shared_ptr<data::Dataset> dataset, const Score *scores) const {
79  if (dataset->num_queries() == 0)
80  return 0.0;
81  MetricScore avg_score = 0.0;
82  for (size_t q = 0; q < dataset->num_queries(); q++) {
83  std::shared_ptr<data::QueryResults> r = dataset->getQueryResults(q);
84  avg_score += evaluate_result_list(r.get(), scores);
85  scores += r->num_results();
86  }
87  avg_score /= (MetricScore) dataset->num_queries();
88  return avg_score;
89  }
90 
92  const std::shared_ptr<data::VerticalDataset> dataset,
93  const Score *scores) const {
94  if (dataset->num_queries() == 0)
95  return 0.0;
96  MetricScore avg_score = 0.0;
97  for (unsigned int q = 0; q < dataset->num_queries(); q++) {
98  std::shared_ptr<data::QueryResults> r = dataset->getQueryResults(q);
99  avg_score += evaluate_result_list(r.get(), scores);
100  scores += r->num_results();
101  }
102  avg_score /= (MetricScore) dataset->num_queries();
103  return avg_score;
104  }
105 
106  /// Computes the Jacobian matrix.
107  /// This is a symmetric matrix storing the metric "decrease" when two documents scores
108  /// are swapped.
109  /// \param rl A results list.
110  /// \return A smart-pointer to the Jacobian Matrix.
111  /// \todo TODO: provide def implementation
112  virtual std::unique_ptr<Jacobian> jacobian(
113  std::shared_ptr<data::RankedResults> ranked) const {
114  auto jacobian = std::unique_ptr<Jacobian>(
115  new Jacobian(ranked->num_results()));
116  auto results = std::shared_ptr<data::QueryResults>(
117  new data::QueryResults(ranked->num_results(), ranked->sorted_labels(),
118  NULL));
119 
120  MetricScore orig_score = evaluate_result_list(results.get(),
121  ranked->sorted_scores());
122  const size_t size = std::min(cutoff(), results->num_results());
123  for (size_t i = 0; i < size; ++i) {
124  double *p_jacobian = jacobian->vectat(i, i + 1);
125  for (size_t j = i + 1; j < results->num_results(); ++j) {
126  std::swap(ranked->sorted_scores()[i], ranked->sorted_scores()[j]);
127  MetricScore new_score = evaluate_result_list(results.get(),
128  ranked->sorted_scores());
129  *p_jacobian++ = new_score - orig_score;
130  std::swap(ranked->sorted_scores()[i], ranked->sorted_scores()[j]);
131  }
132  }
133 
134  return jacobian;
135  }
136 
137  private:
138 
139  /// The metric cutoff.
140  size_t cutoff_;
141 
142  /// The output stream operator.
143  friend std::ostream &operator<<(std::ostream &os, const Metric &m) {
144  return m.put(os);
145  }
146  /// Prints the short name of the Metric, e.g., "NDCG@K"
147  virtual std::ostream &put(std::ostream &os) const = 0;
148 
149 };
150 
151 } // namespace ir
152 } // namespace metric
153 } // namespace quickrank
Definition: dataset.cc:28
virtual MetricScore evaluate_dataset(const std::shared_ptr< data::Dataset > dataset, const Score *scores) const
Definition: metric.h:77
This class implements the basic functionalities of an IR evaluation metric.
Definition: metric.h:43
virtual MetricScore evaluate_dataset(const std::shared_ptr< data::VerticalDataset > dataset, const Score *scores) const
Definition: metric.h:91
SymMatrix< double > Jacobian
data type for evaluation metric final outcome
Definition: types.h:36
friend std::ostream & operator<<(std::ostream &os, const Metric &m)
The output stream operator.
Definition: metric.h:143
size_t cutoff() const
Returns the current cut-off of the Metric.
Definition: metric.h:61
size_t cutoff_
The metric cutoff.
Definition: metric.h:140
virtual std::ostream & put(std::ostream &os) const =0
Prints the short name of the Metric, e.g., "NDCG@K".
virtual std::string name() const =0
Returns the name of the metric.
virtual ~Metric()
Definition: metric.h:54
virtual std::unique_ptr< Jacobian > jacobian(std::shared_ptr< data::RankedResults > ranked) const
Computes the Jacobian matrix.
Definition: metric.h:112
double Score
data type for instance truth label
Definition: types.h:30
static const size_t NO_CUTOFF
This should be used when no cut-off on the results list is required.
Definition: metric.h:46
Metric(size_t k=NO_CUTOFF)
Creates a new metric with the specified cut-off threshold.
Definition: metric.h:51
virtual MetricScore evaluate_result_list(const quickrank::data::QueryResults *rl, const Score *scores) const =0
Measures the quality of the given results list according to the Metric.
This class wraps a set of results for a given query.
Definition: queryresults.h:36
void set_cutoff(size_t k)
Updates the cut-off of the Metric.
Definition: metric.h:65
double MetricScore
data type for QueryID in L-t-R datasets
Definition: types.h:33