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
| Function | Type | Description |
|---|---|---|
wls_fit | Scalar | Process complete arrays in a single call |
wls_fit_agg | Aggregate | Streaming row-by-row accumulation |
wls_fit_predict | Window | Fit and predict in a single pass |
wls_fit_predict_agg | Aggregate | Fit and predict with GROUP BY support |
wls_fit_predict_by | Table Macro | Per-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:
| Parameter | Type | Description |
|---|---|---|
| y | LIST(DOUBLE) | Response variable values |
| x | LIST(LIST(DOUBLE)) | Feature arrays |
| weights | LIST(DOUBLE) | Observation weights (same length as y) |
| fit_intercept | BOOLEAN | Include intercept term (default: true) |
| compute_inference | BOOLEAN | Compute t-tests, p-values, CIs (default: false) |
| confidence_level | DOUBLE | CI 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:
| Key | Type | Default | Description |
|---|---|---|---|
fit_intercept | BOOLEAN | true | Include intercept term |
compute_inference | BOOLEAN | false | Compute t-tests, p-values, CIs |
confidence_level | DOUBLE | 0.95 | CI confidence level |
solver | VARCHAR | 'svd' | Decomposition method: 'qr', 'svd', 'cholesky' |
hc_type | VARCHAR | '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 / variancewhen variance is known - Sample size:
weight = nwhen 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