WLS (Weighted Least Squares)

March 3, 2026 ยท View on GitHub

Weighted Least Squares regression for heteroscedastic data. Supports SVD, QR, and Cholesky decomposition with optional HC standard errors.

Functions

FunctionTypeDescription
wls_fitScalarProcess complete arrays in a single call
wls_fit_aggAggregateStreaming row-by-row accumulation
wls_fit_predictWindowFit and predict in a single pass
wls_fit_predict_aggAggregateFit and predict with GROUP BY support
wls_fit_predict_byTable MacroPer-group regression with long-format output

anofox_stats_wls_fit

Signature:

anofox_stats_wls_fit(
    y LIST(DOUBLE),
    x LIST(LIST(DOUBLE)),
    weights LIST(DOUBLE),
    [fit_intercept BOOLEAN DEFAULT true],
    [compute_inference BOOLEAN DEFAULT false],
    [confidence_level DOUBLE DEFAULT 0.95]
) -> STRUCT

Parameters:

ParameterTypeDescription
yLIST(DOUBLE)Response variable values
xLIST(LIST(DOUBLE))Feature arrays
weightsLIST(DOUBLE)Observation weights (same length as y)
fit_interceptBOOLEANInclude intercept term (default: true)
compute_inferenceBOOLEANCompute t-tests, p-values, CIs (default: false)
confidence_levelDOUBLECI confidence level (default: 0.95)

Returns: FitResult STRUCT

Example:

SELECT anofox_stats_wls_fit(
    [3.0, 5.0, 7.0, 9.0, 11.0],
    [[1.0, 2.0, 3.0, 4.0, 5.0]],
    [1.0, 2.0, 3.0, 2.0, 1.0]  -- higher weight for middle observations
);

anofox_stats_wls_fit_agg

Streaming WLS aggregate function.

SELECT anofox_stats_wls_fit_agg(y, [x], weight) FROM data;

MAP Options

All WLS functions accept an optional MAP parameter for advanced configuration:

KeyTypeDefaultDescription
fit_interceptBOOLEANtrueInclude intercept term
compute_inferenceBOOLEANfalseCompute t-tests, p-values, CIs
confidence_levelDOUBLE0.95CI confidence level
solverVARCHAR'svd'Decomposition method: 'qr', 'svd', 'cholesky'
hc_typeVARCHAR'none'Heteroscedasticity-consistent SEs: 'none', 'hc0', 'hc1', 'hc2', 'hc3'

Example with MAP options:

-- WLS with Cholesky decomposition
SELECT wls_fit_agg(
    y, [x], weight,
    {'solver': 'cholesky', 'compute_inference': true}
) FROM data;

Choosing Weights

  • Inverse variance: weight = 1 / variance when variance is known
  • Sample size: weight = n when observations are group means
  • Reliability: Higher weights for more reliable observations

Use Cases

  • Heteroscedastic data (non-constant variance)
  • Aggregated data (weighted by sample size)
  • When observation reliability varies
  • Survey data with sampling weights

See Also

  • OLS - Equal-weighted regression
  • RLS - Adaptive/online regression
  • Table Macros - Per-group predictions