Skip to content

Multiple Sparse Priors

Solver ID: MSP

Usage

from invert import Solver

# fwd = ...    (mne.Forward object)
# evoked = ... (mne.Evoked object)

solver = Solver("MSP")
solver.make_inverse_operator(fwd)
stc = solver.apply_inverse_operator(evoked)
stc.plot()

Overview

Bayesian source imaging method that combines multiple spatial priors and estimates their hyperparameters with Restricted Maximum Likelihood (ReML).

References

  1. Friston, K., Harrison, L., Daunizeau, J., Kiebel, S., Phillips, C., Trujillo-Barreto, N., & Mattout, J. (2008). Multiple sparse priors for the M/EEG inverse problem. NeuroImage, 39(3), 1104–1120.

API Reference

Bases: BaseSolver

Class for the Multiple Sparse Priors (MSP) inverse solution with Restricted Maximum Likelihood (ReML) [1].

This method uses ReML to estimate the hyperparameters of multiple sparse priors defined over different spatial patterns in source space.

References

[1] Friston, K., Harrison, L., Daunizeau, J., Kiebel, S., Phillips, C., Trujillo-Barreto, N., ... & Mattout, J. (2008). Multiple sparse priors for the M/EEG inverse problem. NeuroImage, 39(3), 1104-1120.

