Kernel mean matching
neureptrace.decoding.kernel_mean_matching implements dependency-light kernel mean matching (KMM) for unlabeled target-adaptive source weighting.
KMM estimates non-negative source-row weights so the weighted source feature distribution better matches an unlabeled target feature distribution in a kernel space. This is useful for covariate-shift correction in cross-subject decoding.
Protocol interpretation:
- uses source features
X_s, - optionally uses source labels
y_sfor class-balanced post-normalization, - uses unlabeled target features
X_t, - does not accept held-out target labels
y_t.
Therefore, KMM is a Protocol 2 / unlabeled target-adaptive method.
Typical use:
from neureptrace.decoding.kernel_mean_matching import kernel_mean_matching_weights
result = kernel_mean_matching_weights(
X_source,
X_target_unlabeled,
kernel="rbf",
gamma="median",
max_weight=10.0,
)
sample_weight = result.weights
neureptrace.decoding.kernel_mean_matching
Kernel mean matching for unlabeled target-adaptive source weighting.
Kernel mean matching (KMM) reweights source rows so the weighted source feature distribution better matches an unlabeled held-out target feature distribution in an RKHS. The implementation here is deliberately dependency-light: it uses NumPy plus SciPy's SLSQP optimizer rather than an external quadratic-programming package.
The public API is Category 2 in the cross-subject protocol taxonomy. It uses source features and unlabeled target features, and may optionally use source labels only for class-balanced post-normalization. It does not accept held-out target labels.
KernelMeanMatchingConfig
dataclass
Configuration for dependency-light KMM source weighting.
Source code in src/neureptrace/decoding/kernel_mean_matching.py
38 39 40 41 42 43 44 45 46 47 48 49 50 | |
KernelMeanMatchingResult
dataclass
KMM source weights and provenance metadata.
Source code in src/neureptrace/decoding/kernel_mean_matching.py
53 54 55 56 57 58 59 60 61 | |
kernel_mean_matching_weights(source_features, target_features, *, kernel='rbf', gamma=DEFAULT_KMM_GAMMA, max_weight=DEFAULT_KMM_MAX_WEIGHT, epsilon=DEFAULT_KMM_EPSILON, regularization=DEFAULT_KMM_REGULARIZATION, max_iter=DEFAULT_KMM_MAX_ITER, tol=DEFAULT_KMM_TOL, normalize=True, source_labels=None, class_balance=False)
Estimate source-row weights from unlabeled target features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_features
|
Sequence[Sequence[float]] | ndarray
|
Source feature matrix, usually pooled over source subjects in an outer LOSO fold. |
required |
target_features
|
Sequence[Sequence[float]] | ndarray
|
Unlabeled held-out target feature matrix used for covariate-shift
weighting. The function intentionally has no |
required |
kernel
|
str | None
|
|
'rbf'
|
gamma
|
float | str
|
RBF kernel width. |
DEFAULT_KMM_GAMMA
|
max_weight
|
float | str
|
Upper bound for each source weight before optional normalization. |
DEFAULT_KMM_MAX_WEIGHT
|
epsilon
|
float | str | None
|
KMM sum-constraint slack. |
DEFAULT_KMM_EPSILON
|
regularization
|
float | str
|
Diagonal ridge added to the source-source kernel matrix. |
DEFAULT_KMM_REGULARIZATION
|
max_iter
|
int | str
|
SLSQP optimizer controls. |
DEFAULT_KMM_MAX_ITER
|
tol
|
int | str
|
SLSQP optimizer controls. |
DEFAULT_KMM_MAX_ITER
|
normalize
|
bool
|
If true, final weights are rescaled to have mean one. |
True
|
source_labels
|
Sequence[Any] | ndarray | None
|
Optional source labels used only when |
None
|
class_balance
|
bool
|
If true, rescale weights so each source class receives equal total mass. This uses source labels only and remains Category 2. |
False
|
Returns:
| Type | Description |
|---|---|
KernelMeanMatchingResult
|
Mean-one source sample weights and protocol metadata. |
Source code in src/neureptrace/decoding/kernel_mean_matching.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
kernel_mean_matching_weights_from_config(source_features, target_features, config=None, *, source_labels=None)
Estimate KMM weights from a dataclass or mapping configuration.
Source code in src/neureptrace/decoding/kernel_mean_matching.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
kmm_config(*, kernel='rbf', gamma=DEFAULT_KMM_GAMMA, max_weight=DEFAULT_KMM_MAX_WEIGHT, epsilon=DEFAULT_KMM_EPSILON, regularization=DEFAULT_KMM_REGULARIZATION, max_iter=DEFAULT_KMM_MAX_ITER, tol=DEFAULT_KMM_TOL, normalize=True, class_balance=False)
Normalize user-facing KMM configuration values.
Source code in src/neureptrace/decoding/kernel_mean_matching.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
normalize_kmm_kernel(value)
Normalize supported KMM kernel aliases.
Source code in src/neureptrace/decoding/kernel_mean_matching.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
normalize_kmm_epsilon(value, *, n_source)
Resolve KMM sum-constraint slack.
Source code in src/neureptrace/decoding/kernel_mean_matching.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
resolve_kmm_gamma(value, source_features, target_features)
Resolve an RBF gamma value from a numeric value or heuristic alias.
Source code in src/neureptrace/decoding/kernel_mean_matching.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |