metric.h 3.82 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
#ifndef LIGHTGBM_METRIC_H_
#define LIGHTGBM_METRIC_H_

Guolin Ke's avatar
Guolin Ke committed
4
5
6
#include <LightGBM/utils/log.h>
#include <LightGBM/utils/common.h>

Guolin Ke's avatar
Guolin Ke committed
7
8
9
#include <LightGBM/meta.h>
#include <LightGBM/config.h>
#include <LightGBM/dataset.h>
10
#include <LightGBM/objective_function.h>
Guolin Ke's avatar
Guolin Ke committed
11
12
13
14
15
16
17

#include <vector>

namespace LightGBM {

/*!
* \brief The interface of metric.
18
*        Metric is used to calculate metric result
Guolin Ke's avatar
Guolin Ke committed
19
20
21
22
23
24
25
26
27
28
29
30
*/
class Metric {
public:
  /*! \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
31
  virtual void Init(const Metadata& metadata, data_size_t num_data) = 0;
Guolin Ke's avatar
Guolin Ke committed
32

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

35
  virtual double factor_to_bigger_better() const = 0;
Guolin Ke's avatar
Guolin Ke committed
36
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
37
  * \brief Calcaluting and printing metric result
Guolin Ke's avatar
Guolin Ke committed
38
39
  * \param score Current prediction score
  */
Guolin Ke's avatar
Guolin Ke committed
40
  virtual std::vector<double> Eval(const double* score, const ObjectiveFunction* objective) const = 0;
Guolin Ke's avatar
Guolin Ke committed
41

Guolin Ke's avatar
Guolin Ke committed
42
43
44
45
46
47
  Metric() = default;
  /*! \brief Disable copy */
  Metric& operator=(const Metric&) = delete;
  /*! \brief Disable copy */
  Metric(const Metric&) = delete;

Guolin Ke's avatar
Guolin Ke committed
48
49
50
51
52
  /*!
  * \brief Create object of metrics
  * \param type Specific type of metric
  * \param config Config for metric
  */
Guolin Ke's avatar
Guolin Ke committed
53
  LIGHTGBM_EXPORT static Metric* CreateMetric(const std::string& type, const Config& config);
wxchan's avatar
wxchan committed
54

Guolin Ke's avatar
Guolin Ke committed
55
56
57
58
59
60
61
};

/*!
* \brief Static class, used to calculate DCG score
*/
class DCGCalculator {
public:
Guolin Ke's avatar
Guolin Ke committed
62
63
64
  
  static void DefaultEvalAt(std::vector<int>* eval_at);
  static void DefaultLabelGain(std::vector<double>* label_gain);
Guolin Ke's avatar
Guolin Ke committed
65
66
67
68
  /*!
  * \brief Initial logic
  * \param label_gain Gain for labels, default is 2^i - 1
  */
Guolin Ke's avatar
Guolin Ke committed
69
  static void Init(const std::vector<double>& label_gain);
Guolin Ke's avatar
Guolin Ke committed
70
71
72

  /*!
  * \brief Calculate the DCG score at position k
Qiwei Ye's avatar
Qiwei Ye committed
73
  * \param k The position to evaluate
Guolin Ke's avatar
Guolin Ke committed
74
75
76
77
78
  * \param label Pointer of label
  * \param score Pointer of score
  * \param num_data Number of data
  * \return The DCG score
  */
79
  static double CalDCGAtK(data_size_t k, const label_t* label,
80
    const double* score, data_size_t num_data);
Guolin Ke's avatar
Guolin Ke committed
81
82
83

  /*!
  * \brief Calculate the DCG score at multi position
Qiwei Ye's avatar
Qiwei Ye committed
84
  * \param ks The positions to evaluate
Guolin Ke's avatar
Guolin Ke committed
85
86
87
88
89
90
  * \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,
91
    const label_t* label, const double* score,
92
    data_size_t num_data, std::vector<double>* out);
Guolin Ke's avatar
Guolin Ke committed
93
94
95
96
97
98
99
100

  /*!
  * \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
  */
101
  static double CalMaxDCGAtK(data_size_t k,
102
    const label_t* label, data_size_t num_data);
Guolin Ke's avatar
Guolin Ke committed
103

104
105
106
107
108
  /*!
  * \brief Check the label range for NDCG and lambdarank
  * \param label Pointer of label
  * \param num_data Number of data
  */
109
  static void CheckLabel(const label_t* label, data_size_t num_data);
110

Guolin Ke's avatar
Guolin Ke committed
111
112
113
114
115
116
117
118
  /*!
  * \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,
119
    const label_t* label, data_size_t num_data, std::vector<double>* out);
Guolin Ke's avatar
Guolin Ke committed
120
121
122
123
124
125

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

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


}  // namespace LightGBM


Guolin Ke's avatar
Guolin Ke committed
141
#endif   // LightGBM_METRIC_H_