Foundations 1: Fidelity Equations

Back to regularizeNd series home

Prerequisites: none beyond matrix–vector products. Part 1: Introduction and Intuition is optional background on what regularizeNd is for.

Learning goals

NoteSource and attribution

This page is adapted from “Introduction to Regularizing with 2D Data – Part 1 of 3” by Peter Goldstein, published January 29, 2013 on Math for Mere Mortals (under the username kintaar): https://mathformeremortals.wordpress.com/2013/01/29/introduction-to-regularizing-with-2d-data-part-1-of-3/ (Goldstein 2013).

This page is an independent implementation and modernization of that article’s ideas for regularizeNd, not a transcription. Its text, equations, examples, numbers, and figures were written and computed for this site.

The problem: from scattered samples to a table

You have measurements \((x_i, y_i)\), \(i = 1,\dots,m\). They are noisy, and the \(x_i\) fall wherever the experiment put them. What you want is a lookup table: one value \(z_j\) at each node \(g_j\) of an output grid that you choose. Between nodes, the table is read by interpolation.

So the unknowns are the table entries \(z = (z_1,\dots,z_N)^T\). There is no formula with a handful of coefficients to fit. Every node value is a free parameter, and the job is to choose them so the table agrees with the data and behaves sensibly between samples.

This series follows one small example throughout. It has one input variable and one output, which regularizeNd calls 1D input:

Sample \(i\) 1 2 3 4 5
\(x_i\) 0.35 1.00 1.80 2.70 3.65
\(y_i\) 1.0 1.4 0.4 1.2 0.2

The output grid has \(N = 6\) nodes, \(g = [0,\ 1,\ 2,\ 2.5,\ 3,\ 4]\). It is deliberately nonuniform: the spacing is 1 except between 2 and 3, where it is 0.5. The samples are irregular too, and sample 2 lands exactly on node 2.

addpath('fixtures');
r = regularizeNdFoundationsFixtures(Verbose=false);

x = r.oneD.x;      % sample locations
y = r.oneD.y;      % sample values
g = r.oneD.grid;   % output-grid nodes
m = numel(x);
N = numel(g);

Linear interpolation as a row of weights

Suppose sample \(i\) lies in the grid interval \([g_j, g_{j+1}]\). Its position inside that interval is the local fraction

\[ t_i = \frac{x_i - g_j}{g_{j+1} - g_j}, \qquad 0 \le t_i \le 1 . \]

Linear interpolation reads the table at \(x_i\) as a weighted average of the two neighboring node values:

\[ \hat z(x_i) = (1 - t_i)\, z_j + t_i\, z_{j+1}. \]

The weights \(1 - t_i\) and \(t_i\) are nonnegative and sum to one. That matters: a table that is constant, \(z_j = c\) for all \(j\), is read as exactly \(c\) everywhere. The same weights also reproduce \(x_i\) from the node coordinates, \((1 - t_i) g_j + t_i g_{j+1} = x_i\), so straight lines are reproduced exactly too.

Asking the table to agree with sample \(i\) gives one linear equation in the unknowns:

\[ (1 - t_i)\, z_j + t_i\, z_{j+1} \approx y_i . \]

This is a fidelity equation. The “\(\approx\)” is deliberate. The measurements are noisy, so later pages will not insist on equality.

Sample 3 by hand. \(x_3 = 1.8\) lies between \(g_2 = 1\) and \(g_3 = 2\). The fraction is \(t_3 = (1.8 - 1)/(2 - 1) = 0.8\), so the weights are \(0.2\) on node 2 and \(0.8\) on node 3:

\[ 0.2\, z_2 + 0.8\, z_3 \approx 0.4 . \]

Sample 4 and the uneven spacing. \(x_4 = 2.7\) lies in the short interval \([2.5, 3]\), so \(t_4 = (2.7 - 2.5)/0.5 = 0.4\). The weights are \(0.6\) on node 4 and \(0.4\) on node 5. The interval width is in the denominator, which is how a nonuniform grid enters the weights.

Sample 2, on a node. \(x_2 = 1.00\) coincides with \(g_2\), so \(t_2 = 0\). The weight on node 3 is zero, and the row has a single weight of 1 on node 2:

\[ z_2 \approx 1.4 . \]

The code below builds every row the same way. discretize finds the interval \(j\) for each sample, and sparse stores only the nonzero weights, so the zero weight in the exact-node row is not stored.

j = discretize(x, g);                      % interval index: g(j) <= x < g(j+1)
t = (x - g(j)) ./ (g(j+1) - g(j));         % local fraction
Af = sparse([1:m, 1:m]', [j; j+1], [1 - t; t], m, N);

assert(isequal(Af, r.oneD.system.fidelity))        % matches the fixture
assert(max(abs(sum(Af, 2) - 1)) < 1e-12)           % rows sum to one
assert(max(abs(Af*g - x)) < 1e-12)                 % rows reproduce x
assert(nnz(Af(2, :)) == 1)                         % exact-node row
disp(full(Af))
    0.6500    0.3500         0         0         0         0
         0    1.0000         0         0         0         0
         0    0.2000    0.8000         0         0         0
         0         0         0    0.6000    0.4000         0
         0         0         0         0    0.3500    0.6500

Figure 1 shows which nodes each sample touches. Thicker connectors carry larger weights.

close all
figure
hold on
base = -0.3;
for i = 1:m
  [~, cols, w] = find(Af(i, :));
  for k = 1:numel(cols)
    plot([x(i) g(cols(k))], [y(i) base], '-', 'Color', [0.3 0.45 0.8], ...
      'LineWidth', 0.5 + 4*w(k), 'HandleVisibility', 'off');
    text(x(i) + 0.55*(g(cols(k)) - x(i)), y(i) + 0.55*(base - y(i)), ...
      sprintf(' %.2g', w(k)), 'FontSize', 9);
  end
end
plot(g, base*ones(N, 1), '^', 'MarkerSize', 8, 'MarkerFaceColor', [0.5 0.5 0.5], ...
  'MarkerEdgeColor', [0.5 0.5 0.5], 'DisplayName', 'output-grid nodes g_j');
plot(x, y, 'o', 'MarkerSize', 8, 'MarkerFaceColor', [0.85 0.33 0.1], ...
  'MarkerEdgeColor', 'k', 'DisplayName', 'samples (x_i, y_i)');
text(g, base*ones(N, 1) - 0.12, compose('g_%d', (1:N)'), 'HorizontalAlignment', 'center');
grid on
xlim([-0.2 4.2]); ylim([-0.6 1.6]);
xlabel('x'); ylabel('y')
title('Linear-interpolation weights from samples to grid nodes')
legend('Location', 'northeast')
Figure 1: Each sample (circle) is tied to the grid nodes (triangles) that bracket it. The numbers are the linear-interpolation weights. Sample 2 sits on node 2 and uses a single weight of 1.

The fidelity matrix

Stacking one row per sample gives the \(5 \times 6\) fidelity system \(A_f z \approx y\). Columns are the output-grid nodes in order:

\[ \underbrace{\begin{bmatrix} 0.65 & 0.35 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0.2 & 0.8 & 0 & 0 & 0\\ 0 & 0 & 0 & 0.6 & 0.4 & 0\\ 0 & 0 & 0 & 0 & 0.35 & 0.65 \end{bmatrix}}_{A_f} \begin{bmatrix} z_1\\ z_2\\ z_3\\ z_4\\ z_5\\ z_6 \end{bmatrix} \approx \begin{bmatrix} 1.0\\ 1.4\\ 0.4\\ 1.2\\ 0.2 \end{bmatrix}. \]

Each row has at most two nonzeros, and they sit in adjacent columns. Figure 2 shows the same matrix as a heat map.

close all
figure
A = full(Af);
imagesc(A)
colormap(flipud(gray) * 0.75 + 0.25)
clim([0 1]); cb = colorbar; cb.Label.String = 'weight';
for i = 1:m
  for k = find(A(i, :))
    text(k, i, sprintf('%.2g', A(i, k)), 'HorizontalAlignment', 'center', ...
      'FontWeight', 'bold', 'Color', 'k');
  end
end
axis ij; axis tight
xticks(1:N); xticklabels(compose('z_%d (g=%g)', (1:N)', g));
yticks(1:m); yticklabels(compose('sample %d (x=%.2f)', (1:m)', x));
xlabel('output-grid node (column)'); ylabel('sample (row)')
title('Fidelity matrix A_f')
Figure 2: The fidelity matrix for the five-sample example. Rows are samples, columns are output-grid nodes, and blank cells are structural zeros.

Why fidelity alone is not enough

There are 5 equations and 6 unknowns. The rank of \(A_f\), the largest number of its rows that are linearly independent, is 5, so its null space has dimension 1: there is a whole line of tables that satisfy every fidelity equation exactly.

rankAf = rank(full(Af));
assert(rankAf == 5 && N == 6)
assert(all(any(Af, 1)))                     % every node is touched here

The structure shows where the freedom lives. Rows 1–3 involve only nodes 1–3: three equations for three unknowns, so those nodes are pinned at \(z_2 = 1.4\), then \(z_3 = 0.15\) and \(z_1 \approx 0.785\). Rows 4–5 involve only nodes 4–6: two equations for three unknowns. The vector

\[ n = [0,\ 0,\ 0,\ -26,\ 39,\ -21]^T \]

satisfies \(A_f n = 0\) (row 4: \(0.6 \cdot (-26) + 0.4 \cdot 39 = 0\); row 5: \(0.35 \cdot 39 + 0.65 \cdot (-21) = 0\)). If \(z_0\) is one exact fit (\(A_f z_0 = y\); the code below uses the minimum-norm one), then \(z_0 + c\,n\) is also an exact fit for every number \(c\). Every node is touched by some sample here, and the table is still not determined.

nv = [0; 0; 0; -26; 39; -21];
assert(norm(Af*nv) < 1e-12)
z0 = lsqminnorm(full(Af), y);              % one exact fit
assert(norm(Af*z0 - y) < 1e-12)
assert(max(abs(z0(1:3) - [51/65; 1.4; 0.15])) < 1e-12)

close all
figure
hold on
for c = [-1 0 1]
  zc = z0 + c*nv/39;
  assert(norm(Af*zc - y) < 1e-12)
  plot(g, zc, '-s', 'LineWidth', 1.2, 'DisplayName', sprintf('c = %d', c));
end
plot(x, y, 'ko', 'MarkerSize', 8, 'MarkerFaceColor', 'y', 'DisplayName', 'samples');
xline(g, ':', 'HandleVisibility', 'off');
grid on
xlabel('x'); ylabel('table value z')
title('Fidelity alone: many tables fit the samples exactly')
legend('Location', 'southwest')
Figure 3: Three tables \(z_0 + c n / 39\) that satisfy all five fidelity equations exactly. They agree on nodes 1–3 and differ on nodes 4–6. Each line is the table read by linear interpolation, and each passes through every sample.

The exact fits also copy the noise. Nodes 1–3 are forced to follow the samples, including the drop from 1.4 to 0.4 between samples 2 and 3.

Refining the grid makes this worse. Split each interval into 4 equal parts, giving 21 nodes. There are still only 5 samples, so the rank of \(A_f\) is at most 5, and it is exactly 5 here, leaving 16 free directions. Each sample touches at most two of the new nodes, and 12 of the 21 nodes receive no weight from any sample. Those nodes appear in no fidelity equation at all, so nothing in \(A_f z \approx y\) says anything about their values.

gFine = interp1(1:N, g, (1:0.25:N)');      % split each interval into 4
NFine = numel(gFine);
jF = discretize(x, gFine);
tF = (x - gFine(jF)) ./ (gFine(jF+1) - gFine(jF));
AfFine = sparse([1:m, 1:m]', [jF; jF+1], [1 - tF; tF], m, NFine);
assert(max(abs(full(AfFine) - interp1(gFine, eye(NFine), x)), [], 'all') < 1e-12)

untouched = ~any(AfFine, 1)';
nUntouched = nnz(untouched);
assert(NFine == 21 && nUntouched == 12 && rank(full(AfFine)) == 5)

colWeight = full(sum(AfFine, 1))';
close all
figure
stem(gFine(~untouched), colWeight(~untouched), 'filled', 'DisplayName', 'touched node');
hold on
plot(gFine(untouched), zeros(nUntouched, 1), 'ro', 'MarkerSize', 7, 'LineWidth', 1.2, ...
  'DisplayName', 'untouched node');
grid on
ylim([-0.1 1.2])
xlabel('node location g_j'); ylabel('total weight from all samples')
title(sprintf('%d-node grid: %d nodes receive no weight', NFine, nUntouched))
legend('Location', 'northeast')
Figure 4: Total weight each node of a 21-node grid receives from the five samples. Red circles mark nodes that no sample touches.
ImportantKey idea

Fidelity equations constrain only the nodes that samples touch, and only as many independent directions as there are samples. Whenever the grid has more nodes than samples, or some node is touched by no sample, many tables fit the samples equally well. An extra preference is needed to choose among them.

That preference is smoothness, introduced in Foundation 2. Adding smoothness rows changes the goal as well. The table no longer has to reproduce every sample. regularizeNd solves a weighted least-squares compromise between agreeing with the samples and staying smooth. For this fixture at the default smoothness 0.01, the stacked system has full column rank 6, and the fitted value at sample 2 is 1.2046 rather than the measured 1.4.

Connection to regularizeNd

regularizeNdMatrices returns the matrices regularizeNd assembles before it solves. Its first output is the fidelity matrix built on this page:

x = [0.35; 1.00; 1.80; 2.70; 3.65];
xGrid = {[0; 1; 2; 2.5; 3; 4]};
smoothness = 0.01;
[Afidelity, Lreg] = regularizeNdMatrices(x, xGrid, smoothness, 'linear');
% Afidelity: 5-by-6 sparse matrix, one row per sample, rows sum to one
% Lreg: the scaled smoothness blocks (Foundation 2)

The regularizeNd package is not run on this page. The fixture script fixtures/regularizeNdFoundationsFixtures.m reproduces this call. When run with the package on the path, its matrices and solutions, including this fidelity matrix, agreed with the package’s to within \(8.88 \times 10^{-16}\) (recorded in fixtures/fixture-report.md).

The same three properties hold for every interpolation method regularizeNd offers. There is one row per sample, the weights in each row sum to one (cubic weights can be negative, but they still sum to one), and the matrix is stored sparse. What changes is how many nodes each row touches. A sample that sits on a node, like sample 2, stores fewer, because sparse drops the zero weights:

Interpolation Nodes per row (at most), 1D input Nodes per row (at most), \(n\)-D input
'nearest' 1 1
'linear' 2 \(2^n\)
'cubic' 4 \(4^n\)

The Fidelity block section of the deep dive covers the \(n\)-D case and shows example sparsity patterns. Foundation 6 builds the 2D-input (bilinear) rows by hand.

How this differs from the original

  • Terminology. The original calls data with one \(x\) and one \(y\) “2D data”. This site follows regularizeNd and calls it 1D input: one input variable and one output. The original speaks of an output dataset; this site calls it the output grid or lookup table.
  • Interpolation. The original builds its fidelity rows with linear interpolation only, noting that it is the simplest of several possible choices. regularizeNd offers nearest, linear, and cubic fidelity rows.
  • Example. This page uses its own five-sample fixture and six-node grid. They are shared by Foundations 1–5, and every number on the page is computed by the code above.
  • Scope of the fidelity analysis. The original introduces the fidelity rows and leaves smoothness to its next part. It does not compute the rank of the fidelity block or count untouched nodes. This page adds both to show why a second family of equations is needed.

The next page adds that second family. It derives second-difference smoothness rows on the same nonuniform grid, stacks them under \(A_f\), and solves the least-squares problem.

Next

Back to regularizeNd series home

Next: Foundations 2: Smoothness and Least Squares

References

Goldstein, Peter. 2013. “Introduction to Regularizing with 2D Data – Part 1 of 3.” Math for Mere Mortals. https://mathformeremortals.wordpress.com/2013/01/29/introduction-to-regularizing-with-2d-data-part-1-of-3/.