CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
ThreePointCorrelation_comoving_connected.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2015 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 
39 #include "Data1D.h"
41 
42 using namespace std;
43 
44 using namespace cbl;
45 using namespace catalogue;
46 using namespace triplets;
47 using namespace measure;
48 using namespace threept;
49 using namespace glob;
50 
51 
52 // ============================================================================================
53 
54 
55 void cbl::measure::threept::ThreePointCorrelation_comoving_connected::set_parameters (const triplets::TripletType tripletType, const double side_s, const double side_u, const double perc_increase, const int nbins)
56 {
57  double r12 = side_s;
58  double r12_binSize = r12*2.*perc_increase;
59  double r13 = side_s*side_u;
60  double r13_binSize = r13*2.*perc_increase;
61 
62  m_ddd = move(Triplet::Create(tripletType, r12, r12_binSize, r13, r13_binSize, nbins));
63  m_rrr = move(Triplet::Create(tripletType, r12, r12_binSize, r13, r13_binSize, nbins));
64  m_ddr = move(Triplet::Create(tripletType, r12, r12_binSize, r13, r13_binSize, nbins));
65  m_drr = move(Triplet::Create(tripletType, r12, r12_binSize, r13, r13_binSize, nbins));
66 }
67 
68 
69 // ============================================================================================
70 
71 
72 void cbl::measure::threept::ThreePointCorrelation_comoving_connected::set_parameters (const triplets::TripletType tripletType, const double r12, const double r12_binSize, const double r13, const double r13_binSize, const int nbins)
73 {
74  m_ddd = move(Triplet::Create(tripletType, r12, r12_binSize, r13, r13_binSize, nbins));
75  m_rrr = move(Triplet::Create(tripletType, r12, r12_binSize, r13, r13_binSize, nbins));
76  m_ddr = move(Triplet::Create(tripletType, r12, r12_binSize, r13, r13_binSize, nbins));
77  m_drr = move(Triplet::Create(tripletType, r12, r12_binSize, r13, r13_binSize, nbins));
78 }
79 
80 
81 // ============================================================================================
82 
83 
84 void cbl::measure::threept::ThreePointCorrelation_comoving_connected::measure (const std::string dir_output_triplets, const std::vector<std::string> dir_input_triplets, const bool count_ddd, const bool count_rrr, const bool count_ddr, const bool count_drr, const bool tcount, const double fact, const int seed)
85 {
86  (void)seed;
87 
88  // -------- count the data-data-data, random-random-random, data-data-random and data-random-random triplets, or read them from file --------
89 
90  count_allTriplets(dir_output_triplets, dir_input_triplets, count_ddd, count_rrr, count_ddr, count_drr, tcount, fact);
91 
92 
93  // ----------- compute the three-point correlation function -----------
94 
95  m_scale.resize(m_ddd->nbins()); m_zeta.resize(m_ddd->nbins()); m_error.resize(m_ddd->nbins());
96 
97  double nGal = m_data->weightedN();
98  double nRan = m_random->weightedN();
99 
100  coutCBL << "# Galaxies: " << nGal << " ; # Random: " << nRan << endl;
101 
102  double norm1 = (double(nGal)*double(nGal-1)*double(nGal-2))/6.;
103  double norm2 = (double(nGal)*double(nGal-1)*double(nRan))*0.5;
104  double norm3 = (double(nGal)*double(nRan)*double(nRan-1))*0.5;
105  double norm4 = (double(nRan)*double(nRan-1)*double(nRan-2))/6.;
106 
107  for (int i=0; i<m_ddd->nbins(); i++) {
108 
109  m_scale[i] = m_ddd->scale(i);
110 
111  if (m_ddd->TT1D(i)>0 && m_rrr->TT1D(i)>0) {
112  m_zeta[i] = ((m_ddd->TT1D(i)/norm1)/(m_rrr->TT1D(i)/norm4))-3.*((m_ddr->TT1D(i)/norm2)/(m_rrr->TT1D(i)/norm4))+3.*(((m_drr->TT1D(i)/norm3)/(m_rrr->TT1D(i)/norm4)))-1.;
113  m_error[i] = -1.; // work in progress...
114  }
115 
116  }
117 
118  m_dataset = move(unique_ptr<data::Data1D>(new data::Data1D(m_scale, m_zeta, m_error)));
119 }
120 
121 
122 // ============================================================================================
123 
124 
125 void cbl::measure::threept::ThreePointCorrelation_comoving_connected::measure (const std::vector<std::vector<double>> weight, const bool doJK, const std::string dir_output_triplets, const std::vector<std::string> dir_input_triplets, const bool count_ddd, const bool count_rrr, const bool count_ddr, const bool count_drr, const bool tcount, const double fact, const int seed)
126 {
127  (void)seed;
128 
129  // -------- count the data-data-data, random-random-random, data-data-random and data-random-random triplets, or read them from file --------
130 
131  count_allTriplets_region (weight, dir_output_triplets, dir_input_triplets, count_ddd, count_rrr, count_ddr, count_drr, tcount, fact);
132 
133 
134  // ----------- compute the three-point correlation function -----------
135 
136  m_scale.resize(m_ddd->nbins()); m_zeta.resize(m_ddd->nbins()); m_error.resize(m_ddd->nbins());
137 
138  double nGal = m_data->weightedN();
139  double nRan = m_random->weightedN();
140 
141  double norm1 = (double(nGal)*double(nGal-1)*double(nGal-2))/6.;
142  double norm2 = (double(nGal)*double(nGal-1)*double(nRan))*0.5;
143  double norm3 = (double(nGal)*double(nRan)*double(nRan-1))*0.5;
144  double norm4 = (double(nRan)*double(nRan-1)*double(nRan-2))/6.;
145 
146 
147  for (int i=0; i<m_ddd->nbins(); i++) {
148 
149  m_scale[i] = m_ddd->scale(i);
150 
151  if (m_ddd->TT1D(i)>0 && m_rrr->TT1D(i)>0)
152  m_zeta[i] = ((m_ddd->TT1D(i)/norm1)/(m_rrr->TT1D(i)/norm4))-3.*((m_ddr->TT1D(i)/norm2)/(m_rrr->TT1D(i)/norm4))+3.*(((m_drr->TT1D(i)/norm3)/(m_rrr->TT1D(i)/norm4)))-1.;
153 
154  }
155 
157 
158  vector<vector<double>> resampling_threept(weight.size(), vector<double>(m_ddd->nbins(), 0));
159  vector<long> region_list = m_data->region_list();
160 
161  vector<double> nData_reg_weighted, nRandom_reg_weighted;
162 
163  const int nRegions = m_data->nRegions();
164 
165  for (int i=0; i<nRegions; i++) {
166  nData_reg_weighted.push_back(m_data->weightedN_condition(Var::_Region_, region_list[i], region_list[i]+1, 0));
167  nRandom_reg_weighted.push_back(m_random->weightedN_condition(Var::_Region_, region_list[i], region_list[i]+1, 0));
168  }
169 
170 
171  for (size_t i=0; i<weight.size(); i++) {
172 
173  double nData = 0;
174  double nRan = 0;
175 
176  for (size_t j=0; j<weight[i].size(); j++) {
177  nData += weight[i][j]*nData_reg_weighted[j];
178  nRan += weight[i][j]*nRandom_reg_weighted[j];
179  }
180 
181  double norm1 = (double(nData)*double(nData-1)*double(nData-2))/6.;
182  double norm2 = (double(nData)*double(nData-1)*double(nRan))*0.5;
183  double norm3 = (double(nData)*double(nRan)*double(nRan-1))*0.5;
184  double norm4 = (double(nRan)*double(nRan-1)*double(nRan-2))/6.;
185 
186  for (int j=0; j<m_ddd->nbins(); j++)
187  if (m_ddd_regions[i]->TT1D(j)>0 && m_rrr_regions[i]->TT1D(j)>0)
188  resampling_threept[i][j] = ((m_ddd_regions[i]->TT1D(j)/norm1)/(m_rrr_regions[i]->TT1D(j)/norm4))-3.*((m_ddr_regions[i]->TT1D(j)/norm2)/(m_rrr_regions[i]->TT1D(j)/norm4))+3.*(((m_drr_regions[i]->TT1D(j)/norm3)/(m_rrr_regions[i]->TT1D(j)/norm4)))-1.;
189  }
190 
191  vector<vector<double>> cov_mat;
192  cbl::covariance_matrix(resampling_threept, cov_mat, doJK);
193  m_dataset = move(unique_ptr<data::Data1D>(new data::Data1D(m_scale, m_zeta, cov_mat)));
194 
195  m_error = m_dataset->error();
196 }
197 
198 
199 
200 // ============================================================================================
201 
202 
203 void cbl::measure::threept::ThreePointCorrelation_comoving_connected::measure (const cbl::measure::ErrorType errorType, const std::string dir_output_triplets, const std::vector<std::string> dir_input_triplets, const int nResamplings, const bool count_ddd, const bool count_rrr, const bool count_ddr, const bool count_drr, const bool tcount, const double fact, const int seed)
204 {
205 
206  switch(errorType) {
207 
209  {
210  measure(dir_output_triplets, dir_input_triplets, count_ddd, count_rrr, count_ddr, count_drr, tcount, fact);
211  break;
212  }
213 
215  {
216  const int nRegions = m_data->nRegions();
217 
218  vector<vector<double>> weight(nRegions, vector<double>(nRegions, 1));
219  for (int i=0; i<nRegions; i++)
220  weight[i][i] = 0;
221 
222  measure(weight, true, dir_output_triplets, dir_input_triplets, count_ddd, count_rrr, count_ddr, count_drr, tcount, fact);
223  break;
224  }
225 
227  {
228  const int nRegions = m_data->nRegions();
229 
230  random::UniformRandomNumbers_Int ran(0., nRegions-1, seed);
231 
232  int val = 3; // see Norberg et al. 2009
233 
234  vector<vector<double>> weight(nResamplings, vector<double>(nRegions, 0));
235  for (int i=0; i<nResamplings; i++)
236  for (int j=0; j<val*nRegions; j++)
237  weight[i][ran()] ++;
238 
239  measure(weight, false, dir_output_triplets, dir_input_triplets, count_ddd, count_rrr, count_ddr, count_drr, tcount, fact);
240  break;
241  }
242 
243  default:
244  ErrorCBL("no such kind of error type!", "measure", "ThreePointCorrelation_comoving_connected.cpp");
245  }
246 
247 }
248 
249 
250 // ============================================================================================
251 
252 
253 void cbl::measure::threept::ThreePointCorrelation_comoving_connected::write (const std::string dir, const std::string file) const
254 {
255  checkDim(m_scale, m_ddd->nbins(), "scale");
256 
257  string file_out = dir+file;
258  ofstream fout(file_out.c_str()); checkIO(fout, file_out);
259 
260  fout << "# scale zeta error(work in progress)" << endl;
261 
262  for (size_t i=0; i<m_scale.size(); i++)
263  fout << setiosflags(ios::fixed) << setprecision(4) << setw(10) << right << m_scale[i]
264  << " " << setiosflags(ios::fixed) << setprecision(4) << setw(10) << right << m_zeta[i]
265  << " " << setiosflags(ios::fixed) << setprecision(4) << setw(10) << right << m_error[i] << endl;
266 
267  fout.close(); coutCBL << endl << "I wrote the file: " << file_out << endl << endl;
268 }
269 
270 
271 // ============================================================================
272 
273 
274 void cbl::measure::threept::ThreePointCorrelation_comoving_connected::write_covariance (const std::string dir, const std::string file) const
275 {
276  m_dataset->write_covariance(dir, file);
277 }
278 
279 
The class Data1D.
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class ThreePointCorrelation_comoving_connected.
The class Data1D.
Definition: Data1D.h:51
void set_parameters(const triplets::TripletType tripletType, const double side_s, const double side_u, const double perc_increase, const int nbins)
set the binning parameters
void write(const std::string dir, const std::string file) const override
write the monopole of the two-point correlation function
void write_covariance(const std::string dir, const std::string file) const override
write the measured three-point correlation covariance
void measure(const std::string dir_output_triplets, const std::vector< std::string > dir_input_triplets={}, const bool count_ddd=true, const bool count_rrr=true, const bool count_ddr=true, const bool count_drr=true, const bool tcount=true, const double fact=0.1, const int seed=3213) override
method to measure the three-point correlation function
The class UniformRandomNumbers_Int.
ErrorType
the two-point correlation function error type
Definition: Measure.h:59
@ _Bootstrap_
Bootstrap resampling.
@ _None_
No error computed.
@ _Jackknife_
Jackknife resampling.
TripletType
the triplet type
Definition: Triplet.h:62
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void checkDim(const std::vector< T > vect, const int val, const std::string vector, bool equal=true)
check if the dimension of a std::vector is equal/lower than an input value
Definition: Kernel.h:1532
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780
void covariance_matrix(const std::vector< std::vector< double >> mat, std::vector< std::vector< double >> &cov, const bool JK=false)
compute the covariance matrix from an input dataset
Definition: Func.cpp:761