All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
performance_measures.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014 Andrzej Pacuk
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PALL_PERFORMANCE_MEASURES_HPP
16 #define PALL_PERFORMANCE_MEASURES_HPP
17 
18 #include <boost/range/combine.hpp>
19 #include <boost/range/empty.hpp>
20 #include <boost/range/size.hpp>
21 #include <boost/tuple/tuple.hpp>
22 
23 #include <algorithm>
24 #include <cmath>
25 #include <utility>
26 
27 namespace paal {
28 
39 template<typename FloatType = double,
40  typename Probs, typename TestResults>
41 FloatType log_loss(Probs &&probs, TestResults &&test_results) {
42  assert(boost::size(probs) == boost::size(test_results));
43  assert(!boost::empty(probs));
44 
45  FloatType loss{};
46  static FloatType EPSILON{1e-6};
47  for(auto prob_result : boost::combine(probs, test_results)) {
48  FloatType prob, result;
49  boost::tie(prob, result) = prob_result;
50  loss -= std::log(std::max(result ? prob : 1 - prob,
51  EPSILON));
52  }
53 
54  return loss / boost::size(probs);
55 }
56 
65 template<typename FloatType>
66 FloatType likelihood_from_log_loss(FloatType log_loss) {
67  return std::exp(-log_loss);
68 }
69 
80 template<typename FloatType = double,
81  typename Probs, typename TestResults>
82 FloatType likelihood(Probs &&probs, TestResults &&test_results) {
83  return likelihood_from_log_loss(log_loss<FloatType>(std::forward<Probs>(probs),
84  std::forward<TestResults>(test_results)));
85 }
86 
97 template<typename FloatType = double,
98 typename Probs, typename TestResults>
99 FloatType mean_absolute_error(Probs &&probs, TestResults &&test_results) {
100  assert(boost::size(probs) == boost::size(test_results));
101  assert(!boost::empty(probs));
102 
103  FloatType loss{};
104  for(auto prob_result : boost::combine(probs, test_results)) {
105  FloatType prob, result;
106  boost::tie(prob, result) = prob_result;
107  loss += std::abs(prob - result);
108  }
109 
110  return loss / boost::size(probs);
111 }
112 
113 }
114 
115 #endif /* PALL_PERFORMANCE_MEASURES_HPP */
116 
FloatType log_loss(Probs &&probs, TestResults &&test_results)
FloatType likelihood_from_log_loss(FloatType log_loss)
FloatType likelihood(Probs &&probs, TestResults &&test_results)
FloatType mean_absolute_error(Probs &&probs, TestResults &&test_results)