Source code in invert/solvers/bayesian/msp.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 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
208
209
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
237
238
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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
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
class SolverMSP(BaseSolver):
    """Class for the Multiple Sparse Priors (MSP) inverse solution with
    Restricted Maximum Likelihood (ReML) [1].

    This method uses ReML to estimate the hyperparameters of multiple sparse
    priors defined over different spatial patterns in source space.

    References
    ----------
    [1] Friston, K., Harrison, L., Daunizeau, J., Kiebel, S., Phillips, C.,
    Trujillo-Barreto, N., ... & Mattout, J. (2008). Multiple sparse priors for
    the M/EEG inverse problem. NeuroImage, 39(3), 1104-1120.

    """

    meta = SolverMeta(
        acronym="MSP",
        full_name="Multiple Sparse Priors",
        category="Bayesian",
        description=(
            "Bayesian source imaging method that combines multiple spatial priors "
            "and estimates their hyperparameters with Restricted Maximum Likelihood "
            "(ReML)."
        ),
        references=[
            "Friston, K., Harrison, L., Daunizeau, J., Kiebel, S., Phillips, C., Trujillo-Barreto, N., & Mattout, J. (2008). Multiple sparse priors for the M/EEG inverse problem. NeuroImage, 39(3), 1104–1120.",
        ],
    )

    def __init__(
        self,
        patterns=None,
        name="Multiple Sparse Priors",
        include_sensor_noise=True,
        eta=-32.0,
        Pi=1.0 / 256.0,
        max_iter=512,
        tol=1e-4,
        n_patterns=None,
        diffusion_parameter=0.1,
        patch_order=2,
        reduce_rank=True,
        rank="auto",
        **kwargs,
    ):
        """
        Parameters
        ----------
        patterns : list, array, or None
            List of spatial patterns in source space. Each pattern should be
            a 1D array of shape (n_sources,). If None, patterns will be
            automatically generated using graph Laplacian smoothing.
        include_sensor_noise : bool
            Whether to include a sensor noise component in the model.
        eta : float
            Hyperprior mean (per component) in log-space.
        Pi : float
            Hyperprior precision (scalar, becomes Pi * I).
        max_iter : int
            Maximum number of ReML iterations.
        tol : float
            Convergence tolerance for relative change in hyperparameters.
        n_patterns : int or None
            Number of patterns to generate if patterns is None. If None,
            a reasonable default based on the source space will be used.
        diffusion_parameter : float
            Diffusion parameter (alpha) for graph smoothing when generating
            patterns automatically. Default is 0.1.
        patch_order : int
            Order of smoothing for generated patches (1 = small patches,
            2 = medium patches, etc.). Default is 1.
        """
        self.name = name
        self.patterns = patterns
        self.include_sensor_noise = include_sensor_noise
        self.eta = eta
        self.Pi = Pi
        self.max_iter = max_iter
        self.tol = tol
        self.n_patterns = n_patterns
        self.diffusion_parameter = diffusion_parameter
        self.patch_order = patch_order
        return super().__init__(reduce_rank=reduce_rank, rank=rank, **kwargs)

    def make_inverse_operator(self, forward, mne_obj, *args, alpha="auto", **kwargs):
        """Calculate inverse operator using Multiple Sparse Priors with ReML.

        Parameters
        ----------
        forward : mne.Forward
            The mne-python Forward model instance.
        mne_obj : [mne.Evoked, mne.Epochs, mne.io.Raw]
            The MNE data object.
        alpha : float or 'auto'
            Regularization parameter (note: MSP uses its own hyperparameter
            optimization via ReML, so this is mainly for compatibility).

        Return
        ------
        self : object returns itself for convenience
        """
        super().make_inverse_operator(forward, *args, alpha=alpha, **kwargs)
        data = self.unpack_data_obj(mne_obj)
        leadfield = self.leadfield

        # Generate patterns automatically if not provided
        if self.patterns is None:
            if self.verbose:
                logger.info(
                    "Generating spatial patterns using graph Laplacian smoothing..."
                )
            self.patterns = self._generate_patterns(forward)

        # Run MSP-ReML algorithm
        m, v = data.shape
        d = leadfield.shape[1]
        C = (data @ data.T) / float(v)  # sample covariance over time/epochs

        # Build rank-1 vectors u_i = L @ q_i and trace normalization factors
        # Instead of storing full m×m Q_i matrices, store m-vectors u_i
        # so that Q_i = u_i @ u_i^T / trace_i
        U_cols = []
        trace_factors = []

        for q in self.patterns:
            q = q.flatten()
            u = leadfield @ q  # m-vector
            trace_val = np.dot(u, u)  # trace of u @ u^T
            if trace_val > 0:
                U_cols.append(u / np.sqrt(trace_val))  # normalize so u@u^T has trace 1
            else:
                U_cols.append(u)
            trace_factors.append(trace_val)

        # U is m × n_patterns, each column is a normalized u_i
        U = np.column_stack(U_cols) if U_cols else np.empty((m, 0))
        n_source_components = U.shape[1]

        # Total number of components (with optional noise)
        has_noise = self.include_sensor_noise
        p = n_source_components + (1 if has_noise else 0)

        eta_vec = np.full(p, self.eta, dtype=float)
        P_hyper = self.Pi * np.eye(p)

        # Initialize lambdas at the hyperprior mean
        lambdas = np.full(p, self.eta, dtype=float)

        # Add small random perturbations to break symmetry
        rng = np.random.RandomState(42)
        lambdas += rng.randn(p) * 0.1

        if self.verbose:
            logger.info(
                f"Initialized {p} hyperparameters with lambda = {self.eta:.2f} (±0.1)"
            )
            logger.info(
                f"Running ReML optimization (max_iter={self.max_iter}, tol={self.tol})..."
            )

        # Add bounds to prevent numerical overflow
        lambda_min = -32.0
        lambda_max = 32.0

        # Pruning threshold for inactive components
        prune_threshold = 1e-12

        # ReML iterations (Fisher scoring)
        prev = None
        initial_grad_norm = None

        for it in range(1, self.max_iter + 1):
            # Clip lambdas to prevent overflow
            lambdas = np.clip(lambdas, lambda_min, lambda_max)
            scales = np.exp(lambdas)

            # Identify active components (pruning)
            active_mask = scales > prune_threshold

            # Build R = noise_scale * I/m + U_active @ diag(scales_active) @ U_active^T
            if has_noise:
                noise_scale = scales[0]
                source_scales = scales[1:]
                source_active = active_mask[1:]
            else:
                noise_scale = 0.0
                source_scales = scales
                source_active = active_mask

            active_idx = np.where(source_active)[0]
            U_active = U[:, active_idx]
            s_active = source_scales[active_idx]

            # R = noise_scale * I/m + U_active @ diag(s_active) @ U_active^T
            R = np.eye(m) * (noise_scale / m)
            if len(active_idx) > 0:
                # Efficiently: R += (U_active * s_active) @ U_active^T
                R += (U_active * s_active[np.newaxis, :]) @ U_active.T

            # Check for numerical issues in R
            if not np.isfinite(R).all():
                if self.verbose:
                    logger.warning(
                        f"[MSP {it:02d}] Warning: Non-finite values in R, stopping"
                    )
                break

            invR = self._solve_R(R, np.eye(m))

            # Check for numerical issues in invR
            if not np.isfinite(invR).all():
                if self.verbose:
                    logger.warning(
                        f"[MSP {it:02d}] Warning: Non-finite values in invR, stopping"
                    )
                break

            C_minus_R = C - R

            # Precompute W = invR @ U (m × n_source_components)
            W = invR @ U  # m × n_source_components

            # Precompute CmR_W = (C - R) @ W for gradient
            CmR_W = C_minus_R @ W  # m × n_source_components

            # Precompute V = R @ W for Hessian
            V = R @ W  # m × n_source_components

            # Gradient computation
            grad = np.empty(p)

            if has_noise:
                invR_CmR = invR @ C_minus_R  # reuse
                trace_noise = (noise_scale / m) * np.sum(invR * invR_CmR.T)
                grad[0] = 0.5 * v * trace_noise - P_hyper[0, 0] * (
                    lambdas[0] - eta_vec[0]
                )

                w_dot_cmrw = np.sum(W * CmR_W, axis=0)  # length n_source_components
                grad[1:] = 0.5 * v * source_scales * w_dot_cmrw - np.diag(P_hyper)[
                    1:
                ] * (lambdas[1:] - eta_vec[1:])
            else:
                w_dot_cmrw = np.sum(W * CmR_W, axis=0)
                grad[:] = 0.5 * v * source_scales * w_dot_cmrw - np.diag(P_hyper) * (
                    lambdas - eta_vec
                )

            # Hessian computation
            Fkk = np.empty((p, p))

            if has_noise:
                WtV = W.T @ V  # n_source × n_source
                ss_outer = np.outer(source_scales, source_scales)
                Fkk[1:, 1:] = -0.5 * v * ss_outer * (WtV**2) - P_hyper[1:, 1:]

                invR_sq_trace = np.sum(invR * invR.T)
                Fkk[0, 0] = (
                    -0.5 * v * (noise_scale / m) ** 2 * invR_sq_trace - P_hyper[0, 0]
                )

                w_norms_sq = np.sum(W**2, axis=0)  # ||w_j||^2 for each j
                cross = (noise_scale / m) * source_scales * w_norms_sq
                Fkk[0, 1:] = -0.5 * v * cross - P_hyper[0, 1:]
                Fkk[1:, 0] = Fkk[0, 1:]
            else:
                WtV = W.T @ V
                ss_outer = np.outer(source_scales, source_scales)
                Fkk[:, :] = -0.5 * v * ss_outer * (WtV**2) - P_hyper

            # Check for numerical issues
            if not np.isfinite(grad).all() or not np.isfinite(Fkk).all():
                if self.verbose:
                    logger.warning(
                        f"[MSP {it:02d}] Warning: Non-finite values in grad/Fkk, stopping"
                    )
                break

            # Newton step with regularization
            try:
                eigvals = np.linalg.eigvalsh(Fkk)
                cond_num = np.abs(eigvals).max() / (np.abs(eigvals).min() + 1e-12)

                if cond_num > 1e12 or np.min(eigvals) > -1e-8:
                    reg_factor = max(1e-6, 1e-4 * np.abs(np.diag(Fkk)).mean())
                    Fkk_reg = Fkk - reg_factor * np.eye(p)
                    step = -np.linalg.solve(Fkk_reg, grad)
                else:
                    step = -np.linalg.solve(Fkk, grad)
            except np.linalg.LinAlgError:
                step = -grad / (np.abs(np.diag(Fkk)).mean() + 1e-6)

            if not np.isfinite(step).all():
                if self.verbose:
                    logger.warning(f"[MSP {it:02d}] Warning: Non-finite step, stopping")
                break

            lambdas_new = np.clip(lambdas + step, lambda_min, lambda_max)

            # Convergence check
            delta = lambdas_new - lambdas
            rel = np.linalg.norm(delta) / (np.linalg.norm(lambdas) + 1e-12)
            grad_norm = np.linalg.norm(grad)

            if initial_grad_norm is None:
                initial_grad_norm = max(grad_norm, 1e-10)

            grad_rel = grad_norm / initial_grad_norm

            lambdas = lambdas_new

            if self.verbose:
                logger.debug(
                    f"[MSP {it:02d}] grad_norm={grad_norm:.3e} (rel={grad_rel:.3e})  "
                    f"change={rel:.3e}  lambda_range=[{lambdas.min():.2f}, {lambdas.max():.2f}]"
                )

            if prev is not None:
                if rel < self.tol and grad_rel < self.tol:
                    if self.verbose:
                        logger.info(
                            f"MSP converged after {it} iterations (change={rel:.3e}, grad_rel={grad_rel:.3e})"
                        )
                    break
                elif rel < 1e-8:
                    if self.verbose:
                        logger.info(
                            f"MSP stopped after {it} iterations (minimal change, rel={rel:.3e})"
                        )
                    break
            prev = lambdas.copy()

        # Final covariance
        lambdas = np.clip(lambdas, lambda_min, lambda_max)
        scales = np.exp(lambdas)

        if has_noise:
            noise_scale = scales[0]
            source_scales = scales[1:]
        else:
            noise_scale = 0.0
            source_scales = scales

        R = np.eye(m) * (noise_scale / m)
        if U.shape[1] > 0:
            R += (U * source_scales[np.newaxis, :]) @ U.T

        invR = self._solve_R(R, np.eye(m))

        # Build source-space prior Re = sum_i (scale_i / trace_i) * q_i @ q_i^T
        # Using rank-1 structure for efficiency
        Re = np.zeros((d, d))
        for i, q in enumerate(self.patterns):
            q = q.flatten()
            tf = trace_factors[i]
            coeff = source_scales[i] / tf if tf > 0 else source_scales[i]
            q_col = q.reshape(-1, 1)
            Re += coeff * (q_col @ q_col.T)

        # Posterior mean inverse operator: M = Re L^T R^{-1}
        M = Re @ leadfield.T @ invR

        # Store diagnostics
        self.lambdas = lambdas
        self.R = R
        self.invR = invR
        self.Re = Re

        # Create inverse operator (single operator for MSP)
        self.inverse_operators = [InverseOperator(M, self.name)]
        return self

    def _generate_patterns(self, forward):
        """Generate spatial patterns using graph Laplacian smoothing.

        This method creates localized spatial patterns by:
        1. Computing the adjacency matrix of the source space
        2. Applying graph Laplacian diffusion smoothing
        3. Selecting diverse spatial locations to center the patterns

        Parameters
        ----------
        forward : mne.Forward
            The forward solution containing source space information.

        Return
        ------
        patterns : list
            List of spatial patterns (numpy arrays).
        """
        # Get source space adjacency
        adjacency = mne.spatial_src_adjacency(forward["src"], verbose=0)
        adjacency = csr_matrix(adjacency)
        n_dipoles = adjacency.shape[0]

        # Build implicit diffusion operator: (I + alpha * L)
        from scipy.sparse import eye as speye

        L_sparse = laplacian(adjacency)
        smoother = speye(n_dipoles, format="csr") + self.diffusion_parameter * L_sparse

        # Determine number of patterns
        if self.n_patterns is None:
            self.n_patterns = int(np.clip(np.sqrt(n_dipoles), 300, 1000))

        if self.verbose:
            logger.info(
                f"Generating {self.n_patterns} spatial patterns with order {self.patch_order}"
            )

        # Farthest-point sampling on 3D source coordinates
        coords = []
        for src_hemi in forward["src"]:
            coords.append(src_hemi["rr"][src_hemi["vertno"]])
        coords = np.vstack(coords)  # (n_dipoles, 3)

        n_select = min(self.n_patterns, n_dipoles)
        rng = np.random.RandomState(0)
        pattern_centers = np.empty(n_select, dtype=int)
        pattern_centers[0] = rng.randint(n_dipoles)
        min_dist = np.full(n_dipoles, np.inf)
        for k in range(1, n_select):
            diff = coords - coords[pattern_centers[k - 1]]
            dist = np.sum(diff**2, axis=1)
            min_dist = np.minimum(min_dist, dist)
            pattern_centers[k] = np.argmax(min_dist)

        # Factor the smoother once with splu, then solve for each RHS
        lu = splu(smoother.tocsc())

        patterns = []
        for center in pattern_centers:
            rhs = np.zeros(n_dipoles)
            rhs[center] = 1.0

            pattern = rhs
            for _ in range(self.patch_order):
                pattern = lu.solve(pattern)

            norm = np.linalg.norm(pattern)
            if norm > 0:
                pattern /= norm

            patterns.append(pattern)

        if self.verbose:
            logger.info(
                f"Generated {len(patterns)} patterns, each with {n_dipoles} sources"
            )

        return patterns

    @staticmethod
    def _solve_R(R, X, reg=1e-12):
        """Solve R Z = X stably using Cholesky decomposition

        Parameters
        ----------
        R : array
            Covariance matrix to invert
        X : array
            Right-hand side
        reg : float
            Regularization parameter for numerical stability
        """
        m = R.shape[0]

        # Add small regularization for numerical stability
        R_reg = R + reg * np.trace(R) / m * np.eye(m)

        try:
            U = np.linalg.cholesky(R_reg)
            Y_ = np.linalg.solve(U, X)
            Z = np.linalg.solve(U.T, Y_)
            return Z
        except np.linalg.LinAlgError:
            # If Cholesky fails, try with more regularization
            R_reg = R + 1e-6 * np.trace(R) / m * np.eye(m)
            try:
                return np.linalg.solve(R_reg, X)
            except np.linalg.LinAlgError:
                # Last resort: use pseudoinverse
                return np.linalg.pinv(R) @ X

