Exemplo n.º 1
0
    def test_turnover_knee_prior(self):
        """Test that red noise signal returns correct values."""
        # set up signal parameter
        pr = gp_priors.turnover_knee(
            log10_A=parameter.Uniform(-18, -12),
            gamma=parameter.Uniform(1, 7),
            lfb=parameter.Uniform(-9, -7.5),
            lfk=parameter.Uniform(-9, -7.5),
            kappa=parameter.Uniform(2.5, 5),
            delta=parameter.Uniform(0.01, 1),
        )
        basis = gp_bases.createfourierdesignmatrix_red(nmodes=30)
        rn = gp_signals.BasisGP(priorFunction=pr,
                                basisFunction=basis,
                                name="red_noise")
        rnm = rn(self.psr)

        # parameters
        log10_A, gamma, lfb = -14.5, 4.33, -8.5
        lfk, kappa, delta = -8.5, 3, 0.5
        params = {
            "B1855+09_red_noise_log10_A": log10_A,
            "B1855+09_red_noise_gamma": gamma,
            "B1855+09_red_noise_lfb": lfb,
            "B1855+09_red_noise_lfk": lfk,
            "B1855+09_red_noise_kappa": kappa,
            "B1855+09_red_noise_delta": delta,
        }

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

        # spectrum test
        phi = gp_priors.turnover_knee(f2,
                                      log10_A=log10_A,
                                      gamma=gamma,
                                      lfb=lfb,
                                      lfk=lfk,
                                      kappa=kappa,
                                      delta=delta)
        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
