Microsimulation API

microsimulation.h
Go to the documentation of this file.
1
30 #ifndef MICROSIMULATION_H
31 #define MICROSIMULATION_H
32
33 // #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
34 // #pragma GCC diagnostic ignored "-Wlong-long"
35
36 #include <RcppCommon.h>
37 #include <boost/tuple/tuple.hpp>
38 #include <boost/tuple/tuple_comparison.hpp>
39 #include <boost/unordered_map.hpp>
40 #include <vector>
41 #include <utility>
42 #include <map>
43 #include <string>
44
45 // http://stackoverflow.com/questions/3611951/building-an-unordered-map-with-tuples-as-keys
46 namespace boost {
47 namespace tuples { // fixed cf. the source URL
48 namespace detail {
49 template <class Tuple, size_t Index = length<Tuple>::value - 1>
50 struct HashValueImpl
51 {
52 static void apply(size_t& seed, Tuple const& tuple)
53 {
54 HashValueImpl<Tuple, Index-1>::apply(seed, tuple);
55 boost::hash_combine(seed, get<Index>(tuple));
56 }
57 };
58 template <class Tuple>
59 struct HashValueImpl<Tuple,0>
60 {
61 static void apply(size_t& seed, Tuple const& tuple)
62 {
63 boost::hash_combine(seed, get<0>(tuple));
64 }
65 };
66 } // namespace detail
67 template <class Tuple>
68 size_t hash_value(Tuple const& tuple)
69 {
70 size_t seed = 0;
71 detail::HashValueImpl<Tuple>::apply(seed, tuple);
72 return seed;
73 }
74 }
75 }
76
77 namespace Rcpp {
78
79 // vectors of pairs: recursively defined
80 template <class T1, class T2>
81 SEXP wrap(const std::vector<std::pair<T1,T2> > v);
82
83 // vectors tuples: enumerated cases
84 template <class T1, class T2>
85 SEXP wrap(const std::vector<boost::tuple<T1,T2> > v);
86
87 template <class T1, class T2, class T3>
88 SEXP wrap(const std::vector<boost::tuple<T1,T2,T3> > v);
89
90 template <class T1, class T2, class T3, class T4>
91 SEXP wrap(const std::vector<boost::tuple<T1,T2,T3,T4> > v);
92
93 template <class T1, class T2, class T3, class T4, class T5>
94 SEXP wrap(const std::vector<boost::tuple<T1,T2,T3,T4,T5> > v);
95
96 template <class T1, class T2, class T3, class T4, class T5, class T6>
97 SEXP wrap(const std::vector<boost::tuple<T1,T2,T3,T4,T5,T6> > v);
98
99 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
100 SEXP wrap(const std::vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7> > v);
101
102 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
103 SEXP wrap(const std::vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8> > v);
104
105 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
106 SEXP wrap(const std::vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> > v);
107
108 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10>
109 SEXP wrap(const std::vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> > v);
110
111 // maps defined in terms of vectors
112 template <class T1, class T2>
113 SEXP wrap_map(const std::map<T1,T2> v);
114
115 template <class T1a, class T1b, class T2>
116 SEXP wrap_map(const std::map<std::pair<T1a,T1b>,T2> v,
117 std::string key, std::string name1, std::string name2);
118
119 template <class T1a, class T1b, class T1c, class T2>
120 SEXP wrap_map(const std::map<boost::tuple<T1a,T1b,T1c>,T2> v,
121 std::string key, std::string name1, std::string name2, std::string name3);
122
123 template <class T1, class T2, class T3, class T4, class T5>
124 SEXP wrap_map(const std::map<std::pair<boost::tuple<T1,T2,T3>,T4>,T5> v,
125 std::string key, std::string name1, std::string name2);
126
127 // unordered_maps defined in terms of vectors
128 template <class T1, class T2>
129 SEXP wrap_map(const boost::unordered_map<T1,T2> v);
130
131 template <class T1a, class T1b, class T2>
132 SEXP wrap_map(const boost::unordered_map<std::pair<T1a,T1b>,T2> v,
133 std::string key, std::string name1, std::string name2);
134
135 template <class T1a, class T1b, class T1c, class T2>
136 SEXP wrap_map(const boost::unordered_map<boost::tuple<T1a,T1b,T1c>,T2> v,
137 std::string key, std::string name1, std::string name2, std::string name3);
138
139 template <class T1, class T2, class T3, class T4, class T5>
140 SEXP wrap_map(const boost::unordered_map<std::pair<boost::tuple<T1,T2,T3>,T4>,T5> v,
141 std::string key, std::string name1, std::string name2);
142
143
144 } // namespace Rcpp
145
146 #include <Rcpp.h>
147 #include <R.h>
148 #include <Rmath.h>
149 #include <Rdefines.h>
150 #include <R_ext/Random.h>
151
152 #include <siena/ssim.h>
153 #include <RngStream.h>
154 #include <rcpp_table.h>
155
156 #include <string>
157 #include <algorithm>
158 //#include <map>
159 #include <functional>
160 #include <set>
161
162 #include <boost/bind.hpp>
163 #include <boost/functional.hpp>
164
165 namespace ssim {
166
167 using std::string;
168 using std::vector;
169 using std::map;
170 using std::pair;
171 using std::greater;
172
173 // forward declarations
174 class cProcess;
175
180 #define WithRNG(rng,expr) (rng->set(), expr)
181
191 class cMessage : public ssim::Event {
192 public:
193 cMessage(const short k = -1, const string n = "") : kind(k), name(n), sendingTime(-1.0), timestamp(0) { }
194 // currently no setters (keep it lightweight?)
195 short getKind() { return kind; }
196 string getName() { return name; }
197 Time getTimestamp() { return timestamp; }
198 Time getSendingTime() {return sendingTime; }
199 short kind;
200 string name;
201 Time sendingTime, timestamp;
202 string str() const {
203 std::ostringstream stringStream;
204 stringStream << "kind=";
205 stringStream << kind;
206 stringStream << ",name=";
207 stringStream << name;
208 string str = stringStream.str();
209 return str;
210 }
211 // this does NOT include schedulePriority
212 };
213
214 inline bool cMessagePred(const ssim::Event* e, boost::function<bool(const cMessage * msg)> pred) {
215 const cMessage * msg = dynamic_cast<const cMessage *>(e);
216 return (msg != 0 && pred(msg));
217 }
218
219 inline bool cMessageNamePred(const ssim::Event* e, const string s) {
220 const cMessage * msg = dynamic_cast<const cMessage *>(e);
221 return (msg != 0 && msg->name == s);
222 }
223
224 inline bool cMessageKindPred(const ssim::Event* e, const short k) {
225 const cMessage * msg = dynamic_cast<const cMessage *>(e);
226 return (msg != 0 && msg->kind == k);
227 }
228
229
236 class cProcess : public ssim::Process {
237 public:
238 cProcess(Time startTime = Time(0.0)) : startTime(startTime), previousEventTime(startTime) { }
239 virtual void handleMessage(const cMessage * msg) = 0;
240 virtual void process_event(const ssim::Event * e) { // virtual or not?
241 const cMessage * msg;
242 if ((msg = dynamic_cast<const cMessage *>(e)) != 0) {
243 handleMessage(msg);
244 previousEventTime = Sim::clock();
245 } else {
246 // cf. cerr, specialised for R
247 REprintf("cProcess is only written to receive cMessage events\n");
248 }
249 }
250 virtual void scheduleAt(Time t, cMessage * msg) { // virtual or not?
251 msg->timestamp = t;
252 msg->sendingTime = Sim::clock();
253 Sim::self_signal_event(msg, t - Sim::clock());
254 }
255 virtual void scheduleAt(Time t, string s) {
256 scheduleAt(t, new cMessage(-1,s));
257 }
258 virtual void scheduleAt(Time t, short k) {
259 scheduleAt(t, new cMessage(k,""));
260 }
261 virtual void init() = 0;
262 void initialize() { init(); previousEventTime = startTime; }
263 Time startTime, previousEventTime;
264 };
265
269 inline void RemoveKind(short kind) {
270 Sim::ignore_event(boost::bind(cMessageKindPred,_1,kind));
271 }
272
276 inline void RemoveName(string name) {
277 Sim::ignore_event(boost::bind(cMessageNamePred,_1,name));
278 }
279
280
284 typedef Time simtime_t;
285
289 Time simTime();
290
294 Time now();
295
300 class Means {
301 public:
302 double mean() { return _sum/_n; }
303 double var() {return (long double)_n/(_n-1)*(_sumsq/_n - mean()*mean()); }
304 int n() {return _n;}
305 double sum() {return _sum;}
306 double sd() {return sqrt(var()); }
307 Means() : _n(0), _sum(0.0), _sumsq(0.0) {}
308 Means* operator +=(const double value) {
309 _n++;
310 _sum += (long double) value;
311 _sumsq += (long double)value * (long double)value;
312 return this;
313 }
314 SEXP wrap() {
315 return Rcpp::DataFrame::create(_("n")=n(),
316 _("mean")=mean(),
317 _("var")=var(),
318 _("sd")=sd(),
319 _("se")=sd()/sqrt(n()),
320 _("sum")=sum(),
321 _("sumsq")=_sumsq
322 );
323 }
324 //friend std::ostream& operator<<(std::ostream& os, Means& m);
325 private:
326 int _n;
327 long double _sum, _sumsq;
328 };
329
336 class Rpexp {
337 public:
338 Rpexp() {} // blank default constructor
339 Rpexp(double *hin, double *tin, int nin) : n(nin) {
340 int i;
341 H.resize(n);
342 t.resize(n);
343 h.resize(n);
344 H[0]=0.0; h[0]=hin[0]; t[0]=tin[0];
345 if (n>1) {
346 for(i=1;i<n;i++) {
347 h[i]=hin[i]; t[i]=tin[i];
348 H[i] = H[i-1]+(t[i]-t[i-1])*h[i-1];
349 }
350 }
351 }
352 /* Rpexp(vector<double> hin, vector<double> tin) : h(hin), t(tin) { */
353 /* n = h.size(); */
354 /* H.resize(n); */
355 /* H[0] = 0.0; */
356 /* if (n>1) { */
357 /* for(i=1;i<n;i++) { */
358 /* H[i] = H[i-1]+(t[i]-t[i-1])*h[i-1]; */
359 /* } */
360 /* } */
361 /* } */
362 double rand(double u, double from = 0.0) {
363 double v = 0.0, H0 = 0.0, tstar = 0.0;
364 int i = 0, i0 = 0;
365 if (from > 0.0) {
366 i0 = (from >= t[n-1]) ? (n-1) : int(lower_bound(t.begin(), t.end(), from) - t.begin())-1;
367 H0 = H[i0] + (from - t[i0])*h[i0];
368 }
369 v = -log(u) + H0;
370 i = (v >= H[n-1]) ? (n-1) : int(lower_bound(H.begin(), H.end(), v) - H.begin())-1;
371 tstar = t[i]+(v-H[i])/h[i];
372 return tstar;
373 }
374
375 private:
376 vector<double> H, h, t;
377 int n;
378 };
379
380
384 double rweibullHR(double shape, double scale, double hr);
385
386
392 static int counter_id = 0;
393 class Rng : public RngStream {
394 public:
395 typedef double result_type;
396 result_type operator()() { return RandU01(); }
397 result_type min() { return 0.0; }
398 result_type max() { return 1.0; }
399 Rng() : RngStream() { id = ++counter_id; }
400 virtual ~Rng();
401 void seed(const double seed[6]) {
402 SetSeed(seed);
403 }
404 void set();
405 void nextSubstream() { ResetNextSubstream(); }
406 int id;
407 };
408
409
410 extern "C" { // functions that will be called from R
411
416 void r_create_current_stream();
417
422 void r_remove_current_stream();
423
427 void r_set_user_random_seed(double * seed);
428
432 void r_get_user_random_seed(double * seed);
433
434 /* /\** */
435 /* @brief A utility function to move to the next user random stream for the simulation. */
436 /* *\/ */
437 /* void r_next_rng_stream(); */
438
442 void r_next_rng_substream();
443
447 void r_rng_advance_substream(double * seed, int * n);
448
452 void test_rstream2(double * x);
453
454 } // extern "C"
455
456
461 inline double discountedInterval(double start, double end, double discountRate) {
462 if (discountRate == 0.0) return end - start;
463 //else if (start == 0.0) return (1.0 - (1.0+discountRate)^(-end)) / log(1.0+discountRate);
464 else return (pow(1.0+discountRate,-start) - pow(1.0+discountRate,-end)) / log(1.0+discountRate);
465 }
466
467
474 template <class T>
475 std::vector<std::vector<T> > transpose(const std::vector<std::vector<T> > data) {
476 std::vector<std::vector<T> > result(data[0].size(),
477 std::vector<T>(data.size()));
478 for (typename std::vector<T>::size_type i = 0; i < data[0].size(); i++)
479 for (typename std::vector<T>::size_type j = 0; j < data.size(); j++) {
480 result[i][j] = data[j][i];
481 }
482 return result;
483 }
484
485
489 template< class State, class Event, class Time = double, class Utility = double>
490 class EventReport {
491 public:
492 typedef std::set<Time, std::greater<Time> > Partition; // NB: greater<Time> cf. lesser<Time>
493 typedef typename Partition::iterator Iterator;
494 typedef std::pair<State,Time> Pair;
495 typedef boost::unordered_map<pair<State,Time>, int > PrevMap;
496 typedef boost::unordered_map<pair<State,Time>, Utility > UtilityMap;
497 typedef boost::unordered_map<pair<State,Time>, Time > PtMap;
498 typedef boost::unordered_map<boost::tuple<State,Event,Time>, int > EventsMap;
499 typedef vector<Utility> IndividualUtilities;
500 EventReport(Utility discountRate = 0.0, bool outputUtilities = true, int size = 1, Time startReportAge = Time(0), bool indiv = false) :
501 discountRate(discountRate), outputUtilities(outputUtilities), startReportAge(startReportAge), id(0), indiv(indiv) {
502 _vector.resize(size);
503 }
504 void resize(int size) {
505 _vector.resize(size);
506 }
507 void setPartition(const vector<Time> v) {
508 copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
509 }
510 void setPartition(const Time start, const Time finish, const Time delta,
511 const Time maxTime = Time(1.0e100)) {
512 _partition.clear();
513 for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
514 _partition.insert(maxTime);
515 }
516 void setIndivN(const int n) {
517 resize(n);
518 indiv = true;
519 }
520 void setStartReportAge(const Time a) {
521 startReportAge = a;
522 }
523 void clear() {
524 _ut.clear();
525 _pt.clear();
526 _events.clear();
527 _prev.clear();
528 _partition.clear();
529 _vector.clear();
530 current = Utility(0);
531 }
532 void individualReset () {
533 if (now() >= startReportAge)
534 mean_utilities += double(current);
535 if (indiv) {
536 if (id>=int(_vector.size()))
537 REprintf("Vector too small in EventReport: use resize(int) method");
538 else
539 _vector[id] = (now() >= startReportAge) ? double(current) : NA_REAL;
540 }
541 current = Utility(0);
542 id++;
543 }
544 Utility discountedUtilities(Time a, Time b, Utility utility = 1.0) {
545 if (discountRate == 0.0) return utility * (b-a);
546 else if (a==b) return 0.0;
547 else if (discountRate>0.0) {
548 double alpha = log(1.0+discountRate);
549 return utility/alpha*(exp(-a*alpha) - exp(-b*alpha));
550 }
551 else {
552 REprintf("discountRate less than zero.");
553 return 0.0;
554 }
555 }
556 void addBrief(const Time lhs, const Time rhs, const Utility utility = 1) {
557 if (rhs >= startReportAge)
558 current += discountedUtilities(std::max<Time>(lhs-startReportAge,Time(0)), rhs-startReportAge, utility);
559 }
560 void add(const State state, const Event event, const Time lhs, const Time rhs, const Utility utility = 1, int index = 0 /* deprecated argument */) {
561 addBrief(lhs, rhs, utility);
562 Iterator lo, hi, it, last;
563 lo = _partition.lower_bound(lhs);
564 hi = _partition.lower_bound(rhs);
565 last = _partition.begin(); // because it is ordered by greater<Time>
566 ++_events[boost::make_tuple(state,event,*hi)];
567 bool iterating;
568 for(it = lo, iterating = true; iterating; iterating = (it != hi), --it) { // decrement for greater<Time>
569 if (lhs<=(*it) && (*it)<rhs) // cadlag
570 ++_prev[Pair(state,*it)];
571 if (it == last) {
572 if (outputUtilities) {
573 Utility u = discountedUtilities(std::max<Time>(lhs,*it), rhs, utility);
574 _ut[Pair(state,*it)] += u;
575 }
576 _pt[Pair(state,*it)] += rhs - std::max<Time>(lhs,*it);
577 }
578 else {
579 Time next_value = *(--it); it++; // decrement/increment for greater<Time>
580 if (outputUtilities) {
581 Utility u = discountedUtilities(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), utility);
582 _ut[Pair(state,*it)] += u;
583 }
584 _pt[Pair(state,*it)] += std::min<Time>(rhs,next_value) - std::max<Time>(lhs,*it);
585 }
586 }
587 }
588 template<class T>
589 void append_map(T & base_map, T & new_map) {
590 typename T::iterator it;
591 for (it = new_map.begin(); it != new_map.end(); ++it)
592 base_map[it->first] += it->second;
593 }
594 void append(EventReport<State,Event,Time,Utility> & er) {
595 append_map<PrevMap>(_prev,er._prev);
596 append_map<EventsMap>(_events,er._events);
597 append_map<PtMap>(_pt,er._pt);
598 append_map<UtilityMap>(_ut,er._ut);
599 _vector.insert(_vector.end(), er._vector.begin(), er._vector.end());
600 }
601 SEXP wrap() {
602 using namespace Rcpp;
603 if (_events.size() == 0) return List::create();
604 else if (outputUtilities)
605 return List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
606 _("ut") = wrap_map<State,Time,Utility>(_ut,"Key","age","utility"),
607 _("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
608 _("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"));
609 else
610 return List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
611 _("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
612 _("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"));
613 }
614 SEXP wrap_indiv() {
615 return Rcpp::wrap(_vector);
616 }
617 SEXP wrap_means() {
618 return mean_utilities.wrap();
619 }
620 Utility discountRate, current;
621 bool outputUtilities;
622 Partition _partition;
623 PrevMap _prev;
624 UtilityMap _ut;
625 PtMap _pt;
626 EventsMap _events;
627 IndividualUtilities _vector;
628 Means mean_utilities;
629 Time startReportAge;
630 int id;
631 bool indiv;
632 };
633
637 template< class State, class Event = short, class Time = double, class Utility = double, class Cost = double>
638 class SummaryReport {
639 public:
640 typedef std::set<Time, std::greater<Time> > Partition; // NB: greater<Time> cf. lesser<Time>
641 typedef typename Partition::iterator Iterator;
642 typedef std::pair<State,Time> Pair;
643 typedef boost::unordered_map<pair<State,Time>, int > PrevMap;
644 typedef boost::unordered_map<pair<State,Time>, Utility > UtilityMap;
645 typedef boost::unordered_map<pair<State,Time>, Time > PtMap;
646 typedef boost::unordered_map<boost::tuple<State,Event,Time>, int > EventMap;
647 typedef boost::unordered_map<pair<State,Time>, Cost > CostMap;
648 typedef std::vector<Cost> IndividualCosts;
649 typedef std::vector<Utility> IndividualUtilities;
650 SummaryReport(int n = 1, bool indivp = true, Utility discountRate = 0.0) : n(n), indivp(indivp) {
651 resize(indivp ? n : 1);
652 setDiscountRate(discountRate);
653 setUtility(1.0);
654 setCost(0.0);
655 }
656 void resize(int size) {
657 _indivUtilities.resize(size);
658 _indivCosts.resize(size);
659 }
660 void setDiscountRate(Utility discountRate) {
661 setUtilityDiscountRate(discountRate);
662 setCostDiscountRate(discountRate);
663 }
664 void setUtilityDiscountRate(Utility discountRate) {
665 this->utilityDiscountRate = discountRate;
666 utilityAlpha = log(1.0+utilityDiscountRate);
667 }
668 void setCostDiscountRate(Cost discountRate) {
669 this->costDiscountRate = discountRate;
670 costAlpha = log(1.0+costDiscountRate);
671 }
672 void setPartition(const vector<Time> v) {
673 copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
674 }
675 void setPartition(const Time start, const Time finish, const Time delta,
676 const Time maxTime = Time(1.0e100)) {
677 _partition.clear();
678 for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
679 _partition.insert(maxTime);
680 }
681 void setUtility(Utility _utility) {
682 this->utility = _utility;
683 }
684 void setCost(Cost _cost) {
685 this->cost = _cost;
686 }
687 void clear() {
688 _ut.clear();
689 _pt.clear();
690 _events.clear();
691 _prev.clear();
692 _partition.clear();
693 _costs.clear();
694 _indivUtilities.clear();
695 _indivCosts.clear();
696 }
697 void add(const State state, const Event event, const Time lhs, const Time rhs, int index = 0) {
698 // Time lhs = process->previousEventTime;
699 // Time rhs = ssim::now();
700 if (!indivp) index = 0;
701 Iterator lo, hi, it, last;
702 lo = _partition.lower_bound(lhs);
703 hi = _partition.lower_bound(rhs);
704 last = _partition.begin(); // because it is ordered by greater<Time>
705 ++_events[boost::make_tuple(state,event,*hi)];
706 bool iterating;
707 for(it = lo, iterating = true; iterating; iterating = (it != hi), --it) { // decrement for greater<Time>
708 if (lhs<=(*it) && (*it)<rhs) // cadlag
709 ++_prev[Pair(state,*it)];
710 if (it == last) {
711 Utility u = discountedUtilityInterval(std::max<Time>(lhs,*it), rhs, utility);
712 _ut[Pair(state,*it)] += u;
713 _indivUtilities[index] += u;
714 Cost c = discountedCostInterval(std::max<Time>(lhs,*it), rhs, cost);
715 _costs[Pair(state,*it)] += c;
716 _indivCosts[index] += c;
717 _pt[Pair(state,*it)] += rhs - std::max<Time>(lhs,*it);
718 }
719 else {
720 Time next_value = *(--it); it++; // decrement/increment for greater<Time>
721 Utility u = discountedUtilityInterval(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), utility);
722 _ut[Pair(state,*it)] += u;
723 _indivUtilities[index] += u;
724 Cost c = discountedCostInterval(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), cost);
725 _costs[Pair(state,*it)] += c;
726 _indivCosts[index] += c;
727 _pt[Pair(state,*it)] += std::min<Time>(rhs,next_value) - std::max<Time>(lhs,*it);
728 }
729 }
730 }
731 // void addPointCost(const State state, const Time time, const Cost cost, const int index = 0) {
732 void addPointCost(const State state, const Cost cost, int index = 0) {
733 if (!indivp) index = 0;
734 Time time = ssim::now();
735 Time time_lhs = * _partition.lower_bound(time);
736 Cost c = discountedCost(time,cost);
737 _costs[Pair(state,time_lhs)] += c;
738 _indivCosts[index] += c;
739 }
740 void append(SummaryReport<State,Event,Time,Utility> & er) {
741 append_map<PrevMap>(_prev,er._prev);
742 append_map<EventMap>(_events,er._events);
743 append_map<PtMap>(_pt,er._pt);
744 append_map<UtilityMap>(_ut,er._ut);
745 append_map<CostMap>(_costs,er._costs);
746 _indivUtilities.insert(_indivUtilities.end(), er._indivUtilities.begin(), er._indivUtilities.end());
747 _indivCosts.insert(_indivCosts.end(), er._indivCosts.begin(), er._indivCosts.end());
748 }
749 Rcpp::List asList() {
750 using namespace Rcpp;
751 if (_events.size() == 0) return List::create();
752 Rcpp::DataFrame indiv =
753 Rcpp::DataFrame::create(_("utilities")=_indivUtilities,
754 _("costs")=_indivCosts);
755 Rcpp::List out = List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
756 _("ut") = wrap_map<State,Time,Utility>(_ut,"Key","age","utility"),
757 _("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
758 _("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"),
759 _("costs") = wrap_map<State,Time,Cost>(_costs,"Key","age","cost"),
760 _("n")=n,
761 _("indivp")=indivp,
762 _("utilityDiscountRate")=utilityDiscountRate,
763 _("costDiscountRate")=costDiscountRate,
764 _("indiv")=indiv);
765 out.attr("class")="SummaryReport";
766 return out;
767 }
768 Utility discountedUtilityInterval(Time a, Time b, Utility utility) {
769 if (a == b || utility == 0.0) return 0.0;
770 else if (utilityDiscountRate == 0.0) return utility * (b-a);
771 else if (utilityDiscountRate>0.0) {
772 return utility/utilityAlpha*(exp(-a*utilityAlpha) - exp(-b*utilityAlpha));
773 }
774 else {
775 REprintf("utilityDiscountRate less than zero: %f.\n", utilityDiscountRate);
776 return 0.0;
777 }
778 }
779 Cost discountedCostInterval(Time a, Time b, Cost cost) {
780 if (a == b || cost == 0.0) return 0.0;
781 else if (costDiscountRate == 0.0) return cost * (b-a);
782 else if (costDiscountRate>0.0) {
783 return cost/costAlpha*(exp(-a*costAlpha) - exp(-b*costAlpha));
784 }
785 else {
786 REprintf("costDiscountRate less than zero: %f.\n", costDiscountRate);
787 return 0.0;
788 }
789 }
790 Cost discountedCost(Time a, Cost cost) {
791 if (cost == 0.0) return 0.0;
792 else if (costDiscountRate == 0.0) return cost;
793 else if (costDiscountRate>0)
794 return cost*exp(-a*costAlpha);
795 else {
796 REprintf("costDiscountRate less than zero: %f.\n", costDiscountRate);
797 return 0;
798 }
799 }
800 template<class T>
801 void append_map(T & base_map, T & new_map) {
802 typename T::iterator it;
803 for (it = new_map.begin(); it != new_map.end(); ++it)
804 base_map[it->first] += it->second;
805 }
806 int n;
807 bool indivp;
808 Partition _partition;
809 PrevMap _prev;
810 UtilityMap _ut;
811 PtMap _pt;
812 EventMap _events;
813 CostMap _costs;
814 IndividualUtilities _indivUtilities;
815 IndividualCosts _indivCosts;
816 // cProcess *process;
817 Utility utilityDiscountRate, utilityAlpha, utility;
818 Cost costDiscountRate, costAlpha, cost;
819 };
820
824 template< class State, class Time = double, class Cost = double>
825 class CostReport {
826 public:
827 typedef std::set<Time, std::greater<Time> > Partition;
828 typedef std::pair<State,Time> Pair;
829 typedef CostReport<State,Time,Cost> This;
830 typedef boost::unordered_map<pair<State,Time>, Cost > Table;
831 typedef std::vector<Cost> IndividualCosts;
832 CostReport(Cost discountRate = 0, int size = 1, Time startReportAge = Time(0), bool indiv = false) : discountRate(discountRate), startReportAge(startReportAge), id(0), indiv(indiv) {
833 _vector.resize(size);
834 }
835 void individualReset () {
836 if (now() >= startReportAge)
837 mean_costs += double(current);
838 if (indiv) {
839 if (id>=int(_vector.size()))
840 REprintf("Vector too small in CostReport: use resize(int) method");
841 else
842 _vector[id] = (now() >= startReportAge) ? double(current) : NA_REAL;
843 }
844 current = Cost(0);
845 id++;
846 }
847 void setIndivN(const int n) {
848 resize(n);
849 indiv = true;
850 }
851 void setStartReportAge(const Time a) {
852 startReportAge = a;
853 }
854 Cost discountedCost(Time a, Cost cost) {
855 if (discountRate == 0.0) return cost;
856 else if (discountRate>0.0)
857 return cost/pow(1+discountRate,a);
858 else {
859 REprintf("discountRate less than zero.");
860 return 0;
861 }
862 }
863 void setPartition(const vector<Time> v) {
864 copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
865 }
866 void setPartition(const Time start, const Time finish, const Time delta,
867 const Time maxTime = Time(1.0e100)) {
868 _partition.clear();
869 for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
870 _partition.insert(maxTime);
871 }
872 void clear() {
873 _table.clear();
874 _partition.clear();
875 _vector.clear();
876 current = Cost(0);
877 }
878 void resize(int size) {
879 _vector.resize(size);
880 }
881 void append(This & new_report) { // assuming that discountRate and _partition are the same for both reports
882 typename Table::iterator it;
883 _vector.insert(_vector.start(), new_report._vector.begin(), new_report._vector.end());
884 for(it = new_report._table.begin(); it != new_report._table.end(); ++it) {
885 _table[it->first] += it->second;
886 }
887 }
888 void add(const State state, const Time time, const Cost cost, const int index = 0 /* deprecated argument */) {
889 Time time_lhs = * _partition.lower_bound(time);
890 Cost c = discountedCost(time,cost);
891 _table[Pair(state,time_lhs)] += c;
892 if (startReportAge == 0.0)
893 current += c;
894 else
895 if (time>=startReportAge)
896 current += discountedCost(time-startReportAge,cost);
897 }
898 SEXP wrap() {
899 return Rcpp::wrap_map(_table,"Key","age","cost");
900 }
901 SEXP wrap_indiv() {
902 return Rcpp::wrap(_vector);
903 }
904 SEXP wrap_means() {
905 return mean_costs.wrap();
906 }
907 Cost discountRate, current;
908 Partition _partition;
909 Table _table;
910 IndividualCosts _vector;
911 Means mean_costs;
912 Time startReportAge;
913 int id;
914 bool indiv;
915 };
916
920 template<class T = double>
921 class SimpleReport {
922 public:
926 typedef map<string,vector<T> > Map;
930 void record(string field, T value) {
931 _data[field].push_back(value);
932 }
937 void revise(string field, T value) {
938 if (!_data[field].empty())
939 _data[field].pop_back();
940 _data[field].push_back(value);
941 }
945 void clear() { _data.clear(); }
949 SEXP wrap() {
950 return Rcpp::wrap(_data);
951 }
955 void append(SimpleReport<T> & obj) {
956 for(typename Map::iterator it = obj._data.begin(); it != obj._data.end(); ++it) {
957 _data[it->first].insert(_data[it->first].end(), it->second.begin(), it->second.end());
958 }
959 }
963 Map _data;
964 };
965
966 } // namespace ssim
967
968 namespace Rcpp {
969
970 template <class T1, class T2>
971 SEXP wrap(const vector<pair<T1,T2> > v) {
972 vector<T1> v1;
973 vector<T2> v2;
974 typename vector<pair<T1,T2> >::const_iterator it;
975 for (it=v.begin(); it<v.end(); ++it) {
976 v1.push_back(it->first);
977 v2.push_back(it->second);
978 }
979 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2));
980 }
981
982 template <class T1, class T2>
983 SEXP wrap(const vector<boost::tuple<T1,T2> > v) {
984 int i, n=v.size();
985 vector<T1> v1(n);
986 vector<T2> v2(n);
987 typename vector<boost::tuple<T1,T2> >::const_iterator it;
988 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
989 v1[i] = boost::get<0>(*it);
990 v2[i] = boost::get<1>(*it);
991 }
992 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2));
993 }
994
995 template <class T1, class T2, class T3>
996 SEXP wrap(const vector<boost::tuple<T1,T2,T3> > v) {
997 int i, n=v.size();
998 vector<T1> v1(n);
999 vector<T2> v2(n);
1000 vector<T3> v3(n);
1001 typename vector<boost::tuple<T1,T2,T3> >::const_iterator it;
1002 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1003 v1[i] = boost::get<0>(*it);
1004 v2[i] = boost::get<1>(*it);
1005 v3[i] = boost::get<2>(*it);
1006 }
1007 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),_("Var3")=wrap(v3));
1008 }
1009
1010
1011 template <class T1, class T2, class T3, class T4>
1012 SEXP wrap(const vector<boost::tuple<T1,T2,T3,T4> > v) {
1013 int i, n=v.size();
1014 vector<T1> v1(n);
1015 vector<T2> v2(n);
1016 vector<T3> v3(n);
1017 vector<T4> v4(n);
1018 typename vector<boost::tuple<T1,T2,T3,T4> >::const_iterator it;
1019 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1020 v1[i] = boost::get<0>(*it);
1021 v2[i] = boost::get<1>(*it);
1022 v3[i] = boost::get<2>(*it);
1023 v4[i] = boost::get<3>(*it);
1024 }
1025 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1026 _("Var3")=wrap(v3),_("Var4")=wrap(v4));
1027 }
1028
1029 template <class T1, class T2, class T3, class T4, class T5>
1030 SEXP wrap(const vector<boost::tuple<T1,T2,T3,T4,T5> > v) {
1031 int i, n=v.size();
1032 vector<T1> v1(n);
1033 vector<T2> v2(n);
1034 vector<T3> v3(n);
1035 vector<T4> v4(n);
1036 vector<T5> v5(n);
1037 typename vector<boost::tuple<T1,T2,T3,T4,T5> >::const_iterator it;
1038 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1039 v1[i] = boost::get<0>(*it);
1040 v2[i] = boost::get<1>(*it);
1041 v3[i] = boost::get<2>(*it);
1042 v4[i] = boost::get<3>(*it);
1043 v5[i] = boost::get<4>(*it);
1044 }
1045 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1046 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1047 _("Var5")=wrap(v5));
1048 }
1049
1050 template <class T1, class T2, class T3, class T4, class T5, class T6>
1051 SEXP wrap(const vector<boost::tuple<T1,T2,T3,T4,T5,T6> > v) {
1052 int i, n=v.size();
1053 vector<T1> v1(n);
1054 vector<T2> v2(n);
1055 vector<T3> v3(n);
1056 vector<T4> v4(n);
1057 vector<T5> v5(n);
1058 vector<T6> v6(n);
1059 typename vector<boost::tuple<T1,T2,T3,T4,T5,T6> >::const_iterator it;
1060 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1061 v1[i] = boost::get<0>(*it);
1062 v2[i] = boost::get<1>(*it);
1063 v3[i] = boost::get<2>(*it);
1064 v4[i] = boost::get<3>(*it);
1065 v5[i] = boost::get<4>(*it);
1066 v6[i] = boost::get<5>(*it);
1067 }
1068 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1069 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1070 _("Var5")=wrap(v5),_("Var6")=wrap(v6));
1071 }
1072
1073 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
1074 SEXP wrap(const vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7> > v) {
1075 int i, n=v.size();
1076 vector<T1> v1(n);
1077 vector<T2> v2(n);
1078 vector<T3> v3(n);
1079 vector<T4> v4(n);
1080 vector<T5> v5(n);
1081 vector<T6> v6(n);
1082 vector<T7> v7(n);
1083 typename vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7> >::const_iterator it;
1084 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1085 v1[i] = boost::get<0>(*it);
1086 v2[i] = boost::get<1>(*it);
1087 v3[i] = boost::get<2>(*it);
1088 v4[i] = boost::get<3>(*it);
1089 v5[i] = boost::get<4>(*it);
1090 v6[i] = boost::get<5>(*it);
1091 v7[i] = boost::get<6>(*it);
1092 }
1093 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1094 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1095 _("Var5")=wrap(v5),_("Var6")=wrap(v6),
1096 _("Var7")=wrap(v7));
1097 }
1098
1099 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
1100 SEXP wrap(const vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8> > v) {
1101 int i, n=v.size();
1102 vector<T1> v1(n);
1103 vector<T2> v2(n);
1104 vector<T3> v3(n);
1105 vector<T4> v4(n);
1106 vector<T5> v5(n);
1107 vector<T6> v6(n);
1108 vector<T7> v7(n);
1109 vector<T8> v8(n);
1110 typename vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8> >::const_iterator it;
1111 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1112 v1[i] = boost::get<0>(*it);
1113 v2[i] = boost::get<1>(*it);
1114 v3[i] = boost::get<2>(*it);
1115 v4[i] = boost::get<3>(*it);
1116 v5[i] = boost::get<4>(*it);
1117 v6[i] = boost::get<5>(*it);
1118 v7[i] = boost::get<6>(*it);
1119 v8[i] = boost::get<7>(*it);
1120 }
1121 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1122 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1123 _("Var5")=wrap(v5),_("Var6")=wrap(v6),
1124 _("Var7")=wrap(v7),_("Var8")=wrap(v8));
1125 }
1126
1127 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
1128 SEXP wrap(const vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> > v) {
1129 int i, n=v.size();
1130 vector<T1> v1(n);
1131 vector<T2> v2(n);
1132 vector<T3> v3(n);
1133 vector<T4> v4(n);
1134 vector<T5> v5(n);
1135 vector<T6> v6(n);
1136 vector<T7> v7(n);
1137 vector<T8> v8(n);
1138 vector<T9> v9(n);
1139 typename vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> >::const_iterator it;
1140 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1141 v1[i] = boost::get<0>(*it);
1142 v2[i] = boost::get<1>(*it);
1143 v3[i] = boost::get<2>(*it);
1144 v4[i] = boost::get<3>(*it);
1145 v5[i] = boost::get<4>(*it);
1146 v6[i] = boost::get<5>(*it);
1147 v7[i] = boost::get<6>(*it);
1148 v8[i] = boost::get<7>(*it);
1149 v9[i] = boost::get<8>(*it);
1150 }
1151 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1152 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1153 _("Var5")=wrap(v5),_("Var6")=wrap(v6),
1154 _("Var7")=wrap(v7),_("Var8")=wrap(v8),
1155 _("Var9")=wrap(v9));
1156 }
1157
1158 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10>
1159 SEXP wrap(const vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> > v) {
1160 int i, n=v.size();
1161 vector<T1> v1(n);
1162 vector<T2> v2(n);
1163 vector<T3> v3(n);
1164 vector<T4> v4(n);
1165 vector<T5> v5(n);
1166 vector<T6> v6(n);
1167 vector<T7> v7(n);
1168 vector<T8> v8(n);
1169 vector<T9> v9(n);
1170 vector<T10> v10(n);
1171 typename vector<boost::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> >::const_iterator it;
1172 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1173 v1[i] = boost::get<0>(*it);
1174 v2[i] = boost::get<1>(*it);
1175 v3[i] = boost::get<2>(*it);
1176 v4[i] = boost::get<3>(*it);
1177 v5[i] = boost::get<4>(*it);
1178 v6[i] = boost::get<5>(*it);
1179 v7[i] = boost::get<6>(*it);
1180 v8[i] = boost::get<7>(*it);
1181 v9[i] = boost::get<8>(*it);
1182 v10[i] = boost::get<9>(*it);
1183 }
1184 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1185 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1186 _("Var5")=wrap(v5),_("Var6")=wrap(v6),
1187 _("Var7")=wrap(v7),_("Var8")=wrap(v8),
1188 _("Var9")=wrap(v9),_("Var10")=wrap(v10));
1189 }
1190
1191
1192 template <class T1, class T2>
1193 SEXP wrap_map(const std::map<T1,T2> v) {
1194 int i;
1195 int n = v.size();
1196 vector<T1> x(n);
1197 vector<T2> y(n);
1198 typename std::map<T1,T2>::const_iterator it;
1199 for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
1200 x[i] = (*it).first;
1201 y[i] = (*it).second;
1202 }
1203 return DataFrame::create(_("Key")=wrap(x),_("Value")=wrap(y));
1204 }
1205
1206 // Special cases for wrap_map:
1207 //
1208 // map<pair<Tstate,T>, int > _prev;
1209 // map<pair<Tstate,T>, T > _pt;
1210 // map<boost::tuple<Tstate,Tevent,T>, int > _events;
1211
1212 template <class T1a, class T1b, class T1c, class T2>
1213 SEXP wrap_map(const std::map<boost::tuple<T1a,T1b,T1c>,T2> v,
1214 std::string key, std::string name1, std::string name2, std::string name3) {
1215 typedef boost::tuple<T1a,T1b,T1c> Tuple;
1216 int i;
1217 int n = v.size();
1218 vector<T1a> xa(n);
1219 vector<T1b> xb(n);
1220 vector<T1c> xc(n);
1221 vector<T2> y(n);
1222 typename std::map<Tuple,T2>::const_iterator it;
1223 for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
1224 xa[i] = get<0>((*it).first);
1225 xb[i] = get<1>((*it).first);
1226 xc[i] = get<2>((*it).first);
1227 y[i] = (*it).second;
1228 }
1229 return Rcpp::DataFrame::create(_[key]=Rcpp::wrap(xa), _[name1]=Rcpp::wrap(xb),
1230 _[name2]=Rcpp::wrap(xc), _[name3]=Rcpp::wrap(y));
1231 }
1232
1233
1234 template <class T1a, class T1b, class T2>
1235 SEXP wrap_map(const std::map<std::pair<T1a,T1b>,T2> v,
1236 std::string key, std::string name1, std::string name2) {
1237 typedef std::pair<T1a,T1b> Pair;
1238 int i;
1239 int n = v.size();
1240 vector<T1a> xa(n);
1241 vector<T1b> xb(n);
1242 vector<T2> y(n);
1243 typename std::map<Pair,T2>::const_iterator it;
1244 for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
1245 xa[i] = (*it).first.first;
1246 xb[i] = (*it).first.second;
1247 y[i] = (*it).second;
1248 }
1249 return Rcpp::DataFrame::create(_[key]=Rcpp::wrap(xa), _[name1]=Rcpp::wrap(xb),
1250 _[name2]=Rcpp::wrap(y));
1251 }
1252
1253
1254 template <class T1, class T2>
1255 SEXP wrap_map(const boost::unordered_map<T1,T2> ov) {
1256 std::map<T1,T2> v(ov.begin(), ov.end());
1257 return wrap_map<T1,T2>(v);
1258 }
1259
1260 template <class T1a, class T1b, class T1c, class T2>
1261 SEXP wrap_map(const boost::unordered_map<boost::tuple<T1a,T1b,T1c>,T2> ov,
1262 std::string key, std::string name1, std::string name2, std::string name3) {
1263 std::map<boost::tuple<T1a,T1b,T1c>,T2> v(ov.begin(), ov.end());
1264 return wrap_map<T1a, T1b, T1c, T2>(v, key, name1, name2, name3);
1265 }
1266
1267 template <class T1a, class T1b, class T2>
1268 SEXP wrap_map(const boost::unordered_map<std::pair<T1a,T1b>,T2> ov,
1269 std::string key, std::string name1, std::string name2) {
1270 std::map<std::pair<T1a,T1b>,T2> v(ov.begin(), ov.end());
1271 return wrap_map<T1a, T1b, T2>(v, key, name1, name2);
1272 }
1273
1274 template <class T1, class T2, class T3, class T4, class T5>
1275 SEXP wrap_map(const std::map<std::pair<boost::tuple<T1,T2,T3>,T4>,T5> v,
1276 std::string name1, std::string name2) {
1277 typedef boost::tuple<T1,T2,T3> Tuple;
1278 typedef std::pair<Tuple,T4> Pair;
1279 int i;
1280 int n = v.size();
1281 vector<T1> x1(n);
1282 vector<T2> x2(n);
1283 vector<T3> x3(n);
1284 vector<T4> x4(n);
1285 vector<T5> y(n);
1286 typename std::map<Pair,T5>::const_iterator it;
1287 for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
1288 Tuple tuple = (*it).first.first;
1289 x1[i] = boost::get<0>(tuple);
1290 x2[i] = boost::get<1>(tuple);
1291 x3[i] = boost::get<2>(tuple);
1292 x4[i] = (*it).first.second;
1293 y[i] = (*it).second;
1294 }
1295 DataFrame out = Rcpp::DataFrame::create(Rcpp::Named("col1")=wrap(x1),
1296 Rcpp::Named("col2")=wrap(x2),
1297 Rcpp::Named("col3")=wrap(x3),
1298 Rcpp::Named(name1)=wrap(x4),
1299 Rcpp::Named(name2)=wrap(y));
1300 return out;
1301 }
1302
1303 template <class T1, class T2, class T3, class T4, class T5>
1304 SEXP wrap_map(const boost::unordered_map<std::pair<boost::tuple<T1,T2,T3>,T4>,T5> ov,
1305 std::string name1, std::string name2) {
1306 std::map<std::pair<boost::tuple<T1,T2,T3>,T4>,T5> v(ov.begin(), ov.end());
1307 return wrap_map<T1,T2,T3,T4,T5>(v, name1, name2);
1308 }
1309
1310
1311
1312 } // Rcpp namespace
1313
1314
1315 namespace R {
1320 double rnormPos(double mean, double sd);
1321
1326 double rllogis(double shape, double scale);
1332 double rllogis_trunc(double shape, double scale, double left);
1333
1334 }
1335
1336 #endif
ssim::EventReport::wrap
SEXP wrap()
Definition: microsimulation.h:601
boost::tuples::detail::HashValueImpl
Definition: microsimulation.h:50
ssim::CostReport::mean_costs
Means mean_costs
Definition: microsimulation.h:911
ssim::simtime_t
Time simtime_t
simtime_t typedef for OMNET++ API compatibility
Definition: microsimulation.h:284
ssim::CostReport::setPartition
void setPartition(const Time start, const Time finish, const Time delta, const Time maxTime=Time(1.0e100))
Definition: microsimulation.h:866
ssim::EventReport::mean_utilities
Means mean_utilities
Definition: microsimulation.h:628
ssim::EventReport::_pt
PtMap _pt
Definition: microsimulation.h:625
ssim::EventReport::setStartReportAge
void setStartReportAge(const Time a)
Definition: microsimulation.h:520
boost::tuples::hash_value
size_t hash_value(Tuple const &tuple)
Definition: microsimulation.h:68
ssim::EventReport::append
void append(EventReport< State, Event, Time, Utility > &er)
Definition: microsimulation.h:594
ssim::SummaryReport::PrevMap
boost::unordered_map< pair< State, Time >, int > PrevMap
Definition: microsimulation.h:643
ssim::EventReport::outputUtilities
bool outputUtilities
Definition: microsimulation.h:621
ssim::CostReport::_table
Table _table
Definition: microsimulation.h:909
ssim::Means::n
int n()
Definition: microsimulation.h:304
ssim::EventReport::IndividualUtilities
vector< Utility > IndividualUtilities
Definition: microsimulation.h:499
ssim::CostReport::IndividualCosts
std::vector< Cost > IndividualCosts
Definition: microsimulation.h:831
ssim::Means::Means
Means()
Definition: microsimulation.h:307
ssim::CostReport::_partition
Partition _partition
Definition: microsimulation.h:908
ssim::EventReport::append_map
void append_map(T &base_map, T &new_map)
Definition: microsimulation.h:589
ssim::CostReport::Pair
std::pair< State, Time > Pair
Definition: microsimulation.h:828
ssim::cMessage::cMessage
cMessage(const short k=-1, const string n="")
Definition: microsimulation.h:193
ssim::SummaryReport::Iterator
Partition::iterator Iterator
Definition: microsimulation.h:641
ssim::Rng::operator()
result_type operator()()
Definition: microsimulation.h:396
ssim::Sim::self_signal_event
static void self_signal_event(const Event *e)
signal an event to the current process immediately
Definition: ssim.cc:270
ssim::SummaryReport::_ut
UtilityMap _ut
Definition: microsimulation.h:810
ssim::CostReport::Table
boost::unordered_map< pair< State, Time >, Cost > Table
Definition: microsimulation.h:830
ssim::CostReport::wrap_indiv
SEXP wrap_indiv()
Definition: microsimulation.h:901
ssim::SummaryReport::addPointCost
void addPointCost(const State state, const Cost cost, int index=0)
Definition: microsimulation.h:732
ssim::CostReport
CostReport class for collecting statistics on costs.
Definition: microsimulation.h:825
it
GNU Free Documentation License March Inc Temple MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed PREAMBLE The purpose of this License is to make a or other written document free in the sense of with or without modifying it
Definition: fdl.txt:15
ssim::EventReport::addBrief
void addBrief(const Time lhs, const Time rhs, const Utility utility=1)
Definition: microsimulation.h:556
R
Definition: microsimulation.cc:106
ssim::Rng
Definition: microsimulation.h:393
ssim::Means::mean
double mean()
Definition: microsimulation.h:302
ssim::SummaryReport::clear
void clear()
Definition: microsimulation.h:687
ssim::cMessageKindPred
bool cMessageKindPred(const ssim::Event *e, const short k)
Definition: microsimulation.h:224
ssim::SummaryReport::resize
void resize(int size)
Definition: microsimulation.h:656
ssim::r_create_current_stream
void r_create_current_stream()
A utility function to create the current_stream. Used when initialising the microsimulation package i...
Definition: microsimulation.cc:37
ssim::CostReport::resize
void resize(int size)
Definition: microsimulation.h:878
ssim::CostReport::setIndivN
void setIndivN(const int n)
Definition: microsimulation.h:847
ssim::Means::sd
double sd()
Definition: microsimulation.h:306
ssim::SummaryReport::_pt
PtMap _pt
Definition: microsimulation.h:811
ssim::EventReport::Partition
std::set< Time, std::greater< Time > > Partition
Definition: microsimulation.h:492
ssim::CostReport::clear
void clear()
Definition: microsimulation.h:872
ssim::simTime
Time simTime()
simTime() function for OMNET++ API compatibility
Definition: microsimulation.cc:18
ssim::EventReport::_vector
IndividualUtilities _vector
Definition: microsimulation.h:627
ssim::Rng::seed
void seed(const double seed[6])
Definition: microsimulation.h:401
ssim::RngStream
Definition: RngStream.h:30
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:14
ssim::cProcess::handleMessage
virtual void handleMessage(const cMessage *msg)=0
ssim::SummaryReport::_costs
CostMap _costs
Definition: microsimulation.h:813
ssim::Means
Utility class to incrementally add values to calculate the mean, sum, variance and standard deviation...
Definition: microsimulation.h:300
ssim::SummaryReport::indivp
bool indivp
Definition: microsimulation.h:807
ssim::SummaryReport::asList
Rcpp::List asList()
Definition: microsimulation.h:749
ssim::Rng::Rng
Rng()
Definition: microsimulation.h:399
R::rnormPos
double rnormPos(double mean, double sd)
rnorm function constrained to be positive. This uses brute-force re-sampling rather than conditioning...
Definition: microsimulation.cc:107
rcpp_table.h
ssim::SummaryReport
SummaryReport class for collecting statistics on person-time, prevalence, events and costs.
Definition: microsimulation.h:638
ssim::Rpexp::t
vector< double > t
Definition: microsimulation.h:376
ssim::Means::_n
int _n
Definition: microsimulation.h:326
ssim::EventReport::add
void add(const State state, const Event event, const Time lhs, const Time rhs, const Utility utility=1, int index=0)
Definition: microsimulation.h:560
ssim::SummaryReport::append
void append(SummaryReport< State, Event, Time, Utility > &er)
Definition: microsimulation.h:740
ssim::SummaryReport::setPartition
void setPartition(const vector< Time > v)
Definition: microsimulation.h:672
ssim::SummaryReport::setUtilityDiscountRate
void setUtilityDiscountRate(Utility discountRate)
Definition: microsimulation.h:664
ssim::discountedInterval
double discountedInterval(double start, double end, double discountRate)
Simple function to calculate the integral between the start and end times for (1+kappa)^(-u),...
Definition: microsimulation.h:461
ssim::SimpleReport::clear
void clear()
clear the report data
Definition: microsimulation.h:945
ssim::EventReport::Pair
std::pair< State, Time > Pair
Definition: microsimulation.h:494
ssim::SummaryReport::SummaryReport
SummaryReport(int n=1, bool indivp=true, Utility discountRate=0.0)
Definition: microsimulation.h:650
ssim::SimpleReport::record
void record(string field, T value)
record adds a value for a given field into the SimpleReport
Definition: microsimulation.h:930
ssim::Means::_sumsq
long double _sumsq
Definition: microsimulation.h:327
Table
Definition: rcpp_table.h:148
ssim::cMessage::getKind
short getKind()
Definition: microsimulation.h:195
ssim::SummaryReport::setDiscountRate
void setDiscountRate(Utility discountRate)
Definition: microsimulation.h:660
ssim::SummaryReport::EventMap
boost::unordered_map< boost::tuple< State, Event, Time >, int > EventMap
Definition: microsimulation.h:646
ssim::Process
Virtual class (interface) representing processes running within the simulator.
Definition: ssim.h:130
ssim::SummaryReport::discountedCostInterval
Cost discountedCostInterval(Time a, Time b, Cost cost)
Definition: microsimulation.h:779
ssim::cMessage::sendingTime
Time sendingTime
Definition: microsimulation.h:201
ssim::SummaryReport::costDiscountRate
Cost costDiscountRate
Definition: microsimulation.h:818
copy
it can be used for any textual regardless of subject matter or whether it is published as a printed book We recommend this License principally for works whose purpose is instruction or reference APPLICABILITY AND DEFINITIONS This License applies to any manual or other work that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License The refers to any such manual or work Any member of the public is a and is addressed as you A Modified Version of the Document means any work containing the Document or a portion of either copied or with modifications and or translated into another language A Secondary Section is a named appendix or a front matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Document s overall if the Document is in part a textbook of a Secondary Section may not explain any mathematics The relationship could be a matter of historical connection with the subject or with related or of ethical or political position regarding them The Invariant Sections are certain Secondary Sections whose titles are as being those of Invariant in the notice that says that the Document is released under this License The Cover Texts are certain short passages of text that are as Front Cover Texts or Back Cover in the notice that says that the Document is released under this License A Transparent copy of the Document means a machine readable copy
Definition: fdl.txt:65
ssim::CostReport::setStartReportAge
void setStartReportAge(const Time a)
Definition: microsimulation.h:851
ssim::SummaryReport::discountedCost
Cost discountedCost(Time a, Cost cost)
Definition: microsimulation.h:790
ssim::Rpexp::h
vector< double > h
Definition: microsimulation.h:376
ssim::RemoveKind
void RemoveKind(short kind)
RemoveKind is a function to remove messages with the given kind from the queue (NB: void)
Definition: microsimulation.h:269
ssim::cProcess
cProcess class for OMNET++ API compatibility. This provides a default for Process::process_event() th...
Definition: microsimulation.h:236
ssim::SimpleReport::append
void append(SimpleReport< T > &obj)
append another SimpleReport, which is useful for aggregating multiple reports.
Definition: microsimulation.h:955
ssim::EventReport::wrap_means
SEXP wrap_means()
Definition: microsimulation.h:617
ssim::SummaryReport::Pair
std::pair< State, Time > Pair
Definition: microsimulation.h:642
ssim::cProcess::cProcess
cProcess(Time startTime=Time(0.0))
Definition: microsimulation.h:238
ssim::Rpexp::H
vector< double > H
Definition: microsimulation.h:376
ssim::EventReport::PrevMap
boost::unordered_map< pair< State, Time >, int > PrevMap
Definition: microsimulation.h:495
ssim::EventReport::individualReset
void individualReset()
Definition: microsimulation.h:532
ssim::EventReport::setIndivN
void setIndivN(const int n)
Definition: microsimulation.h:516
ssim::RngStream::RandU01
double RandU01()
Definition: RngStream.cpp:566
ssim::SummaryReport::setPartition
void setPartition(const Time start, const Time finish, const Time delta, const Time maxTime=Time(1.0e100))
Definition: microsimulation.h:675
ssim::cMessage::getTimestamp
Time getTimestamp()
Definition: microsimulation.h:197
ssim::EventReport::current
Utility current
Definition: microsimulation.h:620
boost
Definition: rngstream-boost.hpp:52
ssim::EventReport::_partition
Partition _partition
Definition: microsimulation.h:622
ssim::SummaryReport::CostMap
boost::unordered_map< pair< State, Time >, Cost > CostMap
Definition: microsimulation.h:647
ssim::EventReport::indiv
bool indiv
Definition: microsimulation.h:631
ssim::CostReport::add
void add(const State state, const Time time, const Cost cost, const int index=0)
Definition: microsimulation.h:888
ssim::CostReport::individualReset
void individualReset()
Definition: microsimulation.h:835
ssim::SummaryReport::add
void add(const State state, const Event event, const Time lhs, const Time rhs, int index=0)
Definition: microsimulation.h:697
ssim::RemoveName
void RemoveName(string name)
RemoveName is a function to remove messages with the given name from the queue (NB: void)
Definition: microsimulation.h:276
ssim::r_remove_current_stream
void r_remove_current_stream()
A utility function to remove the current_stream. Used when finalising the microsimulation package in ...
Definition: microsimulation.cc:43
ssim::cProcess::process_event
virtual void process_event(const ssim::Event *e)
action executed in response to an event signaled to this process.
Definition: microsimulation.h:240
ssim::cMessage::timestamp
Time timestamp
Definition: microsimulation.h:201
ssim::EventReport::EventsMap
boost::unordered_map< boost::tuple< State, Event, Time >, int > EventsMap
Definition: microsimulation.h:498
Rcpp
Definition: microsimulation.h:77
ssim::cProcess::scheduleAt
virtual void scheduleAt(Time t, string s)
Definition: microsimulation.h:255
ssim::r_set_user_random_seed
void r_set_user_random_seed(double *inseed)
A utility function to set the user random seed for the simulation.
Definition: microsimulation.cc:48
ssim::CostReport::discountedCost
Cost discountedCost(Time a, Cost cost)
Definition: microsimulation.h:854
ssim::Rpexp::Rpexp
Rpexp(double *hin, double *tin, int nin)
Definition: microsimulation.h:339
ssim::Sim::clock
static Time clock()
returns the current virtual time for the current process
Definition: ssim.cc:266
ssim::EventReport::discountedUtilities
Utility discountedUtilities(Time a, Time b, Utility utility=1.0)
Definition: microsimulation.h:544
Rcpp::wrap
SEXP wrap(const std::vector< std::pair< T1, T2 > > v)
ssim::SummaryReport::_events
EventMap _events
Definition: microsimulation.h:812
ssim::EventReport::_prev
PrevMap _prev
Definition: microsimulation.h:623
ssim::Rng::min
result_type min()
Definition: microsimulation.h:397
Rcpp::wrap_map
SEXP wrap_map(const boost::unordered_map< std::pair< boost::tuple< T1, T2, T3 >, T4 >, T5 > ov, std::string name1, std::string name2)
Definition: microsimulation.h:1304
ssim::CostReport::append
void append(This &new_report)
Definition: microsimulation.h:881
ssim::SummaryReport::n
int n
Definition: microsimulation.h:806
ssim::EventReport::discountRate
Utility discountRate
Definition: microsimulation.h:620
ssim::cProcess::scheduleAt
virtual void scheduleAt(Time t, cMessage *msg)
Definition: microsimulation.h:250
ssim::cMessage::str
string str() const
Definition: microsimulation.h:202
ssim::SummaryReport::utilityDiscountRate
Utility utilityDiscountRate
Definition: microsimulation.h:817
ssim::Means::_sum
long double _sum
Definition: microsimulation.h:327
ssim::SummaryReport::discountedUtilityInterval
Utility discountedUtilityInterval(Time a, Time b, Utility utility)
Definition: microsimulation.h:768
ssim::SummaryReport::UtilityMap
boost::unordered_map< pair< State, Time >, Utility > UtilityMap
Definition: microsimulation.h:644
ssim::SimpleReport::_data
Map _data
_data class member of a map from strings to vector<T>.
Definition: microsimulation.h:963
ssim::CostReport::id
int id
Definition: microsimulation.h:913
Rcpp::wrap_map
SEXP wrap_map(const std::map< T1, T2 > v)
Definition: microsimulation.h:1193
ssim::EventReport::Iterator
Partition::iterator Iterator
Definition: microsimulation.h:493
ssim::Means::sum
double sum()
Definition: microsimulation.h:305
ssim::EventReport::clear
void clear()
Definition: microsimulation.h:523
ssim::transpose
std::vector< std::vector< T > > transpose(const std::vector< std::vector< T > > data)
Function to transpose a vector of vectors. This assumes that all inner vectors have the same size and...
Definition: microsimulation.h:475
ssim::cProcess::init
virtual void init()=0
ssim::SummaryReport::append_map
void append_map(T &base_map, T &new_map)
Definition: microsimulation.h:801
ssim::EventReport::setPartition
void setPartition(const vector< Time > v)
Definition: microsimulation.h:507
ssim::Time
double Time
virtual time type
Definition: ssim.h:79
ssim::Event
basic event in the simulation.
Definition: ssim.h:111
ssim::SummaryReport::setUtility
void setUtility(Utility _utility)
Definition: microsimulation.h:681
ssim::SummaryReport::PtMap
boost::unordered_map< pair< State, Time >, Time > PtMap
Definition: microsimulation.h:645
ssim::SimpleReport
SimpleReport class for collecting data for homogeneous fields of type T with string names.
Definition: microsimulation.h:921
boost::tuples::detail::HashValueImpl< Tuple, 0 >::apply
static void apply(size_t &seed, Tuple const &tuple)
Definition: microsimulation.h:61
ssim::SummaryReport::IndividualCosts
std::vector< Cost > IndividualCosts
Definition: microsimulation.h:648
ssim::Rng::id
int id
Definition: microsimulation.h:406
ssim::cProcess::startTime
Time startTime
Definition: microsimulation.h:263
ssim::cMessage::getSendingTime
Time getSendingTime()
Definition: microsimulation.h:198
ssim::cProcess::scheduleAt
virtual void scheduleAt(Time t, short k)
Definition: microsimulation.h:258
ssim::SummaryReport::_prev
PrevMap _prev
Definition: microsimulation.h:809
ssim::EventReport::UtilityMap
boost::unordered_map< pair< State, Time >, Utility > UtilityMap
Definition: microsimulation.h:496
boost::tuples::detail::HashValueImpl::apply
static void apply(size_t &seed, Tuple const &tuple)
Definition: microsimulation.h:52
ssim::SummaryReport::setCost
void setCost(Cost _cost)
Definition: microsimulation.h:684
ssim::RngStream::SetSeed
bool SetSeed(const double seed[6])
Definition: RngStream.cpp:402
ssim::Rng::set
void set()
Definition: microsimulation.cc:31
ssim::CostReport::Partition
std::set< Time, std::greater< Time > > Partition
Definition: microsimulation.h:827
ssim::EventReport::startReportAge
Time startReportAge
Definition: microsimulation.h:629
RngStream.h
ssim::Rpexp
Rpexp is a random number generator class for piecewise constant hazards. Given time lower bounds t an...
Definition: microsimulation.h:336
ssim::cProcess::initialize
void initialize()
action executed when the process is initialized.
Definition: microsimulation.h:262
ssim::EventReport::setPartition
void setPartition(const Time start, const Time finish, const Time delta, const Time maxTime=Time(1.0e100))
Definition: microsimulation.h:510
ssim::SimpleReport::wrap
SEXP wrap()
wrap the report as a DataFrame or a List
Definition: microsimulation.h:949
ssim::SummaryReport::IndividualUtilities
std::vector< Utility > IndividualUtilities
Definition: microsimulation.h:649
ssim::SummaryReport::setCostDiscountRate
void setCostDiscountRate(Cost discountRate)
Definition: microsimulation.h:668
ssim::Rng::nextSubstream
void nextSubstream()
Definition: microsimulation.h:405
ssim::CostReport::wrap
SEXP wrap()
Definition: microsimulation.h:898
ssim::Rpexp::rand
double rand(double u, double from=0.0)
Definition: microsimulation.h:362
ssim::rweibullHR
double rweibullHR(double shape, double scale, double hr)
Random Weibull distribution for a given shape, scale and hazard ratio.
Definition: microsimulation.cc:10
ssim::SummaryReport::_indivCosts
IndividualCosts _indivCosts
Definition: microsimulation.h:815
ssim::r_rng_advance_substream
void r_rng_advance_substream(double *inoutseed, int *n)
A utility function to advance the random sub-stream n steps for a specified seed.
Definition: microsimulation.cc:69
ssim::RngStream::ResetNextSubstream
void ResetNextSubstream()
Definition: RngStream.cpp:381
ssim::CostReport::startReportAge
Time startReportAge
Definition: microsimulation.h:912
ssim::SummaryReport::_indivUtilities
IndividualUtilities _indivUtilities
Definition: microsimulation.h:814
ssim::cMessage::name
string name
Definition: microsimulation.h:200
ssim::EventReport::_ut
UtilityMap _ut
Definition: microsimulation.h:624
ssim::EventReport::id
int id
Definition: microsimulation.h:630
R::rllogis
double rllogis(double shape, double scale)
rllogis function for a random covariate from a log-logistic distribution with shape and scale....
Definition: microsimulation.cc:113
ssim::Rng::~Rng
virtual ~Rng()
Definition: microsimulation.cc:26
ssim::EventReport::PtMap
boost::unordered_map< pair< State, Time >, Time > PtMap
Definition: microsimulation.h:497
ssim::cMessage::kind
short kind
Definition: microsimulation.h:199
ssim::EventReport::resize
void resize(int size)
Definition: microsimulation.h:504
ssim::EventReport::wrap_indiv
SEXP wrap_indiv()
Definition: microsimulation.h:614
ssim::SummaryReport::_partition
Partition _partition
Definition: microsimulation.h:808
ssim::EventReport
EventReport class for collecting statistics on person-time, prevalence and numbers of events.
Definition: microsimulation.h:490
ssim::Rpexp::Rpexp
Rpexp()
Definition: microsimulation.h:338
ssim::r_next_rng_substream
void r_next_rng_substream()
A utility function to move to the next user random stream for the simulation.
Definition: microsimulation.cc:65
ssim::cProcess::previousEventTime
Time previousEventTime
Definition: microsimulation.h:263
ssim::cMessagePred
bool cMessagePred(const ssim::Event *e, boost::function< bool(const cMessage *msg)> pred)
Definition: microsimulation.h:214
ssim::EventReport::_events
EventsMap _events
Definition: microsimulation.h:626
ssim::test_rstream2
void test_rstream2(double *x)
Simple test of the random streams (with a stupid name)
Definition: microsimulation.cc:91
ssim::Means::var
double var()
Definition: microsimulation.h:303
ssim::CostReport::_vector
IndividualCosts _vector
Definition: microsimulation.h:910
ssim::Rng::max
result_type max()
Definition: microsimulation.h:398
ssim::SimpleReport::revise
void revise(string field, T value)
revise changes the last value in a given field. Reminder: std::map::operator[] creates an element for...
Definition: microsimulation.h:937
ssim::Means::wrap
SEXP wrap()
Definition: microsimulation.h:314
ssim::Rpexp::n
int n
Definition: microsimulation.h:377
ssim.h
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:8
ssim::EventReport::EventReport
EventReport(Utility discountRate=0.0, bool outputUtilities=true, int size=1, Time startReportAge=Time(0), bool indiv=false)
Definition: microsimulation.h:500
ssim::CostReport::This
CostReport< State, Time, Cost > This
Definition: microsimulation.h:829
ssim::SimpleReport::Map
map< string, vector< T > > Map
Map typedef for a map of strings to vector<T>
Definition: microsimulation.h:926
ssim::cMessage
cMessage class for OMNET++ API compatibility. This provides a heavier message class than Sim::Event,...
Definition: microsimulation.h:191
ssim::Rng::result_type
double result_type
Definition: microsimulation.h:395
ssim::CostReport::setPartition
void setPartition(const vector< Time > v)
Definition: microsimulation.h:863
ssim::cMessageNamePred
bool cMessageNamePred(const ssim::Event *e, const string s)
Definition: microsimulation.h:219
ssim::CostReport::discountRate
Cost discountRate
Definition: microsimulation.h:907
ssim::Means::operator+=
Means * operator+=(const double value)
Definition: microsimulation.h:308
ssim::cMessage::getName
string getName()
Definition: microsimulation.h:196
ssim::CostReport::CostReport
CostReport(Cost discountRate=0, int size=1, Time startReportAge=Time(0), bool indiv=false)
Definition: microsimulation.h:832
ssim::counter_id
static int counter_id
C++ wrapper class for the RngStream library. set() sets the current R random number stream to this st...
Definition: microsimulation.h:392
ssim::CostReport::indiv
bool indiv
Definition: microsimulation.h:914
ssim::r_get_user_random_seed
void r_get_user_random_seed(double *outseed)
A utility function to set the user random seed for the simulation.
Definition: microsimulation.cc:57
R::rllogis_trunc
double rllogis_trunc(double shape, double scale, double left)
rllogis_trunc function for a random covariate from a log-logistic distribution with shape and scale w...
Definition: microsimulation.cc:118
ssim::CostReport::wrap_means
SEXP wrap_means()
Definition: microsimulation.h:904
ssim::Sim::ignore_event
static void ignore_event(EventPredicate pred)
Definition: ssim.cc:133
ssim::SummaryReport::Partition
std::set< Time, std::greater< Time > > Partition
Definition: microsimulation.h:640