Ejemplo n.º 1
0
    def test_powerlaw_genmodes_prior(self):
        """Test that red noise signal returns correct values."""
        # set up signal parameter
        pr = gp_priors.powerlaw_genmodes(log10_A=parameter.Uniform(-18, -12),
                                         gamma=parameter.Uniform(1, 7))
        basis = gp_bases.createfourierdesignmatrix_chromatic(nmodes=30)
        rn = gp_signals.BasisGP(priorFunction=pr,
                                basisFunction=basis,
                                name="red_noise")
        rnm = rn(self.psr)

        # parameters
        log10_A, gamma = -14.5, 4.33
        params = {
            "B1855+09_red_noise_log10_A": log10_A,
            "B1855+09_red_noise_gamma": gamma
        }

        # basis matrix test
        F, f2 = gp_bases.createfourierdesignmatrix_chromatic(self.psr.toas,
                                                             self.psr.freqs,
                                                             nmodes=30)
        msg = "F matrix incorrect for turnover."
        assert np.allclose(F, rnm.get_basis(params)), msg

        # spectrum test
        phi = gp_priors.powerlaw_genmodes(f2, log10_A=log10_A, gamma=gamma)
        msg = "Spectrum incorrect for turnover."
        assert np.all(rnm.get_phi(params) == phi), msg

        # inverse spectrum test
        msg = "Spectrum inverse incorrect for turnover."
        assert np.all(rnm.get_phiinv(params) == 1 / phi), msg

        # test shape
        msg = "F matrix shape incorrect"
        assert rnm.get_basis(params).shape == F.shape, msg
Ejemplo n.º 2
0
    # timing model
    s = gp_signals.MarginalizingTimingModel()

    s += blocks.white_noise_block(vary=False, inc_ecorr=True, select='backend')

    s += blocks.red_noise_block(
        psd='powerlaw',
        prior='log-uniform',
        Tspan=Tspan_PTA,
        modes=modes,
        wgts=wgts,
    )

    gamma_gw = parameter.Uniform(0, 7)('gw_gamma')
    log10_Agw = parameter.Uniform(-18, -11)('gw_log10_A')
    cpl = gpp.powerlaw_genmodes(log10_A=log10_Agw, gamma=gamma_gw, wgts=wgts)
    s += gp_signals.FourierBasisGP(cpl, modes=modes, name='gw_crn')

    # gw = blocks.common_red_noise_block(psd='powerlaw', prior='log-uniform', Tspan=Tspan_PTA,
    #                                    components=5, gamma_val=4.33, name='gw', orf='hd')

    crn_models = [s(psr) for psr in pkl_psrs]
    # gw_models = [(m + gw)(psr) for psr,m in  zip(final_psrs,psr_models)]

    pta_crn = signal_base.PTA(crn_models)
    # pta_gw = signal_base.PTA(gw_models)

    # # delta_common=0.,
    # ptas = {0:pta_crn,
    #         1:pta_gw}
Ejemplo n.º 3
0
def red_noise_block(psd='powerlaw',
                    prior='log-uniform',
                    Tspan=None,
                    components=30,
                    gamma_val=None,
                    coefficients=False,
                    select=None,
                    modes=None,
                    wgts=None,
                    break_flat=False,
                    break_flat_fq=None):
    """
    Returns red noise model:
        1. Red noise modeled as a power-law with 30 sampling frequencies
    :param psd:
        PSD function [e.g. powerlaw (default), turnover, spectrum, tprocess]
    :param prior:
        Prior on log10_A. Default if "log-uniform". Use "uniform" for
        upper limits.
    :param Tspan:
        Sets frequency sampling f_i = i / Tspan. Default will
        use overall time span for indivicual pulsar.
    :param components:
        Number of frequencies in sampling of red noise
    :param gamma_val:
        If given, this is the fixed slope of the power-law for
        powerlaw, turnover, or tprocess red noise
    :param coefficients: include latent coefficients in GP model?
    """
    # red noise parameters that are common
    if psd in [
            'powerlaw', 'powerlaw_genmodes', 'turnover', 'tprocess',
            'tprocess_adapt', 'infinitepower'
    ]:
        # parameters shared by PSD functions
        if prior == 'uniform':
            log10_A = parameter.LinearExp(-20, -11)
        elif prior == 'log-uniform' and gamma_val is not None:
            if np.abs(gamma_val - 4.33) < 0.1:
                log10_A = parameter.Uniform(-20, -11)
            else:
                log10_A = parameter.Uniform(-20, -11)
        else:
            log10_A = parameter.Uniform(-20, -11)

        if gamma_val is not None:
            gamma = parameter.Constant(gamma_val)
        else:
            gamma = parameter.Uniform(0, 7)

        # different PSD function parameters
        if psd == 'powerlaw':
            pl = utils.powerlaw(log10_A=log10_A, gamma=gamma)
        elif psd == 'powerlaw_genmodes':
            pl = gpp.powerlaw_genmodes(log10_A=log10_A, gamma=gamma, wgts=wgts)
        elif psd == 'turnover':
            kappa = parameter.Uniform(0, 7)
            lf0 = parameter.Uniform(-9, -7)
            pl = utils.turnover(log10_A=log10_A,
                                gamma=gamma,
                                lf0=lf0,
                                kappa=kappa)
        elif psd == 'tprocess':
            df = 2
            alphas = gpp.InvGamma(df / 2, df / 2, size=components)
            pl = gpp.t_process(log10_A=log10_A, gamma=gamma, alphas=alphas)
        elif psd == 'tprocess_adapt':
            df = 2
            alpha_adapt = gpp.InvGamma(df / 2, df / 2, size=1)
            nfreq = parameter.Uniform(-0.5, 10 - 0.5)
            pl = gpp.t_process_adapt(log10_A=log10_A,
                                     gamma=gamma,
                                     alphas_adapt=alpha_adapt,
                                     nfreq=nfreq)
        elif psd == 'infinitepower':
            pl = gpp.infinitepower()

    if psd == 'spectrum':
        if prior == 'uniform':
            log10_rho = parameter.LinearExp(-10, -4, size=components)
        elif prior == 'log-uniform':
            log10_rho = parameter.Uniform(-10, -4, size=components)

        pl = gpp.free_spectrum(log10_rho=log10_rho)

    if select == 'backend':
        # define selection by observing backend
        selection = selections.Selection(selections.by_backend)
    elif select == 'band' or select == 'band+':
        # define selection by observing band
        selection = selections.Selection(selections.by_band)
    else:
        # define no selection
        selection = selections.Selection(selections.no_selection)

    if break_flat:
        log10_A_flat = parameter.Uniform(-20, -11)
        gamma_flat = parameter.Constant(0)
        pl_flat = utils.powerlaw(log10_A=log10_A_flat, gamma=gamma_flat)

        freqs = 1.0 * np.arange(1, components + 1) / Tspan
        components_low = sum(f < break_flat_fq for f in freqs)
        if components_low < 1.5:
            components_low = 2

        rn = gp_signals.FourierBasisGP(pl,
                                       components=components_low,
                                       Tspan=Tspan,
                                       coefficients=coefficients,
                                       selection=selection)

        rn_flat = gp_signals.FourierBasisGP(pl_flat,
                                            modes=freqs[components_low:],
                                            coefficients=coefficients,
                                            selection=selection,
                                            name='red_noise_hf')
        rn = rn + rn_flat
    else:
        rn = gp_signals.FourierBasisGP(pl,
                                       components=components,
                                       Tspan=Tspan,
                                       coefficients=coefficients,
                                       selection=selection,
                                       modes=modes)

    if select == 'band+':  # Add the common component as well
        rn = rn + gp_signals.FourierBasisGP(
            pl, components=components, Tspan=Tspan, coefficients=coefficients)

    return rn