Exemplo n.º 2
0
def common_red_noise_block(psd='powerlaw',
                           prior='log-uniform',
                           Tspan=None,
                           components=30,
                           log10_A_val=None,
                           gamma_val=None,
                           delta_val=None,
                           orf=None,
                           orf_ifreq=0,
                           leg_lmax=5,
                           name='gw',
                           coefficients=False,
                           pshift=False,
                           pseed=None):
    """
    Returns common red noise model:

        1. Red noise modeled with user defined PSD with
        30 sampling frequencies. Available PSDs are
        ['powerlaw', 'turnover' 'spectrum']

    :param psd:
        PSD to use for common red noise signal. Available options
        are ['powerlaw', 'turnover' 'spectrum', 'broken_powerlaw']
    :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 individual pulsar.
    :param log10_A_val:
        Value of log10_A parameter for fixed amplitude analyses.
    :param gamma_val:
        Value of spectral index for power-law and turnover
        models. By default spectral index is varied of range [0,7]
    :param delta_val:
        Value of spectral index for high frequencies in broken power-law
        and turnover models. By default spectral index is varied in range [0,7].
    :param orf:
        String representing which overlap reduction function to use.
        By default we do not use any spatial correlations. Permitted
        values are ['hd', 'dipole', 'monopole'].
    :param orf_ifreq:
        Frequency bin at which to start the Hellings & Downs function with 
        numbering beginning at 0. Currently only works with freq_hd orf.
    :param leg_lmax:
        Maximum multipole of a Legendre polynomial series representation 
        of the overlap reduction function [default=5]
    :param pshift:
        Option to use a random phase shift in design matrix. For testing the
        null hypothesis.
    :param pseed:
        Option to provide a seed for the random phase shift.
    :param name: Name of common red process

    """

    orfs = {
        'crn':
        None,
        'hd':
        utils.hd_orf(),
        'dipole':
        utils.dipole_orf(),
        'monopole':
        utils.monopole_orf(),
        'param_hd':
        model_orfs.param_hd_orf(a=parameter.Uniform(-1.5,
                                                    3.0)('gw_orf_param0'),
                                b=parameter.Uniform(-1.0,
                                                    0.5)('gw_orf_param1'),
                                c=parameter.Uniform(-1.0,
                                                    1.0)('gw_orf_param2')),
        'spline_orf':
        model_orfs.spline_orf(
            params=parameter.Uniform(-0.9, 0.9, size=7)('gw_orf_spline')),
        'bin_orf':
        model_orfs.bin_orf(
            params=parameter.Uniform(-1.0, 1.0, size=7)('gw_orf_bin')),
        'zero_diag_hd':
        model_orfs.zero_diag_hd(),
        'zero_diag_bin_orf':
        model_orfs.zero_diag_bin_orf(params=parameter.Uniform(
            -1.0, 1.0, size=7)('gw_orf_bin_zero_diag')),
        'freq_hd':
        model_orfs.freq_hd(params=[components, orf_ifreq]),
        'legendre_orf':
        model_orfs.legendre_orf(
            params=parameter.Uniform(-1.0, 1.0, size=leg_lmax +
                                     1)('gw_orf_legendre')),
        'zero_diag_legendre_orf':
        model_orfs.zero_diag_legendre_orf(
            params=parameter.Uniform(-1.0, 1.0, size=leg_lmax +
                                     1)('gw_orf_legendre_zero_diag'))
    }

    # common red noise parameters
    if psd in ['powerlaw', 'turnover', 'turnover_knee', 'broken_powerlaw']:
        amp_name = '{}_log10_A'.format(name)
        if log10_A_val is not None:
            log10_Agw = parameter.Constant(log10_A_val)(amp_name)
        else:
            if prior == 'uniform':
                log10_Agw = parameter.LinearExp(-18, -11)(amp_name)
            elif prior == 'log-uniform' and gamma_val is not None:
                if np.abs(gamma_val - 4.33) < 0.1:
                    log10_Agw = parameter.Uniform(-18, -14)(amp_name)
                else:
                    log10_Agw = parameter.Uniform(-18, -11)(amp_name)
            else:
                log10_Agw = parameter.Uniform(-18, -11)(amp_name)

        gam_name = '{}_gamma'.format(name)
        if gamma_val is not None:
            gamma_gw = parameter.Constant(gamma_val)(gam_name)
        else:
            gamma_gw = parameter.Uniform(0, 7)(gam_name)

        # common red noise PSD
        if psd == 'powerlaw':
            cpl = utils.powerlaw(log10_A=log10_Agw, gamma=gamma_gw)
        elif psd == 'broken_powerlaw':
            delta_name = '{}_delta'.format(name)
            kappa_name = '{}_kappa'.format(name)
            log10_fb_name = '{}_log10_fb'.format(name)
            kappa_gw = parameter.Uniform(0.01, 0.5)(kappa_name)
            log10_fb_gw = parameter.Uniform(-10, -7)(log10_fb_name)

            if delta_val is not None:
                delta_gw = parameter.Constant(delta_val)(delta_name)
            else:
                delta_gw = parameter.Uniform(0, 7)(delta_name)
            cpl = gpp.broken_powerlaw(log10_A=log10_Agw,
                                      gamma=gamma_gw,
                                      delta=delta_gw,
                                      log10_fb=log10_fb_gw,
                                      kappa=kappa_gw)
        elif psd == 'turnover':
            kappa_name = '{}_kappa'.format(name)
            lf0_name = '{}_log10_fbend'.format(name)
            kappa_gw = parameter.Uniform(0, 7)(kappa_name)
            lf0_gw = parameter.Uniform(-9, -7)(lf0_name)
            cpl = utils.turnover(log10_A=log10_Agw,
                                 gamma=gamma_gw,
                                 lf0=lf0_gw,
                                 kappa=kappa_gw)
        elif psd == 'turnover_knee':
            kappa_name = '{}_kappa'.format(name)
            lfb_name = '{}_log10_fbend'.format(name)
            delta_name = '{}_delta'.format(name)
            lfk_name = '{}_log10_fknee'.format(name)
            kappa_gw = parameter.Uniform(0, 7)(kappa_name)
            lfb_gw = parameter.Uniform(-9.3, -8)(lfb_name)
            delta_gw = parameter.Uniform(-2, 0)(delta_name)
            lfk_gw = parameter.Uniform(-8, -7)(lfk_name)
            cpl = gpp.turnover_knee(log10_A=log10_Agw,
                                    gamma=gamma_gw,
                                    lfb=lfb_gw,
                                    lfk=lfk_gw,
                                    kappa=kappa_gw,
                                    delta=delta_gw)

    if psd == 'spectrum':
        rho_name = '{}_log10_rho'.format(name)
        if prior == 'uniform':
            log10_rho_gw = parameter.LinearExp(-9, -4,
                                               size=components)(rho_name)
        elif prior == 'log-uniform':
            log10_rho_gw = parameter.Uniform(-9, -4, size=components)(rho_name)

        cpl = gpp.free_spectrum(log10_rho=log10_rho_gw)

    if orf is None:
        crn = gp_signals.FourierBasisGP(cpl,
                                        coefficients=coefficients,
                                        components=components,
                                        Tspan=Tspan,
                                        name=name,
                                        pshift=pshift,
                                        pseed=pseed)
    elif orf in orfs.keys():
        if orf == 'crn':
            crn = gp_signals.FourierBasisGP(cpl,
                                            coefficients=coefficients,
                                            components=components,
                                            Tspan=Tspan,
                                            name=name,
                                            pshift=pshift,
                                            pseed=pseed)
        else:
            crn = gp_signals.FourierBasisCommonGP(cpl,
                                                  orfs[orf],
                                                  components=components,
                                                  Tspan=Tspan,
                                                  name=name,
                                                  pshift=pshift,
                                                  pseed=pseed)
    elif isinstance(orf, types.FunctionType):
        crn = gp_signals.FourierBasisCommonGP(cpl,
                                              orf,
                                              components=components,
                                              Tspan=Tspan,
                                              name=name,
                                              pshift=pshift,
                                              pseed=pseed)
    else:
        raise ValueError('ORF {} not recognized'.format(orf))

    return crn
