32 #ifndef SHARK_STATISTICS_MULTINOMIALDISTRIBUTION_H 33 #define SHARK_STATISTICS_MULTINOMIALDISTRIBUTION_H 54 typedef std::size_t result_type;
61 : m_probabilities(probabilities){
68 template<
typename Archive>
69 void serialize( Archive & ar,
const unsigned int version ) {
70 ar & BOOST_SERIALIZATION_NVP( m_probabilities );
71 ar & BOOST_SERIALIZATION_NVP( m_q );
72 ar & BOOST_SERIALIZATION_NVP( m_J );
77 return m_probabilities;
85 return m_probabilities;
89 template<
class randomType>
91 std::size_t numStates = m_probabilities.size();
103 std::size_t numStates = m_probabilities.size();
104 m_q.resize(numStates);
105 m_J.resize(numStates);
106 m_probabilities/=sum(m_probabilities);
110 std::deque<std::size_t> smaller;
111 std::deque<std::size_t> larger;
112 for(std::size_t i = 0;i != numStates; ++i){
113 m_q(i) = numStates*m_probabilities(i);
115 smaller.push_back(i);
122 while(!smaller.empty() && !larger.empty()){
123 std::size_t smallIndex = smaller.front();
124 std::size_t largeIndex = larger.front();
128 m_J[smallIndex] = largeIndex;
129 m_q[largeIndex] -= 1.0 - m_q[smallIndex];
131 if(m_q[largeIndex] < 1.0)
132 smaller.push_back(largeIndex);
134 larger.push_back(largeIndex);
136 for(std::size_t i = 0; i != larger.size(); ++i){
137 m_q[larger[i]]=std::min(m_q[larger[i]],1.0);
142 RealVector m_probabilities;
144 blas::vector<std::size_t> m_J;