Linear least-squares unmixing with analytical uncertainties - #20
Open
AlexLipp wants to merge 3 commits into
Open
Linear least-squares unmixing with analytical uncertainties#20AlexLipp wants to merge 3 commits into
AlexLipp wants to merge 3 commits into
Conversation
Implements the linear formulation of Appendix A of the preprint as a second,
independent solver. Where SampleNetworkUnmixer penalises relative (log-ratio)
misfit, LinearSampleNetworkUnmixer penalises absolute misfit, which makes the
forward model an exactly invertible matrix and admits closed-form uncertainty.
The mixing matrix M is square (one sub-basin per sample site), row-stochastic,
and lower triangular in topological order with diagonal q_i/Q_i > 0, so it is
always invertible and its inverse has the sparse closed form
c_i = (Q_i d_i - sum_p alpha_p Q_p d_p) / q_i
Key properties this buys:
- Generalised least squares weighted by the data covariance, solved via a
whitening transform, with Tikhonov regularisation lambda||Pc||^2 penalising
the variance of the model about its own mean.
- A guaranteed unique solution for every lambda >= 0, since M^T W M is positive
definite even though the penalty operator P is singular.
- Closed-form error propagation C_c = R C_d R^T and C_dhat = M C_c M^T, using
exactly the estimator that produced the point estimate.
- The Bayesian posterior covariance (M^T C_d^-1 M + lambda P)^-1, which unlike
the propagated covariance does not understate the error at large lambda.
- Resolution matrix and effective degrees of freedom.
- Diagnostics for when the method is inapplicable: per-site noise amplification
Q_i/q_i, condition number, and the unconstrained estimate before clamping.
Also fixes a latent bug in nx_get_downstream_data, which used a truthiness test
on the downstream node name. A node named integer 0 was reported as having no
downstream neighbour, silently disconnecting the outlet of every integer-labelled
network. The existing round-trip tests passed only because forward_model and
SampleNetworkUnmixer both used the same broken accessor, cancelling the error.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A standalone derivation of the linear formulation, written to be checked line by line. Takes Appendix A of Barnes and Lipp (2024) as its starting point and uses its notation, then works out the consequences and extends it. Contents: - The forward model as a matrix, generalised to include first-order decay. - Proof that M is row-substochastic (stochastic when conservative) and lower triangular in topological order with diagonal q_i/Q_i, hence invertible with |det M| = prod(q_i/Q_i). - Closed-form inverse as a local differencing stencil on the network, derived from tracer conservation, showing M^-1 is sparse despite M being dense. - The amplification factor kappa_i = Q_i/q_i, and the exact condition under which the non-negativity constraint binds. - Proof that the unregularized problem is degenerate: any positive-definite weighting gives the same zero-residual answer, so weighting is inert until a competing term is added to the objective. - Generalised least squares via whitening, and why a proportional error model makes the weighted linear misfit a relative misfit, as in the parent study. - Tikhonov regularization of the model variance: existence and uniqueness for every lambda, the linear estimator, and both limits of the regularization path. - Error propagation: source and prediction covariances, the Cramer-Rao result at lambda = 0, bias and resolution, and the exact identity relating the propagated covariance to the Bayesian posterior covariance. Builds with latexmk + biber. Uses preprint.sty from the parent manuscript for visual consistency. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a second, independent solver implementing the linear formulation of Appendix A of the preprint, together with a standalone mathematical note deriving it.
Where
SampleNetworkUnmixerpenalises relative (log-ratio) misfit,LinearSampleNetworkUnmixerpenalises absolute misfit. That makes the forward model an exactly invertible matrix and, crucially, admits closed-form uncertainties — no Monte Carlo required.Why this works
The mixing matrix
Mis square (one sub-basin per sample site), row-stochastic, and lower triangular in topological order with diagonalq_i/Q_i > 0. It is therefore always invertible, and its inverse is a sparse local differencing stencil:Mis dense and global;M^-1is sparse and local. All the long-range mixing cancels.What the solver provides
lambda||Pc||^2penalising the variance of the model about its own mean.lambda >= 0, sinceM^T C_d^-1 Mis positive definite even though the penalty operatorPis singular.C_c = R C_d R^TandC_dhat = M C_c M^T, using exactly the estimator that produced the point estimate. When no constraint is active the returned estimate is the analytical one, not the solver's approximation, so the two provably match.(M^T C_d^-1 M + lambda P)^-1. The propagated covariance shrinks withlambdaand understates the error; the posterior does not. They are related by an exact identity and coincide atlambda = 0, where both attain the Cramer-Rao bound.Q_i/q_i, condition number, and the unconstrained estimate before clamping.get_misfit/get_roughnessmatch the existing semantics, soplot_sweep_of_regularizer_strengthworks on the new solver unchanged.Note on an unrelated bug fix
nx_get_downstream_dataused a truthiness test on the downstream node name, so a node named integer0was reported as having no downstream neighbour — silently disconnecting the outlet of every integer-labelled network.nx.balanced_tree's root is always0, so this affected all synthetic tests. They passed only becauseforward_modelandSampleNetworkUnmixerboth used the same broken accessor, cancelling the error. Real CSV-named data is unaffected. Fixed here because the new matrix builder uses the correct accessor and so disagreed withforward_model.How the two solvers compare
tests/linear_vs_nonlinear_benchmark.pysweeps source range on a 100-site network (areas +/-10%, 20% relative error, each method at its oracle-best lambda). RMS factor-of error:The two are indistinguishable up to ~1 order of magnitude of source variation, and the gap widens smoothly to ~1.5x by 6 orders — no cliff. Weighting by the data covariance is what keeps the linear solver competitive at range: it permits ~10^4 times harder damping without sacrificing low-concentration sites. Note that unregularized inversion is unusable for both solvers.
Mathematical note
docs/linear_unmixing_note.pdf(9 pages, built withlatexmk+biber) derives all of the above from first principles so it can be checked line by line, using the preprint's notation and style. It covers the structure and invertibility ofM, the closed-form inverse, why the unregularized problem is degenerate (any positive-definite weighting gives the same zero-residual answer), GLS by whitening, existence and uniqueness under regularisation, and the full error propagation including the exact identity relating the propagated and posterior covariances.Testing
31 tests pass. The load-bearing ones verify the mixing matrix reproduces
forward_modelto 1e-12, exact round-trip recovery at 1e-8, that the closed form matches an explicit dense inverse, that the whitening reproduces the textbook GLS normal equations, and that the analytical covariance matches a 20,000-draw Monte Carlo. Existing tests are unaffected.🤖 Generated with Claude Code