jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
Classes | Public Member Functions | Protected Attributes | Private Attributes | List of all members
jubatus::core::stat::stat Class Reference

#include <stat.hpp>

Inheritance diagram for jubatus::core::stat::stat:
Inheritance graph
Collaboration diagram for jubatus::core::stat::stat:
Collaboration graph

Classes

struct  stat_val
 

Public Member Functions

virtual void clear ()
 
virtual double entropy () const
 
virtual void get_diff (std::pair< double, size_t > &ret) const
 
storage::version get_version () const
 
double max (const std::string &key) const
 
double min (const std::string &key) const
 
virtual void mix (const std::pair< double, size_t > &lhs, std::pair< double, size_t > &ret) const
 
double moment (const std::string &key, int n, double c) const
 
 MSGPACK_DEFINE (window_size_, window_, stats_, e_, n_)
 
virtual void pack (framework::packer &packer) const
 
void push (const std::string &key, double val)
 
virtual bool put_diff (const std::pair< double, size_t > &)
 
 stat (size_t window_size)
 
double stddev (const std::string &key) const
 
double sum (const std::string &key) const
 
std::string type () const
 
virtual void unpack (msgpack::object o)
 
virtual ~stat ()
 

Protected Attributes

jubatus::util::data::unordered_map< std::string, stat_valstats_
 
std::deque< std::pair< uint64_t, std::pair< std::string, double > > > window_
 

Private Attributes

double e_
 
double n_
 
size_t window_size_
 

Detailed Description

Definition at line 55 of file stat.hpp.

Constructor & Destructor Documentation

jubatus::core::stat::stat::stat ( size_t  window_size)
explicit

Definition at line 32 of file stat.cpp.

References JUBATUS_EXCEPTION.

