jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
stat.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_STAT_STAT_HPP_
18 #define JUBATUS_CORE_STAT_STAT_HPP_
19 
20 #include <stdint.h>
21 #include <algorithm>
22 #include <cstdlib>
23 #include <deque>
24 #include <string>
25 #include <utility>
26 #include "jubatus/util/concurrent/rwmutex.h"
27 #include "jubatus/util/data/unordered_map.h"
28 #include "jubatus/util/lang/enable_shared_from_this.h"
29 #include "jubatus/util/lang/shared_ptr.h"
30 #include "../common/version.hpp"
31 #include "../common/exception.hpp"
32 #include "../common/unordered_map.hpp"
33 #include "../framework/mixable_helper.hpp"
34 
35 namespace jubatus {
36 namespace core {
37 namespace stat {
38 
39 class stat_error : public common::exception::jubaexception<stat_error> {
40  public:
41  explicit stat_error(const std::string& msg)
42  : msg_(msg) {
43  }
44  ~stat_error() throw() {
45  }
46 
47  const char* what() const throw() {
48  return msg_.c_str();
49  }
50 
51  private:
52  std::string msg_;
53 };
54 
55 class stat : public jubatus::util::lang::enable_shared_from_this<stat> {
56  public:
57  explicit stat(size_t window_size);
58  virtual ~stat();
59 
60  virtual void get_diff(std::pair<double, size_t>& ret) const;
61  virtual bool put_diff(const std::pair<double, size_t>&);
62  virtual void mix(
63  const std::pair<double, size_t>& lhs,
64  std::pair<double, size_t>& ret) const;
65 
66  void push(const std::string& key, double val);
67 
68  double sum(const std::string& key) const;
69  double stddev(const std::string& key) const;
70  double max(const std::string& key) const;
71  double min(const std::string& key) const;
72 
73  virtual double entropy() const;
74  double moment(const std::string& key, int n, double c) const;
75 
76  virtual void clear();
78  return storage::version();
79  }
80 
81  virtual void pack(framework::packer& packer) const;
82  virtual void unpack(msgpack::object o);
83  std::string type() const;
84 
85  protected:
86  struct stat_val {
88  : n_(0),
89  sum_(0),
90  sum2_(0),
91  max_(0),
92  min_(0) {
93  }
94 
95  void add(double d) {
96  n_ += 1;
97  sum_ += d;
98  sum2_ += d * d;
99 
100  if (n_ > 1) {
101  max_ = std::max(max_, d);
102  } else {
103  max_ = d;
104  }
105 
106  if (n_ > 1) {
107  min_ = std::min(min_, d);
108  } else {
109  min_ = d;
110  }
111  }
112 
113  void rem(double d, const std::string& key, stat& st) {
114  n_ -= 1;
115  sum_ -= d;
116  sum2_ -= d * d;
117  if (max_ == d) {
118  if (n_ > 0) {
119  bool first = true;
120  for (size_t i = 0; i < st.window_.size(); ++i) {
121  if (st.window_[i].second.first != key) {
122  continue;
123  }
124  double d = st.window_[i].second.second;
125  if (first) {
126  max_ = d;
127  first = false;
128  } else {
129  max_ = std::max(max_, d);
130  }
131  }
132  } else {
133  max_ = 0;
134  }
135  }
136  if (min_ == d) {
137  if (n_ > 0) {
138  bool first = true;
139  for (size_t i = 0; i < st.window_.size(); ++i) {
140  if (st.window_[i].second.first != key) {
141  continue;
142  }
143  double d = st.window_[i].second.second;
144  if (first) {
145  min_ = d;
146  first = false;
147  } else {
148  min_ = std::min(min_, d);
149  }
150  }
151  } else {
152  min_ = 0;
153  }
154  }
155  }
156 
157  size_t n_;
158 
159  double sum_, sum2_;
160  double max_;
161  double min_;
162 
163  MSGPACK_DEFINE(n_, sum_, sum2_, max_, min_);
164  };
165 
166  std::deque<std::pair<uint64_t, std::pair<std::string, double> > > window_;
167  jubatus::util::data::unordered_map<std::string, stat_val> stats_;
168 
169  private:
170  size_t window_size_;
171 
172  double e_;
173  double n_;
174 
175  public:
176  MSGPACK_DEFINE(window_size_, window_, stats_, e_, n_);
177 };
178 
181 
182 } // namespace stat
183 } // namespace core
184 } // namespace jubatus
185 
186 #endif // JUBATUS_CORE_STAT_STAT_HPP_
virtual double entropy() const
Definition: stat.cpp:127
MSGPACK_DEFINE(n_, sum_, sum2_, max_, min_)
double stddev(const std::string &key) const
Definition: stat.cpp:97
void rem(double d, const std::string &key, stat &st)
Definition: stat.hpp:113
double max(const std::string &key) const
Definition: stat.cpp:107
std::deque< std::pair< uint64_t, std::pair< std::string, double > > > window_
Definition: stat.hpp:166
double sum(const std::string &key) const
Definition: stat.cpp:88
virtual void unpack(msgpack::object o)
Definition: stat.cpp:189
stat(size_t window_size)
Definition: stat.cpp:32
virtual void pack(framework::packer &packer) const
Definition: stat.cpp:186
MSGPACK_DEFINE(window_size_, window_, stats_, e_, n_)
double min(const std::string &key) const
Definition: stat.cpp:117
double moment(const std::string &key, int n, double c) const
Definition: stat.cpp:147
virtual void clear()
Definition: stat.cpp:181
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
virtual void get_diff(std::pair< double, size_t > &ret) const
Definition: stat.cpp:45
void push(const std::string &key, double val)
Definition: stat.cpp:70
framework::linear_mixable_helper< stat, std::pair< double, size_t > > mixable_stat
Definition: stat.hpp:180
virtual void mix(const std::pair< double, size_t > &lhs, std::pair< double, size_t > &ret) const
Definition: stat.cpp:63
const char * what() const
Definition: stat.hpp:47
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167
storage::version get_version() const
Definition: stat.hpp:77
std::string type() const
Definition: stat.cpp:192
stat_error(const std::string &msg)
Definition: stat.hpp:41
virtual bool put_diff(const std::pair< double, size_t > &)
Definition: stat.cpp:57