__init__

__init__(
    patterns=None,
    name="Multiple Sparse Priors",
    include_sensor_noise=True,
    eta=-32.0,
    Pi=1.0 / 256.0,
    max_iter=512,
    tol=0.0001,
    n_patterns=None,
    diffusion_parameter=0.1,
    patch_order=2,
    reduce_rank=True,
    rank="auto",
    **kwargs,
)

Parameters:

Name Type Description Default
patterns list, array, or None

List of spatial patterns in source space. Each pattern should be a 1D array of shape (n_sources,). If None, patterns will be automatically generated using graph Laplacian smoothing.

None
include_sensor_noise bool

Whether to include a sensor noise component in the model.

True
eta float

Hyperprior mean (per component) in log-space.

-32.0
Pi float

Hyperprior precision (scalar, becomes Pi * I).

1.0 / 256.0
max_iter int

Maximum number of ReML iterations.

512
tol float

Convergence tolerance for relative change in hyperparameters.

0.0001
n_patterns int or None

Number of patterns to generate if patterns is None. If None, a reasonable default based on the source space will be used.

None
diffusion_parameter float

Diffusion parameter (alpha) for graph smoothing when generating patterns automatically. Default is 0.1.

0.1
patch_order int

Order of smoothing for generated patches (1 = small patches, 2 = medium patches, etc.). Default is 1.