33  : window_size_(window_size),
34  e_(0),
35  n_(0) {
36  if (!(1 <= window_size)) {
37  throw JUBATUS_EXCEPTION(
38  common::invalid_parameter("1 <= window_size"));
39  }
40 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::core::stat::stat::~stat ( )
virtual

Definition at line 42 of file stat.cpp.

42  {
43 }

Member Function Documentation

void jubatus::core::stat::stat::clear ( )
virtual

Definition at line 181 of file stat.cpp.

References stats_, and window_.

181  {
182  window_.clear();
183  stats_.clear();
184 }
std::deque< std::pair< uint64_t, std::pair< std::string, double > > > window_
Definition: stat.hpp:166
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167
double jubatus::core::stat::stat::entropy ( ) const
virtual

Definition at line 127 of file stat.cpp.

References e_, n_, and stats_.

127  {
128  if (n_ == 0) {
129  // not MIXed ever yet
130  size_t total = 0;
131  for (jubatus::util::data::unordered_map<std::string, stat_val>::
132  const_iterator p = stats_.begin(); p != stats_.end(); ++p) {
133  total += p->second.n_;
134  }
135  double ret = 0;
136  for (jubatus::util::data::unordered_map<std::string, stat_val>::
137  const_iterator p = stats_.begin(); p != stats_.end(); ++p) {
138  double pr = p->second.n_ / static_cast<double>(total);
139  ret += pr * std::log(pr);
140  }
141  return -1.0 * ret;
142  }
143  double n = n_;
144  return std::log(n) - e_ / n_;
145 }
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167
void jubatus::core::stat::stat::get_diff ( std::pair< double, size_t > &  ret) const
virtual

Definition at line 45 of file stat.cpp.

References stats_.

45  {
46  ret.first = 0;
47  ret.second = 0;
48 
49  for (jubatus::util::data::unordered_map<std::string, stat_val>::
50  const_iterator p = stats_.begin(); p != stats_.end(); ++p) {
51  double pr = p->second.n_;
52  ret.first += pr * std::log(pr);
53  ret.second += pr;
54  }
55 }
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167
storage::version jubatus::core::stat::stat::get_version ( ) const
inline

Definition at line 77 of file stat.hpp.

77  {
78  return storage::version();
79  }
double jubatus::core::stat::stat::max ( const std::string &  key) const

Definition at line 107 of file stat.cpp.

References JUBATUS_EXCEPTION, jubatus::core::stat::stat::stat_val::max_, and stats_.

107  {
108  jubatus::util::data::unordered_map<std::string, stat_val>::const_iterator p =
109  stats_.find(key);
110  if (p == stats_.end()) {
111  throw JUBATUS_EXCEPTION(stat_error("max: key " + key + " not found"));
112  }
113  const stat_val& st = p->second;
114  return st.max_;
115 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167
double jubatus::core::stat::stat::min ( const std::string &  key) const

Definition at line 117 of file stat.cpp.

References JUBATUS_EXCEPTION, jubatus::core::stat::stat::stat_val::min_, and stats_.

117  {
118  jubatus::util::data::unordered_map<std::string, stat_val>::const_iterator p =
119  stats_.find(key);
120  if (p == stats_.end()) {
121  throw JUBATUS_EXCEPTION(stat_error("min: key " + key + " not found"));
122  }
123  const stat_val& st = p->second;
124  return st.min_;
125 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167
void jubatus::core::stat::stat::mix ( const std::pair< double, size_t > &  lhs,
std::pair< double, size_t > &  ret 
) const
virtual

Definition at line 63 of file stat.cpp.

65  {
66  ret.first += lhs.first;
67  ret.second += lhs.second;
68 }
double jubatus::core::stat::stat::moment ( const std::string &  key,
int  n,
double  c 
) const

Definition at line 147 of file stat.cpp.

References JUBATUS_EXCEPTION, jubatus::core::stat::stat::stat_val::n_, stats_, jubatus::core::stat::stat::stat_val::sum2_, jubatus::core::stat::stat::stat_val::sum_, and window_.

Referenced by stddev().

147  {
148  if (n < 0) {
149  return -1;
150  }
151  jubatus::util::data::unordered_map<std::string, stat_val>::const_iterator p =
152  stats_.find(key);
153  if (p == stats_.end()) {
154  throw JUBATUS_EXCEPTION(stat_error("moment: key " + key + " not found"));
155  }
156  const stat_val& st = p->second;
157 
158  if (n == 0) {
159  return 1;
160  }
161 
162  if (n == 1) {
163  return (st.sum_ - c * st.n_) / st.n_;
164  }
165 
166  if (n == 2) {
167  return (st.sum2_ - 2 * st.sum_ * c) / st.n_ + c * c;
168  }
169 
170  // fallback
171  double ret = 0;
172  for (size_t i = 0; i < window_.size(); ++i) {
173  if (window_[i].second.first != key) {
174  continue;
175  }
176  ret += std::pow(window_[i].second.second - c, n);
177  }
178  return ret / st.n_;
179 }
std::deque< std::pair< uint64_t, std::pair< std::string, double > > > window_
Definition: stat.hpp:166
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167

Here is the caller graph for this function:

jubatus::core::stat::stat::MSGPACK_DEFINE ( window_size_  ,
window_  ,
stats_  ,
e_  ,
n_   
)
void jubatus::core::stat::stat::pack ( framework::packer packer) const
virtual

Definition at line 186 of file stat.cpp.

186  {
187  packer.pack(*this);
188 }
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
void jubatus::core::stat::stat::push ( const std::string &  key,
double  val 
)

Definition at line 70 of file stat.cpp.

References jubatus::core::stat::stat::stat_val::n_, jubatus::core::stat::stat::stat_val::rem(), stats_, window_, and window_size_.

70  {
71  {
72  clock_time ct = get_clock_time();
73  window_.push_back(make_pair((uint64_t) ct, make_pair(key, val)));
74  stats_[key].add(val);
75  }
76  while (window_.size() > window_size_) {
77  string key = window_.front().second.first;
78  double val = window_.front().second.second;
79  stat_val& st = stats_[key];
80  window_.pop_front();
81  st.rem(val, key, *this);
82  if (st.n_ == 0) {
83  stats_.erase(key);
84  }
85  }
86 }
std::deque< std::pair< uint64_t, std::pair< std::string, double > > > window_
Definition: stat.hpp:166
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167

Here is the call graph for this function:

bool jubatus::core::stat::stat::put_diff ( const std::pair< double, size_t > &  e)
virtual

Definition at line 57 of file stat.cpp.

References e_, and n_.

57  {
58  e_ = e.first;
59  n_ = e.second;
60  return true;
61 }
double jubatus::core::stat::stat::stddev ( const std::string &  key) const

Definition at line 97 of file stat.cpp.

References JUBATUS_EXCEPTION, moment(), jubatus::core::stat::stat::stat_val::n_, stats_, and jubatus::core::stat::stat::stat_val::sum_.

97  {
98  jubatus::util::data::unordered_map<std::string, stat_val>::const_iterator p =
99  stats_.find(key);
100  if (p == stats_.end()) {
101  throw JUBATUS_EXCEPTION(stat_error("stddev: key " + key + " not found"));
102  }
103  const stat_val& st = p->second;
104  return std::sqrt(moment(key, 2, st.sum_ / st.n_));
105 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
double moment(const std::string &key, int n, double c) const
Definition: stat.cpp:147
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167

Here is the call graph for this function:

double jubatus::core::stat::stat::sum ( const std::string &  key) const

Definition at line 88 of file stat.cpp.

References JUBATUS_EXCEPTION, and stats_.

88  {
89  jubatus::util::data::unordered_map<std::string, stat_val>::const_iterator p =
90  stats_.find(key);
91  if (p == stats_.end()) {
92  throw JUBATUS_EXCEPTION(stat_error("sum: key " + key + " not found"));
93  }
94  return p->second.sum_;
95 }
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::util::data::unordered_map< std::string, stat_val > stats_
Definition: stat.hpp:167
std::string jubatus::core::stat::stat::type ( ) const

Definition at line 192 of file stat.cpp.

192  {
193  return "stat";
194 }
void jubatus::core::stat::stat::unpack ( msgpack::object  o)
virtual

Definition at line 189 of file stat.cpp.

189  {
190  o.convert(this);
191 }

Member Data Documentation

double jubatus::core::stat::stat::e_
private

Definition at line 172 of file stat.hpp.

Referenced by entropy(), and put_diff().

double jubatus::core::stat::stat::n_
private

Definition at line 173 of file stat.hpp.

Referenced by entropy(), and put_diff().

jubatus::util::data::unordered_map<std::string, stat_val> jubatus::core::stat::stat::stats_
protected

Definition at line 167 of file stat.hpp.

Referenced by clear(), entropy(), get_diff(), max(), min(), moment(), push(), stddev(), and sum().

std::deque<std::pair<uint64_t, std::pair<std::string, double> > > jubatus::core::stat::stat::window_
protected

Definition at line 166 of file stat.hpp.

Referenced by clear(), moment(), and push().

size_t jubatus::core::stat::stat::window_size_
private

Definition at line 170 of file stat.hpp.

Referenced by push().


The documentation for this class was generated from the following files: