Microsimulation API

/home/marcle/src/R/microsimulation/src/microsimulation.cc
Go to the documentation of this file.
1 #include <microsimulation.h>
2
3 namespace ssim {
4
5 double rweibullHR(double shape, double scale, double hr){
6 return R::rweibull(shape, scale*pow(hr,1.0/shape));
7 }
8
9 Time now() {
10 return Sim::clock();
11 }
12
13 Time simTime() {
14 return Sim::clock();
15 }
16
17
18 static Rng * default_stream, * current_stream;
19 static double rn = 0.0;
20
21 Rng::~Rng() {
22 if (current_stream->id == this->id)
23 current_stream = default_stream;
24 }
25
26 void Rng::set() {
27 current_stream = this;
28 }
29
30 extern "C" {
31
32 void r_create_current_stream()
33 {
34 default_stream = new Rng();
35 current_stream = default_stream;
36 }
37
38 void r_remove_current_stream()
39 {
40 delete default_stream;
41 }
42
43 void r_set_user_random_seed(double * inseed) {
44 double seed[6];
45 for(int i=0; i<6; i++) {
46 seed[i] = inseed[i];
47 }
48 Rng::SetPackageSeed(seed);
49 default_stream->SetSeed(seed);
50 }
51
52 void r_get_user_random_seed(double * outseed) {
53 double seed[6];
54 default_stream->GetState(seed);
55 for(int i=0; i<6; i++) {
56 outseed[i] = (double)seed[i];
57 }
58 }
59
60 void r_next_rng_substream() {
61 default_stream->ResetNextSubstream();
62 }
63
64 void r_rng_advance_substream(double * inoutseed, int * n) {
65 RngStream r;
66 double seed[6];
67 for (int i=0; i<6; i++)
68 seed[i]=inoutseed[i];
69 r.SetSeed(seed);
70 r.AdvanceSubstream(0, *n);
71 r.GetState(seed);
72 for (int i=0; i<6; i++)
73 inoutseed[i]= seed[i];
74 }
75
76 double *user_unif_rand ()
77 {
78 if (!current_stream) {
79 REprintf("user_unif_rand(): No stream created yet!");
80 return NULL;
81 }
82 rn = current_stream->RandU01();
83 return &rn;
84 }
85
86 void test_rstream2(double * x) {
87 Rng * s1 = new Rng();
88 Rng * s2 = new Rng();
89 x[0]=WithRNG(s1,R::rexp(1.0));
90 x[1]=WithRNG(s2,R::rexp(1.0));
91 s1->ResetNextSubstream();
92 x[2]=R::rexp(1.0);
93 delete s1;
94 delete s2;
95 }
96
97 } // extern "C"
98
99 } // namespace ssim
100
101 namespace R {
102 double rnormPos(double mean, double sd) {
103 double x;
104 while ((x=R::rnorm(mean,sd))<0.0) { }
105 return x;
106 }
107
108 double rllogis(double shape, double scale) {
109 double u = R::runif(0.0,1.0);
110 return scale*exp(-log(1.0/u-1.0)/shape);
111 }
112
113 double rllogis_trunc(double shape, double scale, double left) {
114 double S0 = 1.0/(1.0+exp(log(left/scale)*shape));
115 double u = R::runif(0.0,1.0);
116 return scale*exp(log(1.0/(u*S0)-1.0)/shape);
117 }
118
119 double rgompertz(double shape, double rate) {
120 double u = 1.0 - R::runif(0.0, 1.0);
121 return (shape < 0.0 && u<exp(rate/shape)) ? R_PosInf :
122 log(1.0 - shape*log(u)/rate)/shape;
123 }
124 }
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:32
ssim::Rng::~Rng
virtual ~Rng()
Definition: microsimulation.cc:21
ssim::rn
static double rn
Definition: microsimulation.cc:19
ssim::RngStream::SetPackageSeed
static bool SetPackageSeed(const double seed[6])
Definition: RngStream.cpp:391
R::rgompertz
double rgompertz(double shape, double rate)
Random draw from a Gompertz distribution.
Definition: microsimulation.cc:119
ssim::simTime
Time simTime()
simTime() function for OMNET++ API compatibility
Definition: microsimulation.cc:13
ssim::default_stream
static Rng * default_stream
Definition: microsimulation.cc:18
ssim::RngStream::AdvanceSubstream
void AdvanceSubstream(int32_t e, int32_t c)
Definition: RngStream.cpp:454
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:113
WithRNG
#define WithRNG(rng, expr)
WithRNG is a macro for using the random number generator rng and then evaluating the expression expr.
Definition: microsimulation.h:200
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:43
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:9
ssim::current_stream
static Rng * current_stream
Definition: microsimulation.cc:18
ssim::RngStream::SetSeed
bool SetSeed(const double seed[6])
Definition: RngStream.cpp:402
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:108
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:38
ssim::RngStream::ResetNextSubstream
void ResetNextSubstream()
Definition: RngStream.cpp:381
ssim::RngStream::GetState
void GetState(double seed[6]) const
Definition: RngStream.cpp:496
R
Definition: microsimulation.cc:101
ssim::Sim::clock
static Time clock()
returns the current virtual time for the current process
Definition: ssim.cc:264
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:64
ssim::Time
double Time
virtual time type
Definition: ssim.h:75
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
ssim::rweibullHR
double rweibullHR(double shape, double scale, double hr)
Random Weibull distribution for a given shape, scale and hazard ratio.
Definition: microsimulation.cc:5
ssim::RngStream::RandU01
double RandU01()
Definition: RngStream.cpp:566
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:52
ssim::Rng::id
int id
Definition: microsimulation.h:561
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:60
ssim::test_rstream2
void test_rstream2(double *x)
Simple test of the random streams (with a stupid name)
Definition: microsimulation.cc:86
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:102
ssim::Rng
Definition: microsimulation.h:548
microsimulation.h
ssim::Rng::set
void set()
Definition: microsimulation.cc:26
ssim::user_unif_rand
double * user_unif_rand()
Definition: microsimulation.cc:76
ssim::RngStream
Definition: RngStream.h:30