1 #ifndef MARGINAL_CELL_H_
2 #define MARGINAL_CELL_H_
17 #include <boost/shared_ptr.hpp>
18 #include <boost/make_shared.hpp>
19 #include <boost/unordered_map.hpp>
31 template<
typename T>
class Prior_cell;
36 template<
typename T,
size_t N>
class Marginal_cell;
51 typedef boost::shared_ptr<Prior_cell<T> >
Self_ptr;
55 typedef boost::unordered_map<Key_type, Self_ptr>
Map;
74 return T::prior_prob(key_.
front(), key_.
back());
89 Cell::Step_sizes::const_reverse_iterator,
95 typename Map::iterator it =
map.find(key);
96 if(it ==
map.end()) result = boost::make_shared<Self>(key);
97 else result = it->second;
106 template<
class T,
size_t N>
118 template<
class T,
size_t N>
126 template<
class T,
size_t N>
127 class Marginal_cell :
public Marginal_cell_base
138 typedef boost::unordered_map<Key_type, Self_ptr>
Map;
186 void increment(
const size_t& table_code);
190 void decrement(
const size_t& table_code);
211 assert(!unused_table_codes_.empty());
219 table_weights.insert(
220 unused_table_codes_.front(),
225 if(result == unused_table_codes_.front())
229 std::pair<size_t, size_t>(unused_table_codes_.front(), 0)
232 unused_table_codes_.pop_front();
233 used_table_codes_.push_back(result);
234 if(unused_table_codes_.empty())
240 unused_table_codes_.push_back(used_table_codes_.size());
253 context_->table_backoff_weight() * margin_->backoff_probability();
263 return count_ / (context_->effective_observations()) +
264 context_->observation_backoff_weight() * margin_->backoff_probability();
271 update_probabilities();
272 return smoothed_probability_;
283 Cell::Step_sizes::const_reverse_iterator first,
285 const size_t& out_size
295 friend std::ostream& operator<< <T,N>(std::ostream& os,
const Map&
map);
299 void update_probabilities();
304 std::list<size_t> used_table_codes_;
305 std::list<size_t> unused_table_codes_;
306 double smoothed_probability_;
311 template<
class T,
size_t N>
312 class Context_cell :
public Context_cell_base
322 typedef boost::shared_ptr<Context_cell<T, N> >
Self_ptr;
325 typedef boost::unordered_map<Key_type, Self_ptr>
Map;
369 friend std::ostream& operator<< <T,N>(
382 template<
class T,
size_t N>
388 std::cerr <<
"Constructed context event at level " << N
389 <<
" with key " << key_ << std::endl;
396 template<
class T,
size_t N>
403 std::cerr <<
"COPYING Context_cell<T,N>" << std::endl;
409 template<
class T,
size_t N>
417 template<
class T,
size_t N>
420 for(
typename Map::iterator it = map.begin();
421 it != map.end(); ++it)
423 it->second->resample_alpha();
470 template<
class T,
size_t N>
477 unused_table_codes_(1,0),
478 smoothed_probability_(1.0)
483 template<
class T,
size_t N>
488 margin_(other.margin_),
489 context_(other.context_)
494 template<
class T,
size_t N>
500 template<
class T,
size_t N>
503 margin_->increment(table_code);
504 if(
count() == 0) context_->increment_diversity();
505 context_->increment();
506 if(per_table_counts_[table_code] == 0) {increment_table_count();}
507 ++per_table_counts_[table_code];
511 std::cerr <<
"Incremented marginal cell"
512 <<
"at level " << N <<
" with key " << key_
520 template<
class T,
size_t N>
523 assert(per_table_counts_[table_code] > 0);
526 --per_table_counts_[table_code];
528 if(per_table_counts_[table_code] == 0)
530 unused_table_codes_.push_front(table_code);
531 used_table_codes_.remove(table_code);
532 decrement_table_count();
533 per_table_counts_.erase(table_code);
535 context_->decrement();
537 if(
count() == 0) context_->decrement_diversity();
538 margin_->decrement(table_code);
543 template<
class T,
size_t N>
546 margin_->increment_table_count();
547 context_->increment_table_count();
553 template<
class T,
size_t N>
557 context_->decrement_table_count();
558 margin_->decrement_table_count();
562 template<
class T,
size_t N>
565 const int f = context_ ->
count();
566 const int u = context_ -> diversity();
567 const double next_prob = margin_ -> smoothed_probability();
570 smoothed_probability_ = next_prob;
572 assert(f > 0 && u > 0);
573 const double bo = T::diversity_weight() * u;
575 smoothed_probability_ = log(bo * exp(next_prob) +
count()) - log(f + bo);
583 template<
typename T,
size_t N>
592 template<
typename T,
size_t N>
595 Cell::Step_sizes::const_reverse_iterator first,
597 const size_t& out_size
601 typename Map::iterator it = map.find(key);
606 result = boost::make_shared<Self>(key);
607 map.insert(
typename Map::value_type(key,result));
608 result->context_ = add_context_event(context_key);
610 Margin_type::add_event(
611 margin_key, first + 1, size - *first, out_size);
620 template<
typename T,
size_t N>
626 typename Context_type::Map::iterator it = Context_type::map.find(key);
627 if(it == Context_type::map.end())
629 result = boost::make_shared<Context_type>(key);
630 Context_type::map.insert(
631 typename Context_type::Map::value_type(key,result)
641 template<
class T,
size_t N>
651 os << (it->first) <<
" : " << (it->second->count()) << std::endl;
668 os <<
"Total : " << (it->second->count()) << std::endl;
675 template<
class T,
size_t N>
685 os << (it->first) <<
" : " << (it->second->count()) << std::endl;
695 template<
class T,
size_t N>
700 template<
class T,
size_t N>
710 template<
typename T,
size_t N>
715 template<
typename T,
size_t N>
Prior_cell< T > Self
Definition: Marginal_cell.h:50
Context_base_ptr context() const
get ptr to conditioning context cell
Definition: Marginal_cell.h:70
Definition: Marginal_cell.h:442
const Key_type & key() const
return data
Definition: Marginal_cell.h:170
Categorical_event< N > Key_type
Definition: Marginal_cell.h:137
Categorical_event< N > get_context(const Categorical_event< N > &event, const size_t steps=1)
returns RHS of conditioning expression
Definition: Categorical_event.h:283
double smoothed_probability()
predictive probability under original Collins algorithm
Definition: Marginal_cell.h:269
Definition of various standard probability distributions.
Definition: Marginal_cell.h:431
static Context_ptr add_context_event(const typename Context_type::Key_type &key)
creates a new context cell and returns a pointer to it
Definition: Marginal_cell.h:622
Definition: Cell_base_classes.h:156
Definition: Cell_traits.h:28
Definition: Cell_base_classes.h:82
Prior_cell(const Key_type &key)
Definition: Marginal_cell.h:58
Marginal_cell_base::Self_ptr Base_ptr
Definition: Marginal_cell.h:135
size_t sample_table_assignment()
sample from the conditional predictive distribution of table assignment
Definition: Marginal_cell.h:66
boost::shared_ptr< Context_cell_base > Context_ptr
Definition: Cell_base_classes.h:86
Traits::Margin_type Margin_type
Definition: Marginal_cell.h:141
std::ostream & operator<<(std::ostream &os, const Categorical_event_base &e)
display key in human readable form
Definition: Categorical_event.cpp:13
Categorical_event< N-1 > smooth(const Categorical_event< N > &event, const size_t steps=1)
marginalizes look-up key by <steps> from back (generalizes context)
Definition: Categorical_event.h:264
Definition: Cell_base_classes.h:24
Base_ptr margin() const
get smoothed marginal cell ptr
Definition: Marginal_cell.h:68
const Key_type & key() const
return variable values in this cell
Definition: Marginal_cell.h:60
boost::shared_ptr< Prior_cell< T > > Self_ptr
Definition: Marginal_cell.h:51
Cell_traits< T, N > Traits
Definition: Marginal_cell.h:323
Context_cell_base::Self_ptr Context_base_ptr
Definition: Marginal_cell.h:53
class containing conditioning context for marginal cells
Definition: Marginal_cell.h:41
Categorical_event< N > Key_type
Definition: Marginal_cell.h:324
Vector sample(const MV_gaussian_distribution &dist)
Sample from a multivariate normal distribution.
Definition: prob_sample.cpp:42
Marginal_cell< T, N > Self
Definition: Marginal_cell.h:132
Marginal_cell(const Key_type &key)
ctor to create a new cell out of a key
Definition: Marginal_cell.h:471
static bool & VERBOSE
Definition: Marginal_cell.h:145
virtual ~Context_cell()
destructor
Definition: Marginal_cell.h:410
double predictive_probability() const
predictive probability of this outcome in this context
Definition: Marginal_cell.h:261
boost::shared_ptr< Context_cell< T, N > > Self_ptr
Definition: Marginal_cell.h:322
double backoff_probability() const
prob of this outcome conditioned on starting a new table
Definition: Marginal_cell.h:250
void increment(const size_t &)
increment the count associated with a particular table
Definition: Marginal_cell.h:62
boost::shared_ptr< Context_type > Context_ptr
Definition: Marginal_cell.h:140
Table_count_map per_table_counts_
Definition: Cell_base_classes.h:151
double predictive_probability() const
predictive probability of this outcome in this context
Definition: Marginal_cell.h:82
Marginal_cell_base::Self_ptr Base_ptr
Definition: Marginal_cell.h:52
concrete class for contingency table cell
Definition: Cell_traits.h:24
boost::shared_ptr< Marginal_cell_base > Self_ptr
Definition: Cell_base_classes.h:85
Context_base_ptr context() const
get conditioning context
Definition: Marginal_cell.h:178
size_t count_
Definition: Cell_base_classes.h:68
const Key_type & key() const
return data
Definition: Marginal_cell.h:357
virtual void increment_table_count()
increment table count
Definition: Cell_base_classes.h:57
static Self_ptr add_event(const Key_type &key, Cell::Step_sizes::const_reverse_iterator, const size_t &, const size_t &)
Definition: Marginal_cell.h:87
virtual void decrement()
decrement count
Definition: Cell_base_classes.h:49
boost::shared_ptr< Self > Self_ptr
Definition: Marginal_cell.h:133
Context_cell< T, N > Context_type
Definition: Marginal_cell.h:139
Context_cell_base::Self_ptr Context_base_ptr
Definition: Marginal_cell.h:136
virtual void increment()
increment count
Definition: Cell_base_classes.h:45
count
Definition: APPgetLargeConnectedEdges.m:71
void decrement(const size_t &)
increment the count associated with a particular table
Definition: Marginal_cell.h:64
boost::shared_ptr< Context_cell_base > Self_ptr
Definition: Cell_base_classes.h:162
virtual void decrement_table_count()
decrement table count
Definition: Cell_base_classes.h:61
Sampling functionality for the different distributions defined in "prob_distributions.h".
size_t sample_table_assignment()
stochastically return a new table assignment for an observation
Definition: Marginal_cell.h:209
Context_cell(const Key_type &key)
Constructs a cell which is purely context information.
Definition: Marginal_cell.h:383
Base_ptr margin() const
get smoothed cell
Definition: Marginal_cell.h:174
void increment_table_count()
increment the number of distinct tables with this value
Definition: Marginal_cell.h:544
double backoff_probability() const
prob of this outcome conditioned on starting a new table
Definition: Marginal_cell.h:72
static bool VERBOSE
Definition: Cell_base_classes.h:29
virtual ~Marginal_cell()
destructor
Definition: Marginal_cell.h:495
const size_t & front() const
get first data entry
Definition: Categorical_event.h:127
Categorical_event< 0 > Key_type
Definition: Marginal_cell.h:54
boost::unordered_map< Key_type, Self_ptr > Map
Definition: Marginal_cell.h:55
boost::unordered_map< Key_type, Self_ptr > Map
Definition: Marginal_cell.h:325
boost::shared_ptr< Cell > Self_ptr
Definition: Cell_base_classes.h:27
static Map map
Definition: Marginal_cell.h:144
static void resample_all_alphas()
resample all CRP alpha values at this level only
Definition: Marginal_cell.h:418
size_t table_count_
Definition: Cell_base_classes.h:69
Definition: Cell_traits.h:18
boost::shared_ptr< Margin_type > Margin_ptr
Definition: Marginal_cell.h:142
boost::bimap< int, std::string > Map
Definition: Event_traits.h:34
static Map map
Definition: Marginal_cell.h:330
void decrement_table_count()
decrement the number of distinct tables with this value
Definition: Marginal_cell.h:554
static bool & VERBOSE
Definition: Marginal_cell.h:331
static void recursively_resample_all_alphas(const Dummy_template_class< 1 > &)
Definition: Marginal_cell.h:455
const size_t & back() const
get last data entry
Definition: Categorical_event.h:131
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
Cell_traits< T, N > Traits
Definition: Marginal_cell.h:134
boost::unordered_map< Key_type, Self_ptr > Map
Definition: Marginal_cell.h:138
double smoothed_probability()
get smoothed probability estimate
Definition: Marginal_cell.h:77
void set_contingency_table_verbosity(bool value)
Definition: Marginal_cell.h:584
static Map map
Definition: Marginal_cell.h:56
static void recursively_resample_all_alphas(const Dummy_template_class< M > &)
Definition: Marginal_cell.h:447
static Self_ptr add_event(const Key_type &key, Cell::Step_sizes::const_reverse_iterator first, const size_t &size, const size_t &out_size)
creates a new instance and sets its margin and context
Definition: Marginal_cell.h:593