CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
ModelFunction_TwoPointCorrelation1D_monopole.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2016 by Federico Marulli and Alfonso Veropalumbo *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
38 #include "HaloProfile.h"
39 
40 using namespace std;
41 
42 using namespace cbl;
43 
44 
45 // ============================================================================================
46 
47 
48 std::vector<double> cbl::modelling::twopt::xi0_BAO_sigmaNL (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
49 {
50  // structure contaning the required input data
51  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
52 
53  // input parameters
54 
55  // parameter that control the BAO shape
56  double sigmaNL = parameter[0];
57 
58  // AP parameter that contains the distance information
59  double alpha = parameter[1];
60 
61  vector<double> new_rad;
62 
63  for (size_t i=0; i<rad.size(); i++)
64  new_rad.push_back(rad[i]*alpha);
65 
66  // compute the 2pcf signal
67 
68  vector<double> Pk(pp->kk.size(), 0);
69 
70  vector<double> Pklin = pp->func_Pk->y();
71  vector<double> PkNW = pp->func_Pk_NW->y();
72 
73  for (size_t i =0; i<pp->kk.size(); i++) {
74  Pk[i] = PkNW[i]*(1.+(Pklin[i]/PkNW[i]-1.)*exp(-0.5*pow(pp->kk[i]*sigmaNL, 2)));
75  }
76 
77  vector<double> xi = wrapper::fftlog::transform_FFTlog(new_rad, 1, pp->kk, Pk, 0);
78 
79  // return the monopole of the two-point correlation function
80 
81  for (size_t i =0; i<xi.size(); i++) {
82 
83  double poly = 0;
84  for (int j = 0;j<pp->poly_order; j++)
85  poly += parameter[j+3]*pow(rad[i], -j);
86 
87  xi[i] = pow(parameter[2], 2)*xi[i]+poly;
88  }
89 
90  return xi;
91 }
92 
93 
94 // ============================================================================================
95 
96 
97 std::vector<double> cbl::modelling::twopt::xi0_linear (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
98 {
99  // structure contaning the required input data
100  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
101 
102  // input parameters
103 
104  // AP parameter that contains the distance information
105  double alpha = parameter[0];
106 
107  // f(z)*sigma8(z)
108  double fsigma8 = parameter[1];
109 
110  // bias(z)*sigma8(z)
111  double bsigma8 = parameter[2];
112 
113  // return the redshift-space monopole of the two-point correlation function
114 
115  vector<double> xi(rad.size(), 0);
116 
117  for (size_t i =0; i<xi.size(); i++) {
118 
119  double poly = 0;
120  for (int j = 0;j<pp->poly_order; j++)
121  poly += parameter[j+3]*pow(rad[i], -j);
122 
123  xi[i] = xi_ratio(fsigma8, bsigma8)*pow(bsigma8/pp->sigma8_z, 2)*pp->func_xi->operator()(rad[i]*alpha)+poly;
124  }
125 
126  return xi;
127 }
128 
129 
130 // ============================================================================================
131 
132 
133 std::vector<double> cbl::modelling::twopt::xi0_polynomial_LinearPoint (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
134 {
135  // structure contaning the required input data
136  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
137 
138  vector<double> model(rad.size(), 0);
139 
140  vector<double> poly_params(parameter.size()-3,0);
141  for (size_t i=0; i<poly_params.size(); i++)
142  poly_params[i] = parameter[i+3];
143 
144  for (size_t i=0; i<model.size(); i++)
145  model[i] = wrapper::gsl::GSL_polynomial_eval(rad[i], NULL, poly_params);
146 
147  vector<double> deriv_coeff(pp->poly_order-1, 0);
148  vector<vector<double>> roots;
149 
150  for (size_t i=1; i<poly_params.size(); i++)
151  deriv_coeff[i-1] = i*poly_params[i];
152 
153  vector<double> D1_coeff(pp->poly_order-1, 0);
154  for (size_t i=1; i<poly_params.size(); i++)
155  D1_coeff[i-1] = i*poly_params[i];
156 
157  auto model_derivative = [&] (double rad)
158  {
159  double mm = 0.;
160  for (size_t i=0; i<D1_coeff.size(); i++)
161  mm += D1_coeff[i]*pow(rad, i);
162  return mm;
163  };
164 
165  double xmin=95, xmax=105;
166 
167  bool end=false;
168  while (!end) {
169  parameter[0] = wrapper::gsl::GSL_root_brent(model_derivative, 0., xmin, xmax, 1.e-10);
170  if (fabs(parameter[0]-xmin)<0.1)
171  xmin -=2;
172  else if (fabs(parameter[0]-xmax)<0.1)
173  xmax +=2;
174  else
175  end=true;
176  if ((xmin<80) || (xmax>120)) {
177  end=true;
178  parameter[0]=0;
179  }
180  }
181 
182  parameter[1]=0;
183 
184  if ((parameter[0]>xmin) && (parameter[0]<xmax)) {
185  xmin = parameter[0]-22; xmax = parameter[0]-2;
186  end = false;
187  while (!end) {
188  parameter[1] = wrapper::gsl::GSL_root_brent(model_derivative, 0., xmin, xmax, 1.e-10);
189 
190  if (fabs(parameter[1]-xmin)<0.1)
191  xmin -=2;
192  else if (fabs(parameter[1]-xmax)<0.1) {
193  end=true;
194  parameter[0]=0;
195  parameter[1]=0;
196  }
197  else{
198  end=true;
199  }
200 
201  if (xmin<60) {
202  end=true;
203  parameter[0]=0;
204  parameter[1]=0;
205  }
206  }
207  }
208 
209  parameter[2] = (parameter[0]+parameter[1])*0.5;
210 
211  return model;
212 }
213 
214 
215 // ============================================================================================
216 
217 
218 std::vector<double> cbl::modelling::twopt::xi0_linear_LinearPoint (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
219 {
220  // structure contaning the required input data
221  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
222 
223  // input parameters
224 
225  // ap parameter that contains the distance information
226  double alpha = parameter[3];
227 
228  // f(z)*sigma8(z)
229  double fsigma8 = parameter[4];
230 
231  // bias(z)*sigma8(z)
232  double bsigma8 = parameter[5];
233 
234  // return the redshift-space monopole of the two-point correlation function
235 
236  vector<double> model(rad.size(), 0);
237 
238  for (size_t i =0; i<model.size(); i++) {
239 
240  double poly = 0;
241  for (int j = 0;j<pp->poly_order; j++)
242  poly += parameter[j+6]*pow(rad[i], -j);
243 
244  model[i] = xi_ratio(fsigma8, bsigma8)*pow(bsigma8/pp->sigma8_z, 2)*pp->func_xi->operator()(rad[i]*alpha)+poly;
245 
246  }
247 
248  // Linear point section
249  auto model_derivative = [&] (double rr)
250  {
251  double poly_der = 0.;
252  for (int j = 1;j<pp->poly_order; j++) {
253  poly_der += -j*parameter[j+6]*pow(rr, -j-1);
254  }
255  return xi_ratio(fsigma8, bsigma8)*pow(bsigma8/pp->sigma8_z, 2)*pp->func_xi->D1v(rr*alpha)+poly_der;
256  };
257 
258  double xmin = 95., xmax = 105.;
259 
260  bool end = false;
261  while (!end) {
262  parameter[0] = wrapper::gsl::GSL_root_brent(model_derivative, 0., xmin, xmax, 1.e-10);
263  if (fabs(parameter[0]-xmin)<0.1)
264  xmin -= 2;
265  else if (fabs(parameter[0]-xmax)<0.1)
266  xmax += 2;
267  else
268  end=true;
269  if ((xmin<70) || (xmax>160)) {
270  end = true;
271  parameter[0] = 0;
272  }
273  }
274 
275  parameter[1] = 0;
276  if ((parameter[0]>xmin) && (parameter[0]<xmax)) {
277  parameter[1] = wrapper::gsl::GSL_root_brent(model_derivative, 0., 40, parameter[0]-2, 1.e-10);
278  if (parameter[0]-parameter[1]<2.1) {
279  parameter[1] = 0;
280  parameter[0] = 0;
281  }
282  }
283 
284  parameter[2] = (parameter[0]+parameter[1])*0.5;
285 
286  return model;
287 }
288 
289 
290 // ============================================================================================
291 
292 
293 std::vector<double> cbl::modelling::twopt::xi0_linear_sigma8_bias (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
294 {
295  // structure contaning the required input data
296  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
297 
298 
299  // input likelihood parameters
300 
301  // sigma8(z)
302  double sigma8 = parameter[0];
303 
304  // bias
305  double bias = parameter[1];
306 
307 
308  // fixed parameters
309 
310  // f(z)*sigma8(z)
311  double fsigma8 = pp->linear_growth_rate_z*sigma8;
312 
313 
314  // return the redshift-space monopole of the two-point correlation function
315 
316  vector<double> xi(rad.size(), 0);
317 
318  const double fact = xi_ratio(fsigma8, bias*sigma8)*pow(bias, 2)*pow(sigma8/pp->sigma8_z, 2);
319 
320  for (size_t i =0; i<xi.size(); i++)
321  xi[i] = fact*pp->func_xi->operator()(rad[i]);
322 
323  return xi;
324 }
325 
326 
327 // ============================================================================================
328 
329 
330 std::vector<double> cbl::modelling::twopt::xi0_linear_cosmology (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
331 {
332  // structure contaning the required input data
333  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
334 
335 
336  // input parameters
337 
338  // AP parameter that contains the distance information
339  double alpha = parameter[0];
340 
341  // f(z)*sigma8(z)
342  double fsigma8 = parameter[1];
343 
344  // bias(z)*sigma8(z)
345  double bsigma8 = parameter[2];
346 
347  // set the cosmological parameters used to compute the dark matter
348  // two-point correlation function in real space
349  for (size_t i=0; i<parameter.size(); ++i)
350  pp->cosmology->set_parameter(pp->Cpar[i], parameter[i]);
351 
352  // return the redshift-space monopole of the two-point correlation function
353 
354  vector<double> xi(rad.size(), 0);
355 
356  for (size_t i =0; i<xi.size(); i++) {
357 
358  double poly = 0.;
359 
360  for (int j = 0;j<pp->poly_order; j++)
361  poly += parameter[j+3]*pow(rad[i], -j);
362 
363  xi[i] = xi_ratio(fsigma8, bsigma8)*pp->cosmology->xi_matter(rad[i]*alpha, pp->method_Pk, pp->NL, pp->redshift, true, pp->output_root, pp->norm, pp->k_min, pp->k_max, pp->aa, pp->GSL, pp->prec, pp->file_par)/pow(pp->sigma8_z, 2)+poly;
364  }
365  if (!pp->store_output)
366  pp->cosmology->remove_output_Pk_tables(pp->method_Pk, pp->NL, pp->redshift, pp->output_root);
367 
368  return xi;
369 }
370 
371 
372 // ============================================================================================
373 
374 
375 std::vector<double> cbl::modelling::twopt::xi0_linear_bias_cosmology (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
376 {
377  // structure contaning the required input data
378  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
379 
380  // redefine the cosmology
381  cosmology::Cosmology cosmo = *pp->cosmology;
382 
383 
384  // input likelihood parameters
385 
386  // bias
387  double bias = parameter[0];
388 
389 
390  // set the cosmological parameters used to compute the dark matter
391  // two-point correlation function in real space
392  for (size_t i=0; i<pp->Cpar.size(); ++i)
393  cosmo.set_parameter(pp->Cpar[i], parameter[i+1]);
394 
395 
396  // fixed parameters
397 
399  double alpha = cosmo.D_V(pp->redshift)/pp->DVfid;
400 
401  // return the redshift-space monopole of the two-point correlation function
402  vector<double> new_rad = rad;
403  for (size_t i=0; i<rad.size(); i++)
404  new_rad[i] *= alpha;
405 
406  return cosmo.xi0_Kaiser(new_rad, bias, pp->method_Pk, pp->redshift, false, pp->output_root, pp->NL, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->file_par);
407 }
408 
409 
410 // ============================================================================================
411 
412 
413 std::vector<double> cbl::modelling::twopt::xi0_damped_scaling_relation_sigmaz (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
414 {
415  // structure contaning the required input data
416  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
417 
418  // input likelihood parameters
419 
420  // scaling relation
421  const double M0 = parameter[0];
422  const double slope = parameter[1];
423  const double scatter = parameter[2];
424 
425  // damping scale
426  const double SigmaS = par::cc*parameter[3]/pp->HHfid;
427 
428  // bias computation
429 
430  vector<double> mass(pp->cluster_mass_proxy->ndata(), 0), _bias(pp->cluster_mass_proxy->ndata());
431 
432  for (int i=0; i<pp->cluster_mass_proxy->ndata(); i++) {
433 
434  bool isNan = true;
435  double log10_proxy = 0;
436 
437  while (isNan) {
438  log10_proxy = log10(pp->gau_ran->operator()()*pp->cluster_mass_proxy->error(i)+pp->cluster_mass_proxy->data(i));
439  isNan = (log10_proxy!=log10_proxy);
440  }
441  double log10_mass = 14.+M0+slope*log10_proxy+pp->gau_ran->operator()()*(scatter);
442 
443  mass[i] = pow(10, log10_mass);
444 
445  _bias[i] = pp->cosmology->bias_halo(mass[i], pp->func_sigma->operator()(mass[i]), pp->cluster_mass_proxy->xx(i), pp->model_bias, false, par::defaultString, "Linear", pp->Delta);
446  }
447 
448  const double bias = Average(_bias);
449 
450  // return the value of the bias
451  parameter[4] = bias;
452 
453  return modelling::twopt::damped_Xi(rad, bias, pp->linear_growth_rate_z, SigmaS, pp->kk, pp->func_Pk);
454 }
455 
456 
457 
458 // ============================================================================================
459 
460 
461 std::vector<double> cbl::modelling::twopt::xi0_damped_scaling_relation_sigmaz_cosmology (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
462 {
463  // structure contaning the required input data
464  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
465 
466  // redefine the cosmology
467  cbl::cosmology::Cosmology cosmo = *pp->cosmology;
468 
469  // set the cosmological parameters
470  for (size_t i=0; i<pp->Cpar.size(); ++i)
471  cosmo.set_parameter(pp->Cpar[i], parameter[i]);
472 
473  auto cosmo_ptr = std::make_shared<cbl::cosmology::Cosmology>(cosmo);
474 
475  // damping scale
476  const double SigmaS = par::cc*parameter[parameter.size()-2]/cosmo.HH(pp->redshift);
477 
478  // scaling relation parameters
479  std::vector<double> scalRel_pars;
480  for (size_t i=0; i<pp->Cpar.size(); ++i)
481  scalRel_pars.push_back(parameter[i]);
482  for (size_t i=pp->Cpar.size(); i<parameter.size()-2; i++)
483  scalRel_pars.emplace_back(parameter[i]);
484 
485  const double alpha = scalRel_pars[scalRel_pars.size()-8];
486  const double beta = scalRel_pars[scalRel_pars.size()-7];
487  const double gamma = scalRel_pars[scalRel_pars.size()-6];
488  const double scatter0 = scalRel_pars[scalRel_pars.size()-5];
489  const double scatterM = scalRel_pars[scalRel_pars.size()-4];
490  const double scatterM_exp = scalRel_pars[scalRel_pars.size()-3];
491  const double scatterz = scalRel_pars[scalRel_pars.size()-2];
492  const double scatterz_exp = scalRel_pars[scalRel_pars.size()-1];
493 
494  // compute the power spectrum
495  vector<double> Pk = cosmo.Pk_matter(pp->kk, pp->method_Pk, pp->NL, pp->redshift, false, pp->output_root, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->file_par);
496 
497  std::shared_ptr<glob::FuncGrid> Pk_interp = make_shared<glob::FuncGrid>(glob::FuncGrid(pp->kk, Pk, "Spline"));
498 
499 
500  // »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
501  // Derive the effective bias from the scaling relation
502  // »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
503 
504  // Interpolate sigmaM
505  const std::vector<double> Mass_vector = cbl::logarithmic_bin_vector(300, 1.e10, 1.e16);
506  std::vector<double> sigmaM (Mass_vector.size(), 0.);
507  for (size_t i=0; i<sigmaM.size(); i++)
508  sigmaM[i] = sqrt( cosmo.sigma2M(Mass_vector[i], pp->method_Pk, 0., false, "test", "Linear", 100.) );
509 
510  cbl::glob::FuncGrid sigmaM_interp (Mass_vector, sigmaM, "Spline");
511 
512  // Compute the bias
513  double log_base = (pp->scaling_relation)->data_model().log_base;
514  double mass_pivot = (pp->scaling_relation)->data_model().mass_pivot;
515  double proxy_pivot = (pp->scaling_relation)->data_model().proxy_pivot;
516  double redshift_pivot = (pp->scaling_relation)->data_model().redshift_pivot;
517 
518  double bias = 0;
519 
520  if (pp->z_abs_err == -1 && pp->proxy_rel_err == -1) {
521 
522  // Interpolate the normalised amplitude of the growing mode
523  const std::vector<double> z_for_DN = cbl::linear_bin_vector(100, 0.0001, cbl::Max((pp->scaling_relation)->data_model().redshift));
524  std::vector<double> DN (z_for_DN.size(), 0.);
525  for (size_t i=0; i<z_for_DN.size(); i++)
526  DN[i] = cosmo.DN(z_for_DN[i]);
527  cbl::glob::FuncGrid DN_interp (z_for_DN, DN, "Spline");
528 
529  vector<double> _bias(pp->scaling_relation->data()->xx().size());
530 
531  for (size_t i=0; i<pp->scaling_relation->data()->xx().size(); i++) {
532 
533  double log_lambda = log(pp->scaling_relation->data()->xx(i)/proxy_pivot)/log(log_base);
534  double log_fz = log( (pp->scaling_relation)->data_model().fz((pp->scaling_relation)->data_model().redshift[i],redshift_pivot,cosmo_ptr) )/log(log_base);
535 
536  double scatter_intr = scatter0 + scatterM*pow(log_lambda, scatterM_exp) + scatterz*pow(log_fz, scatterz_exp);
537 
538  double log_mass = (pp->scaling_relation)->likelihood()->get_m_model()->operator()(pp->scaling_relation->data()->xx(i), scalRel_pars) + scatter_intr;
539  double mass = pow(log_base, log_mass) * mass_pivot;
540 
541  double Delta = (pp->isDelta_critical) ? pp->Delta_input/cosmo.OmegaM((pp->scaling_relation)->data_model().redshift[i]) : pp->Delta_input;
542  double z = (pp->scaling_relation)->data_model().redshift[i];
543 
544  _bias[i] = cosmo.bias_halo(mass, sigmaM_interp(mass), z, DN_interp(z), pp->model_bias, false, par::defaultString, "Linear", Delta, -1, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->method_Pk);
545 
546  }
547 
548  bias = Average(_bias);
549 
550  } else {
551 
552  // !!
553  // WARNING: actually we only integrate over M, neglecting the integrals over z and proxy (such integrations are negligible).
554  // !!
555  // To verify yourself that integrating over z and proxy produces negligible differences, uncomment the commented "integrand" as well as the following line:
556  //
557  // _bias[i] = CW.IntegrateVegas(integration_limits,false);
558  //
559  // Beware that, in an example test, computing such full integral increases the time from 2 second to 3 minutes!
560  // Definitely not worthy to compute.
561 
562  // Interpolate the normalised amplitude of the growing mode
563  const std::vector<double> z_for_DN = cbl::linear_bin_vector(100, 0.0001, cbl::Max((pp->scaling_relation)->data_model().redshift)+5.*pp->z_abs_err);
564  std::vector<double> DN (z_for_DN.size(), 0.);
565  for (size_t i=0; i<z_for_DN.size(); i++)
566  DN[i] = cosmo.DN(z_for_DN[i]);
567  cbl::glob::FuncGrid DN_interp (z_for_DN, DN, "Spline");
568 
569  // Define the integrand
570  double dummy_proxy, dummy_z;
571  std::shared_ptr<void> ptr;
572 
573  auto integrand = [&] (const double x)
574  {
575  double mass = pow(log_base,x)*mass_pivot;
576 
577  // Compute P(M|lambda,z)
578  double log_lambda = log(dummy_proxy/proxy_pivot)/log(log_base);
579  double log_f_z = log( (pp->scaling_relation)->data_model().fz(dummy_z, redshift_pivot, cosmo_ptr) )/log(log_base);
580 
581  double mean = alpha + beta*log_lambda + gamma*log_f_z;
582  double scatter_intr = std::abs(scatter0 + scatterM*pow(log_lambda, scatterM_exp) + scatterz*pow(log_f_z, scatterz_exp));
583  double P_M__lambda_z = cbl::gaussian(x, ptr, {mean,scatter_intr});
584 
585  // Compute the halo bias
586  double Delta = (pp->isDelta_critical) ? pp->Delta_input/cosmo.OmegaM(dummy_z) : pp->Delta_input;
587  double bias_halo = cosmo.bias_halo(mass, sigmaM_interp(mass), dummy_z, DN_interp(dummy_z), pp->model_bias, false, par::defaultString, "Linear", Delta, -1, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->method_Pk);
588 
589  return bias_halo * P_M__lambda_z;
590  };
591  /*
592  auto integrand = [&] (const std::vector<double> x)
593  {
594 
595  double mass = pow(log_base,x[0])*mass_pivot;
596 
597  // Compute P(M|lambda,z)
598  double log_lambda = log(x[2]/proxy_pivot)/log(log_base);
599  double log_f_z = log( (pp->scaling_relation)->data_model().fz(x[1], redshift_pivot, cosmo_ptr) )/log(log_base);
600 
601  double mean = alpha + beta*log_lambda + gamma*log_f_z;
602  double scatter_intr = scatter0 + scatterM*pow(log_lambda, scatterM_exp) + scatterz*pow(log_f_z, scatterz_exp);
603  double P_M__lambda_z = (cbl::gaussian(x[0], ptr, {mean,scatter_intr}));
604 
605  // Compute P(z|z_ob)
606  double Pz = cbl::gaussian(x[1], ptr, {dummy_z,pp->z_abs_err});
607 
608  // Compute P(proxy|proxy_ob)
609  double Pproxy = cbl::gaussian(x[2], ptr, {dummy_proxy,pp->proxy_rel_err*dummy_proxy});
610 
611  // Compute the halo bias
612  double Delta = (pp->isDelta_critical) ? pp->Delta_input/cosmo.OmegaM(x[1]) : pp->Delta_input;
613  double bias_halo = cosmo.bias_halo(mass, sigmaM_interp(mass), x[1], DN_interp(x[1]), pp->model_bias, false, par::defaultString, "Linear", Delta, -1, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->method_Pk);
614 
615  return bias_halo * P_M__lambda_z * Pz * Pproxy;
616 
617  };
618  std::vector<std::vector<double>> integration_limits(3);
619  cbl::wrapper::cuba::CUBAwrapper CW (integrand, (int)(integration_limits.size()));
620  */
621 
622 
623  // Compute the bias
624  vector<double> _bias(pp->scaling_relation->data()->xx().size());
625 
626  for (size_t i=0; i<pp->scaling_relation->data()->xx().size(); i++) {
627 
628  dummy_proxy = pp->scaling_relation->data()->xx(i);
629  dummy_z = (pp->scaling_relation)->data_model().redshift[i];
630 
631  // Find the minimum and maximum masses, given the parameters of the scaling relation
632  double log_lambda_min = log(dummy_proxy*(1-pp->proxy_rel_err)/proxy_pivot)/log(log_base);
633  double log_lambda_max = log(dummy_proxy*(1+pp->proxy_rel_err)/proxy_pivot)/log(log_base);
634  double log_f_z_min = log( (pp->scaling_relation)->data_model().fz(dummy_z-pp->z_abs_err, redshift_pivot, cosmo_ptr) )/log(log_base);
635  double log_f_z_max = log( (pp->scaling_relation)->data_model().fz(dummy_z+pp->z_abs_err, redshift_pivot, cosmo_ptr) )/log(log_base);
636 
637  double logM1 = alpha + beta*log_lambda_min + gamma*log_f_z_min;
638  double logM2 = alpha + beta*log_lambda_max + gamma*log_f_z_min;
639  double logM3 = alpha + beta*log_lambda_min + gamma*log_f_z_max;
640  double logM4 = alpha + beta*log_lambda_max + gamma*log_f_z_max;
641 
642  double min1 = std::min(logM1, logM2);
643  double min2 = std::min(min1, logM3);
644  double min_logM = std::min(min2, logM4);
645  double max1 = std::max(logM1, logM2);
646  double max2 = std::max(max1, logM3);
647  double max_logM = std::max(max2, logM4);
648 
649  // Find the maximum value of the intrinsic scatter
650  double s1 = std::abs( scatter0 + scatterM*pow(log_lambda_min, scatterM_exp) + scatterz*pow(log_f_z_min, scatterz_exp) );
651  double s2 = std::abs( scatter0 + scatterM*pow(log_lambda_max, scatterM_exp) + scatterz*pow(log_f_z_min, scatterz_exp) );
652  double s3 = std::abs( scatter0 + scatterM*pow(log_lambda_min, scatterM_exp) + scatterz*pow(log_f_z_max, scatterz_exp) );
653  double s4 = std::abs( scatter0 + scatterM*pow(log_lambda_max, scatterM_exp) + scatterz*pow(log_f_z_max, scatterz_exp) );
654 
655  double maxs1 = std::max(s1, s2);
656  double maxs2 = std::max(maxs1, s3);
657  double max_scatter_intr = std::max(maxs2, s4);
658 
659  // Integrate
660  _bias[i] = wrapper::gsl::GSL_integrate_qag(integrand, std::max(min_logM-3.5*max_scatter_intr,log(cbl::Min(Mass_vector)/mass_pivot)/log(log_base)), std::min(max_logM+3.5*max_scatter_intr,log(cbl::Max(Mass_vector)/mass_pivot)/log(log_base)));
661 
662  /*
663  integration_limits[0] = {std::max(min_logM-3.5*max_scatter_intr,log(cbl::Min(Mass_vector)/mass_pivot)/log(log_base)), std::min(max_logM+3.5*max_scatter_intr,log(cbl::Max(Mass_vector)/mass_pivot)/log(log_base))};
664  integration_limits[1] = {cbl::Min(z_for_DN), cbl::Max(z_for_DN)};
665  integration_limits[2] = {std::max(dummy_proxy - 3.5*pp->proxy_rel_err*dummy_proxy, 0.00001), dummy_proxy + 3.5*pp->proxy_rel_err*dummy_proxy};
666  _bias[i] = CW.IntegrateVegas(integration_limits,false);
667  */
668 
669  }
670 
671  bias = Average(_bias);
672 
673  }
674 
675  // set the value of the bias
676  parameter[parameter.size()-1] = bias;
677 
678  return modelling::twopt::damped_Xi(rad, bias, cosmo.linear_growth_rate(pp->redshift), SigmaS, pp->kk, Pk_interp);
679 }
680 
681 
682 // ============================================================================================
683 
684 
685 std::vector<double> cbl::modelling::twopt::xi0_damped_bias_sigmaz (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
686 {
687  // structure contaning the required input data
688  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
689 
690  // input likelihood parameters
691 
692  // bias
693  const double bias = parameter[0];
694 
695  // damping scale
696  const double SigmaS = par::cc*parameter[1]/pp->HHfid;
697 
698  return modelling::twopt::damped_Xi(rad, bias, pp->linear_growth_rate_z, SigmaS, pp->kk, pp->func_Pk);
699 }
700 
701 
702 // ============================================================================================
703 
704 
705 std::vector<double> cbl::modelling::twopt::xi0_linear_sigma8_clusters (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
706 {
707  // structure contaning the required input data
708  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
709 
710  // redefine the cosmology
711  cosmology::Cosmology cosmo = *pp->cosmology;
712 
713 
714  // input likelihood parameters
715 
716  // sigma8
717  const double sigma8 = parameter[0];
718  cosmo.set_sigma8(sigma8);
719 
720  // sigma8(z)
721  const double sigma8_z = sigma8*cosmo.DN(pp->redshift);
722 
723 
724  // output likelihood parameters
725 
726  // mean bias
727 
728  vector<double> bias(pp->cluster_mass_proxy->ndata());
729 
730  for (int k=0; k<pp->cluster_mass_proxy->ndata(); k++) {
731 
732  const double sigma8fid = pp->sigma8_z/cosmo.DN(pp->redshift);
733 
734  const double sigma = pp->cluster_mass_proxy->extra_info(0, k)*(sigma8/sigma8fid);
735 
736  bias[k] = cosmo.bias_halo(pp->cluster_mass_proxy->data(k), sigma, pp->cluster_mass_proxy->xx(k), pp->model_bias, false, pp->output_root, "Spline", pp->Delta, 1., pp->norm, pp->k_min, pp->k_max, pp->prec, pp->method_Pk);
737  }
738 
739  const double mean_bias = Average(bias);
740  parameter[1] = mean_bias;
741 
742 
743  // sigma8(z)
744  parameter[2] = sigma8_z;
745 
746 
747  // fixed parameters
748 
749  // f(z)*sigma8(z)
750  const double fsigma8 = pp->linear_growth_rate_z*sigma8_z;
751 
752 
753  // return the redshift-space monopole of the two-point correlation function
754 
755  vector<double> xi(rad.size(), 0);
756 
757  const double fact = xi_ratio(fsigma8, mean_bias*sigma8_z)*pow(mean_bias, 2)*pow(sigma8_z/pp->sigma8_z, 2);
758 
759  for (size_t i =0; i<xi.size(); i++)
760  xi[i] = fact*pp->func_xi->operator()(rad[i]);
761 
762  return xi;
763 
764 }
765 
766 
767 // ============================================================================================
768 
769 
770 std::vector<double> cbl::modelling::twopt::xi0_linear_one_cosmo_par_clusters (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
771 {
772  // structure contaning the required input data
773  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
774 
775  // redefine the cosmology
776  cosmology::Cosmology cosmo = *pp->cosmology;
777 
778  // input likelihood parameter: the cosmological parameter used to
779  // compute the dark matter two-point correlation function in real
780  // space
781  cosmo.set_parameter(pp->Cpar[0], parameter[0]);
782 
783  // the linear bias
784  const double bias = pp->cosmopar_bias_interp_1D(parameter[0]);
785  parameter[1] = bias;
786 
787  // the AP parameter
788  const double alpha = cosmo.D_V(pp->redshift)/pp->DVfid;
789 
790 
791  // return the redshift-space monopole of the two-point correlation function
792 
793  vector<double> new_rad = rad;
794  for (size_t i=0; i<rad.size(); i++)
795  new_rad[i] *= alpha;
796 
797  return cosmo.xi0_Kaiser(new_rad, bias, pp->method_Pk, pp->redshift, false, pp->output_root, pp->NL, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->file_par);
798 }
799 
800 
801 // ============================================================================================
802 
803 
804 std::vector<double> cbl::modelling::twopt::xi0_linear_two_cosmo_pars_clusters (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
805 {
806  // structure contaning the required input data
807  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
808 
809  // redefine the cosmology
810  cosmology::Cosmology cosmo = *pp->cosmology;
811 
812  // input likelihood parameters: the cosmological parameters used to
813  // compute the dark matter two-point correlation function in real
814  // space
815  cosmo.set_parameter(pp->Cpar[0], parameter[0]);
816  cosmo.set_parameter(pp->Cpar[1], parameter[1]);
817 
818  // the linear bias
819  const double bias = pp->cosmopar_bias_interp_2D(parameter[0], parameter[1]);
820  parameter[2] = bias;
821 
823  const double alpha = cosmo.D_V(pp->redshift)/pp->DVfid;
824  parameter[3] = alpha;
825 
826 
827  // return the redshift-space monopole of the two-point correlation function
828 
829  vector<double> new_rad = rad;
830  for (size_t i=0; i<rad.size(); i++)
831  new_rad[i] *= alpha;
832 
833  return cosmo.xi0_Kaiser(new_rad, bias, pp->method_Pk, pp->redshift, false, pp->output_root, pp->NL, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->file_par);
834 }
835 
836 
837 // ============================================================================================
838 
839 
840 std::vector<double> cbl::modelling::twopt::xi0_linear_cosmology_clusters (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
841 {
842  // structure contaning the required input data
843  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
844 
845  // redefine the cosmology
846  cosmology::Cosmology cosmo = *pp->cosmology;
847 
848  // input likelihood parameters
849 
850  // set the cosmological parameters used to compute the dark matter
851  // two-point correlation function in real space
852  for (size_t i=0; i<pp->Cpar.size(); ++i)
853  cosmo.set_parameter(pp->Cpar[i], parameter[i]);
854 
855 
856  // bias
857 
858  vector<double> mass_grid = logarithmic_bin_vector(pp->cluster_mass_proxy->ndata()/10, Min(pp->cluster_mass_proxy->data()), Max(pp->cluster_mass_proxy->data()));
859 
860  const double bias = cosmo.bias_eff_mass(pp->cluster_mass_proxy->data(), mass_grid, pp->cluster_mass_proxy->xx(), pp->model_bias, pp->method_Pk, pp->meanType, false, pp->output_root, pp->Delta)[0];
861 
862 
863  // fixed parameters
864 
866  const double alpha = cosmo.D_V(pp->redshift)/pp->DVfid;
867 
868  // return the redshift-space monopole of the two-point correlation function
869  vector<double> new_rad = rad;
870  for (size_t i=0; i<rad.size(); i++)
871  new_rad[i] *= alpha;
872 
873  // return the redshift-space monopole of the two-point correlation function
874  const double sigma8 = parameter[0];
875  const double sigma8_z = sigma8*pp->cosmology->DN(pp->redshift);
876  const double fsigma8 = pp->linear_growth_rate_z*sigma8_z;
877 
878  vector<double> xi(rad.size(), 0);
879 
880  const double fact = xi_ratio(fsigma8, bias*sigma8_z)*pow(bias, 2)*pow(sigma8_z/pp->sigma8_z, 2);
881 
882  for (size_t i =0; i<xi.size(); i++)
883  xi[i] = fact*pp->func_xi->operator()(new_rad[i]);
884 
885  return xi;
886 
887 }
888 
889 
890 // ============================================================================================
891 
892 
893 std::vector<double> cbl::modelling::twopt::xi0_linear_cosmology_clusters_selection_function (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
894 {
895  // structure contaning the required input data
896  shared_ptr<STR_data_model> pp = static_pointer_cast<STR_data_model>(inputs);
897 
898  // set the test cosmology
899  cosmology::Cosmology cosmo = *pp->test_cosmology;
900 
901 
902  // ----- input likelihood parameters -----
903 
904  // set the cosmological parameters used to compute the dark matter
905  // two-point correlation function in real space
906  for (size_t i=0; i<pp->Cpar.size(); ++i)
907  cosmo.set_parameter(pp->Cpar[i], parameter[i]);
908 
909  // set the alpha parameter of the cluster mass scaling relation
910  const double alpha = parameter[pp->Cpar.size()];
911 
912 
913  // set the function to estimate the linear dark matter power spectrum at z=0, by interpolating it from a grid
914 
915  const vector<double> Pk_grid = cosmo.Pk_matter(pp->kk, pp->method_Pk, false, 0., false, pp->output_root, -1, pp->k_min, pp->k_max, pp->prec, pp->file_par);
916  glob::FuncGrid interp_Pk(pp->kk, Pk_grid, "Spline");
917 
918 
919  // compute sigma and dlnsigma, by interpolating them from a grid
920 
921  // set the cluster masses and rho_m
922  const vector<double> mass = pp->mass;
923  const double rho = cosmo.rho_m(0.);
924 
925  vector<double> sigma_grid, dnsigma_grid;
926 
927  for (size_t i=0; i<mass.size(); i++) {
928 
929  const double RR = Radius(mass[i], rho);
930 
931  // compute sigma
932  auto func_sigma = [&] (double kk)
933  {
934  return pow(TopHat_WF(kk*RR)*kk, 2)*interp_Pk(kk);
935  };
936  sigma_grid.emplace_back(sqrt(1./(2.*pow(par::pi, 2))*wrapper::gsl::GSL_integrate_qag(func_sigma, pp->k_min, pp->k_max, pp->prec)));
937 
938  // compute dlnsigma
939  const double dRdM = pow(3./(4.*par::pi*rho), 1./3.)*pow(mass[i], -2./3.)/3.;
940  auto func_dnsigma = [&] (const double kk)
941  {
942  double filter = 2*TopHat_WF(kk*RR)*TopHat_WF_D1(kk*RR)*kk*dRdM;
943  return filter*pow(kk, 2)*interp_Pk(kk);
944  };
945  dnsigma_grid.emplace_back(1./(2.*pow(par::pi, 2))*wrapper::gsl::GSL_integrate_qag(func_dnsigma, pp->k_min, pp->k_max, pp->prec));
946 
947  }
948 
949  glob::FuncGrid interp_sigma(mass, sigma_grid, "Spline");
950  glob::FuncGrid interp_DnSigma(mass, dnsigma_grid, "Spline");
951 
952 
953  // compute the bias
954  const double bias = cosmo.bias_eff_selection_function(interp_sigma, interp_DnSigma, *pp->interp_SelectionFunction_cut, pp->Mass_min, pp->Mass_max, {pp->redshift}, pp->model_bias, pp->model_MF, "EisensteinHu", alpha, false, pp->output_root, pp->Delta, -1., "Spline", pp->norm, pp->k_min, pp->k_max, pp->prec)[0]; // check!!!
955  parameter[pp->Cpar.size()+1] = bias;
956 
957  // set the AP factor
958  const double AP_factor = cosmo.D_V(pp->redshift)/pp->DVfid;
959 
960  // rescale with the AP factor
961  vector<double> new_rad = rad;
962  for (size_t i=0; i<rad.size(); i++)
963  new_rad[i] *= AP_factor;
964 
965  // compute the real-space monopole of the two-point correlation function at z=0, by Fourier transforming the P(k)
966  vector<double> xi = wrapper::fftlog::transform_FFTlog(new_rad, 1, pp->kk, Pk_grid, 0);
967 
968  // compute the redshift-space monopole at z=pp->redshift (scaling by D(z)/D(0) the monopole at z=0)
969  const double fact = bias*bias*xi_ratio(cosmo.linear_growth_rate(pp->redshift)/bias)*pow(cosmo.DN(pp->redshift), 2);
970  for (size_t i=0; i<xi.size(); i++)
971  xi[i] *= fact;
972 
973  return xi;
974 }
975 
976 
977 // ============================================================================================
978 
979 
980 double cbl::modelling::twopt::Ncen (const double Mass, const double lgMmin, const double sigmalgM)
981 {
982  const double Nc = 0.5*(1.+gsl_sf_erf(log10(Mass)-lgMmin)/(sqrt(2.)*sigmalgM));
983  return (Nc<0 || std::isnan(Nc)) ? 0. : Nc;
984 }
985 
986 
987 // ============================================================================================
988 
989 
990 double cbl::modelling::twopt::Nsat (const double Mass, const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha)
991 {
992  const double Ns = Ncen(Mass, lgMmin, sigmalgM)*pow((Mass-pow(10.,lgM0))/pow(10.,lgM1),alpha);
993  return (Ns<0 || std::isnan(Ns)) ? 0. : Ns;
994 }
995 
996 
997 // ============================================================================================
998 
999 
1000 double cbl::modelling::twopt::Navg (const double Mass, const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha)
1001 {
1002  return Ncen(Mass, lgMmin, sigmalgM) + Nsat(Mass, lgMmin, sigmalgM, lgM0, lgM1, alpha);
1003 }
1004 
1005 
1006 // ============================================================================================
1007 
1008 
1009 double cbl::modelling::twopt::ng (const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha, const std::shared_ptr<void> inputs)
1010 {
1011  // structure contaning the HOD input data
1012  shared_ptr<STR_data_HOD> pp = static_pointer_cast<STR_data_HOD>(inputs);
1013 
1014  auto ng_integrand = [&] (const double lgmass)
1015  {
1016  return pp->interpMF(pow(10., lgmass))*Navg(pow(10., lgmass), lgMmin, sigmalgM, lgM0, lgM1, alpha)*pow(10., lgmass)*log(10.);
1017  };
1018 
1019  return wrapper::gsl::GSL_integrate_qag(ng_integrand, log10(1.e10), log10(1.e16));
1020 }
1021 
1022 
1023 // ============================================================================================
1024 
1025 
1026 double cbl::modelling::twopt::bias (const double Mmin, const double sigmalgM, const double M0, const double M1, const double alpha, const std::shared_ptr<void> inputs)
1027 {
1028  // structure contaning the HOD input data
1029  shared_ptr<STR_data_HOD> pp = static_pointer_cast<STR_data_HOD>(inputs);
1030 
1031  auto func = [&] (double mass)
1032  {
1033  // halo mass function -> it depends on cosmology
1034  const double dndM = pp->cosmology->mass_function(mass, pp->func_sigma->operator()(mass), pp->func_dlnsigma->operator()(mass), pp->redshift, pp->model_MF, false, pp->output_root, pp->Delta, pp->interpType, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->method_Pk, pp->input_file, pp->is_parameter_file);
1035 
1036  // halo bias -> it depends on cosmology
1037  const double bias_halo = pp->cosmology->bias_halo(mass, pp->func_sigma->operator()(mass), pp->redshift, pp->model_bias, false, pp->output_root, pp->interpType, pp->Delta, pp->kk, pp->norm, pp->k_min, pp->k_max, pp->prec, pp->method_Pk, pp->input_file, pp->is_parameter_file);
1038 
1039  // average number of galaxies in each halo -> it depends on
1040  // galaxy evolution
1041  const double NN = Navg(mass, Mmin, sigmalgM, M0, M1, alpha);
1042 
1043  return dndM*NN*bias_halo;
1044  };
1045 
1046  return 1./ng(Mmin, sigmalgM, M0, M1, alpha, inputs)*wrapper::gsl::GSL_integrate_qag(func, pp->Mh_min, pp->Mh_max);
1047 }
1048 
1049 
1050 // ============================================================================================
1051 
1052 
1053 double cbl::modelling::twopt::NcNs (const double Mass, const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha)
1054 {
1055  return Ncen(Mass, lgMmin, sigmalgM)*Nsat(Mass, lgMmin, sigmalgM, lgM0, lgM1, alpha);
1056 }
1057 
1058 
1059 // ============================================================================================
1060 
1061 
1062 double cbl::modelling::twopt::NsNs1 (const double Mass, const double Mmin, const double sigmalgM, const double M0, const double M1, const double alpha)
1063 {
1064  return pow(Nsat(Mass, Mmin, sigmalgM, M0, M1, alpha), 2);
1065 }
1066 
1067 
1068 // ============================================================================================
1069 
1070 
1071 double cbl::modelling::twopt::Pk_cs (const double kk, const std::shared_ptr<void> inputs, std::vector<double> &parameter) {
1072  // structure contaning the HOD input data
1073  shared_ptr<STR_data_HOD> pp = static_pointer_cast<STR_data_HOD>(inputs);
1074 
1075  // input parameters
1076  const double lgMmin = parameter[0];
1077  const double sigmalgM = parameter[1];
1078  const double lgM0 = parameter[2];
1079  const double lgM1 = parameter[3];
1080  const double alpha = parameter[4];
1081 
1082  // build a HaloProfile object for computing the density profile in Fourier space
1083  cbl::cosmology::HaloProfile halo_profile (*pp->cosmology, pp->redshift, "Duffy", 0., 200., pp->profile, pp->halo_def);
1084 
1085  auto Pk_cs_integrand = [&] (const double lgmass) {
1086  halo_profile.set_mass(pow(10., lgmass));
1087  return pp->interpMF(pow(10., lgmass))*NcNs(pow(10., lgmass), lgMmin, sigmalgM, lgM0, lgM1, alpha)*halo_profile.density_profile_FourierSpace(kk)*pow(10., lgmass)*log(10.);
1088  };
1089 
1090  return 2./pow(ng(lgMmin, sigmalgM, lgM0, lgM1, alpha, inputs), 2)*wrapper::gsl::GSL_integrate_qag(Pk_cs_integrand, log10(pp->Mh_min), log10(pp->Mh_max));
1091 
1092 }
1093 
1094 
1095 // ============================================================================================
1096 
1097 
1098 double cbl::modelling::twopt::Pk_ss (const double kk, const std::shared_ptr<void> inputs, std::vector<double> &parameter) {
1099 
1100  // structure contaning the HOD input data
1101  shared_ptr<STR_data_HOD> pp = static_pointer_cast<STR_data_HOD>(inputs);
1102 
1103  // input parameters
1104  const double lgMmin = parameter[0];
1105  const double sigmalgM = parameter[1];
1106  const double lgM0 = parameter[2];
1107  const double lgM1 = parameter[3];
1108  const double alpha = parameter[4];
1109 
1110  // build a HaloProfile object for computing the density profile in Fourier space
1111  cbl::cosmology::HaloProfile halo_profile (*pp->cosmology, pp->redshift, "Duffy", 0., 200., pp->profile, pp->halo_def);
1112 
1113  auto Pk_ss_integrand = [&] (const double lgmass) {
1114  halo_profile.set_mass(pow(10., lgmass));
1115  return pp->interpMF(pow(10., lgmass))*NsNs1(pow(10., lgmass), parameter[0], parameter[1], parameter[2], parameter[3], parameter[4])*pow(halo_profile.density_profile_FourierSpace(kk), 2)*pow(10., lgmass)*log(10.);
1116  };
1117 
1118  return 1./pow(ng(lgMmin, sigmalgM, lgM0, lgM1, alpha, inputs), 2)*wrapper::gsl::GSL_integrate_qag(Pk_ss_integrand, log10(pp->Mh_min), log10(pp->Mh_max));
1119 }
1120 
1121 
1122 // ============================================================================================
1123 
1124 
1125 double cbl::modelling::twopt::Pk_1halo (const double kk, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1126 {
1127  return Pk_cs(kk, inputs, parameter)+Pk_ss(kk, inputs, parameter);
1128 }
1129 
1130 
1131 // ============================================================================================
1132 
1133 
1134 double cbl::modelling::twopt::Pk_2halo (const double kk, const std::shared_ptr<void> inputs, std::vector<double> &parameter) {
1135 
1136  // structure contaning the HOD input data
1137  shared_ptr<STR_data_HOD> pp = static_pointer_cast<STR_data_HOD>(inputs);
1138 
1139  // input parameters
1140  const double lgMmin = parameter[0];
1141  const double sigmalgM = parameter[1];
1142  const double lgM0 = parameter[2];
1143  const double lgM1 = parameter[3];
1144  const double alpha = parameter[4];
1145 
1146  // build a HaloProfile object for computing the density profile in Fourier space
1147  cbl::cosmology::HaloProfile halo_profile (*pp->cosmology, pp->redshift, "Duffy", 0., 200., pp->profile, pp->halo_def);
1148 
1149  auto Pk_2halo_integrand = [&] (const double lgmass) {
1150  halo_profile.set_mass(pow(10., lgmass));
1151  return pp->interpMF(pow(10., lgmass))*Navg(pow(10., lgmass), parameter[0], parameter[1], parameter[2], parameter[3], parameter[4])*pp->interpBias(pow(10., lgmass))*halo_profile.density_profile_FourierSpace(kk)*pow(10., lgmass)*log(10.);
1152  };
1153  return pp->interpPk(kk)*1./pow(ng(lgMmin, sigmalgM, lgM0, lgM1, alpha, inputs),2.)*pow(wrapper::gsl::GSL_integrate_qag(Pk_2halo_integrand, log10(pp->Mh_min), log10(pp->Mh_max)), 2.);
1154 }
1155 
1156 
1157 // ============================================================================================
1158 
1159 
1160 double cbl::modelling::twopt::Pk_HOD (const double kk, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1161 {
1162  return Pk_1halo(kk, inputs, parameter)+Pk_2halo(kk, inputs, parameter);
1163 }
1164 
1165 
1166 // ============================================================================================
1167 
1168 
1169 vector<double> cbl::modelling::twopt::xi_1halo (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1170 {
1171  //structure contaning the required input data
1172  shared_ptr<STR_data_HOD> pp = static_pointer_cast<STR_data_HOD>(inputs);
1173 
1174  vector<double> Pk(pp->kkvec.size(), 0);
1175 
1176  #pragma omp parallel num_threads(omp_get_max_threads())
1177  {
1178  #pragma omp for schedule(static, 2)
1179  for (size_t i=0; i<pp->kkvec.size(); i++)
1180  Pk[i] = Pk_1halo(pp->kkvec[i], inputs, parameter);
1181  }
1182 
1183  return wrapper::fftlog::transform_FFTlog(rad, 1, pp->kkvec, Pk, 0);
1184 }
1185 
1186 
1187 // ============================================================================================
1188 
1189 
1190 vector<double> cbl::modelling::twopt::xi_2halo (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1191 {
1192  //structure contaning the required input data
1193  shared_ptr<STR_data_HOD> pp = static_pointer_cast<STR_data_HOD>(inputs);
1194 
1195  vector<double> Pk(pp->kkvec.size(), 0);
1196 
1197  #pragma omp parallel num_threads(omp_get_max_threads())
1198  {
1199  #pragma omp for schedule(static, 2)
1200  for (size_t i=0; i<pp->kkvec.size(); i++)
1201  Pk[i] = Pk_2halo(pp->kkvec[i], inputs, parameter);
1202  }
1203 
1204  return wrapper::fftlog::transform_FFTlog(rad, 1, pp->kkvec, Pk, 0);
1205 
1206 }
1207 
1208 
1209 // ============================================================================================
1210 
1211 
1212 std::vector<double> cbl::modelling::twopt::xi_HOD (const std::vector<double> rad, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1213 {
1214  const vector<double> xi1h = xi_1halo(rad, inputs, parameter);
1215  const vector<double> xi2h = xi_2halo(rad, inputs, parameter);
1216  vector<double> xi(rad.size());
1217 
1218 #pragma omp parallel num_threads(omp_get_max_threads())
1219  {
1220 #pragma omp for schedule(static, 2)
1221  for (size_t i=0; i<rad.size(); i++)
1222  xi[i] = xi1h[i]+xi2h[i];
1223  }
1224 
1225  return xi;
1226 }
1227 
1228 
1229 // ============================================================================================
1230 
1231 
1232 double cbl::modelling::twopt::xi_zspace (FunctionVectorVectorPtrVectorRef func, const double rp, const double pi, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1233 {
1234  // structure contaning the HOD input data
1235 
1236  shared_ptr<STR_data_HOD> pp = static_pointer_cast<STR_data_HOD>(inputs);
1237 
1238 
1239  // input parameters
1240 
1241  const double Mmin = parameter[0];
1242  const double sigmalgM = parameter[1];
1243  const double M0 = parameter[2];
1244  const double M1 = parameter[3];
1245  const double alpha = parameter[4];
1246 
1247 
1248  const double rad = sqrt(pow(rp, 2)+pow(pi, 2));
1249  const double mu = rp/rad;
1250 
1251  const double beta2 = pp->cosmology->linear_growth_rate(pp->redshift)/bias(Mmin, sigmalgM, M0, M1, alpha, inputs);
1252 
1253  const double fact_xi0 = 1.+2./3.*beta2+1./5.*beta2*beta2;
1254  const double fact_xi2 = (4./3.*beta2+4./7.*beta2*beta2);
1255  const double fact_xi4 = 8./35.*beta2*beta2;
1256 
1257  auto func3 = [&] (double yy) { return func({yy}, inputs, parameter)[0]*pow(yy, 2); };
1258  const double J3 = pow(rad, -3)*wrapper::gsl::GSL_integrate_qag(func3, 0., rad);
1259 
1260  auto func5 = [&] (double yy) { return func({yy}, inputs, parameter)[0]*pow(yy, 4); };
1261  const double J5 = pow(rad, -5)*wrapper::gsl::GSL_integrate_qag(func5, 0., rad);
1262 
1263  const double xi0 = fact_xi0*func({rad}, inputs, parameter)[0];
1264  const double xi2 = fact_xi2*(func({rad}, inputs, parameter)[0]-3.*J3);
1265  const double xi4 = fact_xi4*(func({rad}, inputs, parameter)[0]+15./2.*J3-35./2.*J5);
1266 
1267  return xi0 + xi2*legendre_polynomial(mu, 2) + xi4*legendre_polynomial(mu, 4);
1268 }
1269 
1270 
1271 // ============================================================================================
1272 
1273 
1274 double cbl::modelling::twopt::xi_1halo_zspace (const double rp, const double pi, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1275 {
1276  return xi_zspace(xi_1halo, rp, pi, inputs, parameter);
1277 }
1278 
1279 
1280 // ============================================================================================
1281 
1282 
1283 double cbl::modelling::twopt::xi_2halo_zspace (const double rp, const double pi, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1284 {
1285  return xi_zspace(xi_2halo, rp, pi, inputs, parameter);
1286 }
1287 
1288 
1289 // ============================================================================================
1290 
1291 
1292 double cbl::modelling::twopt::xi_HOD_zspace (const double rp, const double pi, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
1293 {
1294  return xi_zspace(xi_HOD, rp, pi, inputs, parameter);
1295 }
1296 
The class HaloProfile.
Global functions to model the monopole of the two-point correlation function.
The class Cosmology.
Definition: Cosmology.h:277
double DN(const double redshift, const double redshift_norm=0., const double prec=1.e-4) const
the normalised amplitude of the growing mode at a given redshift,
Definition: Cosmology.cpp:691
double rho_m(const double redshift=0., const bool unit1=false, const bool nu=false) const
the mean cosmic background density
Definition: Cosmology.cpp:1274
double xi0_Kaiser(const double rad, const double f_sigma8, const double bias_sigma8, const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const bool xiType=0, const double k_star=-1., const bool NL=false, const int norm=-1, const double r_min=0.1, const double r_max=150., const double k_min=0.001, const double k_max=100., const double aa=0., const bool GSL=false, const double prec=1.e-2, const std::string file_par=par::defaultString)
monopole of the redshift-space two-point correlation function in the Kaiser limit
Definition: PkXizSpace.cpp:46
std::vector< double > bias_eff_selection_function(const glob::FuncGrid interp_sigma, const glob::FuncGrid interp_DnSigma, const glob::FuncGrid interp_SF, const double Mass_min, const double Mass_max, const std::vector< double > redshift, const std::string model_bias, const std::string model_MF, const std::string method_SS, const double alpha=1., const bool store_output=true, const std::string output_root="test", const double Delta_crit=200., const double kk=-1., const std::string interpType="Linear", const int norm=-1, const double k_min=0.001, const double k_max=100., const double prec=1.e-2, const std::string input_file=par::defaultString, const bool is_parameter_file=true)
effective bias of dark matter haloes, computed using a given selection function; σ(mass) and dlnσ/dM ...
Definition: Bias.cpp:385
void set_sigma8(const double sigma8=-1.)
set the value of σ8
Definition: Cosmology.h:1547
void set_parameter(const CosmologicalParameter parameter, const double value)
set the value of one cosmological paramter
Definition: Cosmology.cpp:424
double D_V(const double redshift) const
the average distance at a given redshift, used to rescale the correlation function
Definition: Cosmology.cpp:884
double HH(const double redshift=0.) const
the Hubble function
Definition: Cosmology.cpp:570
double bias_halo(const double Mass, const double redshift, const std::string author, const std::string method_SS, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double Delta=200., const double kk=-1., const int norm=-1, const double k_min=0.001, const double k_max=100., const double prec=1.e-2, const std::string input_file=par::defaultString, const bool is_parameter_file=true)
bias of dark matter haloes
Definition: Bias.cpp:44
std::vector< double > Pk_matter(const std::vector< double > kk, const std::string method_Pk, const bool NL, const double redshift, const bool store_output=true, const std::string output_root="test", const int norm=-1, const double k_min=0.001, const double k_max=100., const double prec=1.e-2, const std::string file_par=par::defaultString, const bool unit1=false)
the dark matter power spectrum
Definition: PkXi.cpp:1331
std::vector< double > bias_eff_mass(const std::vector< double > MM, const std::vector< double > redshift, const std::string model_bias, const std::string method_SS, const std::string meanType="mean_bias", const bool store_output=true, const std::string output_root="test", const double Delta=200., const double kk=-1., const std::string interpType="Linear", const int norm=-1, const double k_min=0.001, const double k_max=100., const double prec=1.e-2, const std::string input_file=par::defaultString, const bool is_parameter_file=true)
effective bias of dark matter haloes, computed by averaging the bias of a set of haloes
Definition: Bias.cpp:298
double sigma2M(const double mass, const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true, const bool unit1=false) const
the mass variance,
Definition: Sigma.cpp:230
double linear_growth_rate(const double redshift, const double prec=1.e-4) const
the linear growth rate at a given redshift,
Definition: Cosmology.cpp:662
double OmegaM(const double redshift=0.) const
the matter density at a given redshift
Definition: Cosmology.cpp:579
The class HaloProfile.
Definition: HaloProfile.h:52
void set_mass(const double mass=par::defaultDouble)
set the private member m_mass
Definition: HaloProfile.h:977
double density_profile_FourierSpace(const double kk)
the Fourier transform of the normalised halo density
The class FuncGrid.
Definition: FuncGrid.h:55
static const std::string defaultString
default std::string value
Definition: Constants.h:336
static const double pi
: the ratio of a circle's circumference to its diameter
Definition: Constants.h:199
static const double alpha
: the fine-structure constant
Definition: Constants.h:233
static const double cc
: the speed of light in vacuum (the value is exact) [km/sec]
Definition: Constants.h:221
std::vector< double > xi0_polynomial_LinearPoint(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function
std::vector< double > xi0_linear(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function
double Nsat(const double Mass, const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha)
the average number of satellite galaxies hosted in a dark matter halo of a given mass
std::vector< double > xi0_linear_bias_cosmology(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function
std::vector< double > damped_Xi(const std::vector< double > ss, const double bias, const double linear_growth_rate, const double SigmaS, const std::vector< double > kk, const std::shared_ptr< cbl::glob::FuncGrid > PkDM)
the damped two-point correlation monopole; from Sereno et al. 2015
std::vector< double > xi0_linear_two_cosmo_pars_clusters(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function, the bias is computed by the input clust...
std::vector< double > xi0_damped_scaling_relation_sigmaz_cosmology(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
the damped two-point correlation monopole; from Sereno et al. 2015
double Navg(const double Mass, const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha)
the average number of galaxies hosted in a dark matter halo of a given mass
double NsNs1(const double Mass, const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha)
the mean number of satellite-satellite galaxy pairs
double Pk_ss(const double kk, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the satellite-satellite part of the 1-halo term of the power spectrum
double NcNs(const double Mass, const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha)
the mean number of central-satellite galaxy pairs
std::vector< double > xi0_damped_scaling_relation_sigmaz(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
the damped two-point correlation monopole; from Sereno et al. 2015
double xi_HOD_zspace(const double rp, const double pi, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
HOD model of the redshift-space monopole of the two-point correlation function.
std::vector< double > xi_HOD(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
HOD model of the monopole of the two-point correlation function.
std::vector< double > xi0_linear_cosmology_clusters_selection_function(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the redshift-space two-point correlation function of galaxy clusters,...
double Pk_1halo(const double kk, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the 1-halo term of the power spectrum
double xi_2halo_zspace(const double rp, const double pi, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the 2-halo term of the redshift-space monopole of the two-point correlation function
std::vector< double > xi0_linear_one_cosmo_par_clusters(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function, the bias is computed by the input clust...
std::vector< double > xi0_damped_bias_sigmaz(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
the damped two-point correlation monopole; from Sereno et al. 2015
std::vector< double > xi0_BAO_sigmaNL(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the BAO signal in the monopole of the two-point correlation function
double xi_1halo_zspace(const double rp, const double pi, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the 1-halo term of the redshift-space monopole of the two-point correlation function
double xi_zspace(FunctionVectorVectorPtrVectorRef func, const double rp, const double pi, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
function used to compute the redshift-space monopole of the two-point correlation function
double Ncen(const double Mass, const double lgMmin, const double sigmalgM)
the average number of central galaxies hosted in a dark matter halo of a given mass
std::vector< double > xi_2halo(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the 2-halo term of the monopole of the two-point correlation function
double Pk_cs(const double kk, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the central-satellite part of the 1-halo term of the power spectrum
std::vector< double > xi0_linear_cosmology_clusters(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function, the bias is computed by the input clust...
double Pk_HOD(const double kk, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
HOD model of the power spectrum.
std::vector< double > xi0_linear_sigma8_bias(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function in redshift space
double Pk_2halo(const double kk, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the 2-halo term of the power spectrum
std::vector< double > xi0_linear_sigma8_clusters(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function, the bias is computed by the input clust...
std::vector< double > xi0_linear_cosmology(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function
double bias(const double Mmin, const double sigmalgM, const double M0, const double M1, const double alpha, const std::shared_ptr< void > inputs)
the mean galaxy bias
std::vector< double > xi_1halo(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the 1-halo term of the monopole of the two-point correlation function
double ng(const double lgMmin, const double sigmalgM, const double lgM0, const double lgM1, const double alpha, const std::shared_ptr< void > inputs)
the galaxy number density
std::vector< double > xi0_linear_LinearPoint(const std::vector< double > rad, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
model for the monopole of the two-point correlation function
std::vector< double > transform_FFTlog(const std::vector< double > yy, const int dir, const std::vector< double > xx, const std::vector< double > fx, const double mu=0, const double q=0, const double kr=1, const int kropt=0)
wrapper of the FFTlog to compute the discrete Fourier sine or cosine transform of a logarithmically...
Definition: FFTlog.cpp:43
double GSL_polynomial_eval(const double x, const std::shared_ptr< void > fixed_parameters, const std::vector< double > coeff)
evaluate a polynomial
Definition: GSLwrapper.cpp:715
double GSL_root_brent(gsl_function Func, const double low_guess, const double up_guess, const double rel_err=1.e-3, const double abs_err=0)
function to find roots using GSL qag method
Definition: GSLwrapper.cpp:430
double GSL_integrate_qag(gsl_function Func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000, const int rule=6)
integral, computed using the GSL qag method
Definition: GSLwrapper.cpp:180
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
T Min(const std::vector< T > vect)
minimum element of a std::vector
Definition: Kernel.h:1324
double Average(const std::vector< double > vect)
the average of a std::vector
Definition: Func.cpp:870
T Mass(const T RR, const T Rho)
the mass of a sphere of a given radius and density
Definition: Func.h:1243
T Radius(const T Mass, const T Rho)
the radius of a sphere of a given mass and density
Definition: Func.h:1220
std::vector< T > logarithmic_bin_vector(const size_t nn, const T min, const T max)
fill a std::vector with logarithmically spaced values
Definition: Kernel.h:1621
std::vector< T > linear_bin_vector(const size_t nn, const T min, const T max)
fill a std::vector with linearly spaced values
Definition: Kernel.h:1604
T TopHat_WF(const T kR)
the top-hat window function
Definition: Func.h:1195
std::function< std::vector< double >std::vector< double >, std::shared_ptr< void >, std::vector< double > &)> FunctionVectorVectorPtrVectorRef
typedef of a function returning a vector with a vector, a pointer and a vector reference in input
Definition: Kernel.h:705
T Max(const std::vector< T > vect)
maximum element of a std::vector
Definition: Kernel.h:1336
double xi_ratio(const double beta)
the ratio between the redshift-space and real-space correlation functions
Definition: FuncXi.cpp:287
T gaussian(T xx, std::shared_ptr< void > pp, std::vector< double > par)
the Gaussian function
Definition: Func.h:1127
T TopHat_WF_D1(const T kR)
the derivative of the top-hat window function
Definition: Func.h:1208
double legendre_polynomial(const double mu, const int l)
the order l Legendre polynomial
Definition: Func.cpp:1786