Skip to content

sigway.perturbations

The primordial curvature power spectrum \(\mathcal{P}_\zeta(k)\). Choose one of the implementations — or subclass the base — and hand it to OmegaGW.

Base class

sigway.perturbations.ScalarPerturbations

Abstract base class for the primordial curvature power spectrum \(\mathcal{P}_\zeta(k)\).

Every spectrum model in SIGWAY inherits from this class and implements __call__(k, *params) to return \(\mathcal{P}_\zeta\) evaluated at wavenumber \(k\) for a given set of model parameters \(\theta\). The class attributes below form the contract that OmegaGW and the gravitational-wave integrator rely on.

Attributes:

Name Type Description
param_names tuple of str

Ordered names of the free model parameters \(\theta\) passed as *params to __call__ and prepare. The order must be consistent with the parameter vector used for inference. For example, ("logAs", "logks") means the first positional argument after \(k\) is the log-amplitude and the second is the log-peak scale.

jittable bool

Whether \(\mathcal{P}_\zeta(k,\theta)\) can be evaluated inside the compiled, differentiable code path (JAX). Analytic spectra set this to True. The Mukhanov-Sasaki solver sets it to False because it calls SciPy ODE routines internally and must therefore be run separately, before the main integration.

nonsmooth_params tuple of str

Subset of param_names whose partial derivatives cannot be obtained by automatic differentiation. This arises when a parameter enters a sharp feature — for example, a hard ultraviolet cutoff \(k_{\max}\) implemented as a Heaviside step function, or a parameter that sets an integration limit. At a discontinuity, automatic differentiation returns zero or nan rather than the correct finite-difference slope. OmegaGW uses central finite differences for these parameters when building the Jacobian. Leave as an empty tuple if all parameters enter the spectrum smoothly.

__call__

__call__(k, *params)

Evaluate \(\mathcal{P}_\zeta\) at wavenumber \(k\).

Parameters:

Name Type Description Default
k array_like

Comoving wavenumber(s) \(k\), in the units used throughout the model (\(\mathrm{s}^{-1}\)), at which to evaluate the primordial power spectrum.

required
*params float

Model parameters \(\theta\) in the order given by param_names.

()

Returns:

Type Description
array_like

\(\mathcal{P}_\zeta(k,\theta)\) evaluated at the supplied \(k\).

Raises:

Type Description
NotImplementedError

Always — concrete subclasses must override this method.

prepare

prepare(kint, *params)

Return a single-argument \(\mathcal{P}_\zeta(k)\) with parameters fixed.

The gravitational-wave integrator calls this once before evaluating \(\mathcal{P}_\zeta\) on its dense \((k u,\, k v)\) grid. The default implementation simply closes over __call__ with the given params. SingleFieldPerturbations overrides this to solve the Mukhanov-Sasaki equation once on kint and return a fast spline interpolant, avoiding a full ODE solve at every grid point.

Parameters:

Name Type Description Default
kint array_like

Wavenumber grid on which the integrator will sample \(\mathcal{P}_\zeta\). Forwarded to the Mukhanov-Sasaki solver when overridden; ignored by the default analytic implementation.

required
*params float

Model parameters \(\theta\) in the order given by param_names.

()

Returns:

Type Description
callable

A single-argument function f(k) returning \(\mathcal{P}_\zeta(k)\) with \(\theta\) already applied.

Implementations

sigway.perturbations.AnalyticPerturbations

AnalyticPerturbations(func, param_names=(), nonsmooth_params=())

Bases: ScalarPerturbations

Closed-form primordial power spectrum \(\mathcal{P}_\zeta(k,\theta)\).

The lightest-weight way to define a spectrum model: supply any function func(k, *params) that computes \(\mathcal{P}_\zeta\) analytically together with the ordered names of its free parameters. The result is a fully compliant ScalarPerturbations object that can be evaluated inside the compiled, differentiable code path, provided func itself uses JAX-compatible operations (e.g. jax.numpy).

Parameters:

Name Type Description Default
func callable

A function with signature func(k, *params) -> array_like returning \(\mathcal{P}_\zeta\) at wavenumber \(k\) for parameters \(\theta\). Use jax.numpy operations to keep the spectrum differentiable and fast.

required
param_names tuple of str

Ordered names of the free parameters expected by func, e.g. ("logAs", "logks", "sigma"). Defaults to an empty tuple.

()
nonsmooth_params tuple of str