2
Source code in invert/solvers/bayesian/msp.py
def __init__(
    self,
    patterns=None,
    name="Multiple Sparse Priors",
    include_sensor_noise=True,
    eta=-32.0,
    Pi=1.0 / 256.0,
    max_iter=512,
    tol=1e-4,
    n_patterns=None,
    diffusion_parameter=0.1,
    patch_order=2,
    reduce_rank=True,
    rank="auto",
    **kwargs,
):
    """
    Parameters
    ----------
    patterns : list, array, or None
        List of spatial patterns in source space. Each pattern should be
        a 1D array of shape (n_sources,). If None, patterns will be
        automatically generated using graph Laplacian smoothing.
    include_sensor_noise : bool
        Whether to include a sensor noise component in the model.
    eta : float
        Hyperprior mean (per component) in log-space.
    Pi : float
        Hyperprior precision (scalar, becomes Pi * I).
    max_iter : int
        Maximum number of ReML iterations.
    tol : float
        Convergence tolerance for relative change in hyperparameters.
    n_patterns : int or None
        Number of patterns to generate if patterns is None. If None,
        a reasonable default based on the source space will be used.
    diffusion_parameter : float
        Diffusion parameter (alpha) for graph smoothing when generating
        patterns automatically. Default is 0.1.
    patch_order : int
        Order of smoothing for generated patches (1 = small patches,
        2 = medium patches, etc.). Default is 1.
    """
    self.name = name
    self.patterns = patterns
    self.include_sensor_noise = include_sensor_noise
    self.eta = eta
    self.Pi = Pi
    self.max_iter = max_iter
    self.tol = tol
    self.n_patterns = n_patterns
    self.diffusion_parameter = diffusion_parameter
    self.patch_order = patch_order
    return super().__init__(reduce_rank=reduce_rank, rank=rank, **kwargs)

