19_diagonal_of_the_inverse.cpp¶
Estimate the diagonal of the inverse of a large sparse matrix.
For a nonsingular M-matrix, inverse_diagonal(F, A) estimates every entry of \(\operatorname{diag}(A^{-1})\) at once from a block of Gaussian probes. It first scales \(A\) by a diagonal similarity that makes its symmetric part positive definite, which turns each entry into a squared row norm. The estimate is unbiased and positive, and its relative error shrinks like \(\sqrt{2/\text{probes}}\). F can be any factorization of \(A\). Here it is the sparse one, which also gives the exact diagonal below with one solve per entry.
#include <cmath>
#include <io/json.hpp>
#include <numerics.hpp>
using namespace num;
namespace {
// A ring of n states with unequal clockwise and counterclockwise rates, plus decay: a
// nonsymmetric, nonreversible M-matrix.
spmat ring(idx n) {
array<idx> rows, columns;
array<real> values;
for (idx i = 0; i < n; ++i) {
const real forward = 1.0 + (0.5 * std::sin(static_cast<real>(i)));
const real backward = 0.4;
rows.insert(rows.end(), {i, i, i});
columns.insert(columns.end(), {(i + 1) % n, (i + n - 1) % n, i});
values.insert(values.end(), {-forward, -backward, forward + backward + 0.05});
}
return spmat::from_triplets(n, n, rows, columns, values);
}
} // namespace
int main() {
constexpr idx n = 1000;
const spmat A = ring(n);
const auto F = lu(dense(A));
vec<real> exact(n), unit(n, 0.0), column;
for (idx i = 0; i < n; ++i) {
unit[i] = 1.0;
solve(F, unit, column); // column i of A^{-1}
exact[i] = column[i];
unit[i] = 0.0;
}
io::json results = io::json::array();
for (idx probes : {50, 200, 800}) {
const vec<real> estimate = inverse_diagonal(F, A, {}, {.probes = probes, .seed = 7});
real mean = 0.0, worst = 0.0;
for (idx i = 0; i < n; ++i) {
const real relative = std::abs(estimate[i] - exact[i]) / exact[i];
mean += relative / static_cast<real>(n);
worst = std::max(worst, relative);
}
results.push_back({{"probes", probes}, {"mean_relative_error", mean},
{"worst_relative_error", worst},
{"expected_scale", std::sqrt(2.0 / static_cast<real>(probes))}});
}
io::write_json({{"size", n}, {"results", results}}, "result.json");
}
Saved result:
{
"results": [
{
"expected_scale": 0.2,
"mean_relative_error": 0.15826622882068125,
"probes": 50,
"worst_relative_error": 0.7457680713550993
},
{
"expected_scale": 0.1,
"mean_relative_error": 0.07901451081745044,
"probes": 200,
"worst_relative_error": 0.3813838832827567
},
{
"expected_scale": 0.05,
"mean_relative_error": 0.0409277856912636,
"probes": 800,
"worst_relative_error": 0.17044712137826667
}
],
"size": 1000
}