-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCauchy_Toy_Example_Bounded_Hazard.cpp
More file actions
309 lines (271 loc) · 11.4 KB
/
Cauchy_Toy_Example_Bounded_Hazard.cpp
File metadata and controls
309 lines (271 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <Rcpp.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace Rcpp;
using namespace std;
// ============= dataset being used in this example ============================== //
static std::vector<double> data{2.65226687,1.27648783,1.61011759,1.27433040,0.08721209};
static int N = data.size();
//==================================================================================//
// ========= gradient of the log-likelihood function (i.e. drift function ========= //
// Recall that it uses a standard cauchy density as a prior
double drift (double x){
double result = 0;
// sum the gradient corresponding to each data point in a loop
for (int i =0; i<N; i++){
result = result + 2*(data[i] - x)/(1 + pow((data[i] - x),2));
}
// sum the gradient corresponding to the prior
result = result - 2*x/(1+x*x);
return result;
}
//==================================================================================//
// =============== second derivative function of the log-likelihood function ===== //
double divergence (double x){
double result = 0;
// sum the second derivative function for each data point in a loop
for (int i =0; i<N; i++){
result = result - 2*(1-pow((data[i] - x),2))/pow((1 + pow((data[i] - x),2)),2);
}
// sum the second derivative corresponding to standard cauchy prior
result = result - 2*(1-x*x)/pow((1+x*x),2);
return result;
}
//==================================================================================//
// ================== kappa function for this toy example ========================= //
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
double kappa(double x){
// sum of the squared drift and the divergence which is then halved
double result = 0.5*(pow(drift(x),2) + divergence(x));
// -2.379829 is the global lower bound of the term
result = result + 2.379829;
return result;
}
//==================================================================================//
// ================== upper bound function ========================================//
double M_Cauchy(){
return 14.0;
}
//==================================================================================//
// ================== un-normalised posterior density ==============================//
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
std::vector<double> unnormalised_posterior(std::vector<double> x){
double prod;
int Nx = x.size();
std::vector<double> result(Nx);
for (int j=0; j < Nx; j++){
prod = 1;
for(int i=0; i <N; i++){
prod = prod*1/(1+pow((data[i]-x[j]),2));
}
prod = prod/(1+x[j]*x[j]);
result[j] = prod;
}
return result;
}
// RESCALE CODE ==========================================
// the structure which will hold the positions at different times for the Brownian
// motion.
struct rescale_object {
std::vector<double> time;
std::vector<double> pos;
std::vector<double> pty;
};
// Function returns the simulated Brownian bridge positions at some intermediate points
// of a given left and right positions of a Brownian motion.
std::vector<double> two_point_bb(
std::vector<double> times, // already known times as a vector of 2 elements
std::vector<double> pos, // already known positions as a vector of 2 elements
std::vector<double> req_time // intermediate times at which position is sought
){
int N = req_time.size(); // size of required positions
std::vector<double> new_times(N + 2); // newly created times as a vector of (N+2) elements
std::vector<double> new_pos(N + 2); // Newly created vector of positions
std::vector<double> result(N); // stores the positions at required times.
int i = 0;
new_times[i] = times[i]; new_pos[i] = pos[i]; // initialisations
new_times[N+1] = times[1]; new_pos[N+1] = pos[1];
// stores the mean and sd for the normal distribution for caculation of BB
double mean, sd;
if (N != 0)
{
// loop runs through N times and evaluates the positions at required times
// using the definition of Brownian bridge.
for (i = 1; i < (N+1); i++) {
// loop that fills the required times at newly created time vector
new_times[i] = req_time[i-1];
// calculate the mean and sd of normal distribution to be used to simulate BB
mean = (new_pos[N+1]*(new_times[i] - new_times[i-1]) +
new_pos[i-1]*(new_times[N+1] - new_times[i]))/
(new_times[N+1] - new_times[i-1]);
sd = sqrt((new_times[i] - new_times[i-1])*(new_times[N+1] -
new_times[i])/(new_times[N+1] - new_times[i-1]));
// simulated BB position using a normal distribution
new_pos[i] = rnorm(1, mean, sd)[0];
// store the simulated position in the result/output
result[i-1] = new_pos[i];
}
}
return(result);
}
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
std::vector<double> posterior(std::vector<double> x){
double prod;
int Nx = x.size();
std::vector<double> result(Nx);
for (int j=0; j < Nx; j++){
prod = 1;
for(int i=0; i <N; i++){
prod = prod*1/(1+pow((data[i]-x[j]),2));
}
prod = prod * 1/(1+x[j]*x[j]);
result[j] = 1/0.06281079*prod;
}
return result;
}
// Function returns one segment/skeleton of a killed Brownian motion for a given
// startting time and starting position.
rescale_object segments (double start_time, double start_pos, double M )
{
std::vector<double> times; // stores the poisson event times
std::vector<double> pos; // stores the positions visited by the Brownian motion
std::vector<double> PTY;
times.push_back(start_time); // store the starting time
pos.push_back(start_pos); // store the initial poistion in the position vector.
PTY.push_back(0);
int kill = 0; // a check to evaluate if kill occurs or not
// numeric values to store the current time and current positions.
double cur_time = start_time; double cur_val = start_pos;
double phi_val;
// we loop until kill happens for a Brownian motion.
while (kill == 0){
double wait_time = rexp(1, M)[0]; // simulate the waiting time for the poisson event
cur_time += wait_time; // get the actual poisson event times.
double BM = rnorm(1, cur_val, sqrt(wait_time))[0]; // simulate the Brownian motion at current time.
phi_val = kappa(BM);
if (runif(1)[0] < phi_val/M) // checks if kill occurs
{
kill = 1; // if kill occurs then change the kill value to 1.
times.push_back(cur_time); // store the kill time in the vector.
pos.push_back(BM); // store the kill poistion at the
PTY.push_back(13);
}
else
{
kill = 0; // if the kill does not occur then kill is still 0.
cur_val = BM; // in this case set the current value as the simulated Brownian motion position.
times.push_back(cur_time); // store the current time in the vector
pos.push_back(BM); // store the simulated position in the vector.
PTY.push_back(1);
}
}
rescale_object skeleton; // final result as the rescale_object type.
skeleton.time = times; // store the poisson event times in the skeleton
skeleton.pos = pos; // store the positions visited in the skeleton.
skeleton.pty = PTY;
return skeleton;
}
// Function returns the index of the last element which is less than given node/element
// in an increasing sequence of vector.
int index_last_less_ele (std::vector<double> seq, double node){
int i = 0;
while (seq[i] < node){
i++;
}
int two_closet = i-1;
return two_closet;
}
// Function returns the all the positions visited by a killed Brownian motion until some
// maximum time for a given starting poistion. The segments are acreated according to the
// RESCALE methodology wherein once the brownian motion is killed then it is regenrated
// by simulating from the states visited by it.
rescale_object test_segments (double max_time, double start_pos,
double M,double Lambda =0,
double prior_C = 0){
rescale_object skeleton;
std::vector<double> full_time, full_pos, full_pty;
std::vector<double> seg_time, seg_pos, seg_pty;
std::vector<double> reg_val(1);
std::vector<double> reg_time(1);
int N, clo_time;
double t_kill, cur_time = 0, start_time = 0;
std::vector<double> bb_time(2), bb_pos(2);
std::vector<double>::iterator it1, it2, it3;
int size = 0;
while(cur_time < max_time){
skeleton = segments(start_time, start_pos, M);
seg_time = skeleton.time;
seg_pos = skeleton.pos;
seg_pty = skeleton.pty;
full_time.insert(full_time.end(),seg_time.begin(),seg_time.end());
full_pos.insert(full_pos.end(),seg_pos.begin(),seg_pos.end());
full_pty.insert(full_pty.end(),seg_pty.begin(),seg_pty.end());
N = seg_time.size();
t_kill = seg_time[N-1];
reg_time[0] = runif(1, Lambda*t_kill, t_kill)[0];
clo_time = index_last_less_ele(full_time,reg_time[0]);
bb_time[0] = full_time[clo_time]; bb_time[1] = full_time[clo_time+1];
bb_pos[0] = full_pos[clo_time]; bb_pos[1] = full_pos[clo_time+1];
if(runif(1)[0] <= (1 - prior_C/(prior_C+t_kill))){
reg_val = two_point_bb(bb_time,bb_pos,reg_time);
}else{
reg_val[0] = runif(1,-10.0,10.0)[0];
}
it1 = full_time.begin(); it2 = full_pos.begin(); it3 = full_pty.begin();
full_time.emplace(it1+clo_time+1,reg_time[0]);
full_pos.emplace(it2+clo_time+1,reg_val[0]);
full_pty.emplace(it3+clo_time+1,2);
start_time = t_kill;
start_pos = reg_val[0];
cur_time = t_kill;
size = size + seg_time.size();
}
rescale_object result;
result.time = full_time; result.pos = full_pos; result.pty = full_pty;
return result;
}
//=======================================================================================
// Function to restrict the outputs generated by test_segments to maximum time
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
Rcpp::DataFrame rescale_skeleton(double max_time, double start_pos, double M, double Lambda =0,
double prior_C = 0){
// store the skeleton generated by the test_skeleton in temp_skeleton
rescale_object temp_skeleton = test_segments(max_time, start_pos, M, Lambda, prior_C);
// extract the event times and positions
std::vector<double> full_time = temp_skeleton.time;
std::vector<double> full_pos = temp_skeleton.pos;
std::vector<double> full_pty = temp_skeleton.pty;
// Store the last element in the sorted vector of full times which is less than regenerated time.
int index = index_last_less_ele(full_time,max_time);
std::vector<double> bb_time(2), bb_pos(2); // stores the nearest times and its positions.
// store the two closest times to regenerated time for the purpose of calculating the brownian bridge value
bb_time[0] = full_time[index]; bb_time[1] = full_time[index+1];
// Store the corresponding positions of two closest times.
bb_pos[0] = full_pos[index]; bb_pos[1] = full_pos[index+1];
std::vector<double> reg_val(1), m_time(1);
m_time[0] = max_time;
// Brownian brdige value is the Brownian bridge calculated at max_time.
reg_val = two_point_bb (bb_time,bb_pos,m_time);
int N = full_time.size()-index;
for (int i = 0; i < N; i++ ){
full_time.pop_back();
full_pos.pop_back();
full_pty.pop_back();
}
full_time.push_back(max_time);
full_pos.push_back(reg_val[0]);
full_pty.push_back(1);
// rescale_object result;
// result.time = full_time;
// result.pos = full_pos;
// return result;
return(Rcpp::DataFrame::create(
_["t"]= full_time,
_["x"] = full_pos,
_["pty"] = full_pty));
}