cov
Utilities for computing covariances.
apply_harm_noise(x, ivar, ps2d, include_corr)
Apply the inverse covariance for the approximate noise model
where \(D\) is the diagonal map-domain noise-amplitude operator and
\(N_{\mathrm{harm}}\) is the harmonic-space covariance represented by
ps2d.
The inverse covariance is applied by scaling the input map using the per-pixel inverse noise amplitude, optionally filtering it in harmonic space, and applying the map-domain scaling a second time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[ndmap, 'ny nx ncomp']
|
Input map with spatial dimensions |
required |
ivar
|
Float[ndmap, 'ny nx']
|
Per-pixel inverse variance, in inverse squared map units. Pixels with non-finite or non-positive inverse variance receive zero weight. |
required |
ps2d
|
Float[ndmap, 'ny nx']
|
Two-dimensional noise power spectrum. Non-finite and non-positive
values are excluded when constructing the inverse spectrum. Its
shape and WCS must be compatible with the harmonic representation
of |
required |
include_corr
|
bool
|
If |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Ninv_x |
Float[ndmap, 'ny nx ncomp']
|
Map obtained by applying the approximate inverse noise covariance to |
Source code in lat_beams/fitting/map/cov.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
empirical_profile_covariance(all_prof, radial_centers, lmax, n_modes=-1)
Estimate profile and beam-window covariance directly from an ensemble of normalized radial profiles.
This is the sample-space analogue of bessel_profile_covariance.
Rather than estimating a covariance in the potentially degenerate
parameter space, the covariance is estimated directly from the
ensemble of model profiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_prof
|
Float[NDArray, 'n_samp n_radial']
|
Ensemble of normalized radial profile realizations. Each row is
one realization and each column corresponds to |
required |
radial_centers
|
Float[NDArray, n_radial]
|
Radial coordinates corresponding to the profile bins, in radians. |
required |
lmax
|
int
|
Maximum multipole for the beam window function. |
required |
n_modes
|
int
|
Number of covariance modes to retain in |
-1
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
Optional[AxisManager]
|
AxisManager with a structure matching Fields include:
The eigenvalues/eigenvectors correspond to the profile-space
covariance, rather than the original parameter-space covariance.
If |
Source code in lat_beams/fitting/map/cov.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
estimate_noise_autocov(noise_cutouts, max_lag=30)
Estimate the spatial noise autocovariance from noise-only image cutouts.
Each cutout is mean-subtracted, and its spatial autocorrelation is computed using the 2D Fourier transform. The autocorrelations are averaged across cutouts and normalized by the number of overlapping pixel pairs at each lag.
Assuming spatially stationary noise, the autocovariance at lag (dy, dx) is estimated as
where \(X_k\) is the mean-subtracted noise in cutout \(k\), \(P(dy,dx)\) contains pixel positions where both pixels lie within the cutout, and \(n_{\mathrm{pairs}}\) is the number of such pairs per cutout. For rectangular cutouts,
The FFT calculation uses zero-padding to avoid periodic wraparound correlations. The estimate assumes that the cutouts share a common autocovariance function and contain no significant residual sources or spatially varying background structure.
Subtracting each cutout's sample mean can introduce bias because the estimated mean is itself noisy. Let \(M=n_y n_x\) be the number of pixels in a cutout, \(N_i\) the noise at pixel \(i\), and
If the true noise covariance is \(C_{ij}=\\mathbb{E}[N_iN_j]\) and the noise has zero mean, then the covariance after mean subtraction is
Thus, the estimated covariance can differ from the true covariance, particularly for small cutouts or noise with long-range correlations. For independent, identically distributed noise with variance \(\sigma^2\), mean subtraction reduces the expected variance to \(\sigma^2(1-1/M)\) and introduces an expected covariance of \(-\sigma^2/M\) between distinct pixels.
The FFT-based estimate does not correct for this bias. For large cutouts with short-range correlations, the effect is often small. Correcting it for correlated noise generally requires a model of the underlying covariance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
noise_cutouts
|
Float[ndmap, 'ncutouts ny nx']
|
Noise-only image cutouts, should be the same geometry as my map. |
required |
max_lag
|
int
|
Maximum absolute pixel lag along each axis. The output includes
lags from |
30
|
Returns:
| Name | Type | Description |
|---|---|---|
cov |
Float[ndarray, '{2 * max_lag + 1} {2 * max_lag + 1}']
|
Estimated spatial autocovariance. The central element,
|
Source code in lat_beams/fitting/map/cov.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
estimate_noise_ps2d(residual, ivar, guess, window=None, smooth_scale=1.0, floor_fraction=0.001)
Estimate a regularized two-dimensional noise power spectrum.
Whiten the map residual using the per-pixel inverse variance, estimate its two-dimensional power spectrum, and smooth the spectrum using a scale derived from the nominal beam width. Non-finite and non-positive spectral values are replaced by a positive floor defined relative to the estimated white-noise reference level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
residual
|
Float[ndmap, 'ny nx']
|
Full-map data minus the fitted model, in map units. |
required |
ivar
|
Float[ndmap, 'ny nx']
|
Per-pixel inverse variance, in inverse squared map units. Pixels with non-finite or non-positive inverse variance are excluded from the noise estimate. |
required |
guess
|
AxisManager
|
Beam estimate containing |
required |
window
|
Optional[Float[ndmap, 'ny nx']]
|
Optional apodization window. If |
None
|
smooth_scale
|
float
|
Dimensionless multiplier for the beam-derived spectral smoothing width. |
1.0
|
floor_fraction
|
float
|
Minimum spectral power as a fraction of the estimated white-noise reference level. Must be positive. |
1e-3
|
Returns:
| Name | Type | Description |
|---|---|---|
corr_noise |
Float[ndmap, 'ny nx']
|
Regularized two-dimensional noise power spectrum, represented as
an |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in lat_beams/fitting/map/cov.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
invsafe(mat, thresh=1e-14)
Compute a truncated SVD pseudoinverse. Singular values smaller than
$$ {\rm thresh} \times \max(s) $$ are discarded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat
|
Float[NDArray, 'n n']
|
Matrix to invert. |
required |
thresh
|
float
|
Relative singular-value threshold. |
1e-14
|
Returns:
| Type | Description |
|---|---|
Float[NDArray, 'n n']
|
Truncated pseudoinverse of |
Source code in lat_beams/fitting/map/cov.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
invscale(mat, do_invsafe=False, thresh=1e-14)
Invert a matrix after diagonal normalization. The matrix is first rescaled as
where
The scaled matrix is then inverted and transformed back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat
|
Float[NDArray, 'n n']
|
Matrix to invert. |
required |
do_invsafe
|
bool
|
Use |
False
|
thresh
|
float
|
Relative singular-value threshold.
Passed to |
1e-14
|
Returns:
| Type | Description |
|---|---|
Float[NDArray, 'n n']
|
Approximate inverse of |
Source code in lat_beams/fitting/map/cov.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | |