KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Event_view.h
Go to the documentation of this file.
1 #ifndef EVENT_VIEW_H_
2 #define EVENT_VIEW_H_
3 
12 #include "l_cpp/l_exception.h"
13 #include <boost/shared_ptr.hpp>
14 #include <boost/make_shared.hpp>
15 #include <boost/tuple/tuple.hpp>
16 #include <boost/array.hpp>
17 
18 
19 namespace semantics
20 {
21 
22 /*------------------------------------------------------------
23  * FORWARD DECLARATIONS
24  *------------------------------------------------------------*/
25 
26  template<typename T> struct View_traits;
27 
31 template<class T, class Traits = View_traits<T> >
32 class Event_view;
33 
34 /*------------------------------------------------------------
35  * CLASS DEFINITIONS
36  *------------------------------------------------------------*/
37 
38 template<class T, class Traits>
39 class Event_view
40 {
41 public:
42 
43 /*------------------------------------------------------------
44  * TYPEDEFS
45  *------------------------------------------------------------*/
46 
48  typedef T Event;
49  typedef Traits Event_traits;
51  typedef typename M_cell::Key_type Key_type;
52  typedef typename Key_type::Data_type Data_type;
53  typedef typename M_cell::Map Map;
54 
55 /*------------------------------------------------------------
56  * PUBLIC STATIC MEMBERS
57  *------------------------------------------------------------*/
58 
59  static Map& map;
60  static const size_t& smoothing_levels;
61  static bool VERBOSE;
62 
63 /*------------------------------------------------------------
64  * CONSTRUCTORS & DESTRUCTOR
65  *------------------------------------------------------------*/
66 
67  Event_view(const bool learn = true) : cell_ptr_(), learn_(learn) {}
68 
71  Event_view(
72  typename Data_type::iterator first,
73  typename Data_type::iterator last,
74  bool learn = true
75  );
76 
79  Event_view(const Self& other)
80  {
81  values_ = other.values_;
82  learn_ = other.learn_;
83  initialize();
84  }
85 
88  virtual ~Event_view() {cleanup();}
89 
90 /*------------------------------------------------------------
91  * OPERATORS
92  *------------------------------------------------------------*/
93 
96  Self& operator=(const Self& source);
97 
98 /*------------------------------------------------------------
99  * CALCULATION
100  *------------------------------------------------------------*/
101 
104  std::vector<int> get_numerators() const;
105 
108  std::vector<int> get_denominators() const;
109 
112  std::vector<int> get_diversities() const;
113 
116  double smoothed_probability(const bool& collins = false) const
117  {
118  if(collins == true)
119  {
120  return cell_ptr_ -> smoothed_probability();
121  } else {
122  return cell_ptr_ -> predictive_probability();
123  }
124  };
125 
128  double predictive_probability() const
129  {
130  return cell_ptr_ -> predictive_probability();
131  };
132 
133 /*------------------------------------------------------------
134  * STATIC FUNCTIONS
135  *------------------------------------------------------------*/
136 
142  static const double& prior_prob(const size_t& val, const size_t& type)
143  {
144  return Traits::prior_prob(val, type);
145  }
146 
150  static const double& diversity_weight(){return Traits::diversity_weight();}
151 
154  void display_conditioning_expression(std::ostream& os) const;
155 
156 /*------------------------------------------------------------
157  * MANIPULATORS
158  *------------------------------------------------------------*/
159 
162  void unlearn() {if(learn_ == true) {cleanup(); learn_ = false;}}
163 
166  void learn() {if(learn_ == false) {learn_ = true; initialize();}}
167 
171  {
172  if(learn_ == true)
173  {
174  cell_ptr_->decrement(table_code_);
175  table_code_ = cell_ptr_->sample_table_assignment();
176  cell_ptr_->increment(table_code_);
177  }
178  }
179 
180 private:
181  Key_type values_;
182  size_t table_code_;
183  typename M_cell::Base_ptr cell_ptr_;
184  bool learn_;
185  void initialize();
186  void cleanup();
187 };
188 
189 
191 
192 template<class T, class Traits>
194  typename Data_type::iterator first,
195  typename Data_type::iterator last,
196  const bool learn
197  )
198  : values_(first, last, Traits::size), table_code_(0), learn_(learn)
199 {
200  initialize();
201 }
202 
203 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
204 
205 template<class T, class Traits>
207 {
208  values_ = source.values_;
209  table_code_ = source.table_code_;
210  learn_ = source.learn_;
211  initialize();
212  return *this;
213 }
214 
215 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
216 
217 template<class T, class Traits>
219 {
220  using namespace boost;
221  std::vector<int> result(smoothing_levels, 0);
222  typename M_cell::Base_ptr p = cell_ptr_;
223  for(size_t j = 0;
224  p != NULL && j < smoothing_levels;
225  j++
226  )
227  {
228  if(p != NULL && p -> context() != NULL)
229  {
230  result[j] = p -> count();
231  if(VERBOSE)
232  {
233  std::cerr << "Level " << smoothing_levels - j << " key:"
234  << p -> key() << std::endl;
235  }
236  p = p -> margin();
237  }
238  }
239  return result;
240 }
241 
242 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
243 
244 template<class T, class Traits>
246 {
247  using namespace boost;
248  std::vector<int> result(smoothing_levels, 0);
249  typename M_cell::Base_ptr p = cell_ptr_;
250  for(size_t j = 0;
251  j < smoothing_levels;
252  j++)
253  {
254  if(p != NULL && p -> context() != NULL)
255  {
256  result[j] = p -> context() -> count();
257  if(VERBOSE)
258  {
259  std::cerr << "Level " << smoothing_levels - j << " key:"
260  << p -> context() -> key() << std::endl;
261  }
262  p = p -> margin();
263  }
264 
265  }
266  return result;
267 }
268 
269 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
270 
271 template<class T, class Traits>
273 {
274  using namespace boost;
275  std::vector<int> result(smoothing_levels, 0);
276  typename M_cell::Base_ptr p = cell_ptr_;
277  for(size_t j = 0;
278  j < smoothing_levels;
279  j++
280  )
281  {
282  if(p != NULL && p -> context() != NULL)
283  {
284  result[j] = p -> context() -> diversity();
285  p = p -> margin();
286  }
287  }
288  return result;
289 }
290 
291 
292 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
293 
294 template<class T, class Traits> inline
296 {
297  cell_ptr_ =
298  M_cell::add_event(
299  values_,
300  Traits::step_sizes.rbegin(),
301  Traits::size,
302  Traits::out_size
303  );
304  assert(cell_ptr_ != NULL);
305  if(learn_ == true)
306  {
307  table_code_ = cell_ptr_->sample_table_assignment();
308  cell_ptr_->increment(table_code_);
309  assert(cell_ptr_->count() > 0);
310  }
311 }
312 
313 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
314 
315 template<class T, class Traits> inline
316 void Event_view<T, Traits>::cleanup()
317 {
318  if(learn_ == true)
319  {
320  assert(cell_ptr_->count() > 0);
321  cell_ptr_->decrement(table_code_);
322  }
323 }
324 
325 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
326 
327 template<class T, class Traits>
329  std::ostream& os
330  ) const
331 {
332  using namespace std;
333  typename Traits::Var_list::const_iterator marker;
334  typename Traits::Var_list::const_iterator given =
335  Traits::variable_names.begin() + Traits::out_size;
336  for(marker = Traits::variable_names.begin(); marker != given; marker++)
337  {
338  os << *marker << " ";
339  }
340  os << "| ";
341  for(typename Traits::Step_sizes::const_iterator it =
342  Traits::step_sizes.begin();
343  it != Traits::step_sizes.end();
344  it++)
345  {
346  int j = *it;
347  while(j > 0)
348  {
349  os << *marker << " ";
350  marker++;
351  j--;
352  }
353  os << ". ";
354  }
355  os << endl;
356 }
357 
358 /*------------------------------------------------------------
359  * STATIC INITIALIZATION
360  *------------------------------------------------------------*/
361 
362 template<class T, class Traits>
363 const size_t& Event_view<T, Traits>::smoothing_levels = Traits::context_levels;
364 
365 template<class T, class Traits>
366 bool Event_view<T,Traits>::VERBOSE = false;
367 
368 template<class T, class Traits>
371  Marginal_cell<Event_view<T,Traits>, Traits::context_levels>::map;
372 
373 };
374 
375 #endif
M_cell::Map Map
Definition: Event_view.h:53
std::vector< Val_type > Data_type
Definition: Categorical_event.h:63
static const double & prior_prob(const size_t &val, const size_t &type)
unconditional prior probability of a particular outcome
Definition: Event_view.h:142
virtual ~Event_view()
Destructor.
Definition: Event_view.h:88
Self & operator=(const Self &source)
Assignment operator.
Definition: Event_view.h:206
Marginal_cell_base::Self_ptr Base_ptr
Definition: Marginal_cell.h:135
Event_view< T, Traits > Self
Definition: Event_view.h:47
void learn()
add counts to database (treat as observed)
Definition: Event_view.h:166
void display_conditioning_expression(std::ostream &os) const
show output and context variables
Definition: Event_view.h:328
static const size_t & smoothing_levels
Definition: Event_view.h:60
static Map & map
Definition: Event_view.h:59
double smoothed_probability(const bool &collins=false) const
get smoothed conditional probability via Collins' method
Definition: Event_view.h:116
static const double & diversity_weight()
hard-coded diversity-weight parameter for this event type
Definition: Event_view.h:150
T Event
Definition: Event_view.h:48
std::vector< int > get_diversities() const
get counts of how many different events occur at this context
Definition: Event_view.h:272
concrete class for contingency table cell
Definition: Cell_traits.h:24
static bool VERBOSE
Definition: Event_view.h:61
count
Definition: APPgetLargeConnectedEdges.m:71
Marginal_cell< Self, Traits::context_levels > M_cell
Definition: Event_view.h:50
Template class to represent different "views" of syntactic events.
Definition: Event_view.h:32
void unlearn()
remove counts from database (treat as unobserved)
Definition: Event_view.h:162
Key_type::Data_type Data_type
Definition: Event_view.h:52
Traits Event_traits
Definition: Event_view.h:49
look-up key class for contingency table
Definition: Categorical_event.h:29
double predictive_probability() const
get predictive prob according to HCRP model
Definition: Event_view.h:128
Event_view(const Self &other)
Copy constructor.
Definition: Event_view.h:79
M_cell::Key_type Key_type
Definition: Event_view.h:51
void resample_table_assignment()
resample the CRP table assignment for this event
Definition: Event_view.h:170
std::vector< int > get_numerators() const
get counts of event at various smoothing levels
Definition: Event_view.h:218
Support for error handling exception classes in libKJB.
std::vector< int > get_denominators() const
get counts of context at various smoothing levels
Definition: Event_view.h:245
boost::unordered_map< Key_type, Self_ptr > Map
Definition: Marginal_cell.h:138
Event_view(const bool learn=true)
Definition: Event_view.h:67