Guest User

Untitled

a guest
Apr 21st, 2022
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. /* block for user defined functions */
  2. functions {
  3. // The arguments to this function are:
  4. // - `time`: the time to evaluate the DAE system
  5. // - `state`: the state of the DAE system at the time specified
  6. // - `state_derivative`: the time derivatives of the state of the DAE system at the time specified
  7. // - `...`: sequence of arguments passed unmodified from the DAE solve function call.
  8. vector residual(real z, vector state, vector deriv, real lambda, real Omega_m, real Omega_r) {
  9. real E = state[1];
  10. real lhs = (E^2 - 2*lambda)*exp(lambda*E^-2); // left hand side of the equation for E
  11. real rhs = Omega_m*(1+z)^3 + Omega_r*(1+z)^4; // right hand side of the equation for E
  12.  
  13. // compute the residuals
  14. vector[2] res;
  15. res[1] = rhs - lhs; // E satisfies the algebraic relation, meaning that res[1] should be 0
  16. res[2] = 1/E - deriv[2]; // (? -> why is this of this form)
  17.  
  18. return res;
  19. }
  20. }
  21.  
  22. /* block for datasets used
  23. - must match the columns for each input .csv file, and the number of events must be N1, N2, ...
  24. - if two file have the same columns then they will stack as one big array.
  25. - the order of the files in the CLI must be the same as the order on which we define each data variable here, unless we're stacking several measurements with the same column names, those can show up anywhere */
  26. data {
  27. int N1;
  28. array[N1] real redshift;
  29. array[N1] real luminosity_distance;
  30. array[N1] real error;
  31. }
  32.  
  33. /* block for transformed data
  34. - only evaluated in the beginning of each chain */
  35. transformed data {
  36. }
  37.  
  38. /* block for model parameters */
  39. parameters {
  40. real<lower=0> h;
  41. real<lower=0,upper=1> Omega_m;
  42. real<lower=0,upper=1> Omega_r;
  43. }
  44.  
  45. /* block for transformed parameters
  46. - allows derived quantities from parameters and data
  47. - is evaluated on each leapfrog step */
  48. transformed parameters {
  49. array[N1] real dGW; // luminosity distance for gravitational waves
  50. real correction; // correction from modification to gravity
  51. real E; // E(z)
  52. real k = 2.9979; // c/H0 = (k Gpc)/h
  53.  
  54. // quantity derived from Omega_m and Omega_r
  55. real lambda;
  56. lambda = 0.5 + lambert_w0( -(Omega_m + Omega_r)/(2*exp(0.5)) );
  57.  
  58. // compute dE/dz(z = 0), knowing that E(0) = 1
  59. real deriv_0;
  60. deriv_0 = 1/(2*exp(lambda)) * (3*Omega_r + 4*Omega_m) / (1 - lambda + 2*lambda^2);
  61.  
  62. // call the differential algebraic equation solver
  63. // (? -> what are the ' following the arrays? is it because its a vector?)
  64. // `residual`: DAE residual function,
  65. // `initial_state`: initial state, type vector
  66. // `initial_state_derivative`: time derivative of the initial state, type vector
  67. // `initial_time`: initial time, type real
  68. // `times`: solution times, type array[] real
  69. // `...`: sequence of arguments that will be passed through unmodified to the DAE residual function.
  70. array[N1] vector[2] S;
  71. S = dae(residual, [1, 0.0]', [deriv_0, 1]', 0.0, redshift, lambda, Omega_m, Omega_r);
  72.  
  73. // compute theoretical luminosity distance for gravitational waves for each measured redshift
  74. for (i in 1:N1) {
  75. E = S[i,1];
  76. correction = ((1-lambda)/(1-lambda/E^2))^0.5 * exp(lambda/2 * (E^2 - 1)/E^2);
  77. dGW[i] = correction * (1.0+redshift[i]) * (k/h) * S[i,2];
  78. }
  79. }
  80.  
  81. /* block for model definition */
  82. model {
  83. // priors
  84. h ~ normal(0.68, 10);
  85. Omega_m ~ normal(0.353, 0.5);
  86. Omega_r ~ normal(0.021, 0.5);
  87.  
  88. // likelihood
  89. luminosity_distance ~ normal(dGW, error);
  90. }
  91.  
  92. /* block for generated quantites
  93. - allows derived quantities based on parameters, data, and rng
  94. - is executed once per iteration */
  95. generated quantities {
  96. // output the natural logarithm of the likelihood to use in model selection criteria
  97. vector[N1] log_lik;
  98. for (n in 1:N1) {
  99. log_lik[n] = normal_lpdf(luminosity_distance[n] | dGW[n], error[n]);
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment