STWR¶
This page documents 2 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
STWR¶
Spatiotemporal weighted regression.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import STWR |
| Signature | STWR(spatial_bandwidth: 'Bandwidth' = 'cv', *, adaptive: 'bool' = True, kernel: 'str' = 'bisquare', alpha: 'SelectionValue' = 0.3, theta: 'SelectionValue' = 0.0, tick_nums: 'Union[int, str, None]' = None, bandwidth_candidates: 'Optional[Sequence[Number]]' = None, alpha_candidates: 'Optional[Sequence[Number]]' = None, theta_candidates: 'Optional[Sequence[Number]]' = None, tick_candidates: 'Optional[Sequence[int]]' = None, fit_intercept: 'bool' = True, distance_metric: 'str' = 'euclidean', sigma2_v1: 'bool' = True, ridge: 'float' = 0.0, store_weights: 'bool' = True, verbose: 'bool' = False) -> 'None' |
| Maintained example | examples/models/04_stwr.py |
STWR ¶
STWR(
spatial_bandwidth: Bandwidth = "cv",
*,
adaptive: bool = True,
kernel: str = "bisquare",
alpha: SelectionValue = 0.3,
theta: SelectionValue = 0.0,
tick_nums: Union[int, str, None] = None,
bandwidth_candidates: Optional[Sequence[Number]] = None,
alpha_candidates: Optional[Sequence[Number]] = None,
theta_candidates: Optional[Sequence[Number]] = None,
tick_candidates: Optional[Sequence[int]] = None,
fit_intercept: bool = True,
distance_metric: str = "euclidean",
sigma2_v1: bool = True,
ridge: float = 0.0,
store_weights: bool = True,
verbose: bool = False
)
Spatiotemporal weighted regression.
STWR uses the response-value variation rate as its time distance. For a
current regression point :math:i and a past observation :math:j, the
public STWR v1.0 code computes
.. math::
d^T_{ij} = \frac{\Delta t_{\mathrm{all}}}{\Delta t_q}
\left|\frac{y_{j,t-q}-y_{i,t}}{y_{j,t-q}}\right|,
and maps it through :math:\tanh(d^T_{ij}/2). The final weight is a convex
combination of a spatial kernel and that temporal effect. The spatial
bandwidth at earlier stages is changed linearly by tan(theta).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spatial_bandwidth
|
Bandwidth
|
Current-stage fixed distance or adaptive neighbour
count. |
'cv'
|
adaptive
|
bool
|
Interpret spatial bandwidths as neighbour counts at the latest stage before conversion to local distance scales. |
True
|
kernel
|
str
|
Spatial kernel. The published implementation primarily uses
|
'bisquare'
|
alpha
|
SelectionValue
|
Temporal contribution in |
0.3
|
theta
|
SelectionValue
|
Spatial-bandwidth time slope in radians. Earlier bandwidths equal
the latest bandwidth minus |
0.0
|
tick_nums
|
Union[int, str, None]
|
Number of most recent stages used. |
None
|
bandwidth_candidates
|
Optional[Sequence[Number]]
|
Optional candidates for automatic bandwidth search. |
None
|
alpha_candidates
|
Optional[Sequence[Number]]
|
Optional candidates for automatic alpha search. |
None
|
theta_candidates
|
Optional[Sequence[Number]]
|
Optional candidates for automatic theta search. |
None
|
tick_candidates
|
Optional[Sequence[int]]
|
Optional candidates for automatic stage-count search. |
None
|
fit_intercept
|
bool
|
Include a local intercept. |
True
|
distance_metric
|
str
|
Spatial distance metric supported by pyGWRx. |
'euclidean'
|
sigma2_v1
|
bool
|
Use |
True
|
ridge
|
float
|
Non-negative numerical ridge added to local normal matrices. The intercept is not penalized. |
0.0
|
store_weights
|
bool
|
Store the final latest-stage-to-history weight matrix. |
True
|
verbose
|
bool
|
Print selection and fit information. |
False
|
References
Que, X., Ma, X., Ma, C., & Chen, Q. (2020). A spatiotemporal weighted regression model (STWR v1.0) for analyzing local nonstationarity in space and time. Geoscientific Model Development, 13, 6149-6164.
Source code in src/pygwrx/models/stwr.py
fit ¶
fit(
X_list: Sequence[Union[ndarray, DataFrame]],
y_list: Sequence[Union[ndarray, Series]],
coords_list: Sequence[Union[ndarray, DataFrame]],
time_intervals: Sequence[Number],
) -> "STWR"
Fit STWR for the latest time stage using recent historical stages.
Source code in src/pygwrx/models/stwr.py
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 | |
predict_result ¶
predict_result(
X: Union[ndarray, DataFrame],
coords: Union[ndarray, DataFrame],
*,
reference_y: Optional[Union[ndarray, Series]] = None
) -> STWRPredictionResult
Predict at new locations in the latest modeled time stage.
reference_y supplies the current-stage response baseline required by
the STWR variation-rate time distance. When omitted, it is estimated by
inverse-distance weighting from the latest observed responses, following
the prediction strategy in the public STWR code.
Source code in src/pygwrx/models/stwr.py
predict ¶
predict(
X: Union[ndarray, DataFrame],
coords: Union[ndarray, DataFrame],
*,
reference_y: Optional[Union[ndarray, Series]] = None
) -> np.ndarray
Return STWR predictions at latest-stage locations.
Source code in src/pygwrx/models/stwr.py
get_results ¶
Return latest-stage fitted values and local coefficients.
Source code in src/pygwrx/models/stwr.py
STWRPredictionResult¶
Detailed predictions produced at the latest modeled time stage.
| Property | Value |
|---|---|
| Type | class |
| Import | from pygwrx.models import STWRPredictionResult |
| Signature | STWRPredictionResult(predictions: 'np.ndarray', coef: 'np.ndarray', intercept: 'np.ndarray', coords: 'np.ndarray', feature_names: 'Tuple[str, ...]', reference_y: 'np.ndarray') -> None |
| Maintained example | examples/models/04_stwr.py |
STWRPredictionResult
dataclass
¶
STWRPredictionResult(
predictions: ndarray,
coef: ndarray,
intercept: ndarray,
coords: ndarray,
feature_names: Tuple[str, ...],
reference_y: ndarray,
)
Detailed predictions produced at the latest modeled time stage.
to_frame ¶
Return predictions and local parameters as a DataFrame.
Source code in src/pygwrx/models/stwr.py
Runnable examples used on this page¶
examples/models/04_stwr.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Fit STWR from multiple observation snapshots."""
# Allow this script to run directly from any working directory.
import sys
from pathlib import Path
_PROJECT_ROOT = Path(__file__).resolve().parents[2]
_EXAMPLES_ROOT = _PROJECT_ROOT / "examples"
_SRC_ROOT = _PROJECT_ROOT / "src"
for _path in (_SRC_ROOT, _EXAMPLES_ROOT):
if str(_path) not in sys.path:
sys.path.insert(0, str(_path))
from _common import print_model_result, stwr_stages
from pygwrx import STWR, STWRPredictionResult
X_list, y_list, coords_list, intervals = stwr_stages()
model = STWR(
spatial_bandwidth=10,
adaptive=True,
alpha=0.3,
theta=0.0,
tick_nums=2,
store_weights=True,
).fit(X_list, y_list, coords_list, intervals)
print_model_result(model)
result = model.predict_result(
X_list[-1].iloc[:3],
coords_list[-1].iloc[:3],
reference_y=y_list[-1][:3],
)
assert isinstance(result, STWRPredictionResult)
print(result.to_frame())