metric.h 4.04 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
5
6
#ifndef LIGHTGBM_INCLUDE_LIGHTGBM_METRIC_H_
#define LIGHTGBM_INCLUDE_LIGHTGBM_METRIC_H_
Guolin Ke's avatar
Guolin Ke committed
7
8
9

#include <LightGBM/config.h>
#include <LightGBM/dataset.h>
10
#include <LightGBM/meta.h>
11
#include <LightGBM/objective_function.h>
12
13
#include <LightGBM/utils/log.h>
#include <LightGBM/utils/common.h>
Guolin Ke's avatar
Guolin Ke committed
14

15
16
17
#include <string>
#include <vector>

Guolin Ke's avatar
Guolin Ke committed
18
19
20
21
namespace LightGBM {

/*!
* \brief The interface of metric.
22
*        Metric is used to calculate metric result
Guolin Ke's avatar
Guolin Ke committed
23
24
*/
class Metric {
Nikita Titov's avatar
Nikita Titov committed
25
 public:
Guolin Ke's avatar
Guolin Ke committed
26
27
28
29
30
31
32
33
34
  /*! \brief virtual destructor */
  virtual ~Metric() {}

  /*!
  * \brief Initialize
  * \param test_name Specific name for this metric, will output on log
  * \param metadata Label data
  * \param num_data Number of data
  */
Guolin Ke's avatar
Guolin Ke committed
35
  virtual void Init(const Metadata& metadata, data_size_t num_data) = 0;
Guolin Ke's avatar
Guolin Ke committed
36

Guolin Ke's avatar
Guolin Ke committed
37
  virtual const std::vector<std::string>& GetName() const = 0;
38

39
  virtual double factor_to_bigger_better() const = 0;
Guolin Ke's avatar
Guolin Ke committed
40
  /*!
41
  * \brief Calculating and printing metric result
Guolin Ke's avatar
Guolin Ke committed
42
43
  * \param score Current prediction score
  */
Guolin Ke's avatar
Guolin Ke committed
44
  virtual std::vector<double> Eval(const double* score, const ObjectiveFunction* objective) const = 0;
Guolin Ke's avatar
Guolin Ke committed
45

Guolin Ke's avatar
Guolin Ke committed
46
47
48
49
50
51
  Metric() = default;
  /*! \brief Disable copy */
  Metric& operator=(const Metric&) = delete;
  /*! \brief Disable copy */
  Metric(const Metric&) = delete;

Guolin Ke's avatar
Guolin Ke committed
52
53
54
55
56
  /*!
  * \brief Create object of metrics
  * \param type Specific type of metric
  * \param config Config for metric
  */
Guolin Ke's avatar
Guolin Ke committed
57
  LIGHTGBM_EXPORT static Metric* CreateMetric(const std::string& type, const Config& config);
58
59
60
61
62

  /*!
  * \brief Whether boosting is done on CUDA
  */
  virtual bool IsCUDAMetric() const { return false; }
Guolin Ke's avatar
Guolin Ke committed
63
64
65
66
67
68
};

/*!
* \brief Static class, used to calculate DCG score
*/
class DCGCalculator {
Nikita Titov's avatar
Nikita Titov committed
69
 public:
Guolin Ke's avatar
Guolin Ke committed
70
71
  static void DefaultEvalAt(std::vector<int>* eval_at);
  static void DefaultLabelGain(std::vector<double>* label_gain);
Guolin Ke's avatar
Guolin Ke committed
72
73
74
75
  /*!
  * \brief Initial logic
  * \param label_gain Gain for labels, default is 2^i - 1
  */
Guolin Ke's avatar
Guolin Ke committed
76
  static void Init(const std::vector<double>& label_gain);
Guolin Ke's avatar
Guolin Ke committed
77
78
79

  /*!
  * \brief Calculate the DCG score at multi position
Qiwei Ye's avatar
Qiwei Ye committed
80
  * \param ks The positions to evaluate
Guolin Ke's avatar
Guolin Ke committed
81
82
83
84
85
86
  * \param label Pointer of label
  * \param score Pointer of score
  * \param num_data Number of data
  * \param out Output result
  */
  static void CalDCG(const std::vector<data_size_t>& ks,
87
    const label_t* label, const double* score,
88
    data_size_t num_data, std::vector<double>* out);
Guolin Ke's avatar
Guolin Ke committed
89
90
91
92
93
94
95
96

  /*!
  * \brief Calculate the Max DCG score at position k
  * \param k The position want to eval at
  * \param label Pointer of label
  * \param num_data Number of data
  * \return The max DCG score
  */
97
  static double CalMaxDCGAtK(data_size_t k,
98
    const label_t* label, data_size_t num_data);
Guolin Ke's avatar
Guolin Ke committed
99

100
101

  /*!
Andrew Ziem's avatar
Andrew Ziem committed
102
  * \brief Check the metadata for NDCG and LambdaRank
103
104
105
106
107
  * \param metadata Metadata
  * \param num_queries Number of queries
  */
  static void CheckMetadata(const Metadata& metadata, data_size_t num_queries);

108
  /*!
Andrew Ziem's avatar
Andrew Ziem committed
109
  * \brief Check the label range for NDCG and LambdaRank
110
111
112
  * \param label Pointer of label
  * \param num_data Number of data
  */
113
  static void CheckLabel(const label_t* label, data_size_t num_data);
114

Guolin Ke's avatar
Guolin Ke committed
115
116
117
118
119
120
121
122
  /*!
  * \brief Calculate the Max DCG score at multi position
  * \param ks The positions want to eval at
  * \param label Pointer of label
  * \param num_data Number of data
  * \param out Output result
  */
  static void CalMaxDCG(const std::vector<data_size_t>& ks,
123
    const label_t* label, data_size_t num_data, std::vector<double>* out);
Guolin Ke's avatar
Guolin Ke committed
124
125
126
127
128
129

  /*!
  * \brief Get discount score of position k
  * \param k The position
  * \return The discount of this position
  */
130
  inline static double GetDiscount(data_size_t k) { return discount_[k]; }
Guolin Ke's avatar
Guolin Ke committed
131

Nikita Titov's avatar
Nikita Titov committed
132
 private:
Guolin Ke's avatar
Guolin Ke committed
133
  /*! \brief store gains for different label */
134
  static std::vector<double> label_gain_;
Guolin Ke's avatar
Guolin Ke committed
135
  /*! \brief store discount score for different position */
136
  static std::vector<double> discount_;
Guolin Ke's avatar
Guolin Ke committed
137
138
139
140
141
142
143
144
  /*! \brief max position for eval */
  static const data_size_t kMaxPosition;
};


}  // namespace LightGBM


145
#endif   // LIGHTGBM_INCLUDE_LIGHTGBM_METRIC_H_