demo D236
Approximate figures, with the error bound shown
A constant-time estimate next to the figure it stands for, and the average over just the last few thousand readings
grid.statistics.maintenanceTier(fn) · windowed(col, fn, { kind, span })
The configuration
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.min.css">
<div id="app"></div>
<script type="module">
import React from 'https://esm.sh/react@18';
import { createRoot } from 'https://esm.sh/react-dom@18/client';
import { createGrid } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.esm.min.js';
import createLatticeGrid from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/modules/react.esm.min.js';
const LatticeGrid = createLatticeGrid({ React, createGrid });
const columns = [
{ field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
{ field: 'desk', title: 'Desk', filter: { type: 'set' } },
{ field: 'price', title: 'Price', type: 'number' },
{ field: 'bid', title: 'Bid', type: 'number' },
{ field: 'volume', title: 'Volume', type: 'number' },
{ field: 'notional', title: 'Notional', type: 'number',
format: { style: 'currency', currency: 'USD' }, total: 'sum' },
];
const rows = [/* trading book records */];
function App() {
const gridRef = React.useRef(null);
const [out, setOut] = React.useState('');
// Read the statistics API straight from the live grid through the ref. Every
// figure is over the filtered rows, so filter a desk and they all follow.
const report = () => {
const grid = gridRef.current && gridRef.current.grid;
if (!grid) return;
// maintenanceTier names how each reduction is computed, and the sketch and
// stated error bound that would stand in for it on a very large stream.
const tier = grid.statistics.maintenanceTier('distinct');
// windowed reads a figure over just the most recent readings, and states
// the window it covered: here the mean over the last 2,000, not all history.
const recent = grid.statistics.windowed('price', 'avg', { kind: 'count', span: 2000 });
setOut(JSON.stringify({ tier, recent }, null, 2));
};
React.useEffect(report, []);
return (
<>
<LatticeGrid
ref={gridRef}
rowKey="id"
toolPanel={{ side: 'left', panels: ['filters', 'columns'] }}
columns={columns}
rows={rows}
onFilterChanged={report}
style={{ height: '460px' }}
/>
<pre style={{ marginTop: '12px' }}>{out}</pre>
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>
Knowing what a figure actually costs, and what a shortcut would promise instead
A totals row over a large or fast-moving dataset can compute something like a distinct count or a p99 two ways: recompute it exactly on every read, or maintain an approximate version cheaply and say plainly what error bound it carries. Lattice Grid never swaps one in for the other silently; instead grid.statistics.maintenanceTier(fn) tells you, for any reduction, which of three honest positions it is actually in: exact, maintained (a running total that costs nothing extra per read, like a sum or an average), exact, rescan (correct, but a full pass over the data every time it is read, like a median or a percentile), or an approximate alternative with a named sketch and a stated bound, such as HyperLogLog for a distinct count or KLL for a quantile. grid.statistics.approximate is the same information as a lookup table, keyed by reduction name, each entry naming the sketch behind it and the bound it is verified to meet. The point of surfacing this rather than picking a tier for you is that “approximate” without a stated bound is just a wrong answer with better manners; a sketch that ships here is one this codebase runs against a real stream and asserts sits inside the bound it claims, so a figure with an error bound printed next to it is a claim you can check, not a disclaimer.
What is the difference between an exact and an approximate statistic in a data grid?
An exact reduction, whether maintained incrementally or recomputed on read, always matches a full pass over the data. An approximate one, backed by a sketch such as HyperLogLog or KLL, trades a small, stated error for constant memory and constant-time updates on a live or very large dataset. grid.statistics.maintenanceTier(fn) tells you which tier a given reduction is in and names the bound when an approximate form exists, so the choice is visible rather than buried in an implementation.
What is a stated error bound, and why does it matter?
A stated error bound is a specific, checkable claim: a HyperLogLog distinct count promises a standard error tied to its configured precision; a KLL quantile promises the true rank of the returned value is within a stated fraction of the requested rank. Reporting an approximate figure without that bound leaves a reader unable to tell a close estimate from a wrong one. Lattice Grid keeps the bound attached to the figure’s tier information specifically so an approximate result is never presented as an unqualified number.
Reading a figure over just the most recent readings, not the whole history
Over a long or fast-moving column, the figure over everything and the figure lately are different questions, and the average since the start hides a shift that only started an hour ago. grid.statistics.windowed(col, fn, { kind, span }) answers the second one: it reduces a column over a window of the recent readings rather than the whole set, and returns both the value and the window it covers so the answer states its own scope. A count window keeps the last N readings, a time window keeps the last so many minutes, and a session window covers the run so far, each stamping the result with the size of the window behind it. Like every statistic here it reads the filtered rows, so narrowing the grid narrows the window with it. The figure is exact over the readings in the window, so “the mean over the last few thousand” is a precise answer to a smaller question, not an estimate.
What is a windowed statistic in a data grid?
A windowed statistic reduces a column over a moving window of recent readings rather than the entire column. grid.statistics.windowed('price', 'avg', { kind: 'count', span: 2000 }) returns the mean over the last two thousand readings and states the window it covered, so a recent shift shows through instead of being averaged away across all of history. The window can be a count of readings, a span of time, or the whole session, and it follows the grid’s filters like any other figure.