Exemplo n.º 3
0
def common_red_noise_block(psd='powerlaw',
                           prior='log-uniform',
                           Tspan=None,
                           components=30,
                           gamma_val=None,
                           orf=None,
                           name='gw',
                           coefficients=False,
                           pshift=False,
                           pseed=None):
    """
    Returns common red noise model:

        1. Red noise modeled with user defined PSD with
        30 sampling frequencies. Available PSDs are
        ['powerlaw', 'turnover' 'spectrum']

    :param psd:
        PSD to use for common red noise signal. Available options
        are ['powerlaw', 'turnover' 'spectrum']
    :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 gamma_val:
        Value of spectral index for power-law and turnover
        models. By default spectral index is varied of range [0,7]
    :param orf:
        String representing which overlap reduction function to use.
        By default we do not use any spatial correlations. Permitted
        values are ['hd', 'dipole', 'monopole'].
    :param pshift:
        Option to use a random phase shift in design matrix. For testing the
        null hypothesis.
    :param pseed:
        Option to provide a seed for the random phase shift.
    :param name: Name of common red process

    """

    orfs = {
        'hd': utils.hd_orf(),
        'dipole': utils.dipole_orf(),
        'monopole': utils.monopole_orf()
    }

    # common red noise parameters
    if psd in ['powerlaw', 'turnover', 'turnover_knee']:
        amp_name = '{}_log10_A'.format(name)
        if prior == 'uniform':
            log10_Agw = parameter.LinearExp(-18, -11)(amp_name)
        elif prior == 'log-uniform' and gamma_val is not None:
            if np.abs(gamma_val - 4.33) < 0.1:
                log10_Agw = parameter.Uniform(-18, -14)(amp_name)
            else:
                log10_Agw = parameter.Uniform(-18, -11)(amp_name)
        else:
            log10_Agw = parameter.Uniform(-18, -11)(amp_name)

        gam_name = '{}_gamma'.format(name)
        if gamma_val is not None:
            gamma_gw = parameter.Constant(gamma_val)(gam_name)
        else:
            gamma_gw = parameter.Uniform(0, 7)(gam_name)

        # common red noise PSD
        if psd == 'powerlaw':
            cpl = utils.powerlaw(log10_A=log10_Agw, gamma=gamma_gw)
        elif psd == 'turnover':
            kappa_name = '{}_kappa'.format(name)
            lf0_name = '{}_log10_fbend'.format(name)
            kappa_gw = parameter.Uniform(0, 7)(kappa_name)
            lf0_gw = parameter.Uniform(-9, -7)(lf0_name)
            cpl = utils.turnover(log10_A=log10_Agw,
                                 gamma=gamma_gw,
                                 lf0=lf0_gw,
                                 kappa=kappa_gw)
        elif psd == 'turnover_knee':
            kappa_name = '{}_kappa'.format(name)
            lfb_name = '{}_log10_fbend'.format(name)
            delta_name = '{}_delta'.format(name)
            lfk_name = '{}_log10_fknee'.format(name)
            kappa_gw = parameter.Uniform(0, 7)(kappa_name)
            lfb_gw = parameter.Uniform(-9.3, -8)(lfb_name)
            delta_gw = parameter.Uniform(-2, 0)(delta_name)
            lfk_gw = parameter.Uniform(-8, -7)(lfk_name)
            cpl = gpp.turnover_knee(log10_A=log10_Agw,
                                    gamma=gamma_gw,
                                    lfb=lfb_gw,
                                    lfk=lfk_gw,
                                    kappa=kappa_gw,
                                    delta=delta_gw)

    if psd == 'spectrum':
        rho_name = '{}_log10_rho'.format(name)
        if prior == 'uniform':
            log10_rho_gw = parameter.LinearExp(-9, -4,
                                               size=components)(rho_name)
        elif prior == 'log-uniform':
            log10_rho_gw = parameter.Uniform(-9, -4, size=components)(rho_name)

        cpl = gpp.free_spectrum(log10_rho=log10_rho_gw)

    if orf is None:
        crn = gp_signals.FourierBasisGP(cpl,
                                        coefficients=coefficients,
                                        components=components,
                                        Tspan=Tspan,
                                        name=name,
                                        pshift=pshift,
                                        pseed=pseed)
    elif orf in orfs.keys():
        crn = gp_signals.FourierBasisCommonGP(cpl,
                                              orfs[orf],
                                              components=components,
                                              Tspan=Tspan,
                                              name=name,
                                              pshift=pshift,
                                              pseed=pseed)
    elif isinstance(orf, types.FunctionType):
        crn = gp_signals.FourierBasisCommonGP(cpl,
                                              orf,
                                              components=components,
                                              Tspan=Tspan,
                                              name=name,
                                              pshift=pshift,
                                              pseed=pseed)
    else:
        raise ValueError('ORF {} not recognized'.format(orf))

    return crn