smap
Functions:
| Name | Description |
|---|---|
smap | Perform S-Map (local linear regression) from X to Y. |
weights | Compute S-Map exponential weights, zeroing out masked library points. |
smap(X: np.ndarray, Y: np.ndarray, Q: np.ndarray, *, theta: float, alpha: float = 1e-10, mask: np.ndarray | None = None, use_tensor: bool = False) -> np.ndarrayPerform S-Map (local linear regression) from X to Y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X | ndarray | The input data | required |
Y | ndarray | The target data | required |
Q | ndarray | The query points for which to make predictions. | required |
theta | float | Locality parameter. (0: global linear, >0: local linear) | required |
alpha | float | Regularization parameter to stabilize the inversion. | 1e-10 |
use_tensor | bool | Whether to use tinygrad.Tensor for computation. This may be slower than the NumPy implementation in most cases for now. | False |
Returns:
| Name | Type | Description |
|---|---|---|
predictions | ndarray | The predicted values based on the weighted linear regression. |
Raises:
| Type | Description |
|---|---|
ValueError | - If the input arrays X and Y do not have the same number of points. - If theta is negative. |
Examples:
import numpy as np
from edmkit.embedding import lagged_embedfrom edmkit.smap import smap
# Generate a simple time series (logistic map)N = 300x = np.zeros(N)x[0] = 0.4for i in range(1, N): x[i] = 3.9 * x[i - 1] * (1 - x[i - 1])
tau = 2E = 3
embedding = lagged_embed(x, tau=tau, e=E)shift = tau * (E - 1)
lib_size = 200Tp = 1X = embedding[:lib_size - shift]Y = x[shift + Tp : lib_size + Tp]Q = embedding[lib_size - shift : -Tp]actual = x[lib_size + Tp :]
# Local linear with theta=4.0predictions = smap(X, Y, Q, theta=4.0)correlation = np.corrcoef(predictions, actual)[0, 1]print(f"Correlation (theta=4.0): {correlation:.3f}")
# Global linear with theta=0.0predictions_global = smap(X, Y, Q, theta=0.0)correlation_global = np.corrcoef(predictions_global, actual)[0, 1]print(f"Correlation (theta=0.0): {correlation_global:.3f}")weights
Section titled “weights”weights(D: np.ndarray, theta: float, *, mask: np.ndarray | None = None, min_points: int) -> np.ndarrayCompute S-Map exponential weights, zeroing out masked library points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D | ndarray | Distance matrix — (M, N) or (B, M, N). | required |
theta | float | Locality parameter. | required |
mask | ndarray | None | Boolean mask over the library axis — (N,) or (B, N). | None |
min_points | int | Minimum number of valid library points required. | required |