# Least Squares Optimization Spectral Deconvolution

> Use when after GCMSFormer has predicted the pure mass spectral matrix S from overlapped GC-MS peaks.

- Skill: `holobiomicslab/least-squares-optimization-spectral-deconvolution` (Agent Skill)
- Install (CLI): `npx skillmds@latest add holobiomicslab/least-squares-optimization-spectral-deconvolution`
- Raw SKILL.md: https://api.skillmd.com/api/skills/holobiomicslab/least-squares-optimization-spectral-deconvolution/raw
- Safety review: PASS (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: CC-BY-4.0
- Author: HolobiomicsLab (https://skillmd.com/u/holobiomicslab)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/holobiomicslab/least-squares-optimization-spectral-deconvolution

---


# least-squares-optimization-spectral-deconvolution

## Summary

Solves for the concentration distribution matrix C in GC-MS peak deconvolution by minimizing the reconstruction error between predicted mass spectra and observed overlapped peak intensities. This step follows Transformer-based pure spectrum prediction to obtain quantitative abundance estimates for each resolved component.

## When to use

Apply this skill after GCMSFormer has predicted the pure mass spectral matrix S from overlapped GC-MS peaks. Use it when you have resolved mass spectra and need to recover the concentration (abundance) distribution of each component across the chromatographic profile to reconstruct the original overlapped peak data.

## When NOT to use

- Mass spectral matrix S is not yet available or contains unresolved/overlapping peaks
- Input overlapped peak data is already deconvolved into pure component spectra (concentration matrix already solved)
- The least squares problem is underdetermined (fewer time points than components) without regularization

## Inputs

- Predicted mass spectral matrix S (m × n, where m = mass bins, n = number of components)
- Observed overlapped peak intensity data (m × t, where m = mass bins, t = time points)
- Pure mass spectra from GCMSFormer model output

## Outputs

- Concentration distribution matrix C (n × t, where n = components, t = time points)
- Reconstructed overlapped peak matrix (S·C)
- Residuals from reconstruction (||overlapped_peaks - S·C||²)

## How to apply

Formulate a least squares optimization problem: minimize ||overlapped_peaks - S·C||² where S is the m×n resolved mass spectral matrix (m mass bins, n components), C is the n×t unknown concentration matrix (n components, t time points), and overlapped_peaks is the m×t observed intensity matrix. Solve using numpy.linalg.lstsq or PyTorch optimization routines to obtain C. Validate by computing the reconstruction S·C and comparing against the input overlapped peak data using residual magnitude and spectral fidelity metrics; solutions should recover the overlapped data to high precision if the pure spectra are well-resolved.

## Related tools

- **PyTorch** (Solves least squares optimization to compute concentration matrix C using GPU-accelerated tensor operations) — https://pytorch.org/
- **numpy.linalg.lstsq** (CPU-based least squares solver for reconstructing concentration distribution)
- **GCMSFormer** (Produces the resolved mass spectral matrix S that serves as input to the least squares deconvolution) — https://github.com/zxguocsu/GCMSFormer

## Examples

```
import numpy as np; S = np.array([[...], [...]]);  overlapped_peaks = np.array([[...], [...]); C, residuals, rank, s = np.linalg.lstsq(S, overlapped_peaks, rcond=None); reconstructed = S @ C
```

## Evaluation signals

- Reconstruction error (||overlapped_peaks - S·C||²) is minimized and smaller than input noise level
- Reconstructed peaks S·C visually and numerically match the original overlapped peak data within measurement precision
- Concentration matrix C is non-negative (abundance constraints) and physically interpretable
- Residual distribution is white/random (not structured), indicating full spectrum capture
- Solution is stable across multiple runs or different initialization conditions

## Limitations

- Requires accurate pure mass spectra from GCMSFormer; errors in S directly propagate to C
- Least squares solution is sensitive to ill-conditioning when components have highly similar mass spectra
- No explicit regularization (L1/L2) is mentioned; may require Tikhonov or similar if the problem is underdetermined or noisy
- Assumes linear mixing model (overlapped_peaks = S·C); deviations from linearity are not modeled

## Evidence

- [other] minimize ||overlapped_peaks - S·C||² where C is the unknown concentration distribution: "Formulate the least squares problem: minimize ||overlapped_peaks - S·C||² where C is the unknown concentration distribution."
- [other] Solve for C using a least squares solver (e.g., numpy.linalg.lstsq or PyTorch optimization): "Solve for C using a least squares solver (e.g., numpy.linalg.lstsq or PyTorch optimization) to obtain the concentration matrix."
- [intro] GCMSFormer can predict the pure mass spectra of all components in overlapped peaks (mass spectral matrix S), and then use the least squares method to find the concentration distribution matrix C: "GCMSFormer can predict the pure mass spectra of all components in overlapped peaks (mass spectral matrix S), and then use the least squares method to find the concentration distribution matrix C"
- [other] Validate the solution by reconstructing the overlapped peaks as S·C and comparing with the input overlapped data: "Validate the solution by reconstructing the overlapped peaks as S·C and comparing with the input overlapped data."
- [readme] The automatic resolution of the overlapped peaks can be easily achieved.: "then use the least squares method to find the concentration distribution matrix C. The automatic resolution of the overlapped peaks can be easily achieved."

