FIMS  v0.11.0
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
12#include <sstream>
13#include <string>
14#include <utility>
15#include <vector>
16
18#include "../../../distributions/distributions.hpp"
19
25 public:
29 static uint32_t id_g;
33 uint32_t id_m;
37 std::shared_ptr<std::vector<uint32_t>> key_m;
65 SharedString use_mean_m = fims::to_string("no");
71 static std::map<uint32_t, std::shared_ptr<DistributionsInterfaceBase>>
77
81 double lpdf_value = 0;
86 this->key_m = std::make_shared<std::vector<uint32_t>>();
88 /* Create instance of map: key is id and value is pointer to
89 DistributionsInterfaceBase */
90 // DistributionsInterfaceBase::live_objects[this->id_m] = this;
91 }
92
104
109
113 virtual uint32_t get_id() = 0;
114
123 virtual bool set_distribution_links(std::string input_type,
124 Rcpp::IntegerVector ids) {
125 return false;
126 }
127
151 virtual bool set_distribution_mean(double input_value) { return false; }
152
159 virtual bool set_observed_data(int observed_data_id) { return false; }
160
165 virtual double evaluate() = 0;
166
183 const std::vector<std::pair<std::string, VariableVector *>> &parameters) {
184 std::vector<std::string> entries;
185 for (const auto &parameter : parameters) {
186 VariableVector *values = parameter.second;
187 if (values == nullptr || values->size() == 0) {
188 continue;
189 }
190 std::stringstream entry;
191 entry << "{\n";
192 entry << " \"name\": \"" << parameter.first << "\",\n";
193 entry << " \"id\": " << values->id_m << ",\n";
194 entry << " \"type\": \"vector\",\n";
195 entry << " \"dimensionality\": {\n";
196 entry << " \"header\": [null],\n";
197 entry << " \"dimensions\": [" << values->size() << "]\n";
198 entry << " },\n";
199 entry << " \"values\": " << *values << "\n";
200 entry << " }";
201 entries.push_back(entry.str());
202 }
203 if (entries.empty()) {
204 return "";
205 }
206 std::stringstream ss;
207 ss << " \"parameters\": [\n";
208 for (size_t i = 0; i < entries.size(); i++) {
209 ss << (i == 0 ? " " : ",\n ") << entries[i];
210 }
211 ss << "\n ],\n";
212 return ss.str();
213 }
214
225 std::string linked_ids_to_json() {
226 std::stringstream ss;
227 ss << " \"linked_ids\": [";
228 for (size_t i = 0; i < this->key_m->size(); i++) {
229 ss << (i == 0 ? "" : ", ") << this->key_m->at(i);
230 }
231 ss << "],\n";
232 return ss.str();
233 }
234};
235
241 public:
276
289
294
299 virtual uint32_t get_id() { return this->id_m; }
300
305 virtual bool set_observed_data(int observed_data_id) {
306 this->interface_observed_data_id_m.set(observed_data_id);
307 return true;
308 }
309
313 virtual bool set_distribution_mean(double input_value) {
314 this->expected_mean[0].initial_value_m = input_value;
315 this->expected_mean[0].estimation_type_m.set("fixed_effects");
316 this->use_mean_m.set(fims::to_string("yes"));
317 return true;
318 }
319
323 virtual bool set_distribution_links(std::string input_type,
324 Rcpp::IntegerVector ids) {
325 this->input_type_m.set(input_type);
326 this->key_m->resize(ids.size());
327 for (R_xlen_t i = 0; i < ids.size(); i++) {
328 this->key_m->at(i) = ids[i];
329 }
330 return true;
331 }
332
339 virtual double evaluate() {
341 dnorm.observed_values.resize(this->observed_values.size());
342 dnorm.expected_values.resize(this->expected_values.size());
343 dnorm.log_sd.resize(this->log_sd.size());
344 dnorm.expected_mean.resize(this->expected_mean.size());
345 for (size_t i = 0; i < this->observed_values.size(); i++) {
346 dnorm.observed_values[i] = this->observed_values[i].initial_value_m;
347 }
348 for (size_t i = 0; i < this->expected_values.size(); i++) {
349 dnorm.expected_values[i] = this->expected_values[i].initial_value_m;
350 }
351 for (size_t i = 0; i < this->log_sd.size(); i++) {
352 dnorm.log_sd[i] = this->log_sd[i].initial_value_m;
353 }
354 for (size_t i = 0; i < this->expected_mean.size(); i++) {
355 dnorm.expected_mean[i] = this->expected_mean[i].initial_value_m;
356 }
357 dnorm.use_mean = this->use_mean_m;
358 return dnorm.evaluate();
359 }
360
365 virtual void finalize() {
366 if (this->finalized) {
367 // log warning that finalize has been called more than once.
368 FIMS_WARNING_LOG("DnormDistribution " + fims::to_string(this->id_m) +
369 " has been finalized already.");
370 }
371
372 this->finalized = true; // indicate this has been called already
373
374 std::shared_ptr<fims_info::Information<double>> info =
376
378
379 // search for density component in Information
380 it = info->density_components.find(this->id_m);
381 // if not found, just return
382 if (it == info->density_components.end()) {
383 FIMS_WARNING_LOG("DnormDistribution " + fims::to_string(this->id_m) +
384 " not found in Information.");
385 return;
386 } else {
387 std::shared_ptr<fims_distributions::NormalLPDF<double>> dnorm =
388 std::dynamic_pointer_cast<fims_distributions::NormalLPDF<double>>(
389 it->second);
390
391 this->lpdf_value = dnorm->lpdf;
392
393 size_t n_x = dnorm->get_n_x();
394
395 // If input log_sd is a scalar, resize to n_x and fill with the scalar
396 // value
397 if (this->log_sd.size() != n_x) {
398 // If log_sd size == 1 (scalar), repeat the entry
399 if (this->log_sd.size() == 1) {
400 auto tmp = this->log_sd[0]; // copy the one log_sd param
401 this->log_sd.resize(n_x);
402 for (size_t i = 0; i < n_x; ++i) {
403 this->log_sd[i] = tmp; // copies all fields in Param
404 }
405 } else {
406 // Handle error
408 "log_sd size does not match number of observations and is not "
409 "scalar.");
410 }
411 }
412 for (size_t i = 0; i < n_x; i++) {
413 if (this->log_sd[i].estimation_type_m.get() == "constant") {
414 this->log_sd[i].final_value_m = this->log_sd[i].initial_value_m;
415 } else {
416 this->log_sd[i].final_value_m = dnorm->log_sd.get_force_scalar(i);
417 }
418 }
419
420 for (size_t i = 0; i < this->expected_mean.size(); i++) {
421 if (this->expected_mean[i].estimation_type_m.get() == "constant") {
422 this->expected_mean[i].final_value_m =
423 this->expected_mean[i].initial_value_m;
424 } else {
425 this->expected_mean[i].final_value_m = dnorm->expected_mean[i];
426 }
427 }
428
429 this->lpdf_vec = RealVector(n_x);
430 if (this->expected_values.size() == 1) {
431 this->expected_values.resize(n_x);
432 }
433 if (this->observed_values.size() == 1) {
434 this->observed_values.resize(n_x);
435 }
436
437 for (size_t i = 0; i < this->lpdf_vec.size(); i++) {
438 this->lpdf_vec[i] = dnorm->lpdf_vec[i];
439 this->expected_values[i].final_value_m = dnorm->get_expected(i);
440 this->observed_values[i].final_value_m = dnorm->get_observed(i);
441 }
442 }
443 }
444
452 virtual std::string to_json() {
453 std::stringstream ss;
454
455 ss << "{\n";
456 ss << " \"module_name\": \"density\",\n";
457 ss << " \"module_id\": " << this->id_m << ",\n";
458 ss << " \"module_type\": \"normal\",\n";
459 ss << " \"observed_data_id\" : " << this->interface_observed_data_id_m
460 << ",\n";
461 ss << " \"input_type\" : \"" << this->input_type_m << "\",\n";
462 ss << this->linked_ids_to_json();
463 ss << this->parameters_to_json({{"log_sd", &this->log_sd}});
464 ss << " \"density_component\": {\n";
465 ss << " \"lpdf_value\": " << sanitize_val(this->lpdf_value) << ",\n";
466 ss << " \"value\":[";
467 if (this->lpdf_vec.size() == 0) {
468 ss << "],\n";
469 } else {
470 for (size_t i = 0; i < this->lpdf_vec.size() - 1; i++) {
471 ss << this->value_to_string(this->lpdf_vec[i]);
472 ss << ", ";
473 }
474 ss << this->value_to_string(this->lpdf_vec[this->lpdf_vec.size() - 1]);
475
476 ss << "],\n";
477 }
478 ss << " \"expected_values\":[";
479 if (this->expected_values.size() == 0) {
480 ss << "],\n";
481 } else {
482 for (size_t i = 0; i < this->expected_values.size() - 1; i++) {
483 ss << this->value_to_string(this->expected_values[i].final_value_m)
484 << ", ";
485 }
486 ss << this->value_to_string(
487 this->expected_values[this->expected_values.size() - 1]
488 .final_value_m);
489 ss << "],\n";
490 }
491 ss << " \"log_sd_values\":[";
492 if (this->log_sd.size() == 0) {
493 ss << "],\n";
494 } else {
495 for (R_xlen_t i = 0; i < static_cast<R_xlen_t>(this->log_sd.size()) - 1;
496 i++) {
497 ss << this->value_to_string(this->log_sd[i].final_value_m) << ", ";
498 }
499 ss << this->value_to_string(
500 this->log_sd[this->log_sd.size() - 1].final_value_m)
501 << "],\n";
502 }
503 ss << " \"observed_values\":[";
504 if (this->observed_values.size() == 0) {
505 ss << "]\n";
506 } else {
507 for (size_t i = 0; i < this->observed_values.size() - 1; i++) {
508 ss << this->observed_values[i].final_value_m << ", ";
509 }
510 ss << this->observed_values[this->observed_values.size() - 1]
511 .final_value_m
512 << "]\n";
513 }
514 ss << " }}\n";
515 return ss.str();
516 }
517
518#ifdef TMB_MODEL
519
520 template <typename Type>
521 bool add_to_fims_tmb_internal() {
522 std::shared_ptr<fims_info::Information<Type>> info =
524
525 std::shared_ptr<fims_distributions::NormalLPDF<Type>> distribution =
526 std::make_shared<fims_distributions::NormalLPDF<Type>>();
527
528 // interface to data/parameter value
529
530 distribution->observed_data_id_m = interface_observed_data_id_m;
531 std::stringstream ss;
532 distribution->input_type = this->input_type_m;
533 distribution->key.resize(this->key_m->size());
534 for (size_t i = 0; i < this->key_m->size(); i++) {
535 distribution->key[i] = this->key_m->at(i);
536 }
537 distribution->id = this->id_m;
538 distribution->observed_values.resize(this->observed_values.size());
539 for (size_t i = 0; i < this->observed_values.size(); i++) {
540 distribution->observed_values[i] =
541 this->observed_values[i].initial_value_m;
542 }
543 // set relative info
544 distribution->expected_values.resize(this->expected_values.size());
545 for (size_t i = 0; i < this->expected_values.size(); i++) {
546 distribution->expected_values[i] =
547 this->expected_values[i].initial_value_m;
548 }
549 distribution->log_sd.resize(this->log_sd.size());
550 for (size_t i = 0; i < this->log_sd.size(); i++) {
551 distribution->log_sd[i] = this->log_sd[i].initial_value_m;
552 if (this->log_sd[i].estimation_type_m.get() == "fixed_effects") {
553 ss.str("");
554 ss << "dnorm." << this->id_m << ".log_sd." << this->log_sd[i].id_m;
555 info->RegisterParameterName(ss.str());
556 info->RegisterParameter(distribution->log_sd[i]);
557 }
558 if (this->log_sd[i].estimation_type_m.get() == "random_effects") {
559 FIMS_ERROR_LOG("standard deviations cannot be set to random effects");
560 }
561 }
562 info->variable_map[this->log_sd.id_m] = &(distribution)->log_sd;
563
564 distribution->use_mean = this->use_mean_m.get();
565 distribution->expected_mean.resize(this->expected_mean.size());
566 for (size_t i = 0; i < this->expected_mean.size(); i++) {
567 distribution->expected_mean[i] = this->expected_mean[i].initial_value_m;
568 if (this->expected_mean[i].estimation_type_m.get() == "fixed_effects") {
569 ss.str("");
570 ss << "dnorm." << this->id_m << ".expected_mean."
571 << this->expected_mean[i].id_m;
572 info->RegisterParameterName(ss.str());
573 info->RegisterParameter(distribution->expected_mean[i]);
574 }
575 if (this->expected_mean[i].estimation_type_m.get() == "random_effects") {
576 FIMS_ERROR_LOG("expected_mean cannot be set to random effects");
577 }
578 }
579 info->variable_map[this->expected_mean.id_m] =
580 &(distribution)->expected_mean;
581
582 info->density_components[distribution->id] = distribution;
583
584 return true;
585 }
586
591 virtual bool add_to_fims_tmb() {
592 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
593 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
594
595 return true;
596 }
597
598#endif
599};
600
606 public:
639
651
656
661 virtual uint32_t get_id() { return this->id_m; }
662
667 virtual bool set_observed_data(int observed_data_id) {
668 this->interface_observed_data_id_m.set(observed_data_id);
669 return true;
670 }
671
680 virtual bool set_distribution_links(std::string input_type,
681 Rcpp::IntegerVector ids) {
682 this->input_type_m.set(input_type);
683 this->key_m->resize(ids.size());
684 for (R_xlen_t i = 0; i < ids.size(); i++) {
685 this->key_m->at(i) = ids[i];
686 }
687 return true;
688 }
689
696 virtual double evaluate() {
698 dlnorm.observed_values.resize(this->observed_values.size());
699 dlnorm.expected_values.resize(this->expected_values.size());
700 dlnorm.log_sd.resize(this->log_sd.size());
701 // dlnorm.input_type = "prior";
702 for (size_t i = 0; i < this->observed_values.size(); i++) {
703 dlnorm.observed_values[i] = this->observed_values[i].initial_value_m;
704 }
705 for (size_t i = 0; i < this->expected_values.size(); i++) {
706 dlnorm.expected_values[i] = this->expected_values[i].initial_value_m;
707 }
708 for (size_t i = 0; i < this->log_sd.size(); i++) {
709 dlnorm.log_sd[i] = this->log_sd[i].initial_value_m;
710 }
711 return dlnorm.evaluate();
712 }
713
718 virtual void finalize() {
719 if (this->finalized) {
720 // log warning that finalize has been called more than once.
721 FIMS_WARNING_LOG("LogNormalLPDF " + fims::to_string(this->id_m) +
722 " has been finalized already.");
723 }
724
725 this->finalized = true; // indicate this has been called already
726
727 std::shared_ptr<fims_info::Information<double>> info =
729
731
732 // search for density component in Information
733 it = info->density_components.find(this->id_m);
734 // if not found, just return
735 if (it == info->density_components.end()) {
736 FIMS_WARNING_LOG("LogNormalLPDF " + fims::to_string(this->id_m) +
737 " not found in Information.");
738 return;
739 } else {
740 std::shared_ptr<fims_distributions::LogNormalLPDF<double>> dlnorm =
741 std::dynamic_pointer_cast<fims_distributions::LogNormalLPDF<double>>(
742 it->second);
743
744 this->lpdf_value = dlnorm->lpdf;
745
746 size_t n_x = dlnorm->get_n_x();
747
748 if (this->log_sd.size() != n_x) {
749 // If log_sd size == 1 (scalar), repeat the entry
750 if (this->log_sd.size() == 1) {
751 auto tmp = this->log_sd[0]; // copy the one log_sd param
752 this->log_sd.resize(n_x);
753 for (size_t i = 0; i < n_x; ++i) {
754 this->log_sd[i] = tmp; // copies all fields in Param
755 }
756 } else {
757 // Handle error
759 "log_sd size does not match number of observations and is not "
760 "scalar.");
761 }
762 }
763
764 for (size_t i = 0; i < n_x; i++) {
765 if (this->log_sd[i].estimation_type_m.get() == "constant") {
766 this->log_sd[i].final_value_m = this->log_sd[i].initial_value_m;
767 } else {
768 this->log_sd[i].final_value_m = dlnorm->log_sd.get_force_scalar(i);
769 }
770 }
771
772 this->lpdf_vec = RealVector(n_x);
773 if (this->expected_values.size() == 1) {
774 this->expected_values.resize(n_x);
775 }
776 if (this->observed_values.size() == 1) {
777 this->observed_values.resize(n_x);
778 }
779 for (size_t i = 0; i < this->lpdf_vec.size(); i++) {
780 this->lpdf_vec[i] = dlnorm->lpdf_vec[i];
781 this->expected_values[i].final_value_m = dlnorm->get_expected(i);
782 this->observed_values[i].final_value_m = dlnorm->get_observed(i);
783 }
784 }
785 }
786
794 virtual std::string to_json() {
795 std::stringstream ss;
796
797 ss << "{\n";
798 ss << " \"module_name\": \"density\",\n";
799 ss << " \"module_id\": " << this->id_m << ",\n";
800 ss << " \"module_type\": \"log_normal\",\n";
801 ss << " \"observed_data_id\" : " << this->interface_observed_data_id_m
802 << ",\n";
803 ss << " \"input_type\" : \"" << this->input_type_m << "\",\n";
804 ss << this->linked_ids_to_json();
805 ss << this->parameters_to_json({{"log_sd", &this->log_sd}});
806 ss << " \"density_component\": {\n";
807 ss << " \"lpdf_value\": " << sanitize_val(this->lpdf_value) << ",\n";
808 ss << " \"value\":[";
809 if (this->lpdf_vec.size() == 0) {
810 ss << "],\n";
811 } else {
812 for (size_t i = 0; i < this->lpdf_vec.size() - 1; i++) {
813 ss << this->value_to_string(this->lpdf_vec[i]) << ", ";
814 }
815 ss << this->value_to_string(this->lpdf_vec[this->lpdf_vec.size() - 1]);
816
817 ss << "],\n";
818 }
819 ss << " \"expected_values\":[";
820 if (this->expected_values.size() == 0) {
821 ss << "],\n";
822 } else {
823 for (size_t i = 0; i < this->expected_values.size() - 1; i++) {
824 ss << this->value_to_string(this->expected_values[i].final_value_m)
825 << ", ";
826 }
827 ss << this->value_to_string(
828 this->expected_values[this->expected_values.size() - 1]
829 .final_value_m);
830
831 ss << "],\n";
832 }
833 ss << " \"log_sd_values\":[";
834 if (this->log_sd.size() == 0) {
835 ss << "],\n";
836 } else {
837 for (R_xlen_t i = 0; i < static_cast<R_xlen_t>(this->log_sd.size()) - 1;
838 i++) {
839 ss << this->value_to_string(this->log_sd[i].final_value_m) << ", ";
840 }
841 ss << this->value_to_string(
842 this->log_sd[this->log_sd.size() - 1].final_value_m)
843 << "],\n";
844 }
845 ss << " \"observed_values\":[";
846 if (this->observed_values.size() == 0) {
847 ss << "]\n";
848 } else {
849 for (size_t i = 0; i < this->observed_values.size() - 1; i++) {
850 ss << this->observed_values[i].final_value_m << ", ";
851 }
852 ss << this->observed_values[this->observed_values.size() - 1]
853 .final_value_m
854 << "]\n";
855 }
856 ss << " }}\n";
857 return ss.str();
858 }
859
860#ifdef TMB_MODEL
861
862 template <typename Type>
863 bool add_to_fims_tmb_internal() {
864 std::shared_ptr<fims_info::Information<Type>> info =
866
867 std::shared_ptr<fims_distributions::LogNormalLPDF<Type>> distribution =
868 std::make_shared<fims_distributions::LogNormalLPDF<Type>>();
869
870 // set relative info
871 distribution->id = this->id_m;
872 std::stringstream ss;
873 distribution->observed_data_id_m = interface_observed_data_id_m;
874 distribution->input_type = this->input_type_m;
875 distribution->key.resize(this->key_m->size());
876 for (size_t i = 0; i < this->key_m->size(); i++) {
877 distribution->key[i] = this->key_m->at(i);
878 }
879 distribution->observed_values.resize(this->observed_values.size());
880 for (size_t i = 0; i < this->observed_values.size(); i++) {
881 distribution->observed_values[i] =
882 this->observed_values[i].initial_value_m;
883 }
884 // set relative info
885 distribution->expected_values.resize(this->expected_values.size());
886 for (size_t i = 0; i < this->expected_values.size(); i++) {
887 distribution->expected_values[i] =
888 this->expected_values[i].initial_value_m;
889 }
890 distribution->log_sd.resize(this->log_sd.size());
891 for (size_t i = 0; i < this->log_sd.size(); i++) {
892 distribution->log_sd[i] = this->log_sd[i].initial_value_m;
893 if (this->log_sd[i].estimation_type_m.get() == "fixed_effects") {
894 ss.str("");
895 ss << "dlnorm." << this->id_m << ".log_sd." << this->log_sd[i].id_m;
896 info->RegisterParameterName(ss.str());
897 info->RegisterParameter(distribution->log_sd[i]);
898 }
899 if (this->log_sd[i].estimation_type_m.get() == "random_effects") {
900 FIMS_ERROR_LOG("standard deviations cannot be set to random effects");
901 }
902 }
903 info->variable_map[this->log_sd.id_m] = &(distribution)->log_sd;
904
905 info->density_components[distribution->id] = distribution;
906
907 return true;
908 }
909
914 virtual bool add_to_fims_tmb() {
915 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
916 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
917
918 return true;
919 }
920
921#endif
922};
923
929 public:
956
966
979
988 virtual uint32_t get_id() { return this->id_m; }
989
994 virtual bool set_observed_data(int observed_data_id) {
995 this->interface_observed_data_id_m.set(observed_data_id);
996 return true;
997 }
998
1007 virtual bool set_distribution_links(std::string input_type,
1008 Rcpp::IntegerVector ids) {
1009 this->input_type_m.set(input_type);
1010 this->key_m->resize(ids.size());
1011 for (R_xlen_t i = 0; i < ids.size(); i++) {
1012 this->key_m->at(i) = ids[i];
1013 }
1014 return true;
1015 }
1016
1022 void set_note(std::string note) { this->notes.set(note); }
1023
1029 virtual double evaluate() {
1031 // Declare TMBVector in this scope
1032 dmultinom.observed_values.resize(this->observed_values.size());
1033 dmultinom.expected_values.resize(this->expected_values.size());
1034 for (size_t i = 0; i < observed_values.size(); i++) {
1035 dmultinom.observed_values[i] = this->observed_values[i].initial_value_m;
1036 }
1037 for (size_t i = 0; i < expected_values.size(); i++) {
1038 dmultinom.expected_values[i] = this->expected_values[i].initial_value_m;
1039 }
1040 dmultinom.dims.resize(2);
1041 dmultinom.dims[0] = this->dims[0];
1042 dmultinom.dims[1] = this->dims[1];
1043 return dmultinom.evaluate();
1044 }
1045
1046 void finalize() {
1047 if (this->finalized) {
1048 // log warning that finalize has been called more than once.
1049 FIMS_WARNING_LOG("DmultinomDistributions " +
1050 fims::to_string(this->id_m) +
1051 " has been finalized already.");
1052 }
1053
1054 this->finalized = true; // indicate this has been called already
1055
1056 std::shared_ptr<fims_info::Information<double>> info =
1058
1060
1061 // search for density component in Information
1062 it = info->density_components.find(this->id_m);
1063 // if not found, just return
1064 if (it == info->density_components.end()) {
1065 FIMS_WARNING_LOG("DmultinomDistributions " + fims::to_string(this->id_m) +
1066 " not found in Information.");
1067 return;
1068 } else {
1069 std::shared_ptr<fims_distributions::MultinomialLPMF<double>> dmultinom =
1070 std::dynamic_pointer_cast<
1072
1073 this->lpdf_value = dmultinom->lpdf;
1074
1075 size_t n_x = dmultinom->lpdf_vec.size();
1076 this->lpdf_vec = Rcpp::NumericVector(n_x);
1077 if (this->expected_values.size() != n_x) {
1078 this->expected_values.resize(n_x);
1079 }
1080 if (this->observed_values.size() != n_x) {
1081 this->observed_values.resize(n_x);
1082 }
1083 for (size_t i = 0; i < this->lpdf_vec.size(); i++) {
1084 this->lpdf_vec[i] = dmultinom->lpdf_vec[i];
1085 this->expected_values[i].final_value_m = dmultinom->get_expected(i);
1086 if (dmultinom->input_type != "data") {
1087 this->observed_values[i].final_value_m = dmultinom->get_observed(i);
1088 }
1089 }
1090 if (dmultinom->input_type == "data") {
1091 dims.resize(2);
1092 dims[0] = dmultinom->dims[0];
1093 dims[1] = dmultinom->dims[1];
1094 for (size_t i = 0; i < dims[0]; i++) {
1095 for (size_t j = 0; j < dims[1]; j++) {
1096 size_t idx = (i * dims[1]) + j;
1097 this->observed_values[idx].final_value_m = dmultinom->get_observed(
1098 static_cast<size_t>(i), static_cast<size_t>(j));
1099 }
1100 }
1101 }
1102 }
1103 }
1104
1112 virtual std::string to_json() {
1113 std::stringstream ss;
1114
1115 ss << "{\n";
1116 ss << " \"module_name\": \"density\",\n";
1117 ss << " \"module_id\": " << this->id_m << ",\n";
1118 ss << " \"module_type\": \"multinomial\",\n";
1119 ss << "\"observed_data_id\" : " << this->interface_observed_data_id_m
1120 << ",\n";
1121 ss << " \"input_type\" : \"" << this->input_type_m << "\",\n";
1122 ss << " \"density_component\": {\n";
1123 ss << " \"lpdf_value\": " << sanitize_val(this->lpdf_value) << ",\n";
1124 ss << " \"value\":[";
1125 if (this->lpdf_vec.size() == 0) {
1126 ss << "],\n";
1127 } else {
1128 for (size_t i = 0; i < this->lpdf_vec.size() - 1; i++) {
1129 ss << this->value_to_string(this->lpdf_vec[i]);
1130 ss << ", ";
1131 }
1132 ss << this->value_to_string(this->lpdf_vec[this->lpdf_vec.size() - 1]);
1133
1134 ss << "],\n";
1135 }
1136 ss << " \"expected_values\":[";
1137 if (this->expected_values.size() == 0) {
1138 ss << "],\n";
1139 } else {
1140 for (size_t i = 0; i < this->expected_values.size() - 1; i++) {
1141 ss << this->value_to_string(this->expected_values[i].final_value_m)
1142 << ", ";
1143 }
1144 ss << this->value_to_string(
1145 this->expected_values[this->expected_values.size() - 1]
1146 .final_value_m);
1147
1148 ss << "],\n";
1149 }
1150 // no log_sd_values for multinomial
1151 ss << " \"observed_values\":[";
1152 if (this->observed_values.size() == 0) {
1153 ss << "]\n";
1154 } else {
1155 for (size_t i = 0; i < this->observed_values.size() - 1; i++) {
1156 ss << this->observed_values[i].final_value_m << ", ";
1157 }
1158 ss << this->observed_values[this->observed_values.size() - 1]
1159 .final_value_m
1160 << "]\n";
1161 }
1162 ss << " }}\n";
1163 return ss.str();
1164 }
1165
1166#ifdef TMB_MODEL
1167
1168 template <typename Type>
1169 bool add_to_fims_tmb_internal() {
1170 std::shared_ptr<fims_info::Information<Type>> info =
1172
1173 std::shared_ptr<fims_distributions::MultinomialLPMF<Type>> distribution =
1174 std::make_shared<fims_distributions::MultinomialLPMF<Type>>();
1175
1176 distribution->id = this->id_m;
1177 distribution->observed_data_id_m = interface_observed_data_id_m;
1178 distribution->input_type = this->input_type_m;
1179 distribution->key.resize(this->key_m->size());
1180 for (size_t i = 0; i < this->key_m->size(); i++) {
1181 distribution->key[i] = this->key_m->at(i);
1182 }
1183 distribution->observed_values.resize(this->observed_values.size());
1184 for (size_t i = 0; i < this->observed_values.size(); i++) {
1185 distribution->observed_values[i] =
1186 this->observed_values[i].initial_value_m;
1187 }
1188 // set relative info
1189 distribution->expected_values.resize(this->expected_values.size());
1190 for (size_t i = 0; i < this->expected_values.size(); i++) {
1191 distribution->expected_values[i] =
1192 this->expected_values[i].initial_value_m;
1193 }
1194
1195 info->density_components[distribution->id] = distribution;
1196 return true;
1197 }
1198
1199 virtual bool add_to_fims_tmb() {
1200 this->add_to_fims_tmb_internal<TMB_FIMS_REAL_TYPE>();
1201 this->add_to_fims_tmb_internal<TMBAD_FIMS_TYPE>();
1202
1203 return true;
1204 }
1205
1206#endif
1207};
1208
1209#endif
Rcpp interface that serves as the parent class for Rcpp distribution interfaces. This type should be ...
Definition rcpp_distribution.hpp:24
virtual bool set_observed_data(int observed_data_id)
Set the unique ID for the observed data object.
Definition rcpp_distribution.hpp:159
double lpdf_value
The log probability density function value.
Definition rcpp_distribution.hpp:81
virtual ~DistributionsInterfaceBase()
The destructor.
Definition rcpp_distribution.hpp:108
virtual double evaluate()=0
A method for each child distribution interface object to inherit so each distribution can have an eva...
std::string parameters_to_json(const std::vector< std::pair< std::string, VariableVector * > > &parameters)
Writes the parameters of a distribution as a JSON "parameters" member, in the same shape the modules ...
Definition rcpp_distribution.hpp:182
std::string linked_ids_to_json()
Writes the ids of the vectors this distribution is linked to as a JSON "linked_ids" member.
Definition rcpp_distribution.hpp:225
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:37
DistributionsInterfaceBase(const DistributionsInterfaceBase &other)
Construct a new Distributions Interface Base object.
Definition rcpp_distribution.hpp:98
uint32_t id_m
The local ID of the DistributionsInterfaceBase object.
Definition rcpp_distribution.hpp:33
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:65
SharedInt interface_observed_data_id_m
The ID of the observed data object, which is set to -999.
Definition rcpp_distribution.hpp:76
virtual bool set_distribution_mean(double input_value)
Set the expected mean value for the distribution.
Definition rcpp_distribution.hpp:151
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:123
static uint32_t id_g
The static ID of the DistributionsInterfaceBase object.
Definition rcpp_distribution.hpp:29
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:72
SharedString input_type_m
The type of density input. The options are prior, re, or data.
Definition rcpp_distribution.hpp:41
DistributionsInterfaceBase()
The constructor.
Definition rcpp_distribution.hpp:85
The Rcpp interface for Dlnorm to instantiate from R: dlnorm_ <- methods::new(DlnormDistribution).
Definition rcpp_distribution.hpp:605
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:680
DlnormDistributionsInterface(const DlnormDistributionsInterface &other)
Construct a new Dlnorm Distributions Interface object.
Definition rcpp_distribution.hpp:645
DlnormDistributionsInterface()
The constructor.
Definition rcpp_distribution.hpp:633
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_distribution.hpp:794
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_distribution.hpp:661
VariableVector log_sd
The uncertainty, which would be the natural logarithm of the standard deviation (sd) of log(x) for th...
Definition rcpp_distribution.hpp:623
virtual double evaluate()
Evaluate lognormal probability density function (pdf). The natural log of the pdf is returned.
Definition rcpp_distribution.hpp:696
virtual bool set_observed_data(int observed_data_id)
Set the unique ID for the observed data object.
Definition rcpp_distribution.hpp:667
virtual void finalize()
Extracts the derived quantities from Information to the Rcpp object.
Definition rcpp_distribution.hpp:718
VariableVector expected_values
The expected values, which would be the mean of log(x) for this distribution.
Definition rcpp_distribution.hpp:615
virtual ~DlnormDistributionsInterface()
The destructor.
Definition rcpp_distribution.hpp:655
VariableVector observed_values
Observed data.
Definition rcpp_distribution.hpp:610
RealVector lpdf_vec
Vector that records the individual log probability function for each observation.
Definition rcpp_distribution.hpp:628
The Rcpp interface for Dmultinom to instantiate from R: dmultinom_ <- methods::new(DmultinomDistribut...
Definition rcpp_distribution.hpp:928
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:1007
SharedString notes
TODO: document this.
Definition rcpp_distribution.hpp:955
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_distribution.hpp:988
void finalize()
Extracts derived quantities back to the Rcpp interface object from the Information object.
Definition rcpp_distribution.hpp:1046
virtual double evaluate()
Definition rcpp_distribution.hpp:1029
void set_note(std::string note)
Set the note object.
Definition rcpp_distribution.hpp:1022
virtual bool set_observed_data(int observed_data_id)
Set the unique ID for the observed data object.
Definition rcpp_distribution.hpp:994
VariableVector expected_values
The expected values, which should be a vector of length K where each value specifies the probability ...
Definition rcpp_distribution.hpp:939
RealVector dims
The dimensions of the number of rows and columns of the multivariate dataset.
Definition rcpp_distribution.hpp:944
DmultinomDistributionsInterface()
The constructor.
Definition rcpp_distribution.hpp:960
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_distribution.hpp:1112
RealVector lpdf_vec
Vector that records the individual log probability function for each observation.
Definition rcpp_distribution.hpp:949
DmultinomDistributionsInterface(const DmultinomDistributionsInterface &other)
Construct a new Dmultinom Distributions Interface object.
Definition rcpp_distribution.hpp:972
VariableVector observed_values
Observed data, which should be a vector of length K of integers.
Definition rcpp_distribution.hpp:933
virtual ~DmultinomDistributionsInterface()
The destructor.
Definition rcpp_distribution.hpp:983
The Rcpp interface for Dnorm to instantiate from R: dnorm_ <- methods::new(DnormDistribution).
Definition rcpp_distribution.hpp:240
DnormDistributionsInterface(const DnormDistributionsInterface &other)
Construct a new Dnorm Distributions Interface object.
Definition rcpp_distribution.hpp:282
DnormDistributionsInterface()
The constructor.
Definition rcpp_distribution.hpp:270
virtual std::string to_json()
Converts the data to json representation for the output.
Definition rcpp_distribution.hpp:452
virtual ~DnormDistributionsInterface()
The destructor.
Definition rcpp_distribution.hpp:293
RealVector lpdf_vec
Vector that records the individual log probability function for each observation.
Definition rcpp_distribution.hpp:265
virtual double evaluate()
Evaluate normal probability density function (pdf). The natural log of the pdf is returned.
Definition rcpp_distribution.hpp:339
VariableVector observed_values
Observed data.
Definition rcpp_distribution.hpp:245
VariableVector expected_values
The expected values, which would be the mean of x for this distribution.
Definition rcpp_distribution.hpp:250
virtual void finalize()
Extracts the derived quantities from Information to the Rcpp object.
Definition rcpp_distribution.hpp:365
VariableVector log_sd
The uncertainty, which would be the standard deviation of x for the normal distribution.
Definition rcpp_distribution.hpp:260
virtual bool set_observed_data(int observed_data_id)
Set the unique ID for the observed data object.
Definition rcpp_distribution.hpp:305
virtual bool set_distribution_mean(double input_value)
Set the expected mean value for the distribution.
Definition rcpp_distribution.hpp:313
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:323
virtual uint32_t get_id()
Gets the ID of the interface base object.
Definition rcpp_distribution.hpp:299
VariableVector expected_mean
The expected mean, which would be the mean of x for this distribution.
Definition rcpp_distribution.hpp:255
Base class for all interface objects.
Definition rcpp_interface_base.hpp:655
bool finalized
Is the object already finalized? The default is false.
Definition rcpp_interface_base.hpp:660
std::string value_to_string(double value)
Report the variable value as a string.
Definition rcpp_interface_base.hpp:696
static std::vector< std::shared_ptr< FIMSRcppInterfaceBase > > fims_interface_objects
FIMS interface object vectors.
Definition rcpp_interface_base.hpp:665
virtual bool add_to_fims_tmb()
A virtual method to inherit to add objects to the TMB model.
Definition rcpp_interface_base.hpp:670
An Rcpp interface class that defines the RealVector class.
Definition rcpp_interface_base.hpp:431
size_t size()
Returns the size of a RealVector.
Definition rcpp_interface_base.hpp:602
void resize(size_t size)
Resizes a RealVector to the desired length.
Definition rcpp_interface_base.hpp:609
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
An Rcpp interface class that defines the VariableVector class.
Definition rcpp_interface_base.hpp:143
Variable & get(size_t pos)
An internal accessor for calling a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:266
uint32_t id_m
The local ID of the Variable object.
Definition rcpp_interface_base.hpp:156
void set(size_t pos, const Variable &p)
An internal setter for setting a position of a VariableVector from R.
Definition rcpp_interface_base.hpp:282
void resize(size_t size)
Resizes a VariableVector to the desired length.
Definition rcpp_interface_base.hpp:294
size_t size()
Returns the size of a VariableVector.
Definition rcpp_interface_base.hpp:287
void resize(size_t s)
Changes the number of elements stored.
Definition fims_vector.hpp:401
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > > density_components
Definition information.hpp:129
std::map< uint32_t, std::shared_ptr< fims_distributions::DensityComponentBase< Type > > >::iterator density_components_iterator
Definition information.hpp:134
static std::shared_ptr< Information< Type > > GetInstance()
Returns a singleton Information object for type T.
Definition information.hpp:262
#define FIMS_WARNING_LOG(MESSAGE)
Definition def.hpp:648
#define FIMS_ERROR_LOG(MESSAGE)
Definition def.hpp:664
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:112
std::string use_mean
If "yes", expected_mean is used instead of expected_values. The default is "no" leading to the use of...
Definition density_components_base.hpp:72
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
fims::Vector< Type > expected_values
Expected value vector for prior-based pathways.
Definition density_components_base.hpp:44
fims::Vector< Type > expected_mean
The expected mean of the distribution; overrides expected values.
Definition density_components_base.hpp:66
Implements the LogNormalLPDF distribution functor used by FIMS to evaluate observation-level and tota...
Definition lognormal_lpdf.hpp:34
fims::Vector< Type > log_sd
Natural log of the standard deviation of the distribution on the log scale. The argument can be a vec...
Definition lognormal_lpdf.hpp:41
virtual const Type evaluate()
Evaluates the lognormal log probability density function.
Definition lognormal_lpdf.hpp:62
Implements the MultinomialLPMF distribution functor used by FIMS to evaluate the observation-level an...
Definition multinomial_lpmf.hpp:36
virtual const Type evaluate()
Evaluates the multinomial log probability mass function.
Definition multinomial_lpmf.hpp:62
fims::Vector< size_t > dims
Dimensions of the number of rows and columns of the multivariate dataset.
Definition multinomial_lpmf.hpp:41
Implements the NormalLPDF distribution functor used by FIMS to evaluate observation-level and total l...
Definition normal_lpdf.hpp:32
fims::Vector< Type > log_sd
The natural log of the standard deviation of the distribution. The argument can be a vector or scalar...
Definition normal_lpdf.hpp:39
virtual const Type evaluate()
Evaluates the normal log probability density function.
Definition normal_lpdf.hpp:59