make_inverse_operator

make_inverse_operator(
    forward, mne_obj, *args, alpha="auto", **kwargs
)

Calculate inverse operator using Multiple Sparse Priors with ReML.

Parameters:

Name Type Description Default
forward Forward

The mne-python Forward model instance.

required
mne_obj [Evoked, Epochs, Raw]

The MNE data object.

required
alpha float or auto

Regularization parameter (note: MSP uses its own hyperparameter optimization via ReML, so this is mainly for compatibility).

'auto'
Return

self : object returns itself for convenience

Source code in invert/solvers/bayesian/msp.py
def make_inverse_operator(self, forward, mne_obj, *args, alpha="auto", **kwargs):
    """Calculate inverse operator using Multiple Sparse Priors with ReML.

    Parameters
    ----------
    forward : mne.Forward
        The mne-python Forward model instance.
    mne_obj : [mne.Evoked, mne.Epochs, mne.io.Raw]
        The MNE data object.
    alpha : float or 'auto'
        Regularization parameter (note: MSP uses its own hyperparameter
        optimization via ReML, so this is mainly for compatibility).

    Return
    ------
    self : object returns itself for convenience
    """
    super().make_inverse_operator(forward, *args, alpha=alpha, **kwargs)
    data = self.unpack_data_obj(mne_obj)
    leadfield = self.leadfield

    # Generate patterns automatically if not provided
    if self.patterns is None:
        if self.verbose:
            logger.info(
                "Generating spatial patterns using graph Laplacian smoothing..."
            )
        self.patterns = self._generate_patterns(forward)

    # Run MSP-ReML algorithm
    m, v = data.shape
    d = leadfield.shape[1]
    C = (data @ data.T) / float(v)  # sample covariance over time/epochs

    # Build rank-1 vectors u_i = L @ q_i and trace normalization factors
    # Instead of storing full m×m Q_i matrices, store m-vectors u_i
    # so that Q_i = u_i @ u_i^T / trace_i
    U_cols = []
    trace_factors = []

    for q in self.patterns:
        q = q.flatten()
        u = leadfield @ q  # m-vector
        trace_val = np.dot(u, u)  # trace of u @ u^T
        if trace_val > 0:
            U_cols.append(u / np.sqrt(trace_val))  # normalize so u@u^T has trace 1
        else:
            U_cols.append(u)
        trace_factors.append(trace_val)

    # U is m × n_patterns, each column is a normalized u_i
    U = np.column_stack(U_cols) if U_cols else np.empty((m, 0))
    n_source_components = U.shape[1]

    # Total number of components (with optional noise)
    has_noise = self.include_sensor_noise
    p = n_source_components + (1 if has_noise else 0)

    eta_vec = np.full(p, self.eta, dtype=float)
    P_hyper = self.Pi * np.eye(p)

    # Initialize lambdas at the hyperprior mean
    lambdas = np.full(p, self.eta, dtype=float)

    # Add small random perturbations to break symmetry
    rng = np.random.RandomState(42)
    lambdas += rng.randn(p) * 0.1

    if self.verbose:
        logger.info(
            f"Initialized {p} hyperparameters with lambda = {self.eta:.2f} (±0.1)"
        )
        logger.info(
            f"Running ReML optimization (max_iter={self.max_iter}, tol={self.tol})..."
        )

    # Add bounds to prevent numerical overflow
    lambda_min = -32.0
    lambda_max = 32.0

    # Pruning threshold for inactive components
    prune_threshold = 1e-12

    # ReML iterations (Fisher scoring)
    prev = None
    initial_grad_norm = None

    for it in range(1, self.max_iter + 1):
        # Clip lambdas to prevent overflow
        lambdas = np.clip(lambdas, lambda_min, lambda_max)
        scales = np.exp(lambdas)

        # Identify active components (pruning)
        active_mask = scales > prune_threshold

        # Build R = noise_scale * I/m + U_active @ diag(scales_active) @ U_active^T
        if has_noise:
            noise_scale = scales[0]
            source_scales = scales[1:]
            source_active = active_mask[1:]
        else:
            noise_scale = 0.0
            source_scales = scales
            source_active = active_mask

        active_idx = np.where(source_active)[0]
        U_active = U[:, active_idx]
        s_active = source_scales[active_idx]

        # R = noise_scale * I/m + U_active @ diag(s_active) @ U_active^T
        R = np.eye(m) * (noise_scale / m)
        if len(active_idx) > 0:
            # Efficiently: R += (U_active * s_active) @ U_active^T
            R += (U_active * s_active[np.newaxis, :]) @ U_active.T

        # Check for numerical issues in R
        if not np.isfinite(R).all():
            if self.verbose:
                logger.warning(
                    f"[MSP {it:02d}] Warning: Non-finite values in R, stopping"
                )
            break

        invR = self._solve_R(R, np.eye(m))

        # Check for numerical issues in invR
        if not np.isfinite(invR).all():
            if self.verbose:
                logger.warning(
                    f"[MSP {it:02d}] Warning: Non-finite values in invR, stopping"
                )
            break

        C_minus_R = C - R

        # Precompute W = invR @ U (m × n_source_components)
        W = invR @ U  # m × n_source_components

        # Precompute CmR_W = (C - R) @ W for gradient
        CmR_W = C_minus_R @ W  # m × n_source_components

        # Precompute V = R @ W for Hessian
        V = R @ W  # m × n_source_components

        # Gradient computation
        grad = np.empty(p)

        if has_noise:
            invR_CmR = invR @ C_minus_R  # reuse
            trace_noise = (noise_scale / m) * np.sum(invR * invR_CmR.T)
            grad[0] = 0.5 * v * trace_noise - P_hyper[0, 0] * (
                lambdas[0] - eta_vec[0]
            )

            w_dot_cmrw = np.sum(W * CmR_W, axis=0)  # length n_source_components
            grad[1:] = 0.5 * v * source_scales * w_dot_cmrw - np.diag(P_hyper)[
                1:
            ] * (lambdas[1:] - eta_vec[1:])
        else:
            w_dot_cmrw = np.sum(W * CmR_W, axis=0)
            grad[:] = 0.5 * v * source_scales * w_dot_cmrw - np.diag(P_hyper) * (
                lambdas - eta_vec
            )

        # Hessian computation
        Fkk = np.empty((p, p))

        if has_noise:
            WtV = W.T @ V  # n_source × n_source
            ss_outer = np.outer(source_scales, source_scales)
            Fkk[1:, 1:] = -0.5 * v * ss_outer * (WtV**2) - P_hyper[1:, 1:]

            invR_sq_trace = np.sum(invR * invR.T)
            Fkk[0, 0] = (
                -0.5 * v * (noise_scale / m) ** 2 * invR_sq_trace - P_hyper[0, 0]
            )

            w_norms_sq = np.sum(W**2, axis=0)  # ||w_j||^2 for each j
            cross = (noise_scale / m) * source_scales * w_norms_sq
            Fkk[0, 1:] = -0.5 * v * cross - P_hyper[0, 1:]
            Fkk[1:, 0] = Fkk[0, 1:]
        else:
            WtV = W.T @ V
            ss_outer = np.outer(source_scales, source_scales)
            Fkk[:, :] = -0.5 * v * ss_outer * (WtV**2) - P_hyper

        # Check for numerical issues
        if not np.isfinite(grad).all() or not np.isfinite(Fkk).all():
            if self.verbose:
                logger.warning(
                    f"[MSP {it:02d}] Warning: Non-finite values in grad/Fkk, stopping"
                )
            break

        # Newton step with regularization
        try:
            eigvals = np.linalg.eigvalsh(Fkk)
            cond_num = np.abs(eigvals).max() / (np.abs(eigvals).min() + 1e-12)

            if cond_num > 1e12 or np.min(eigvals) > -1e-8:
                reg_factor = max(1e-6, 1e-4 * np.abs(np.diag(Fkk)).mean())
                Fkk_reg = Fkk - reg_factor * np.eye(p)
                step = -np.linalg.solve(Fkk_reg, grad)
            else:
                step = -np.linalg.solve(Fkk, grad)
        except np.linalg.LinAlgError:
            step = -grad / (np.abs(np.diag(Fkk)).mean() + 1e-6)

        if not np.isfinite(step).all():
            if self.verbose:
                logger.warning(f"[MSP {it:02d}] Warning: Non-finite step, stopping")
            break

        lambdas_new = np.clip(lambdas + step, lambda_min, lambda_max)

        # Convergence check
        delta = lambdas_new - lambdas
        rel = np.linalg.norm(delta) / (np.linalg.norm(lambdas) + 1e-12)
        grad_norm = np.linalg.norm(grad)

        if initial_grad_norm is None:
            initial_grad_norm = max(grad_norm, 1e-10)

        grad_rel = grad_norm / initial_grad_norm

        lambdas = lambdas_new

        if self.verbose:
            logger.debug(
                f"[MSP {it:02d}] grad_norm={grad_norm:.3e} (rel={grad_rel:.3e})  "
                f"change={rel:.3e}  lambda_range=[{lambdas.min():.2f}, {lambdas.max():.2f}]"
            )

        if prev is not None:
            if rel < self.tol and grad_rel < self.tol:
                if self.verbose:
                    logger.info(
                        f"MSP converged after {it} iterations (change={rel:.3e}, grad_rel={grad_rel:.3e})"
                    )
                break
            elif rel < 1e-8:
                if self.verbose:
                    logger.info(
                        f"MSP stopped after {it} iterations (minimal change, rel={rel:.3e})"
                    )
                break
        prev = lambdas.copy()

    # Final covariance
    lambdas = np.clip(lambdas, lambda_min, lambda_max)
    scales = np.exp(lambdas)

    if has_noise:
        noise_scale = scales[0]
        source_scales = scales[1:]
    else:
        noise_scale = 0.0
        source_scales = scales

    R = np.eye(m) * (noise_scale / m)
    if U.shape[1] > 0:
        R += (U * source_scales[np.newaxis, :]) @ U.T

    invR = self._solve_R(R, np.eye(m))

    # Build source-space prior Re = sum_i (scale_i / trace_i) * q_i @ q_i^T
    # Using rank-1 structure for efficiency
    Re = np.zeros((d, d))
    for i, q in enumerate(self.patterns):
        q = q.flatten()
        tf = trace_factors[i]
        coeff = source_scales[i] / tf if tf > 0 else source_scales[i]
        q_col = q.reshape(-1, 1)
        Re += coeff * (q_col @ q_col.T)

    # Posterior mean inverse operator: M = Re L^T R^{-1}
    M = Re @ leadfield.T @ invR

    # Store diagnostics
    self.lambdas = lambdas
    self.R = R
    self.invR = invR
    self.Re = Re

    # Create inverse operator (single operator for MSP)
    self.inverse_operators = [InverseOperator(M, self.name)]
    return self