Parameters in param_names that enter a sharp feature in the spectrum (e.g. a hard cutoff \(k_{\max}\) via a Heaviside step), for which automatic differentiation does not give a meaningful slope. OmegaGW falls back to central finite differences for these. Defaults to an empty tuple.

()

Attributes:

Name Type Description
func callable

The wrapped callable as supplied at construction time.

param_names tuple of str

Ordered parameter names.

nonsmooth_params tuple of str

Parameters requiring finite-difference derivatives.

Examples:

Wrap a log-normal peak in \(\mathcal{P}_\zeta(k)\):

>>> import jax.numpy as jnp
>>> from sigway.perturbations import AnalyticPerturbations
>>> def pz(k, logAs, logks):
...     return 10.0**logAs * jnp.exp(-0.5*(jnp.log(k/10**logks)/0.3)**2)
>>> perts = AnalyticPerturbations(pz, ("logAs", "logks"))

If the spectrum has a hard ultraviolet cutoff \(k_{\max}\) whose derivative must be estimated by finite differences, declare it via nonsmooth_params:

>>> perts_cut = AnalyticPerturbations(pz, ("logAs", "kmax"),
...                                   nonsmooth_params=("kmax",))

__call__

__call__(k, *params)

Evaluate the wrapped callable at wavenumber \(k\).

Parameters:

Name Type Description Default
k array_like

Comoving wavenumber(s) \(k\) at which to evaluate \(\mathcal{P}_\zeta\).

required
*params float

Model parameters \(\theta\) in the order given by param_names, forwarded directly to func.

()

Returns:

Type Description
array_like

\(\mathcal{P}_\zeta(k,\theta)\) as returned by func.

sigway.perturbations.SingleFieldPerturbations

SingleFieldPerturbations(solver, param_names=())

Bases: ScalarPerturbations

Primordial power spectrum \(\mathcal{P}_\zeta(k)\) from single-field inflation.

For inflationary models where no analytic expression for \(\mathcal{P}_\zeta(k)\) is available, this class solves the Mukhanov-Sasaki equation numerically using SingleFieldSolver. The result is exposed through the same __call__ / prepare interface as every other ScalarPerturbations subclass, so the gravitational-wave integrator is unaware of whether the spectrum is analytic or numerical.

Because the Mukhanov-Sasaki solver calls SciPy ODE routines internally, it cannot be evaluated inside the compiled, differentiable code path. jittable is therefore False, and the integrator runs this path eagerly. Gradients with respect to inflationary potential parameters must be obtained by finite differences.

Parameters:

Name Type Description Default
solver SingleFieldSolver

A configured SingleFieldSolver instance encoding the inflationary potential and integration settings. Passing any other object raises ValueError.

required
param_names tuple of str

Ordered names of the free parameters forwarded to the solver (e.g. potential coefficients). Defaults to an empty tuple.

()

Attributes:

Name Type Description
solver SingleFieldSolver

The wrapped SingleFieldSolver.

param_names tuple of str

Ordered parameter names.

jittable bool

Always False for this class; see above.

Raises:

Type Description
ValueError

If solver is not an instance of SingleFieldSolver.

__call__

__call__(k, *params)

Evaluate \(\mathcal{P}_\zeta\) by solving the Mukhanov-Sasaki equation at \(k\).

Delegates directly to the wrapped solver. This is the point-by-point evaluation path. For bulk evaluation on the integrator grid — where solving the ODE at every point separately would be prohibitively slow — use prepare instead.

Parameters:

Name Type Description Default
k array_like

Comoving wavenumber(s) \(k\) at which to evaluate \(\mathcal{P}_\zeta\).

required
*params float

Inflationary model parameters in the order given by param_names, forwarded to the solver.

()

Returns:

Type Description
array_like

\(\mathcal{P}_\zeta(k)\) at the requested wavenumbers.

prepare

prepare(kint, *params)

Solve the Mukhanov-Sasaki equation once and return a fast interpolant.

Rather than re-running the full ODE for every point on the integrator's dense \((k u,\, k v)\) grid, this method solves the Mukhanov-Sasaki system once on the supplied wavenumber grid kint and returns a SciPy spline interpolant. The integrator then evaluates the cheap interpolant at each grid point.

Parameters:

Name Type Description Default
kint array_like

Wavenumber grid \(k_{\mathrm{int}}\) on which to solve the Mukhanov-Sasaki equation. Should span the range of \(k\) values required by the subsequent integration.

required
*params float

Inflationary model parameters forwarded to the solver.

()

Returns:

Type Description
callable

A single-argument spline interpolant f(k) approximating \(\mathcal{P}_\zeta(k)\) across the range of kint.