From fb235ae3f931eda6129f7f01b4a718d1b4e59efe Mon Sep 17 00:00:00 2001 From: jacquot Date: Thu, 31 Oct 2024 10:18:20 +0100 Subject: [PATCH 1/4] Adding jittering functions for the distance in PyGRB --- pycbc/conversions.py | 73 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/pycbc/conversions.py b/pycbc/conversions.py index 04f5dc1779d..07a0e99d2b0 100644 --- a/pycbc/conversions.py +++ b/pycbc/conversions.py @@ -28,6 +28,7 @@ one parameter given a set of inputs. """ +import random import copy import numpy import logging @@ -1806,6 +1807,75 @@ def nltides_gw_phase_diff_isco(f_low, f0, amplitude, n, m1, m2): return formatreturn(phi_i - phi_l, input_is_array) +def jittering_distance_gaussian(prev_dist, amp_cal_error, phase_cal_error): + """ Jitter the distance in order to take in account the + calibration errors in phase and amplitude by following a + gaussian distribution. Gaussian distribution is not + recommended because it could give you negative distance + as soon as it covers the entire real space. The formula + for mu and sigma are obtained in [PUT REF HERE] + + + Parameters + ---------- + prev_dist : numpy.array + distance distribution before the jittering + amp_cal_error : float + amplitude calibration error (percentage) of the wanted + detector. It will affect the width of the gaussian + distribution. + phase_cal_error : float + phase calibration error (degrees) of the wanted detector. + It will affect the center of the gaussian distribution. + + Returns + ------- + distance : numpy.array + distance distribution after the jittering, a gaussian + distribution centerend on the phase calibration error + and with a width of the amplitude calibration error. + + """ + mu = prev_dist * (1 + 0.5 * numpy.deg2rad(phase_cal_error)**2) + sigma = (amp_cal_error / 100) * prev_dist + + return numpy.random.normal(mu, sigma) + +def jittering_distance_lognormal(prev_dist, amp_cal_error, phase_cal_error): + """ Jitter the distance in order to take in account the + calibration errors in phase and amplitude by following a + lognormal distribution. The formula for mu and sigma are + obtained in [PUT REF HERE] + + Parameters + ---------- + prev_dist : numpy.array + distance distribution before the jittering + amp_cal_error : float + amplitude calibration error (percentage) of the wanted + detector. It will affect the width of the gaussian + distribution. + phase_cal_error : float + phase calibration error (degrees) of the wanted detector. + It will affect the center of the gaussian distribution. + + Returns + ------- + distance : numpy.array + distance distribution after the jittering, a lognormal + distribution centerend on the phase calibration error + and with a width of the amplitude calibration error. + + """ + mu = prev_dist * (1 + 0.5 * numpy.deg2rad(phase_cal_error)**2) + sigma = (amp_cal_error / 100) * prev_dist + + lognorm = [] + + for i in range(len(prev_dist)): + lognorm.append(numpy.log(random.lognormvariate(mu[i], sigma[i]))) + + return lognorm __all__ = ['dquadmon_from_lambda', 'lambda_tilde', 'lambda_from_mass_tov_file', 'primary_mass', @@ -1847,5 +1917,6 @@ def nltides_gw_phase_diff_isco(f_low, f0, amplitude, n, m1, m2): 'remnant_mass_from_mass1_mass2_cartesian_spin_eos', 'lambda1_from_delta_lambda_tilde_lambda_tilde', 'lambda2_from_delta_lambda_tilde_lambda_tilde', - 'delta_lambda_tilde' + 'delta_lambda_tilde', 'jittering_distance_gaussian', + 'jittering_distance_lognormal' ] From e566f5986869ff0daecbfa56ef9ef42025cac245 Mon Sep 17 00:00:00 2001 From: jacquot Date: Thu, 31 Oct 2024 11:44:19 +0100 Subject: [PATCH 2/4] Optimisation of the jittering_distance_lognormal function --- pycbc/conversions.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pycbc/conversions.py b/pycbc/conversions.py index 07a0e99d2b0..924126130da 100644 --- a/pycbc/conversions.py +++ b/pycbc/conversions.py @@ -28,7 +28,6 @@ one parameter given a set of inputs. """ -import random import copy import numpy import logging @@ -1854,7 +1853,7 @@ def jittering_distance_lognormal(prev_dist, amp_cal_error, phase_cal_error): amp_cal_error : float amplitude calibration error (percentage) of the wanted detector. It will affect the width of the gaussian - distribution. + distribution. phase_cal_error : float phase calibration error (degrees) of the wanted detector. It will affect the center of the gaussian distribution. @@ -1869,13 +1868,8 @@ def jittering_distance_lognormal(prev_dist, amp_cal_error, phase_cal_error): """ mu = prev_dist * (1 + 0.5 * numpy.deg2rad(phase_cal_error)**2) sigma = (amp_cal_error / 100) * prev_dist - - lognorm = [] - - for i in range(len(prev_dist)): - lognorm.append(numpy.log(random.lognormvariate(mu[i], sigma[i]))) - return lognorm + return numpy.log(numpy.random.lognormal(mu, sigma)) __all__ = ['dquadmon_from_lambda', 'lambda_tilde', 'lambda_from_mass_tov_file', 'primary_mass', From a862564b1b1180e1cb290ae4219e74a91f7e2d7a Mon Sep 17 00:00:00 2001 From: jacquot Date: Thu, 31 Oct 2024 13:08:17 +0100 Subject: [PATCH 3/4] Correction of jittering_distance_lognormal's docstring --- pycbc/conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pycbc/conversions.py b/pycbc/conversions.py index 924126130da..87869025bb9 100644 --- a/pycbc/conversions.py +++ b/pycbc/conversions.py @@ -1853,7 +1853,7 @@ def jittering_distance_lognormal(prev_dist, amp_cal_error, phase_cal_error): amp_cal_error : float amplitude calibration error (percentage) of the wanted detector. It will affect the width of the gaussian - distribution. + distribution. phase_cal_error : float phase calibration error (degrees) of the wanted detector. It will affect the center of the gaussian distribution. From f58a7efaacbf008f5e52fbc8e001c63e4781942f Mon Sep 17 00:00:00 2001 From: jacquot Date: Thu, 7 Nov 2024 15:16:11 +0100 Subject: [PATCH 4/4] Modifying the log normal distribution function's argument (mu,sigma) --- pycbc/conversions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pycbc/conversions.py b/pycbc/conversions.py index 87869025bb9..ad3db047001 100644 --- a/pycbc/conversions.py +++ b/pycbc/conversions.py @@ -1869,7 +1869,10 @@ def jittering_distance_lognormal(prev_dist, amp_cal_error, phase_cal_error): mu = prev_dist * (1 + 0.5 * numpy.deg2rad(phase_cal_error)**2) sigma = (amp_cal_error / 100) * prev_dist - return numpy.log(numpy.random.lognormal(mu, sigma)) + mu_log = np.log((mu**2)/(np.sqrt(mu**2 + sigma**2))) + sigma_log = np.sqrt(np.log(1+(sigma**2)/mu**2)) + + return numpy.random.lognormal(mu_log, sigma_log) __all__ = ['dquadmon_from_lambda', 'lambda_tilde', 'lambda_from_mass_tov_file', 'primary_mass',