hispecdrp.primitives.utils.stats

Robust statistics helpers used by HISPEC DRP primitives.

Biweight estimators, weighted moments, outlier-resistant summary stats, and posterior mode / uncertainty extraction.

Functions

biweight_location(x[, M, c])

Calculate the biweight location of an array.

biweight_midvariance(x[, M, c])

Calculate the biweight midvariance of an array.

get_err_from_posterior(x, posterior)

Return the mode, and the left and right errors of a distribution.

mad(x[, M])

Calculate the median absolute deviation of an array.

robust_stats(x[, w, n_sigma, maxiters])

Compute robust mean and standard deviation using iterative outlier rejection based on biweight location and midvariance.

weighted_mean(x, w)

Calculate the weighted mean of an array.

weighted_quantile(x[, w, q])

Calculate the weighted quantile of an array.

weighted_stddev(x, w[, mu])

Calculate the weighted standard deviation of an array.

biweight_location(x, M=None, c=6.0)[source]

Calculate the biweight location of an array.

Parameters:
  • x (np.ndarray) – Array of values.

  • c (float) – Tuning constant for biweight estimator (default=9.0). See astropy documentation for details.

  • M (float | None) – Initial guess of the location. If None, it will be calculated as the median location of x.

biweight_midvariance(x, M=None, c=9.0)[source]

Calculate the biweight midvariance of an array.

See https://docs.astropy.org/en/latest/api/astropy.stats.biweight_midvariance.html for details.

Parameters:
  • x (np.ndarray) – Array of values.

  • c (float) – Tuning constant for biweight estimator (default=9.0). See astropy documentation for details.

  • M (float | None) – Initial guess of the location. If None, it will be calculated as the median location of x.

get_err_from_posterior(x, posterior)[source]

Return the mode, and the left and right errors of a distribution. The errors are defined with a 68% confidence level.

Note: Copy pasted from BREADS https://github.com/jruffio/breads

Parameters:
  • x (Sampling of the 1D posterior)

  • posterior (Posterior array)

Returns:

Mode, left error, right error

mad(x, M=None)[source]

Calculate the median absolute deviation of an array.

Parameters:
  • x – Array of values.

  • M – Optional median value. If None, it will be calculated as the median of x.

Returns:

np.ndarray – Median absolute deviation of the array.

robust_stats(x, w=None, n_sigma=4.0, maxiters=100)[source]

Compute robust mean and standard deviation using iterative outlier rejection based on biweight location and midvariance. Final mean and standard deviation are computed using the remaining good data points based on the weighted mean and weighted standard deviation.

Parameters:
  • x (np.ndarray) – Input array of values.

  • w (np.ndarray, optional) – Weights for each value. If None, uniform weights are used.

  • n_sigma (float, optional) – Sigma threshold for outlier rejection. Defaults to 4.0.

  • maxiters (int, optional) – Maximum iterations for convergence. Defaults to 100.

Returns:
  • float – Robust weighted mean.

  • float – Robust weighted standard deviation.

weighted_mean(x, w)[source]

Calculate the weighted mean of an array.

Parameters:
  • x (np.ndarray) – Array of values.

  • w (np.ndarray) – Array of weights.

Returns:

float – Weighted mean.

weighted_quantile(x, w=None, q=0.5)[source]

Calculate the weighted quantile of an array.

Parameters:
  • x (np.ndarray) – Array of values.

  • w (np.ndarray) – Array of weights (defaults to uniform weights).

  • q (float) – Quantile to compute (0 <= q <= 1).

weighted_stddev(x, w, mu=None)[source]

Calculate the weighted standard deviation of an array.

Parameters:
  • x (np.ndarray) – Array of values.

  • w (np.ndarray) – Array of weights.

  • mu (float | None, optional) – Pre-computed weighted mean. If None, it will be calculated from x and w.

Returns:

float – Weighted standard deviation or NaN if no valid data.