geossm.stmodel.LRPredictionResults¶
- class geossm.stmodel.LRPredictionResults(fitted_state, y_pred, Sigma_y_pred=None, var_pred=None, points=None, block_p=None, timestamps=None, crs=None, tdelta=None, in_support=None, H=None, state_cols=None)[source]¶
Bases:
objectOut-of-sample predictions of a fitted LRStateSpaceModel, returned by LRStateSpaceResults.get_prediction(df).
The predictive mean is H x_t + X beta and the uncertainty is the state-driven covariance H P_t H’ (plug-in parameters, without the measurement error s2e), at the prediction sites and timestamps, for every response variable stacked as in the fit (block_p splits them). All arrays are read-only NumPy arrays in the dtype of the fit.
- y_pred¶
Predicted mean.
- Type:
numpy.ndarray,shape (p,T)
- Sigma_y_pred¶
Predictive covariance (only with
get_prediction(df, std="full"): its memory grows withp^2 T).- Type:
numpy.ndarray,shape (p,p,T), orNone
- var_pred¶
Point-wise predictive variance, the diagonal of the covariance (None with
std=False).- Type:
numpy.ndarray,shape (p,T), orNone
- points¶
Prediction sites of each response variable, shape
(n_i, 2).- Type:
listofnumpy.ndarray
- block_p¶
Row boundaries of each response variable in the stacked arrays.
- Type:
numpy.ndarray
- timestamps¶
Prediction timestamps of each response variable.
- Type:
listofnumpy.ndarray
- crs¶
CRS of the prediction sites.
- Type:
pyproj.CRS
- tdelta¶
Wall-clock time of the prediction kernels (seconds).
- Type:
float
- in_support¶
Prediction sites the latent field enters (inside the latent domains, with a non-empty basis). At the others the mean is X beta and the state-driven variance 0 (a warning is given when the predictions are computed): mask them with in_support.
- Type:
numpy.ndarrayofbool,shape (p,), orNone
- y_pred_back_list, var_pred_back_list, Sigma_y_pred_back_list
Original-scale predictions (delta method), set by back_transform (Sigma_y_pred_back_list only with the full covariance).
- Type:
listofnumpy.ndarrayorNone
- Parameters:
fitted_state (
FittedState) – State of the fit the predictions come from.y_pred – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
Sigma_y_pred – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
var_pred – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
points – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
block_p – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
timestamps – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
crs – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
tdelta – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
in_support – Values of the attributes of the same name; var_pred defaults to the diagonal of Sigma_y_pred. Built by LRStateSpaceResults.get_prediction; not meant to be called directly.
H (optional) – Measurement matrix at the prediction sites and the columns of fitted_state.P_smoothed of the prediction times, for the polygon variances of to_poly without the
(p, p, T)covariance.state_cols (optional) – Measurement matrix at the prediction sites and the columns of fitted_state.P_smoothed of the prediction times, for the polygon variances of to_poly without the
(p, p, T)covariance.
Examples
Typical usage:
pred = results.get_prediction(grid) pred.back_transform(jnp.exp) # if y = np.log(...) gdf = pred.to_geo() areal = pred.to_poly("regions.shp", std=True)
Methods
Map the predictions back to the response's original scale with the second-order delta method, per response variable, and store them as y_pred_back_list/var_pred_back_list (and Sigma_y_pred_back_list with the full covariance) (see LRStateSpaceResults.back_transform for the method and what is estimated: the original-scale mean of the latent signal).
statsmodels-style summary of the predictions: response names, number of sites, timestamps and values, range of the predicted mean, mean predictive standard deviation, runtime.
The predictions as a GeoDataFrame, one row per (site, timestamp), ready to be exported (e.g. .to_file("predictions.shp")).
Change of support: aggregate the point predictions of this object to areal (polygon) predictions, approximating the block mean Y(B) = 1/|B| * integral_B Y(s) ds of each polygon B by the average of the predicted values at the prediction points inside it.
Attributes
Per-response-variable diagonal blocks of Sigma_y_pred (cross-variable covariance dropped).
State of the fit these predictions were computed from (see LRStateSpaceResults.fitted_state).
Per-response-variable view of in_support.
Per-response-variable view of var_pred.
Response-variable names.
Per-response-variable view of y_pred.
- property fitted_state¶
State of the fit these predictions were computed from (see LRStateSpaceResults.fitted_state).
- Type:
FittedState
- property y_name¶
Response-variable names.
- Type:
tuple of str
- property y_pred_list¶
Per-response-variable view of y_pred.
- Type:
list of ndarray
- property in_support_list¶
Per-response-variable view of in_support.
- Type:
list of ndarray or None
- property var_pred_list¶
Per-response-variable view of var_pred.
- Type:
list of ndarray or None
- property Sigma_y_pred_list¶
Per-response-variable diagonal blocks of Sigma_y_pred (cross-variable covariance dropped).
- Type:
list of ndarray or None
- back_transform(g_invs)[source]¶
Map the predictions back to the response’s original scale with the second-order delta method, per response variable, and store them as y_pred_back_list/var_pred_back_list (and Sigma_y_pred_back_list with the full covariance) (see LRStateSpaceResults.back_transform for the method and what is estimated: the original-scale mean of the latent signal).
- Parameters:
g_invs (
Callable[[float],float]orlist/tupleofcallables) – Inverse of the response transform (e.g. jnp.exp for a np.log(y) response), scalar -> scalar and JAX-traceable, or one per response variable.- Returns:
LRPredictionResults– self. Without the variances (std=False) the predictions are not back-transformed (warning).
- to_geo()[source]¶
The predictions as a GeoDataFrame, one row per (site, timestamp), ready to be exported (e.g. .to_file(“predictions.shp”)).
Columns: point_id, timestamp, then y_pred_<var>/std_pred_<var> for every response variable (plus y_pred_back_<var>/ std_pred_back_<var> after back_transform), in the dtype of the fit, and in_support_<var> (see in_support). std_* is the standard deviation of the latent mean (no measurement error).
- Returns:
geopandas.GeoDataFrame
- to_poly(polys, back_transform=False, std=False)[source]¶
Change of support: aggregate the point predictions of this object to areal (polygon) predictions, approximating the block mean Y(B) = 1/|B| * integral_B Y(s) ds of each polygon B by the average of the predicted values at the prediction points inside it.
The intended workflow is to predict on a dense grid first, then aggregate:
pred = results.get_prediction(fine_grid) gdf = pred.to_poly("regions.shp", std=True)
Since the average is linear, with W the (m, n) averaging matrix (W[k, j] = 1/n_k for the n_k points in polygon k), the areal predictive mean and covariance are, per time step t:
y_poly[:, t] = W @ y_pred[:, t] Sigma_poly[:, :, t] = W @ Sigma_pred[:, :, t] @ W.T
The areal variance involves the cross-covariances between points: the std of a polygon mean is sqrt(w_k’ Sigma w_k). With Sigma_t = H P_t H’ it is computed as diag((W H) P_t (W H)’), from the (m, q) matrix W H, so the (p, p, T) covariance is not needed (it is not stored by default). Averaging the point stds instead assumes perfect correlation (overestimating it), and averaging the variances divided by n_k assumes independence (underestimating it, increasingly so for denser grids).
Only the diagonal of Sigma_poly is computed and returned (the std_<var> columns): the cross-covariances between polygons (its off-diagonal) are not provided. The per-polygon std is therefore correct for each polygon on its own, but not sufficient for quantities combining several polygons (e.g. the uncertainty of a sum/difference of regions, or a joint confidence region), which would need the full W Sigma_t W’.
Notes
Sigma_y_pred is the state-driven covariance H P_t H’: it excludes the measurement error (whose contribution to a block mean vanishes as s2e / n_k, so it is correctly left out of the areal signal) and the estimation uncertainty of beta.
The discrete average approximates the areal integral only when the prediction points are dense and roughly uniform within each polygon; polygons containing no point get NaN (with a warning).
Only the per-time-step covariance is available, so the uncertainty of temporal aggregates (e.g. an annual polygon mean) cannot be derived from these outputs.
Cross-variable covariance is dropped (as in Sigma_y_pred_list): each response variable is aggregated independently.
With back_transform=True the original-scale point predictions (y_pred_back_list, from the delta method; covariance D_t H P_t H’ D_t with D_t = diag(h’(mu_t))) are averaged, i.e. the polygon mean of the original-scale process – not g_inv of the polygon mean on the model scale.
- Parameters:
polys (
listofshapely (Multi)Polygon,GeoSeries,GeoDataFrame, orpath) – The target polygons, or a path to a vector file (e.g. a shapefile) readable by geopandas.read_file. Reprojected to the prediction CRS (crs) if needed; geometries without a CRS are assumed to already be in it. The attribute columns of a GeoDataFrame/file are carried over to the output.back_transform (
bool, defaultFalse) – Aggregate the original-scale predictions instead. Requires .back_transform(g_invs) to have been run.std (
bool, defaultFalse) – Also compute the areal predictive std of each polygon mean (std_<var> columns), sqrt(diag(W Sigma_t W’)).
- Returns:
geopandas.GeoDataFrame– One row per (polygon, timestamp), with columns poly_id, timestamp, the input polygon attributes, then, per response variable <var> in y_name: n_points_<var> (prediction points in support in the polygon), <var> (areal mean over them), and std_<var> (if std=True) – the last two suffixed _back if back_transform=True – and geometry last.