QuickRank  v2.0
QuickRank: A C++ suite of Learning to Rank algorithms
ndcg.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 "dcg.h"
26 
27 namespace quickrank {
28 namespace metric {
29 namespace ir {
30 
31 /**
32  * This class implements the Normalized Discounted cumulative Gain NDCG\@k measure.
33  *
34  * NDCG is measured as: \f$ NDCG_k = \frac{1}{IDCG_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  * and \f$IDCG_k\f$ is the NDCG\@K of a perfectly orderd result list.
37  */
38 class Ndcg: public Dcg {
39  public:
40  explicit Ndcg(size_t k = NO_CUTOFF)
41  : Dcg(k) {
42  }
43  virtual ~Ndcg() {
44  }
45 
46  /// Returns the name of the metric.
47  virtual std::string name() const {
48  return NAME_;
49  }
50 
51  static const std::string NAME_;
52 
53  /// \todo TODO: for only zero result slist Yahoo! LTR returns 0.5 instead of 0.0.
54  /// Make this choice available.
55  /// \param rl A results list.
56  /// \param scores a list of scores
57  /// \return The quality score of the result list.
59  const quickrank::data::QueryResults *rl, const Score *scores) const;
60 
61  virtual std::unique_ptr<Jacobian> jacobian(
62  std::shared_ptr<data::RankedResults> ranked) const;
63 
64  protected:
65  /// Computes the IDCG\@K of a given list of labels.
66  /// \param rl The given results list. Only labels are actually used.
67  /// \return IDCG\@K for computed on the given labels.
69 
70  private:
71  friend std::ostream &operator<<(std::ostream &os, const Ndcg &ndcg) {
72  return ndcg.put(os);
73  }
74 
75  virtual std::ostream &put(std::ostream &os) const;
76 
77 };
78 
79 } // namespace ir
80 } // namespace metric
81 } // namespace quickrank
Definition: dataset.cc:28
virtual std::unique_ptr< Jacobian > jacobian(std::shared_ptr< data::RankedResults > ranked) const
Computes the Jacobian matrix.
Definition: ndcg.cc:60
virtual std::string name() const
Returns the name of the metric.
Definition: ndcg.h:47
virtual std::ostream & put(std::ostream &os) const
Prints the short name of the Metric, e.g., "NDCG@K".
Definition: ndcg.cc:91
friend std::ostream & operator<<(std::ostream &os, const Ndcg &ndcg)
Definition: ndcg.h:71
virtual MetricScore evaluate_result_list(const quickrank::data::QueryResults *rl, const Score *scores) const
Definition: ndcg.cc:49
virtual ~Ndcg()
Definition: ndcg.h:43
This class implements the Normalized Discounted cumulative Gain NDCG@k measure.
Definition: ndcg.h:38
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
Ndcg(size_t k=NO_CUTOFF)
Definition: ndcg.h:40
static const std::string NAME_
Definition: ndcg.h:51
MetricScore compute_idcg(const quickrank::data::QueryResults *rl) const
Computes the IDCG@K of a given list of labels.
Definition: ndcg.cc:35
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
double MetricScore
data type for QueryID in L-t-R datasets
Definition: types.h:33