コード例 #1
0
def new_weight(
    log_L,
    parameters,
    comparison_waveform_frequency_domain,
    interferometers,
    duration,
    sampling_frequency,
    maximum_frequency,
    label,
    minimum_log_eccentricity=-4,
    number_of_eccentricity_bins=20
):
    """
    Compute the new weight for a point, weighted by the eccentricity-marginalised likelihood.
    :param log_L: float
        the original likelihood of the point
    :param parameters: dict
        the parameters that define the sample
    :param comparison_waveform_frequency_domain: dict
        frequency-domain waveform polarisations of the waveform used for the original analysis
    :param interferometers: InterferometerList
        list of interferometers used in the detection
    :param duration: int
        time duration of the signal
    :param sampling_frequency: int
        the frequency with which to 'sample' the waveform
    :param maximum_frequency: int
        the maximum frequency at which the data is analysed
    :param label: str
        identifier for results
    :return:
        e: float
            the new eccentricity sample
        average_log_likelihood: float
            the eccentricity-marginalised new likelihood
        log_weight: float
            the log weight of the sample
    """
    # First calculate a grid of likelihoods.
    grid_size = number_of_eccentricity_bins
    eccentricity_grid = np.logspace(
        minimum_log_eccentricity, np.log10(0.2), grid_size
    )
    # Recalculate the log likelihood of the original sample
    recalculated_log_likelihood = log_likelihood_ratio(
        comparison_waveform_frequency_domain, interferometers, parameters, duration
    )
    # Print a warning if this is much different to the likelihood stored in the results
    if abs(recalculated_log_likelihood - log_L) / log_L > 0.1:
        percentage = abs(recalculated_log_likelihood - log_L) / log_L * 100
        print(
            "WARNING :: recalculated log likelihood differs from original by {}%".format(
                percentage
            )
        )
        print('original log L: {}'.format(log_L))
        print('recalculated log L: {}'.format(recalculated_log_likelihood))
    log_likelihood_grid = []
    intermediate_outfile = open("{}_eccentricity_result.txt".format(label), "w")
    intermediate_outfile.write("sample parameters:\n")
    for key in parameters.keys():
        intermediate_outfile.write(key + ":\t" + str(parameters[key]) + "\n")
    intermediate_outfile.write("\n-------------------------\n")
    intermediate_outfile.write("e\t\tlog_L\t\tmaximised_overlap\n")
    # Prepare for the possibility that we have to disregard this sample
    disregard = False
    for e in eccentricity_grid:
        parameters.update({"eccentricity": e})
        # Need to have a set minimum frequency, since this is also the reference frequency
        t, seobnre_waveform_time_domain = wf.seobnre_bbh_with_spin_and_eccentricity(
            parameters=parameters,
            sampling_frequency=sampling_frequency,
            minimum_frequency=10,
            maximum_frequency=maximum_frequency + 1000,
        )
        if t is None:
            print('No waveform generated; disregard sample {}'.format(label))
            intermediate_outfile.write(
                "{}\t\t{}\t\t{}\n".format(e, None, None)
            )
            disregard = True
        else:
            seobnre_wf_td, seobnre_wf_fd, max_overlap, index_shift, phase_shift = ovlp.maximise_overlap(
                seobnre_waveform_time_domain,
                comparison_waveform_frequency_domain,
                sampling_frequency,
                interferometers[0].frequency_array,
                interferometers[0].power_spectral_density,
            )
            eccentric_log_L = log_likelihood_ratio(
                seobnre_wf_fd, interferometers, parameters, duration
            )
            log_likelihood_grid.append(eccentric_log_L)
            intermediate_outfile.write(
                "{}\t\t{}\t\t{}\n".format(e, eccentric_log_L, max_overlap)
            )
    intermediate_outfile.close()
    if not disregard:
        cumulative_density_grid = cumulative_density_function(log_likelihood_grid)
        # We want to pick a weighted random point from within the CDF
        new_e = pick_weighted_random_eccentricity(cumulative_density_grid, eccentricity_grid)
        # Also return average log-likelihood
        average_log_likelihood = np.mean(log_likelihood_grid)
        # Weight calculated using average likelihood
        log_weight = calculate_log_weight(log_likelihood_grid, recalculated_log_likelihood)
        return new_e, average_log_likelihood, log_weight
    else:
        return None, None, None
コード例 #2
0
)
for ifo in interferometers:
    ifo.minimum_frequency = minimum_frequency
    ifo.maximum_frequency = maximum_frequency

# SEBONRe waveform to inject
t, seobnre_waveform_time_domain = wf.seobnre_bbh_with_spin_and_eccentricity(
    parameters=injection_parameters,
    sampling_frequency=sampling_frequency,
    minimum_frequency=minimum_frequency - 10,
    maximum_frequency=maximum_frequency + 1000,
)
seobnre_wf_td, seobnre_wf_fd, max_overlap, index_shift, phase_shift = ovlp.maximise_overlap(
    seobnre_waveform_time_domain,
    comparison_waveform_frequency_domain,
    sampling_frequency,
    interferometers[0].frequency_array,
    interferometers[0].power_spectral_density,
)
print('maximum overlap: ' + str(max_overlap))

# Inject the signal
interferometers.inject_signal(parameters=injection_parameters,
                              injection_polarizations=seobnre_wf_fd)
# plot the data for sanity
signal = interferometers[0].get_detector_response(seobnre_wf_fd,
                                                  injection_parameters)
interferometers.plot_data(signal=signal, outdir=outdir, label=label)

# Set up priors
priors = bb.gw.prior.BBHPriorDict()