QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
dcg.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 "types.h"
25 #include "metric.h"
26 
27 namespace quickrank {
28 namespace metric {
29 namespace ir {
30 
31 /**
32  * This class implements the Discounted cumulative Gain DCG\@K measure.
33  *
34  * DCG is measured as: \f$ DCG_k = \sum_{i=1}^k \frac{2^{l_i}-1}{\log_2 (i+1)}\f$,
35  * where \f$l_i\f$ is the relevance label of the i-th document.
36  */
37 class Dcg: public Metric {
38  public:
39  explicit Dcg(size_t k = NO_CUTOFF)
40  : Metric(k) {
41  }
42  virtual ~Dcg() {
43  }
44 
45  /// Returns the name of the metric.
46  virtual std::string name() const {
47  return NAME_;
48  }
49 
50  static const std::string NAME_;
51 
53  const quickrank::data::QueryResults *rl, const Score *scores) const;
54 
55  virtual std::unique_ptr<Jacobian> jacobian(
56  std::shared_ptr<data::RankedResults> ranked) const;
57 
58  protected:
59  /// Computes the DCG\@K of a given array of labels.
60  /// \param rl The given array of labels.
61  /// \return DCG\@K for computed on the given labels.
62  MetricScore compute_dcg(const Label *labels, size_t len) const;
63 
64  private:
65  friend std::ostream &operator<<(std::ostream &os, const Dcg &ndcg) {
66  return ndcg.put(os);
67  }
68  virtual std::ostream &put(std::ostream &os) const;
69 
70 };
71 
72 } // namespace ir
73 } // namespace metric
74 } // namespace quickrank
Definition: dataset.cc:28
This class implements the basic functionalities of an IR evaluation metric.
Definition: metric.h:43
MetricScore compute_dcg(const Label *labels, size_t len) const
Computes the DCG@K of a given array of labels.
Definition: dcg.cc:33
friend std::ostream & operator<<(std::ostream &os, const Dcg &ndcg)
Definition: dcg.h:65
double Score
data type for instance truth label
Definition: types.h:30
Dcg(size_t k=NO_CUTOFF)
Definition: dcg.h:39
virtual MetricScore evaluate_result_list(const quickrank::data::QueryResults *rl, const Score *scores) const
Measures the quality of the given results list according to the Metric.
Definition: dcg.cc:41
static const size_t NO_CUTOFF
This should be used when no cut-off on the results list is required.
Definition: metric.h:46
virtual ~Dcg()
Definition: dcg.h:42
virtual std::unique_ptr< Jacobian > jacobian(std::shared_ptr< data::RankedResults > ranked) const
Computes the Jacobian matrix.
Definition: dcg.cc:59
static const std::string NAME_
Definition: dcg.h:50
This class implements the Discounted cumulative Gain DCG@K measure.
Definition: dcg.h:37
This class wraps a set of results for a given query.
Definition: queryresults.h:36
virtual std::ostream & put(std::ostream &os) const
Prints the short name of the Metric, e.g., "NDCG@K".
Definition: dcg.cc:86
float Label
Definition: types.h:29
double MetricScore
data type for QueryID in L-t-R datasets
Definition: types.h:33
virtual std::string name() const
Returns the name of the metric.
Definition: dcg.h:46