Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <Rcpp.h>
  2. #include "../include/brownian_bridge_min.hpp"
  3. #include "../include/inverse_gauss.hpp"
  4.  
  5. using namespace Rcpp;
  6.  
  7. // [[Rcpp::plugins(cpp17)]]
  8.  
  9. // [[Rcpp::export]]
  10. double M_function(const double &a, const double &x, const double &y, const double &s, const double &t) {
  11. // M function that is used to simulate a minimum of a Brownian bridge
  12. return exp(-2.0 * (a-x) * (a-y) / (t-s));
  13. }
  14.  
  15. // [[Rcpp::export]]
  16. std::array<double, 2> min_sampler(const double &x, const double &y,
  17. const double &s, const double &t,
  18. const double &low_bound, const double &up_bound,
  19. std::mt19937 &RNG)
  20. {
  21. // function simulates a minimum of a Brownian bridge between (low_bound) and (up_bound)
  22. // first element returned is the simulated minimum
  23. // second element returned is the simulated time which the minimum occurs
  24.  
  25. // calculate bounds for u1
  26. double low_M {M_function(low_bound, x, y, s, t)};
  27. double up_M {M_function(up_bound, x, y, s, t)};
  28.  
  29. // simulate uniform random variables
  30. std::uniform_real_distribution<double> u1(low_M, up_M);
  31. std::uniform_real_distribution<double> u2(0.0, 1.0);
  32.  
  33. // set simulated minimum value
  34. double min {x - (0.5*(sqrt((y-x)*(y-x) - 2.0*(t-s)*log(u1(RNG))) - y + x))};
  35.  
  36. // condition for setting V
  37. double condition {(x-min)/(x+y-(2.0*min))};
  38. // simulating from Inverse Gaussian
  39. double mu, lambda, V;
  40. if (u2(RNG) < condition) {
  41. mu = (y-min)/(x-min);
  42. lambda = (y-min)*(y-min)/(t-s);
  43. V = inv_gauss_sampler(mu, lambda, RNG);
  44. } else {
  45. mu = (x-min)/(y-min);
  46. lambda = (x-min)*(x-min)/(t-s);
  47. V = 1.0 / inv_gauss_sampler(mu, lambda, RNG);
  48. }
  49.  
  50. // set tau (time of simualted minimum)
  51. double tau{((s*V)+t)/(1.0+V)};
  52. // setting simulated minimum and tau in array
  53. std::array<double, 2> simulated_min {min, tau};
  54.  
  55. return simulated_min;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement