13 using namespace Eigen;
22 hist_buffer_(linear_size + circular_size),
23 linear_size_(linear_size),
24 circular_size_(circular_size),
25 state_size_(linear_size + circular_size)
30 extraction_method_(estimate_extraction.extraction_method_),
31 hist_buffer_(std::move(estimate_extraction.hist_buffer_)),
32 sm_weights_(std::move(estimate_extraction.sm_weights_)),
33 wm_weights_(std::move(estimate_extraction.wm_weights_)),
34 em_weights_(std::move(estimate_extraction.em_weights_)),
35 linear_size_(estimate_extraction.linear_size_),
36 circular_size_(estimate_extraction.circular_size_),
37 state_size_(estimate_extraction.state_size_)
39 estimate_extraction.extraction_method_ = ExtractionMethod::emode;
45 if (
this == &estimate_extraction)
49 estimate_extraction.extraction_method_ = ExtractionMethod::emode;
51 hist_buffer_ = std::move(estimate_extraction.hist_buffer_);
53 sm_weights_ = std::move(estimate_extraction.sm_weights_);
54 wm_weights_ = std::move(estimate_extraction.wm_weights_);
55 em_weights_ = std::move(estimate_extraction.em_weights_);
57 linear_size_ = estimate_extraction.linear_size_;
58 circular_size_ = estimate_extraction.circular_size_;
59 state_size_ = estimate_extraction.state_size_;
67 extraction_method_ = extraction_method;
76 return hist_buffer_.setHistorySize(window);
84 VectorXd out_particle(state_size_);
86 bool estimate_available =
true;
88 switch (extraction_method_)
90 case ExtractionMethod::mean :
91 out_particle = mean(particles, weights);
94 case ExtractionMethod::smean :
95 out_particle = simpleAverage(particles, weights, VectorXd(0), VectorXd(0), MatrixXd(0, 0), Statistics::mean);
98 case ExtractionMethod::wmean :
99 out_particle = weightedAverage(particles, weights, VectorXd(0), VectorXd(0), MatrixXd(0, 0), Statistics::mean);
102 case ExtractionMethod::emean :
103 out_particle = exponentialAverage(particles, weights, VectorXd(0), VectorXd(0), MatrixXd(0, 0), Statistics::mean);
106 case ExtractionMethod::mode :
107 out_particle = mode(particles, weights);
110 case ExtractionMethod::smode :
111 out_particle = simpleAverage(particles, weights, VectorXd(0), VectorXd(0), MatrixXd(0, 0), Statistics::mode);
114 case ExtractionMethod::wmode :
115 out_particle = weightedAverage(particles, weights, VectorXd(0), VectorXd(0), MatrixXd(0, 0), Statistics::mode);
118 case ExtractionMethod::emode :
119 out_particle = exponentialAverage(particles, weights, VectorXd(0), VectorXd(0), MatrixXd(0, 0), Statistics::mode);
122 case ExtractionMethod::map :
123 case ExtractionMethod::smap :
124 case ExtractionMethod::wmap :
125 case ExtractionMethod::emap :
129 estimate_available =
false;
133 return std::make_pair(estimate_available, out_particle);
139 const Ref<const MatrixXd>& particles,
140 const Ref<const VectorXd>& weights,
141 const Ref<const VectorXd>& previous_weights,
142 const Ref<const VectorXd>& likelihoods,
143 const Ref<const MatrixXd>& transition_probabilities
146 VectorXd out_particle(state_size_);
148 switch (extraction_method_)
150 case ExtractionMethod::mean :
151 case ExtractionMethod::smean :
152 case ExtractionMethod::wmean :
153 case ExtractionMethod::emean :
154 case ExtractionMethod::mode :
155 case ExtractionMethod::smode :
156 case ExtractionMethod::wmode :
157 case ExtractionMethod::emode :
158 return extract(particles, weights);
160 case ExtractionMethod::map :
161 out_particle = map(particles, previous_weights, likelihoods, transition_probabilities);
164 case ExtractionMethod::smap :
165 out_particle = simpleAverage(particles, weights, previous_weights, likelihoods, transition_probabilities, Statistics::map);
168 case ExtractionMethod::wmap :
169 out_particle = weightedAverage(particles, weights, previous_weights, likelihoods, transition_probabilities, Statistics::map);
172 case ExtractionMethod::emap :
173 out_particle = exponentialAverage(particles, weights, previous_weights, likelihoods, transition_probabilities, Statistics::map);
177 return std::make_pair(
true, out_particle);
183 return hist_buffer_.clear();
189 std::vector<std::string> info;
191 info.push_back(
"<| Current window size: " + std::to_string(hist_buffer_.getHistorySize()) +
" |>");
192 info.push_back(
"<| Available estimate extraction methods: " +
193 std::string(extraction_method_ == ExtractionMethod::mean ?
"1) mean <-- In use; " :
"1) mean; " ) +
194 std::string(extraction_method_ == ExtractionMethod::smean ?
"2) smean <-- In use; " :
"2) smean; ") +
195 std::string(extraction_method_ == ExtractionMethod::wmean ?
"3) wmean <-- In use; " :
"3) wmean; ") +
196 std::string(extraction_method_ == ExtractionMethod::emean ?
"4) emean <-- In use; " :
"4) emean; ") +
197 std::string(extraction_method_ == ExtractionMethod::mode ?
"5) mode <-- In use; " :
"5) mode; " ) +
198 std::string(extraction_method_ == ExtractionMethod::smode ?
"6) smode <-- In use; " :
"6) smode; ") +
199 std::string(extraction_method_ == ExtractionMethod::wmode ?
"7) wmode <-- In use; " :
"7) wmode; ") +
200 std::string(extraction_method_ == ExtractionMethod::emode ?
"8) emode <-- In use; " :
"8) emode" ) +
201 std::string(extraction_method_ == ExtractionMethod::map ?
"9) map <-- In use; " :
"9) map; ") +
202 std::string(extraction_method_ == ExtractionMethod::smap ?
"10) smap <-- In use; " :
"10) smap; ") +
203 std::string(extraction_method_ == ExtractionMethod::wmap ?
"11) wmap <-- In use; " :
"11) wmap; ") +
204 std::string(extraction_method_ == ExtractionMethod::emap ?
"12) emap <-- In use; " :
"12) emap; ") +
" |>");
212 VectorXd out_particle(state_size_);
214 if (linear_size_ > 0)
215 out_particle.head(linear_size_) = particles.topRows(linear_size_) * weights.array().exp().matrix();
217 if (circular_size_ > 0)
226 MatrixXd::Index maxRow;
227 MatrixXd::Index maxCol;
228 weights.maxCoeff(&maxRow, &maxCol);
230 return particles.col(maxRow);
236 const Ref<const MatrixXd>& particles,
237 const Ref<const VectorXd>& previous_weights,
238 const Ref<const VectorXd>& likelihoods,
239 const Ref<const MatrixXd>& transition_probabilities
251 ArrayXd::Index map_index;
253 VectorXd values(particles.cols());
255 double eps = std::numeric_limits<double>::min();
256 for (std::size_t i = 0; i < values.size(); i++)
257 values(i) = std::log(likelihoods(i) + eps) +
utils::log_sum_exp(((transition_probabilities.row(i).transpose().array() + eps).log() + previous_weights.array()).matrix());
259 values.maxCoeff(&map_index);
261 return particles.col(map_index);
267 const Ref<const MatrixXd>& particles,
268 const Ref<const VectorXd>& weights,
269 const Ref<const VectorXd>& previous_weights,
270 const Ref<const VectorXd>& likelihoods,
271 const Ref<const MatrixXd>& transition_probabilities,
275 VectorXd cur_estimates;
276 if (base_est_ext == Statistics::mean)
277 cur_estimates = mean(particles, weights);
278 else if (base_est_ext == Statistics::mode)
279 cur_estimates = mode(particles, weights);
280 else if (base_est_ext == Statistics::map)
281 cur_estimates = map(particles, previous_weights, likelihoods, transition_probabilities);
284 hist_buffer_.addElement(cur_estimates);
286 MatrixXd history = hist_buffer_.getHistoryBuffer();
287 if (sm_weights_.size() != history.cols())
288 sm_weights_ = VectorXd::Constant(history.cols(), -std::log(history.cols()));
291 return mean(history, sm_weights_);
297 const Ref<const MatrixXd>& particles,
298 const Ref<const VectorXd>& weights,
299 const Ref<const VectorXd>& previous_weights,
300 const Ref<const VectorXd>& likelihoods,
301 const Ref<const MatrixXd>& transition_probabilities,
305 VectorXd cur_estimates;
306 if (base_est_ext == Statistics::mean)
307 cur_estimates = mean(particles, weights);
308 else if (base_est_ext == Statistics::mode)
309 cur_estimates = mode(particles, weights);
310 else if (base_est_ext == Statistics::map)
311 cur_estimates = map(particles, previous_weights, likelihoods, transition_probabilities);
314 hist_buffer_.addElement(cur_estimates);
316 MatrixXd history = hist_buffer_.getHistoryBuffer();
317 if (wm_weights_.size() != history.cols())
319 wm_weights_.resize(history.cols());
320 for (
unsigned int i = 0; i < history.cols(); ++i)
321 wm_weights_(i) = std::log(history.cols() - i);
327 return mean(history, wm_weights_);
333 const Ref<const MatrixXd>& particles,
334 const Ref<const VectorXd>& weights,
335 const Ref<const VectorXd>& previous_weights,
336 const Ref<const VectorXd>& likelihoods,
337 const Ref<const MatrixXd>& transition_probabilities,
341 VectorXd cur_estimates;
342 if (base_est_ext == Statistics::mean)
343 cur_estimates = mean(particles, weights);
344 else if (base_est_ext == Statistics::mode)
345 cur_estimates = mode(particles, weights);
346 else if (base_est_ext == Statistics::map)
347 cur_estimates = map(particles, previous_weights, likelihoods, transition_probabilities);
350 hist_buffer_.addElement(cur_estimates);
352 MatrixXd history = hist_buffer_.getHistoryBuffer();
353 if (em_weights_.size() != history.cols())
355 em_weights_.resize(history.cols());
356 for (
unsigned int i = 0; i < history.cols(); ++i)
357 em_weights_(i) = -(
static_cast<double>(i) / history.cols());
362 return mean(history, em_weights_);