Local solvers¶
This page documents 4 public symbols. Each entry includes its purpose, import path, full API docstring, and the maintained example that exercises it.
weighted_least_squares¶
Solve a weighted least-squares problem.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import weighted_least_squares |
| Signature | weighted_least_squares(X: 'np.ndarray', y: 'np.ndarray', weights: 'np.ndarray', *, ridge: 'float' = 1e-08) -> 'Tuple[np.ndarray, np.ndarray]' |
| Maintained example | examples/core/04_solver.py |
weighted_least_squares ¶
weighted_least_squares(
X: ndarray,
y: ndarray,
weights: ndarray,
*,
ridge: float = _DEFAULT_RIDGE
) -> Tuple[np.ndarray, np.ndarray]
Solve a weighted least-squares problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Regression design matrix. |
required |
y
|
ndarray
|
Single-response target vector. |
required |
weights
|
ndarray
|
Non-negative observation weights. Exact zeros are preserved, so zero-weight observations are genuinely excluded from the local objective. |
required |
ridge
|
float
|
Non-negative diagonal regularization used to stabilize the local normal matrix. |
_DEFAULT_RIDGE
|
Returns:
| Name | Type | Description |
|---|---|---|
beta |
ndarray
|
Estimated coefficients. |
inverse_normal_matrix |
ndarray
|
Inverse (or pseudo-inverse) of |
Notes
The function solves
min_beta sum_i weights[i] * (y[i] - X[i] @ beta)^2
+ ridge * ||beta||_2^2.
Unlike the original implementation, zero weights are not replaced by 1e-10.
This preserves compact-support kernels and strict leave-one-out calculations.
Source code in src/pygwrx/core/solver.py
local_regression¶
Perform local weighted regression at target locations.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import local_regression |
| Signature | local_regression(X: 'np.ndarray', y: 'np.ndarray', coords: 'np.ndarray', target_coords: 'np.ndarray', kernel_func: 'KernelFunction', bandwidth: 'float', distance_metric: 'str' = 'euclidean', adaptive: 'bool' = False, *, ridge: 'float' = 1e-08) -> 'np.ndarray' |
| Maintained example | examples/core/04_solver.py |
local_regression ¶
local_regression(
X: ndarray,
y: ndarray,
coords: ndarray,
target_coords: ndarray,
kernel_func: KernelFunction,
bandwidth: float,
distance_metric: str = "euclidean",
adaptive: bool = False,
*,
ridge: float = _DEFAULT_RIDGE
) -> np.ndarray
Perform local weighted regression at target locations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Regression design matrix. |
required |
y
|
ndarray
|
Target vector. |
required |
coords
|
ndarray
|
Training coordinates. |
required |
target_coords
|
ndarray
|
Locations where local coefficients are required. |
required |
kernel_func
|
KernelFunction
|
Function with signature |
required |
bandwidth
|
float
|
Fixed distance when |
required |
distance_metric
|
str
|
Distance metric forwarded to |
'euclidean'
|
adaptive
|
bool
|
Whether |
False
|
ridge
|
float
|
Non-negative regularization shared with |
_DEFAULT_RIDGE
|
Returns:
| Name | Type | Description |
|---|---|---|
local_coefs |
ndarray
|
Local coefficient vectors. |
Notes
If a location has fewer positive-weight observations than design-matrix columns, the function emits a warning and returns the deterministic ridge-regularized solution. It never copies coefficients from a preceding location and never falls back silently to global OLS, so results do not depend on target ordering.
Source code in src/pygwrx/core/solver.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 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 | |
compute_hat_matrix¶
Compute the GWR hat matrix S such that y_hat = S @ y.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import compute_hat_matrix |
| Signature | compute_hat_matrix(X: 'np.ndarray', coords: 'np.ndarray', kernel_func: 'KernelFunction', bandwidth: 'float', distance_metric: 'str' = 'euclidean', adaptive: 'bool' = False, *, ridge: 'float' = 1e-08) -> 'np.ndarray' |
| Maintained example | examples/core/04_solver.py |
compute_hat_matrix ¶
compute_hat_matrix(
X: ndarray,
coords: ndarray,
kernel_func: KernelFunction,
bandwidth: float,
distance_metric: str = "euclidean",
adaptive: bool = False,
*,
ridge: float = _DEFAULT_RIDGE
) -> np.ndarray
Compute the GWR hat matrix S such that y_hat = S @ y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Regression design matrix. |
required |
coords
|
ndarray
|
Training coordinates. |
required |
kernel_func
|
KernelFunction
|
Spatial kernel function. |
required |
bandwidth
|
float
|
Fixed distance or adaptive neighbour-order bandwidth. |
required |
distance_metric
|
str
|
Distance metric forwarded to |
'euclidean'
|
adaptive
|
bool
|
Whether |
False
|
ridge
|
float
|
Non-negative regularization. The same value and normal-system construction are
used by |
_DEFAULT_RIDGE
|
Returns:
| Name | Type | Description |
|---|---|---|
hat_matrix |
ndarray
|
Full smoother matrix. |
Notes
The full matrix requires 8 * n_samples**2 bytes for float64 storage, excluding
the distance matrix and temporary arrays. Large-data models should eventually use
trace-only or chunked diagnostics instead.
Source code in src/pygwrx/core/solver.py
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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | |
adaptive_bandwidth_weights¶
Convert an adaptive neighbour-order bandwidth into a distance scale.
| Property | Value |
|---|---|
| Type | function |
| Import | from pygwrx.core import adaptive_bandwidth_weights |
| Signature | adaptive_bandwidth_weights(distances: 'np.ndarray', k_nearest: 'int') -> 'float' |
| Maintained example | examples/core/04_solver.py |
adaptive_bandwidth_weights ¶
Convert an adaptive neighbour-order bandwidth into a distance scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distances
|
ndarray
|
Non-negative distances from one regression location to all observations. |
required |
k_nearest
|
int
|
One-based neighbour order used to determine the local distance scale. The current PyGWRx convention includes a zero-distance self observation when the regression location is one of the training locations. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bandwidth |
float
|
Strictly positive distance scale corresponding to the k-th ordered distance. |
Notes
np.partition is used for expected O(n) selection. If duplicate coordinates put
the requested ordered distance at zero, the smallest positive distance is used. The
result is advanced by one representable float with np.nextafter so compact
kernels assign a positive (possibly tiny) weight to the boundary neighbour instead
of excluding it exactly at d == bandwidth.
Source code in src/pygwrx/core/solver.py
Runnable examples used on this page¶
examples/core/04_solver.py
# SPDX-FileCopyrightText: 2026 Jinghao Hu
# SPDX-License-Identifier: MIT
"""Run all public local-regression solver utilities."""
# 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 (
adaptive_bandwidth_weights,
compute_hat_matrix,
gaussian_kernel,
local_regression,
weighted_least_squares,
)
rng = np.random.default_rng(0)
coords = rng.uniform(0.0, 5.0, size=(20, 2))
x = rng.normal(size=20)
X = np.column_stack((np.ones(20), x))
y = 1.0 + 2.0 * x + rng.normal(0.0, 0.05, 20)
distances = np.linalg.norm(coords - coords[0], axis=1)
weights = gaussian_kernel(distances, bandwidth=2.0)
beta, covariance = weighted_least_squares(X, y, weights)
print("beta=", beta)
print("covariance_shape=", covariance.shape)
print("adaptive_scale=", adaptive_bandwidth_weights(distances, 8))
print(
"local_parameters=",
local_regression(X, y, coords, coords[:3], gaussian_kernel, 2.0),
)
hat = compute_hat_matrix(X, coords, gaussian_kernel, 2.0)
print("hat_shape_trace=", hat.shape, np.trace(hat))