def test_adapt_t_process_prior(self): """Test that red noise signal returns correct values.""" # set up signal parameter pr = gp_priors.t_process_adapt( log10_A=parameter.Uniform(-18, -12), gamma=parameter.Uniform(1, 7), alphas_adapt=gp_priors.InvGamma(), nfreq=parameter.Uniform(5, 25), ) basis = gp_bases.createfourierdesignmatrix_red(nmodes=30) rn = gp_signals.BasisGP(priorFunction=pr, basisFunction=basis, name="red_noise") rnm = rn(self.psr) # parameters alphas = scipy.stats.invgamma.rvs(1, scale=1, size=1) log10_A, gamma, nfreq = -15, 4.33, 12 params = { "B1855+09_red_noise_log10_A": log10_A, "B1855+09_red_noise_gamma": gamma, "B1855+09_red_noise_alphas_adapt": alphas, "B1855+09_red_noise_nfreq": nfreq, } # basis matrix test F, f2 = gp_bases.createfourierdesignmatrix_red(self.psr.toas, nmodes=30) msg = "F matrix incorrect for free spectrum." assert np.allclose(F, rnm.get_basis(params)), msg # spectrum test phi = gp_priors.t_process_adapt(f2, log10_A=log10_A, gamma=gamma, alphas_adapt=alphas, nfreq=nfreq) msg = "Spectrum incorrect for free spectrum." assert np.all(rnm.get_phi(params) == phi), msg # inverse spectrum test msg = "Spectrum inverse incorrect for free spectrum." 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
def dm_noise_block(gp_kernel='diag', psd='powerlaw', nondiag_kernel='periodic', prior='log-uniform', dt=15, df=200, Tspan=None, components=30, gamma_val=None, coefficients=False): """ Returns DM noise model: 1. DM noise modeled as a power-law with 30 sampling frequencies :param psd: PSD function [e.g. powerlaw (default), spectrum, tprocess] :param prior: Prior on log10_A. Default if "log-uniform". Use "uniform" for upper limits. :param dt: time-scale for linear interpolation basis (days) :param df: frequency-scale for linear interpolation basis (MHz) :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 DM-variations. :param gamma_val: If given, this is the fixed slope of the power-law for powerlaw, turnover, or tprocess DM-variations """ # dm noise parameters that are common if gp_kernel == 'diag': if psd in ['powerlaw', 'turnover', 'tprocess', 'tprocess_adapt']: # parameters shared by PSD functions if prior == 'uniform': log10_A_dm = 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_dm = parameter.Uniform(-20, -11) else: log10_A_dm = parameter.Uniform(-20, -11) else: log10_A_dm = parameter.Uniform(-20, -11) if gamma_val is not None: gamma_dm = parameter.Constant(gamma_val) else: gamma_dm = parameter.Uniform(0, 7) # different PSD function parameters if psd == 'powerlaw': dm_prior = utils.powerlaw(log10_A=log10_A_dm, gamma=gamma_dm) elif psd == 'turnover': kappa_dm = parameter.Uniform(0, 7) lf0_dm = parameter.Uniform(-9, -7) dm_prior = utils.turnover(log10_A=log10_A_dm, gamma=gamma_dm, lf0=lf0_dm, kappa=kappa_dm) elif psd == 'tprocess': df = 2 alphas_dm = gpp.InvGamma(df / 2, df / 2, size=components) dm_prior = gpp.t_process(log10_A=log10_A_dm, gamma=gamma_dm, alphas=alphas_dm) elif psd == 'tprocess_adapt': df = 2 alpha_adapt_dm = gpp.InvGamma(df / 2, df / 2, size=1) nfreq_dm = parameter.Uniform(-0.5, 10 - 0.5) dm_prior = gpp.t_process_adapt(log10_A=log10_A_dm, gamma=gamma_dm, alphas_adapt=alpha_adapt_dm, nfreq=nfreq_dm) if psd == 'spectrum': if prior == 'uniform': log10_rho_dm = parameter.LinearExp(-10, -4, size=components) elif prior == 'log-uniform': log10_rho_dm = parameter.Uniform(-10, -4, size=components) dm_prior = gpp.free_spectrum(log10_rho=log10_rho_dm) dm_basis = utils.createfourierdesignmatrix_dm(nmodes=components, Tspan=Tspan) elif gp_kernel == 'nondiag': if nondiag_kernel == 'periodic': # Periodic GP kernel for DM log10_sigma = parameter.Uniform(-10, -4) log10_ell = parameter.Uniform(1, 4) log10_p = parameter.Uniform(-4, 1) log10_gam_p = parameter.Uniform(-3, 2) dm_basis = gpk.linear_interp_basis_dm(dt=dt * const.day) dm_prior = gpk.periodic_kernel(log10_sigma=log10_sigma, log10_ell=log10_ell, log10_gam_p=log10_gam_p, log10_p=log10_p) elif nondiag_kernel == 'periodic_rfband': # Periodic GP kernel for DM with RQ radio-frequency dependence log10_sigma = parameter.Uniform(-10, -4) log10_ell = parameter.Uniform(1, 4) log10_ell2 = parameter.Uniform(2, 7) log10_alpha_wgt = parameter.Uniform(-4, 1) log10_p = parameter.Uniform(-4, 1) log10_gam_p = parameter.Uniform(-3, 2) dm_basis = gpk.get_tf_quantization_matrix(df=df, dt=dt * const.day, dm=True) dm_prior = gpk.tf_kernel(log10_sigma=log10_sigma, log10_ell=log10_ell, log10_gam_p=log10_gam_p, log10_p=log10_p, log10_alpha_wgt=log10_alpha_wgt, log10_ell2=log10_ell2) elif nondiag_kernel == 'sq_exp': # squared-exponential GP kernel for DM log10_sigma = parameter.Uniform(-10, -4) log10_ell = parameter.Uniform(1, 4) dm_basis = gpk.linear_interp_basis_dm(dt=dt * const.day) dm_prior = gpk.se_dm_kernel(log10_sigma=log10_sigma, log10_ell=log10_ell) elif nondiag_kernel == 'sq_exp_rfband': # Sq-Exp GP kernel for DM with RQ radio-frequency dependence log10_sigma = parameter.Uniform(-10, -4) log10_ell = parameter.Uniform(1, 4) log10_ell2 = parameter.Uniform(2, 7) log10_alpha_wgt = parameter.Uniform(-4, 1) dm_basis = gpk.get_tf_quantization_matrix(df=df, dt=dt * const.day, dm=True) dm_prior = gpk.sf_kernel(log10_sigma=log10_sigma, log10_ell=log10_ell, log10_alpha_wgt=log10_alpha_wgt, log10_ell2=log10_ell2) elif nondiag_kernel == 'dmx_like': # DMX-like signal log10_sigma = parameter.Uniform(-10, -4) dm_basis = gpk.linear_interp_basis_dm(dt=dt * const.day) dm_prior = gpk.dmx_ridge_prior(log10_sigma=log10_sigma) dmgp = gp_signals.BasisGP(dm_prior, dm_basis, name='dm_gp', coefficients=coefficients) return dmgp
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