Metrics¶
This page documents 11 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
compute_r_squared¶
Compute the coefficient of determination, R².
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_r_squared |
| Signature | compute_r_squared(y_true: 'np.ndarray', y_pred: 'np.ndarray') -> 'float' |
| Maintained example | examples/core/05_metrics.py |
compute_r_squared ¶
Compute the coefficient of determination, R².
R² is bounded above by 1 but may be negative. For a constant response, this function returns 1 for exact prediction and 0 otherwise, matching the finite convention commonly used by machine-learning libraries.
Source code in src/pygwrx/core/metrics.py
compute_adjusted_r_squared¶
Compute GWR adjusted R² from residual effective degrees of freedom.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_adjusted_r_squared |
| Signature | compute_adjusted_r_squared(y_true: 'np.ndarray', y_pred: 'np.ndarray', edf: 'float') -> 'float' |
| Maintained example | examples/core/05_metrics.py |
compute_adjusted_r_squared ¶
Compute GWR adjusted R² from residual effective degrees of freedom.
Formula¶
Adj R² = 1 - (1 - R²) * (n - 1) / (EDF - 1)
Here EDF is normally
n - 2 * trace(S) + trace(S.T @ S)
Source code in src/pygwrx/core/metrics.py
compute_aic¶
Compute Gaussian GWR AIC using trace(S) as the complexity term.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_aic |
| Signature | compute_aic(y_true: 'np.ndarray', y_pred: 'np.ndarray', n_params: 'float') -> 'float' |
| Maintained example | examples/core/05_metrics.py |
compute_aic ¶
Compute Gaussian GWR AIC using trace(S) as the complexity term.
Formula¶
AIC = n*log(RSS/n) + n*log(2π) + n + 2*(trace(S) + 1)
Notes
This is a Gaussian RSS-based criterion. It must not be used for Poisson, Binomial, Gamma, or other non-Gaussian GWGLM families.
Source code in src/pygwrx/core/metrics.py
compute_aicc¶
Compute Gaussian GWR corrected AIC (AICc). Compute the corrected Akaike information criterion for Gaussian GWR.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_aicc |
| Signature | compute_aicc(y_true: 'np.ndarray', y_pred: 'np.ndarray', n_params: 'float') -> 'float' |
| Maintained example | examples/core/05_metrics.py |
compute_aicc ¶
Compute Gaussian GWR corrected AIC (AICc). Compute the corrected Akaike information criterion for Gaussian GWR.
Formula¶
AICc = n*log(RSS/n) + n*log(2π) + n*(n + trace(S)) / (n - 2 - trace(S))
Returns infinity when the correction denominator is not positive.
Source code in src/pygwrx/core/metrics.py
compute_bic¶
Compute Gaussian GWR BIC using trace(S).
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_bic |
| Signature | compute_bic(y_true: 'np.ndarray', y_pred: 'np.ndarray', trace_S: 'float') -> 'float' |
| Maintained example | examples/core/05_metrics.py |
compute_bic ¶
Compute Gaussian GWR BIC using trace(S).
Formula¶
BIC = n*log(RSS/n) + n*log(2π) + n + log(n)*(trace(S) + 1)
Notes
This is a Gaussian RSS-based criterion and is not suitable for non-Gaussian GWGLM families.
Source code in src/pygwrx/core/metrics.py
compute_local_r_squared¶
Compute local weighted R² values. Compute a locally weighted coefficient of determination.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_local_r_squared |
| Signature | compute_local_r_squared(y_true: 'np.ndarray', y_pred: 'np.ndarray', weights: 'np.ndarray') -> 'np.ndarray' |
| Maintained example | examples/core/05_metrics.py |
compute_local_r_squared ¶
Compute local weighted R² values. Compute a locally weighted coefficient of determination.
Local R² is bounded above by 1 but may be negative when local predictions are worse than the local weighted-mean baseline.
Source code in src/pygwrx/core/metrics.py
compute_effective_parameters¶
Return trace(S), the first common effective-parameter convention.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_effective_parameters |
| Signature | compute_effective_parameters(hat_matrix: 'np.ndarray') -> 'float' |
| Maintained example | examples/core/05_metrics.py |
compute_effective_parameters ¶
Return trace(S), the first common effective-parameter convention.
Notes
A second convention is 2*trace(S) - trace(S.T @ S), returned by
:func:compute_enp. Keeping these definitions explicit avoids mixing
incompatible EDF/ENP conventions.
Source code in src/pygwrx/core/metrics.py
compute_diagnostics¶
Compute diagnostic statistics for a Gaussian GWR-style model.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_diagnostics |
| Signature | compute_diagnostics(y_true: 'np.ndarray', y_pred: 'np.ndarray', hat_matrix: 'Optional[np.ndarray]' = None, n_features: 'Optional[int]' = None, compute_gwr_stats: 'bool' = False, *, trace_S: 'Optional[float]' = None, trace_StS: 'Optional[float]' = None) -> 'Dict[str, float]' |
| Maintained example | examples/core/05_metrics.py |
compute_diagnostics ¶
compute_diagnostics(
y_true: ndarray,
y_pred: ndarray,
hat_matrix: Optional[ndarray] = None,
n_features: Optional[int] = None,
compute_gwr_stats: bool = False,
*,
trace_S: Optional[float] = None,
trace_StS: Optional[float] = None
) -> Dict[str, float]
Compute diagnostic statistics for a Gaussian GWR-style model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
ndarray
|
Observed values. |
required |
y_pred
|
ndarray
|
Fitted values. |
required |
hat_matrix
|
Optional[ndarray]
|
GWR hat matrix. When supplied, trace(S), trace(S'S), EDF, and ENP are derived from this matrix. |
None
|
n_features
|
Optional[int]
|
Backward-compatible parameter-count fallback used when no hat matrix is available. In the current PyGWRx model implementations this value is the number of columns in the fitted design matrix, so an intercept already present in that matrix must NOT be added again. |
None
|
compute_gwr_stats
|
bool
|
Include trace_S, trace_StS, edf, enp_v1, enp_v2, and compatibility key enp in the returned dictionary. |
False
|
trace_S
|
Optional[float]
|
Precomputed trace of the smoother matrix. Supply together with
|
None
|
trace_StS
|
Optional[float]
|
Precomputed trace of |
None
|
Notes
- Information criteria here are Gaussian RSS-based criteria.
- Models without a reliable hat matrix receive only a parameter-count approximation of complexity.
- For Poisson/Binomial/Gamma GWGLM, use family-specific log-likelihood and deviance diagnostics instead of this function.
Source code in src/pygwrx/core/metrics.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
compute_trace_statistics¶
Compute trace(S) and trace(S'S) from a validated hat matrix.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_trace_statistics |
| Signature | compute_trace_statistics(hat_matrix: 'np.ndarray') -> 'Dict[str, float]' |
| Maintained example | examples/core/05_metrics.py |
compute_trace_statistics ¶
Compute trace(S) and trace(S'S) from a validated hat matrix.
Source code in src/pygwrx/core/metrics.py
compute_edf¶
Compute residual effective degrees of freedom using the GWmodel convention.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_edf |
| Signature | compute_edf(n: 'int', trace_S: 'float', trace_StS: 'float') -> 'float' |
| Maintained example | examples/core/05_metrics.py |
compute_edf ¶
Compute residual effective degrees of freedom using the GWmodel convention.
EDF_v2 = n - 2*trace(S) + trace(S'S)
Source code in src/pygwrx/core/metrics.py
compute_enp¶
Compute the GWmodel-style effective number of parameters. Compute the effective parameter count using the GWmodel convention.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_enp |
| Signature | compute_enp(trace_S: 'float', trace_StS: 'float') -> 'float' |
| Maintained example | examples/core/05_metrics.py |
compute_enp ¶
Compute the GWmodel-style effective number of parameters. Compute the effective parameter count using the GWmodel convention.
ENP_v2 = 2*trace(S) - trace(S'S)
Source code in src/pygwrx/core/metrics.py
Runnable examples used on this page¶
examples/core/05_metrics.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Calculate every public model-fit and effective-parameter metric."""
# 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))
import numpy as np
from pygwrx.core import (
compute_adjusted_r_squared,
compute_aic,
compute_aicc,
compute_bic,
compute_diagnostics,
compute_edf,
compute_effective_parameters,
compute_enp,
compute_local_r_squared,
compute_r_squared,
compute_trace_statistics,
)
y = np.array([1.0, 2.0, 2.8, 4.2, 5.0])
yhat = np.array([1.1, 1.9, 3.0, 4.0, 4.9])
hat = np.eye(5) * 0.4
weights = np.vstack([np.linspace(1.0, 0.2, 5)] * 5)
trace = compute_trace_statistics(hat)
print("r2=", compute_r_squared(y, yhat))
print("adjusted_r2=", compute_adjusted_r_squared(y, yhat, edf=3.0))
print("aic=", compute_aic(y, yhat, n_params=2.0))
print("aicc=", compute_aicc(y, yhat, n_params=2.0))
print("bic=", compute_bic(y, yhat, trace_S=2.0))
print("local_r2=", compute_local_r_squared(y, yhat, weights))
print("effective_parameters=", compute_effective_parameters(hat))
print("trace_statistics=", trace)
print("edf=", compute_edf(5, trace["trace_S"], trace["trace_StS"]))
print("enp=", compute_enp(trace["trace_S"], trace["trace_StS"]))
print(
"diagnostics=",
compute_diagnostics(y, yhat, hat, n_features=1, compute_gwr_stats=True),
)