FIMS  v0.9.3
Loading...
Searching...
No Matches
rcpp_distribution.hpp
Go to the documentation of this file.
1
9#ifndef FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_DISTRIBUTION_HPP
10#define FIMS_INTERFACE_RCPP_RCPP_OBJECTS_RCPP_DISTRIBUTION_HPP
11
13#include "../../../distributions/distributions.hpp"
14
20 public:
24 static uint32_t id_g;
32 std::shared_ptr<std::vector<uint32_t>> key_m;
60 SharedString use_mean_m = fims::to_string("no");
66 static std::map<uint32_t, std::shared_ptr<DistributionsInterfaceBase>>
72
76 double lpdf_value = 0;
81 this->key_m = std::make_shared<std::vector<uint32_t>>();
83 /* Create instance of map: key is id and value is pointer to
84 DistributionsInterfaceBase */
85 // DistributionsInterfaceBase::live_objects[this->id_m] = this;
86 }
87
99
104
108 virtual uint32_t get_id() = 0;
109
118 virtual bool set_distribution_links(std::string input_type,
119 Rcpp::IntegerVector ids) {
120 return false;
121 }
122
146 virtual bool set_distribution_mean(double input_value) { return false; }
147
154 virtual bool set_observed_data(int observed_data_id) { return false; }
155
160 virtual double evaluate() = 0;
161};
162
168 public:
203
216
221
226 virtual uint32_t get_id() { return this->id_m; }
227
233 this->interface_observed_data_id_m.set(observed_data_id);
234 return true;
235 }
236
240 virtual bool set_distribution_mean(double input_value) {
241 this->expected_mean[0].initial_value_m = input_value;
242 this->expected_mean[0].estimation_type_m.set("fixed_effects");
243 this->use_mean_m.set(fims::to_string("yes"));
244 return true;
245 }
246
250 virtual bool set_distribution_links(std::string input_type,
251 Rcpp::IntegerVector ids) {
252 this->input_type_m.set(input_type);
253 this->key_m->resize(ids.size());
254 for (R_xlen_t i = 0; i < ids.size(); i++) {
255 this->key_m->at(i) = ids[i];
256 }
257 return true;
258 }
259
266 virtual double evaluate() {
268 dnorm.observed_values.resize(this->observed_values.size());
269 dnorm.expected_values.resize(this->expected_values.size());
270 dnorm.log_sd.resize(this->log_sd.size());
271 dnorm.expected_mean.resize(this->expected_mean.size());
272 for (size_t i = 0; i < this->observed_values.size(); i++) {
273 dnorm.observed_values[i] = this->observed_values[i].initial_value_m;
274 }
275 for (size_t i = 0; i < this->expected_values.size(); i++) {
276 dnorm.expected_values[i] = this->expected_values[i].initial_value_m;
277 }
278 for (size_t i = 0; i < this->log_sd.size(); i++) {
279 dnorm.log_sd[i] = this->log_sd[i].initial_value_m;
280 }
281 for (size_t i = 0; i < this->expected_mean.size(); i++) {
282 dnorm.expected_mean[i] = this->expected_mean[i].initial_value_m;
283 }
284 dnorm.use_mean = this->use_mean_m;
285 return dnorm.evaluate();
286 }
287
292 virtual void finalize() {
293 if (this->finalized) {
294 // log warning that finalize has been called more than once.
295 FIMS_WARNING_LOG("DnormDistribution " + fims::to_string(this->id_m) +
296 " has been finalized already.");
297 }
298
299 this->finalized = true; // indicate this has been called already
300
301 std::shared_ptr<fims_info::Information<double>> info =
303
305
306 // search for density component in Information
307 it = info->density_components.find(this->id_m);
308 // if not found, just return
309 if (it == info->density_components.end()) {
310 FIMS_WARNING_LOG("DnormDistribution " + fims::to_string(this->id_m) +
311 " not found in Information.");
312 return;
313 } else {
314 std::shared_ptr<fims_distributions::NormalLPDF<double>> dnorm =
315 std::dynamic_pointer_cast<fims_distributions::NormalLPDF<double>>(
316 it->second);
317
318 this->lpdf_value = dnorm->lpdf;
319
320 size_t n_x = dnorm->get_n_x();
321
322 // If input log_sd is a scalar, resize to n_x and fill with the scalar
323 // value
324 if (this->log_sd.size() != n_x) {
325 // If log_sd size == 1 (scalar), repeat the entry
326 if (this->log_sd.size() == 1) {
327 auto tmp = this->log_sd[0]; // copy the one log_sd param
328 this->log_sd.resize(n_x);
329 for (size_t i = 0; i < n_x; ++i) {
330 this->log_sd[i] = tmp; // copies all fields in Param
331 }
332 } else {
333 // Handle error
335 "log_sd size does not match number of observations and is not "
336 "scalar.");
337 }
338 }
339 for (size_t i = 0; i < n_x; i++) {
340 if (this->log_sd[i].estimation_type_m.get() == "constant") {
341 this->log_sd[i].final_value_m = this->log_sd[i].initial_value_m;
342 } else {
343 this->log_sd[i].final_value_m = dnorm->log_sd.get_force_scalar(i);
344 }
345 }
346
347 for (size_t i = 0; i < this->expected_mean.size(); i++) {
348 if (this->expected_mean[i].estimation_type_m.get() == "constant") {
349 this->expected_mean[i].final_value_m =
350 this->expected_mean[i].initial_value_m;
351 } else {
352 this->expected_mean[i].final_value_m = dnorm->expected_mean[i];
353 }
354 }
355
356 this->lpdf_vec = RealVector(n_x);
357 if (this->expected_values.size() == 1) {
358 this->expected_values.resize(n_x);
359 }
360 if (this->observed_values.size() == 1) {
361 this->observed_values.resize(n_x);
362 }
363
364 for (size_t i = 0; i < this->lpdf_vec.size(); i++) {
365 this->lpdf_vec[i] = dnorm->lpdf_vec[i];
366 this->expected_values[i].final_value_m = dnorm->get_expected(i);
367 this->observed_values[i].final_value_m = dnorm->get_observed(i);
368 }
369 }
370 }
371
379 virtual std::string to_json() {
380 std::stringstream ss;
381
382 ss << "{\n";
383 ss << " \"module_name\": \"density\",\n";
384 ss << " \"module_id\": " << this->id_m << ",\n";
385 ss << " \"module_type\": \"normal\",\n";
386 ss << " \"observed_data_id\" : " << this->interface_observed_data_id_m
387 << ",\n";
388 ss << " \"input_type\" : \"" << this->input_type_m << "\",\n";
389 ss << " \"density_component\": {\n";
390 ss << " \"lpdf_value\": " << sanitize_val(this->lpdf_value) << ",\n";
391 ss << " \"value\":[";
392 if (this->lpdf_vec.size() == 0) {
393 ss << "],\n";
394 } else {
395 for (size_t i = 0; i < this->lpdf_vec.size() - 1; i++) {
396 ss << this->value_to_string(this->lpdf_vec[i]);
397 ss << ", ";
398 }
399 ss << this->value_to_string(this->lpdf_vec[this->lpdf_vec.size() - 1]);
400
401 ss << "],\n";
402 }
403 ss << " \"expected_values\":[";
404 if (this->expected_values.size() == 0) {
405 ss << "],\n";
406 } else {
407 for (size_t i = 0; i < this->expected_values.size() - 1; i++) {
408 ss << this->value_to_string(this->expected_values[i].final_value_m)
409 << ", ";
410 }
411 ss << this->value_to_string(
412 this->expected_values[this->expected_values.size() - 1]
413 .final_value_m);
414 ss << "],\n";
415 }
416 ss << " \"log_sd_values\":[";
417 if (this->log_sd.size() == 0) {
418 ss << "],\n";
419 } else {
420 for (R_xlen_t i = 0; i < static_cast<R_xlen_t>(this->log_sd.size()) - 1;
421 i++) {
422 ss << this->value_to_string(this->log_sd[i].final_value_m) << ", ";
423 }
424 ss << this->value_to_string(
425 this->log_sd[this->log_sd.size() - 1].final_value_m)
426 << "],\n";
427 }
428 ss << " \"observed_values\":[";
429 if (this->observed_values.size() == 0) {
430 ss << "]\n";
431 } else {
432 for (size_t i = 0; i < this->observed_values.size() - 1; i++) {
433 ss << this->observed_values[i].final_value_m << ", ";
434 }
435 ss << this->observed_values[this->observed_values.size() - 1]
436 .final_value_m
437 << "]\n";
438 }
439 ss << " }}\n";
440 return ss.str();
441 }
442
443#ifdef TMB_MODEL
444
445 template <typename Type>
447 std::shared_ptr<fims_info::Information<Type>> info =
449
450 std::shared_ptr<fims_distributions::NormalLPDF<Type>> distribution =
451 std::make_shared<fims_distributions::NormalLPDF<Type>>();
452
453 // interface to data/parameter value
454
455 distribution->observed_data_id_m = interface_observed_data_id_m;
456 std::stringstream ss;
457 distribution->input_type = this->input_type_m;
458 distribution->key.resize(this->key_m->size());
459 for (size_t i = 0; i < this->key_m->size(); i++) {
460 distribution->key[i] = this->key_m->at(i);
461 }
462 distribution->id = this->id_m;
463 distribution->observed_values.resize(this->observed_values.size());
464 for (size_t i = 0; i < this->observed_values.size(); i++) {
465 distribution->observed_values[i] =
466 this->observed_values[i].initial_value_m;
467 }
468 // set relative info
469 distribution->expected_values.resize(this->expected_values.size());
470 for (size_t i = 0; i < this->expected_values.size(); i++) {
471 distribution->expected_values[i] =
472 this->expected_values[i].initial_value_m;
473 }
474 distribution->log_sd.resize(this->log_sd.size());
475 for (size_t i = 0; i < this->log_sd.size(); i++) {
476 distribution->log_sd[i] = this->log_sd[i].initial_value_m;
477 if (this->log_sd[i].estimation_type_m.get() == "fixed_effects") {
478 ss.str("");
479 ss << "dnorm." << this->id_m << ".log_sd." << this->log_sd[i].id_m;
480 info->RegisterParameterName(ss.str());
481 info->RegisterParameter(distribution->log_sd[i]);
482 }
483 if (this->log_sd[i].estimation_type_m.get() == "random_effects") {
484 FIMS_ERROR_LOG("standard deviations cannot be set to random effects");
485 }
486 }
487 info->variable_map[this->log_sd.id_m] = &(distribution)->log_sd;
488
489 distribution->use_mean = this->use_mean_m.get();
490 distribution->expected_mean.resize(this->expected_mean.size());
491 for (size_t i = 0; i < this->expected_mean.size(); i++) {
492 distribution->expected_mean[i] = this->expected_mean[i].initial_value_m;
493 if (this->expected_mean[i].estimation_type_m.get() == "fixed_effects") {
494 ss.str("");
495 ss << "dnorm." << this->id_m << ".expected_mean."
496 << this->expected_mean[i].id_m;
497 info->RegisterParameterName(ss.str());
498 info->RegisterParameter(distribution->expected_mean[i]);
499 }
500 if (this->expected_mean[i].estimation_type_m.get() == "random_effects") {
501 FIMS_ERROR_LOG("expected_mean cannot be set to random effects");
502 }
503 }
504 info->variable_map[this->expected_mean.id_m] =
506
507 info->density_components[distribution->id] = distribution;
508
509 return true;
510 }
511
516 virtual bool add_to_fims_tmb() {
519
520 return true;
521 }
522
523#endif
524};
525
531 public:
564
576
581
586 virtual uint32_t get_id() { return this->id_m; }
587
593 this->interface_observed_data_id_m.set(observed_data_id);
594 return true;
595 }
596
605 virtual bool set_distribution_links(std::string input_type,
606 Rcpp::IntegerVector ids) {
607 this->input_type_m.set(input_type);
608 this->key_m->resize(ids.size());
609 for (R_xlen_t i = 0; i < ids.size(); i++) {
610 this->key_m->at(i) = ids[i];
611 }
612 return true;
613 }
614
621 virtual double evaluate() {
623 dlnorm.observed_values.resize(this->observed_values.size());
624 dlnorm.expected_values.resize(this->expected_values.size());
625 dlnorm.log_sd.resize(this->log_sd.size());
626 // dlnorm.input_type = "prior";
627 for (size_t i = 0; i < this->observed_values.size(); i++) {
628 dlnorm.observed_values[i] = this->observed_values[i].initial_value_m;
629 }
630 for (size_t i = 0; i < this->expected_values.size(); i++) {
631 dlnorm.expected_values[i] = this->expected_values[i].initial_value_m;
632 }
633 for (size_t i = 0; i < this->log_sd.size(); i++) {
634 dlnorm.log_sd[i] = this->log_sd[i].initial_value_m;
635 }
636 return dlnorm.evaluate();
637 }
638
643 virtual void finalize() {
644 if (this->finalized) {
645 // log warning that finalize has been called more than once.
646 FIMS_WARNING_LOG("LogNormalLPDF " + fims::to_string(this->id_m) +
647 " has been finalized already.");
648 }
649
650 this->finalized = true; // indicate this has been called already
651
652 std::shared_ptr<fims_info::Information<double>> info =
654
656
657 // search for density component in Information
658 it = info->density_components.find(this->id_m);
659 // if not found, just return
660 if (it == info->density_components.end()) {
661 FIMS_WARNING_LOG("LogNormalLPDF " + fims::to_string(this->id_m) +
662 " not found in Information.");
663 return;
664 } else {
665 std::shared_ptr<fims_distributions::LogNormalLPDF<double>> dlnorm =
666 std::dynamic_pointer_cast<fims_distributions::LogNormalLPDF<double>>(
667 it->second);
668
669 this->lpdf_value = dlnorm->lpdf;
670
671 size_t n_x = dlnorm->get_n_x();
672
673 if (this->log_sd.size() != n_x) {
674 // If log_sd size == 1 (scalar), repeat the entry
675 if (this->log_sd.size() == 1) {
676 auto tmp = this->log_sd[0]; // copy the one log_sd param
677 this->log_sd.resize(n_x);
678 for (size_t i = 0; i < n_x; ++i) {
679 this->log_sd[i] = tmp; // copies all fields in Param
680 }
681 } else {
682 // Handle error
684 "log_sd size does not match number of observations and is not "
685 "scalar.");
686 }
687 }
688
689 for (size_t i = 0; i < n_x; i++) {
690 if (this->log_sd[i].estimation_type_m.get() == "constant") {
691 this->log_sd[i].final_value_m = this->log_sd[i].initial_value_m;
692 } else {
693 this->log_sd[i].final_value_m = dlnorm->log_sd.get_force_scalar(i);
694 }
695 }
696
697 this->lpdf_vec = RealVector(n_x);
698 if (this->expected_values.size() == 1) {
699 this->expected_values.resize(n_x);
700 }
701 if (this->observed_values.size() == 1) {
702 this->observed_values.resize(n_x);
703 }
704 for (size_t i = 0; i < this->lpdf_vec.size(); i++) {
705 this->lpdf_vec[i] = dlnorm->lpdf_vec[i];
706 this->expected_values[i].final_value_m = dlnorm->get_expected(i);
707 this->observed_values[i].final_value_m = dlnorm->get_observed(i);
708 }
709 }
710 }
711
719 virtual std::string to_json() {
720 std::stringstream ss;
721
722 ss << "{\n";
723 ss << " \"module_name\": \"density\",\n";
724 ss << " \"module_id\": " << this->id_m << ",\n";
725 ss << " \"module_type\": \"log_normal\",\n";
726 ss << " \"observed_data_id\" : " << this->interface_observed_data_id_m
727 << ",\n";
728 ss << " \"input_type\" : \"" << this->input_type_m << "\",\n";
729 ss << " \"density_component\": {\n";
730 ss << " \"lpdf_value\": " << sanitize_val(this->lpdf_value) << ",\n";
731 ss << " \"value\":[";
732 if (this->lpdf_vec.size() == 0) {
733 ss << "],\n";
734 } else {
735 for (size_t i = 0; i < this->lpdf_vec.size() - 1; i++) {
736 ss << this->value_to_string(this->lpdf_vec[i]) << ", ";
737 }
738 ss << this->value_to_string(this->lpdf_vec[this->lpdf_vec.size() - 1]);
739
740 ss << "],\n";
741 }
742 ss << " \"expected_values\":[";
743 if (this->expected_values.size() == 0) {
744 ss << "],\n";
745 } else {
746 for (size_t i = 0; i < this->expected_values.size() - 1; i++) {
747 ss << this->value_to_string(this->expected_values[i].final_value_m)
748 << ", ";
749 }
750 ss << this->value_to_string(
751 this->expected_values[this->expected_values.size() - 1]
752 .final_value_m);
753
754 ss << "],\n";
755 }
756 ss << " \"log_sd_values\":[";
757 if (this->log_sd.size() == 0) {
758 ss << "],\n";
759 } else {
760 for (R_xlen_t i = 0; i < static_cast<R_xlen_t>(this->log_sd.size()) - 1;
761 i++) {
762 ss << this->value_to_string(this->log_sd[i].final_value_m) << ", ";
763 }
764 ss << this->value_to_string(
765 this->log_sd[this->log_sd.size() - 1].final_value_m)
766 << "],\n";
767 }
768 ss << " \"observed_values\":[";
769 if (this->observed_values.size() == 0) {
770 ss << "]\n";
771 } else {
772 for (size_t i = 0; i < this->observed_values.size() - 1; i++) {
773 ss << this->observed_values[i].final_value_m << ", ";
774 }
775 ss << this->observed_values[this->observed_values.size() - 1]
776 .final_value_m
777 << "]\n";
778 }
779 ss << " }}\n";
780 return ss.str();
781 }
782
783#ifdef TMB_MODEL
784
785 template <typename Type>
787 std::shared_ptr<fims_info::Information<Type>> info =
789
790 std::shared_ptr<fims_distributions::LogNormalLPDF<Type>> distribution =
791 std::make_shared<fims_distributions::LogNormalLPDF<Type>>();
792
793 // set relative info
794 distribution->id = this->id_m;
795 std::stringstream ss;
796 distribution->observed_data_id_m = interface_observed_data_id_m;
797 distribution->input_type = this->input_type_m;
798 distribution->key.resize(this->key_m->size());
799 for (size_t i = 0; i < this->key_m->size(); i++) {
800 distribution->key[i] = this->key_m->at(i);
801 }
802 distribution->observed_values.resize(this->observed_values.size());
803 for (size_t i = 0; i < this->observed_values.size(); i++) {
804 distribution->observed_values[i] =
805 this->observed_values[i].initial_value_m;
806 }
807 // set relative info
808 distribution->expected_values.resize(this->expected_values.size());
809 for (size_t i = 0; i < this->expected_values.size(); i++) {
810 distribution->expected_values[i] =
811 this->expected_values[i].initial_value_m;
812 }
813 distribution->log_sd.resize(this->log_sd.size());
814 for (size_t i = 0; i < this->log_sd.size(); i++) {
815 distribution->log_sd[i] = this->log_sd[i].initial_value_m;
816 if (this->log_sd[i].estimation_type_m.get() == "fixed_effects") {
817 ss.str("");
818 ss << "dlnorm." << this->id_m << ".log_sd." << this->log_sd[i].id_m;
819 info->RegisterParameterName(ss.str());
820 info->RegisterParameter(distribution->log_sd[i]);
821 }
822 if (this->log_sd[i].estimation_type_m.get() == "random_effects") {
823 FIMS_ERROR_LOG("standard deviations cannot be set to random effects");
824 }
825 }
826 info->variable_map[this->log_sd.id_m] = &(distribution)->log_sd;
827
828 info->density_components[distribution->id] = distribution;
829
830 return true;
831 }
832
837 virtual bool add_to_fims_tmb() {
840
841 return true;
842 }
843
844#endif
845};
846
852 public:
879
889
902
911 virtual uint32_t get_id() { return this->id_m; }
912
918 this->interface_observed_data_id_m.set(observed_data_id);
919 return true;
920 }
921
930 virtual bool set_distribution_links(std::string input_type,
931 Rcpp::IntegerVector ids) {
932 this->input_type_m.set(input_type);
933 this->key_m->resize(ids.size());
934 for (R_xlen_t i = 0; i < ids.size(); i++) {
935 this->key_m->at(i) = ids[i];
936 }
937 return true;
938 }
939
945 void set_note(std::string note) { this->notes.set(note); }
946
952 virtual double evaluate() {
954 // Declare TMBVector in this scope
955 dmultinom.observed_values.resize(this->observed_values.size());
956 dmultinom.expected_values.resize(this->expected_values.size());
957 for (size_t i = 0; i < observed_values.size(); i++) {
958 dmultinom.observed_values[i] = this->observed_values[i].initial_value_m;
959 }
960 for (size_t i = 0; i < expected_values.size(); i++) {
961 dmultinom.expected_values[i] = this->expected_values[i].initial_value_m;
962 }
963 dmultinom.dims.resize(2);
964 dmultinom.dims[0] = this->dims[0];
965 dmultinom.dims[1] = this->dims[1];
966 return dmultinom.evaluate();
967 }
968
969 void finalize() {
970 if (this->finalized) {
971 // log warning that finalize has been called more than once.
972 FIMS_WARNING_LOG("DmultinomDistributions " +
973 fims::to_string(this->id_m) +
974 " has been finalized already.");
975 }
976
977 this->finalized = true; // indicate this has been called already
978
979 std::shared_ptr<fims_info::Information<double>> info =
981
983
984 // search for density component in Information
985 it = info->density_components.find(this->id_m);
986 // if not found, just return
987 if (it == info->density_components.end()) {
988 FIMS_WARNING_LOG("DmultinomDistributions " + fims::to_string(this->id_m) +
989 " not found in Information.");
990 return;
991 } else {
992 std::shared_ptr<fims_distributions::MultinomialLPMF<double>> dmultinom =
993 std::dynamic_pointer_cast<
995
996 this->lpdf_value = dmultinom->lpdf;
997
998 size_t n_x = dmultinom->lpdf_vec.size();
999 this->lpdf_vec = Rcpp::NumericVector(n_x);
1000 if (this->expected_values.size() != n_x) {
1001 this->expected_values.resize(n_x);
1002 }
1003 if (this->observed_values.size() != n_x) {
1004 this->observed_values.resize(n_x);
1005 }
1006 for (size_t i = 0; i < this->lpdf_vec.size(); i++) {
1007 this->lpdf_vec[i] = dmultinom->lpdf_vec[i];
1008 this->expected_values[i].final_value_m = dmultinom->get_expected(i);
1009 if (dmultinom->input_type != "data") {
1010 this->observed_values[i].final_value_m = dmultinom->get_observed(i);
1011 }
1012 }
1013 if (dmultinom->input_type == "data") {
1014 dims.resize(2);
1015 dims[0] = dmultinom->dims[0];
1016 dims[1] = dmultinom->dims[1];
1017 for (size_t i = 0; i < dims[0]; i++) {
1018 for (size_t j = 0; j < dims[1]; j++) {
1019 size_t idx = (i * dims[1]) + j;
1020 this->observed_values[idx].final_value_m = dmultinom->get_observed(
1021 static_cast<size_t>(i), static_cast<size_t>(j));
1022 }
1023 }
1024 }
1025 }
1026 }
1027
1035 virtual std::string to_json() {
1036 std::stringstream ss;
1037
1038 ss << "{\n";
1039 ss << " \"module_name\": \"density\",\n";
1040 ss << " \"module_id\": " << this->id_m << ",\n";
1041 ss << " \"module_type\": \"multinomial\",\n";
1042 ss << "\"observed_data_id\" : " << this->interface_observed_data_id_m
1043 << ",\n";
1044 ss << " \"input_type\" : \"" << this->input_type_m << "\",\n";
1045 ss << " \"density_component\": {\n";
1046 ss << " \"lpdf_value\": " << sanitize_val(this->lpdf_value) << ",\n";
1047 ss << " \"value\":[";
1048 if (this->lpdf_vec.size() == 0) {
1049 ss << "],\n";
1050 } else {
1051 for (size_t i = 0; i < this->lpdf_vec.size() - 1; i++) {
1052 ss << this->value_to_string(this->lpdf_vec[i]);
1053 ss << ", ";
1054 }
1055 ss << this->value_to_string(this->lpdf_vec[this->lpdf_vec.size() - 1]);
1056
1057 ss << "],\n";
1058 }
1059 ss << " \"expected_values\":[";
1060 if (this->expected_values.size() == 0) {
1061 ss << "],\n";
1062 } else {
1063 for (size_t i = 0; i < this->expected_values.size() - 1; i++) {
1064 ss << this->value_to_string(this->expected_values[i].final_value_m)
1065 << ", ";
1066 }
1067 ss << this->value_to_string(
1068 this->expected_values[this->expected_values.size() - 1]
1069 .final_value_m);
1070
1071 ss << "],\n";
1072 }
1073 // no log_sd_values for multinomial
1074 ss << " \"observed_values\":[";
1075 if (this->observed_values.size() == 0) {
1076 ss << "]\n";
1077 } else {
1078 for (size_t i = 0; i < this->observed_values.size() - 1; i++) {
1079 ss << this->observed_values[i].final_value_m << ", ";
1080 }
1081 ss << this->observed_values[this->observed_values.size() - 1]
1082 .final_value_m
1083 << "]\n";
1084 }
1085 ss << " }}\n";
1086 return ss.str();
1087 }
1088
1089#ifdef TMB_MODEL
1090
1091 template <typename Type>
1093 std::shared_ptr<fims_info::Information<Type>> info =
1095
1096 std::shared_ptr<fims_distributions::MultinomialLPMF<Type>> distribution =
1097 std::make_shared<fims_distributions::MultinomialLPMF<Type>>();
1098
1099 distribution->id = this->id_m;
1100 distribution->observed_data_id_m = interface_observed_data_id_m;
1101 distribution->input_type = this->input_type_m;
1102 distribution->key.resize(this->key_m->size());
1103 for (size_t i = 0; i < this->key_m->size(); i++) {
1104 distribution->key[i] = this->key_m->at(i);
1105 }
1106 distribution->observed_values.resize(this->observed_values.size());
1107 for (size_t i = 0; i < this->observed_values.size(); i++) {
1108 distribution->observed_values[i] =
1109 this->observed_values[i].initial_value_m;
1110 }
1111 // set relative info
1112 distribution->expected_values.resize(this->expected_values.size());
1113 for (size_t i = 0; i < this->expected_values.size(); i++) {
1114 distribution->expected_values[i] =
1115 this->expected_values[i].initial_value_m;
1116 }
1117
1118 info->density_components[distribution->id] = distribution;
1119 return true;
1120 }
1121
1122 virtual bool add_to_fims_tmb() {
1125
1126 return true;
1127 }
1128
1129#endif
1130};
1131
1132#endif
Rcpp interface that serves as the parent class for Rcpp distribution interfaces. This type should be ...
Definition rcpp_distribution.hpp:19
virtual bool set_observed_data(int observed_data_id)
Set the unique ID for the observed data object.
Definition rcpp_distribution.hpp:154
double lpdf_value
The log probability density function value.
Definition rcpp_distribution.hpp:76
virtual ~DistributionsInterfaceBase()
The destructor.
Definition rcpp_distribution.hpp:103
virtual double evaluate()=0
A method for each child distribution interface object to inherit so each distribution can have an eva...
std::shared_ptr< std::vector< uint32_t > > key_m
The unique ID for the variable map that points to a fims::Vector.
Definition rcpp_distribution.hpp:32
DistributionsInterfaceBase(const DistributionsInterfaceBase &other)
Construct a new Distributions Interface Base object.
Definition rcpp_distribution.hpp:93
uint32_t id_m
The local ID of the DistributionsInterfaceBase object.
Definition rcpp_distribution.hpp:28
virtual uint32_t get_id()=0
Get the ID for the child distribution interface objects to inherit.
SharedString use_mean_m
Control flag indicating whether to use the expected mean in the distribution calculations.
Definition rcpp_distribution.hpp:60
SharedInt interface_observed_data_id_m
The ID of the observed data object, which is set to -999.
Definition rcpp_distribution.hpp:71
virtual bool set_distribution_mean(double input_value)
Set the expected mean value for the distribution.
Definition rcpp_distribution.hpp:146
virtual bool set_distribution_links(std::string input_type, Rcpp::IntegerVector ids)
Sets pointers for data observations, random effects, or priors.
Definition rcpp_distribution.hpp:118
static uint32_t id_g
The static ID of the DistributionsInterfaceBase object.
Definition rcpp_distribution.hpp:24
static std::map< uint32_t, std::shared_ptr< DistributionsInterfaceBase > > live_objects
The map associating the ID of the DistributionsInterfaceBase to the DistributionsInterfaceBase object...
Definition rcpp_distribution.hpp:67
SharedString input_type_m
The type of density input. The options are prior, re, or data.
Definition rcpp_distribution.hpp:36
DistributionsInterfaceBase()
The constructor.
Definition rcpp_distribution.hpp:80
The Rcpp interface for Dlnorm to instantiate from R: dlnorm_ <- methods::new(DlnormDistribution).
Definition rcpp_distribution.hpp:530
virtual bool set_distribution_links(std::string input_type, Rcpp::IntegerVector ids)
Sets pointers for data observations, random effects, or priors.
Definition rcpp_distribution.hpp:605
DlnormDistributionsInterface(const DlnormDistributionsInterface &other)
Construct a new Dlnorm Distributions Interface object.
Definition rcpp_distribution.hpp:570
DlnormDistributionsInterface()
The constructor.
Definition rcpp_distribution.hpp:558
ParameterVector expected_values
The expected values, which would be the mean of log(x) for this distribution.
Definition rcpp_distribution.hpp:540
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_distribution.hpp:719
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_distribution.hpp:586
virtual double evaluate()
Evaluate lognormal probability density function (pdf). The natural log of the pdf is returned.
Definition rcpp_distribution.hpp:621
ParameterVector observed_values
Observed data.
Definition rcpp_distribution.hpp:535
ParameterVector log_sd
The uncertainty, which would be the natural logarithm of the standard deviation (sd) of log(x) for th...
Definition rcpp_distribution.hpp:548
virtual bool set_observed_data(int observed_data_id)
Set the unique ID for the observed data object.
Definition rcpp_distribution.hpp:592
virtual void finalize()
Extracts the derived quantities from Information to the Rcpp object.
Definition rcpp_distribution.hpp:643
virtual ~DlnormDistributionsInterface()
The destructor.
Definition rcpp_distribution.hpp:580
RealVector lpdf_vec
Vector that records the individual log probability function for each observation.
Definition rcpp_distribution.hpp:553
The Rcpp interface for Dmultinom to instantiate from R: dmultinom_ <- methods::new(DmultinomDistribut...
Definition rcpp_distribution.hpp:851
virtual bool set_distribution_links(std::string input_type, Rcpp::IntegerVector ids)
Sets pointers for data observations, random effects, or priors.
Definition rcpp_distribution.hpp:930
SharedString notes
TODO: document this.
Definition rcpp_distribution.hpp:878
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_distribution.hpp:911
ParameterVector expected_values
The expected values, which should be a vector of length K where each value specifies the probability ...
Definition rcpp_distribution.hpp:862
void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_distribution.hpp:969
virtual double evaluate()
Definition rcpp_distribution.hpp:952
ParameterVector observed_values
Observed data, which should be a vector of length K of integers.
Definition rcpp_distribution.hpp:856
void set_note(std::string note)
Set the note object.
Definition rcpp_distribution.hpp:945
virtual bool set_observed_data(int observed_data_id)
Set the unique ID for the observed data object.
Definition rcpp_distribution.hpp:917
RealVector dims
The dimensions of the number of rows and columns of the multivariate dataset.
Definition rcpp_distribution.hpp:867
DmultinomDistributionsInterface()
The constructor.
Definition rcpp_distribution.hpp:883
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_distribution.hpp:1035
RealVector lpdf_vec
Vector that records the individual log probability function for each observation.
Definition rcpp_distribution.hpp:872
DmultinomDistributionsInterface(const DmultinomDistributionsInterface &other)
Construct a new Dmultinom Distributions Interface object.
Definition rcpp_distribution.hpp:895
virtual ~DmultinomDistributionsInterface()
The destructor.
Definition rcpp_distribution.hpp:906
The Rcpp interface for Dnorm to instantiate from R: dnorm_ <- methods::new(DnormDistribution).
Definition rcpp_distribution.hpp:167
DnormDistributionsInterface(const DnormDistributionsInterface &other)
Construct a new Dnorm Distributions Interface object.
Definition rcpp_distribution.hpp:209
ParameterVector expected_mean
The expected mean, which would be the mean of x for this distribution.
Definition rcpp_distribution.hpp:182
DnormDistributionsInterface()
The constructor.
Definition rcpp_distribution.hpp:197
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_distribution.hpp:379
virtual ~DnormDistributionsInterface()
The destructor.
Definition rcpp_distribution.hpp:220
RealVector lpdf_vec
Vector that records the individual log probability function for each observation.
Definition rcpp_distribution.hpp:192
virtual double evaluate()
Evaluate normal probability density function (pdf). The natural log of the pdf is returned.
Definition rcpp_distribution.hpp:266
ParameterVector expected_values
The expected values, which would be the mean of x for this distribution.
Definition rcpp_distribution.hpp:177
virtual void finalize()
Extracts the derived quantities from Information to the Rcpp object.
Definition rcpp_distribution.hpp:292
virtual bool set_observed_data(int observed_data_id)
Set the unique ID for the observed data object.
Definition rcpp_distribution.hpp:232
ParameterVector observed_values
Observed data.
Definition rcpp_distribution.hpp:172
virtual bool set_distribution_mean(double input_value)
Set the expected mean value for the distribution.
Definition rcpp_distribution.hpp:240
ParameterVector log_sd
The uncertainty, which would be the standard deviation of x for the normal distribution.
Definition rcpp_distribution.hpp:187
virtual bool set_distribution_links(std::string input_type, Rcpp::IntegerVector ids)
Sets pointers for data observations, random effects, or priors.
Definition rcpp_distribution.hpp:250
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_distribution.hpp:226
Base class for all interface objects.
Definition rcpp_interface_base.hpp:628
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:633
std::string value_to_string(double value)
Report the parameter value as a string.
Definition rcpp_interface_base.hpp:669
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:638
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:643
An Rcpp interface class that defines the ParameterVector class.
Definition rcpp_interface_base.hpp:144
void resize(size_t size)
Resizes a ParameterVector to the desired length.
Definition rcpp_interface_base.hpp:295
size_t size()
Returns the size of a ParameterVector.
Definition rcpp_interface_base.hpp:288
uint32_t id_m
The local ID of the Parameter object.
Definition rcpp_interface_base.hpp:157
Parameter & get(size_t pos)
An internal accessor for calling a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:267
void set(size_t pos, const Parameter &p)
An internal setter for setting a position of a ParameterVector from R.
Definition rcpp_interface_base.hpp:283
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:413
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:584
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:591
A class that provides shared ownership of an integer value.
Definition rcpp_shared_primitive.hpp:29
void set(int val)
Change the value of the integer.
Definition rcpp_shared_primitive.hpp:134
A class that provides shared ownership of a string.
Definition rcpp_shared_primitive.hpp:1513
std::string get() const
Retrieves the string value managed by the object.
Definition rcpp_shared_primitive.hpp:1617
void set(std::string val)
Sets the string value of the object.
Definition rcpp_shared_primitive.hpp:1626
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > > density_components
Definition information.hpp:125
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > >::iterator density_components_iterator
Definition information.hpp:130
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:237
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
void clear_internal()
Clears the internal objects.
Definition rcpp_interface.hpp:235
The Rcpp interface to declare objects that are used ubiquitously throughout the Rcpp interface,...
double sanitize_val(double x)
Sanitize a double value by replacing NaN or Inf with -999.0.
Definition rcpp_interface_base.hpp:113
fims::Vector< Type > observed_values
Input value of distribution function for priors or random effects.
Definition density_components_base.hpp:61
Type lpdf
Total log probability density contribution of the distribution.
Definition density_components_base.hpp:180
Implements the LogNormalLPDF distribution functor used by FIMS to evaluate observation-level and tota...
Definition lognormal_lpdf.hpp:34
Implements the MultinomialLPMF distribution functor used by FIMS to evaluate the observation-level an...
Definition multinomial_lpmf.hpp:36
Implements the NormalLPDF distribution functor used by FIMS to evaluate observation-level and total l...
Definition normal_lpdf.hpp:32
virtual const Type evaluate()
Evaluates the normal log probability density function.
Definition normal_lpdf.hpp:59