Linear Models
Classical least squares regression variants: OLS, WLS, and RLS.
OLS - Ordinary Least Squares
The statistical workhorse for linear regression with full inference support.
Variants
- Scalar Fit:
anofox_stats_ols_fit(y, x, [options]) -> STRUCT - Aggregate Fit:
anofox_stats_ols_fit_agg(y, x, [options]) -> STRUCT - Window Predict:
anofox_stats_ols_fit_predict(y, x, [options]) OVER (...) -> STRUCT - Batch Predict:
anofox_stats_ols_predict_agg(y, x, [options]) -> LIST(STRUCT)
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
y | LIST(DOUBLE) / DOUBLE | Yes | - | Target values |
x | LIST(LIST(DOUBLE)) / LIST(DOUBLE) | Yes | - | Predictor matrix |
options | MAP | No | - | Configuration options |
Options MAP:
| Option | Type | Default | Description |
|---|---|---|---|
fit_intercept | BOOLEAN | true | Include intercept term |
compute_inference | BOOLEAN | false | Compute t-stats, p-values, CIs |
confidence_level | DOUBLE | 0.95 | Confidence level for intervals |
Example
SELECT
(model).coefficients[2] as marketing_effect,
(model).p_values[2] as p_value,
(model).r_squared as fit
FROM (
SELECT anofox_stats_ols_fit_agg(
revenue,
[marketing_spend, seasonality_index],
MAP {
'fit_intercept': 'true',
'compute_inference': 'true',
'confidence_level': '0.95'
}
) as model
FROM sales_data
);
WLS - Weighted Least Squares
For heteroscedastic data where variance differs across observations.
Variants
- Scalar Fit:
anofox_stats_wls_fit(y, x, weights, [options]) -> STRUCT - Aggregate Fit:
anofox_stats_wls_fit_agg(y, x, weight, [options]) -> STRUCT - Window Predict:
anofox_stats_wls_fit_predict(y, x, weight, [options]) OVER (...) -> STRUCT - Batch Predict:
anofox_stats_wls_predict_agg(y, x, weight, [options]) -> LIST(STRUCT)
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
y | LIST(DOUBLE) / DOUBLE | Yes | - | Target values |
x | LIST(LIST(DOUBLE)) / LIST(DOUBLE) | Yes | - | Predictor matrix |
weights / weight | LIST(DOUBLE) / DOUBLE | Yes | - | Observation weights |
options | MAP | No | - | Configuration options |
Options MAP:
| Option | Type | Default | Description |
|---|---|---|---|
fit_intercept | BOOLEAN | true | Include intercept term |
compute_inference | BOOLEAN | false | Compute inference statistics |
confidence_level | DOUBLE | 0.95 | Confidence level |
Example
SELECT anofox_stats_wls_fit_agg(
y,
[x1, x2],
sample_size,
MAP {'compute_inference': 'true'}
) as model
FROM aggregated_data;
When to use WLS:
- Residual variance changes with predictor values
- Aggregated data with different sample sizes
- Known measurement precision differences
RLS - Recursive Least Squares
For streaming/online learning with concept drift.
Variants
- Scalar Fit:
anofox_stats_rls_fit(y, x, [options]) -> STRUCT - Aggregate Fit:
anofox_stats_rls_fit_agg(y, x, [options]) -> STRUCT - Window Predict:
anofox_stats_rls_fit_predict(y, x, [options]) OVER (...) -> STRUCT - Batch Predict:
anofox_stats_rls_predict_agg(y, x, [options]) -> LIST(STRUCT)
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
y | LIST(DOUBLE) / DOUBLE | Yes | - | Target values |
x | LIST(LIST(DOUBLE)) / LIST(DOUBLE) | Yes | - | Predictor matrix |
options | MAP | No | - | Configuration options |
Options MAP:
| Option | Type | Default | Description |
|---|---|---|---|
forgetting_factor | DOUBLE | 1.0 | Weight decay (0.9-1.0) |
fit_intercept | BOOLEAN | true | Include intercept term |
initial_p_diagonal | DOUBLE | 100.0 | Initial covariance diagonal |
Example
SELECT
timestamp,
(adaptive_model).coefficients[2] as current_beta
FROM (
SELECT
timestamp,
anofox_stats_rls_fit_predict(
y,
[x],
MAP {'forgetting_factor': '0.95'}
) OVER (
ORDER BY timestamp
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) as adaptive_model
FROM sensor_data
);
When to use RLS:
- Real-time model updates
- Non-stationary relationships
- Concept drift detection