Regression¶
Weighted censored Gaussian regression: tricube kernel weights, negative log-likelihood
with analytical gradients, and the L-BFGS-B optimizer that replaces R's survival::survreg.
wrtds.regression
¶
Censored Gaussian MLE regression and tricube weight functions for WRTDS.
tricube(d, h)
¶
Tricube kernel weight function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Distance values (scalar or array). |
required | |
h
|
Half-window bandwidth (positive scalar). |
required |
Returns:
| Type | Description |
|---|---|
|
Weights in [0, 1]: |
Source code in wrtds/regression.py
compute_weights(dec_year, logq, uncen, target_dec_year, target_logq, window_y, window_q, window_s, min_num_obs=100, min_num_uncen=50, edge_adjust=True, record_start=None, record_end=None)
¶
Compute product of three tricube weights (time, discharge, season).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dec_year
|
Decimal year for each observation. |
required | |
logq
|
Log-discharge for each observation. |
required | |
uncen
|
Boolean or 0/1 array indicating uncensored observations. |
required | |
target_dec_year
|
Estimation point time coordinate. |
required | |
target_logq
|
Estimation point discharge coordinate. |
required | |
window_y
|
Half-window for time dimension (years). |
required | |
window_q
|
Half-window for discharge dimension (log units). |
required | |
window_s
|
Half-window for season dimension (fraction of year). |
required | |
min_num_obs
|
Minimum total observations with nonzero weight. |
100
|
|
min_num_uncen
|
Minimum uncensored observations with nonzero weight. |
50
|
|
edge_adjust
|
If True, expand time window near record edges. |
True
|
|
record_start
|
Earliest DecYear in record (for edge adjustment). |
None
|
|
record_end
|
Latest DecYear in record (for edge adjustment). |
None
|
Returns:
| Type | Description |
|---|---|
|
Array of combined tricube weights, one per observation. |
Source code in wrtds/regression.py
neg_log_likelihood_with_grad(theta, X, y_uncen, y_cen_high, uncen_mask, weights)
¶
Negative log-likelihood and analytical gradient for weighted censored Gaussian.
The function returns (cost, grad) in a single call so that
scipy.optimize.minimize(..., jac=True) can use the exact gradient,
avoiding ~6 finite-difference evaluations per iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
Parameter vector |
required | |
X
|
Design matrix |
required | |
y_uncen
|
|
required | |
y_cen_high
|
|
required | |
uncen_mask
|
Boolean array, True where observation is uncensored. |
required | |
weights
|
Tricube weights |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple |
|
|
|
Source code in wrtds/regression.py
run_surv_reg(sample_data, weights, max_retries=3)
¶
Run one weighted censored Gaussian regression.
Replaces R's survival::survreg(..., dist="gaussian").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_data
|
Dict of arrays with keys |
required | |
weights
|
Tricube weight for each observation. |
required | |
max_retries
|
Number of jitter-and-retry attempts on convergence failure. |
3
|
Returns:
| Type | Description |
|---|---|
|
Tuple |
|
|
and sigma is the positive scale parameter. |
Source code in wrtds/regression.py
predict(beta, sigma, dec_year, logq)
¶
Predict log-concentration, SE, and bias-corrected concentration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beta
|
Regression coefficients |
required | |
sigma
|
Scale parameter (positive scalar). |
required | |
dec_year
|
Decimal year(s) at which to predict. |
required | |
logq
|
Log-discharge(s) at which to predict. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple |
|
|
inputs. |
|
|
bias correction. |
Source code in wrtds/regression.py
jitter_sample(conc_low, conc_high, scale=0.01, rng=None)
¶
Add small multiplicative jitter to concentration bounds.
Matches R's jitterSam — used when MLE fails to converge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conc_low
|
Lower concentration bounds (NaN for left-censored). |
required | |
conc_high
|
Upper concentration bounds. |
required | |
scale
|
Standard deviation of the log-normal jitter. |
0.01
|
|
rng
|
Optional numpy Generator for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
|
Tuple |