demo D182
Correlation and weighted averages: JavaScript Data Grid Demo
Two-column figures from the API, over the filtered rows
grid.statistics.correlation(a, b)
This grid computes two-column figures, correlation and weighted averages, from the API over the filtered rows. It answers how two measures relate across the current selection, updating as the filter changes.
Loading a live grid…
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.103.2/lattice-grid.min.css">
<script src="https://cdn.latticegrid.dev/lattice-grid@1.103.2/lattice-grid.min.js"></script>
<div id="grid" style="height: 420px"></div>
<div id="panel" style="margin-top: 12px; font: 13px system-ui; display: flex; flex-wrap: wrap; gap: .4rem 1.4rem"></div>
<script>
const grid = LatticeGrid.createGrid(document.getElementById('grid'), {
rowKey: 'id',
columnDefaults: { filter: true },
toolPanel: { side: 'left', panels: ['filters', 'columns'] },
columns: [
{ field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
{ field: 'desk', title: 'Desk', filter: { type: 'set' } },
{ field: 'region', title: 'Region', filter: { type: 'set' } },
{ field: 'price', title: 'Price', type: 'number', format: { decimals: 4 } },
{ field: 'bid', title: 'Bid', type: 'number', format: { decimals: 4 } },
{ field: 'volume', title: 'Volume', type: 'number', format: { notation: 'compact' } },
{ field: 'notional', title: 'Notional', type: 'number',
format: { style: 'currency', currency: 'USD', decimals: 2 }, total: 'sum' },
],
rows, // trading book: symbol, desk, region, price, bid, volume, notional
});
const panel = document.getElementById('panel');
const render = () => {
const s = grid.statistics;
const r = s.correlation('price', 'bid'); // Pearson, one number
const reg = s.regression('price', 'bid'); // slope, intercept, r2
const wa = s.weightedAverage('price', 'volume'); // volume-weighted price
panel.innerHTML =
'<span><strong>corr(price, bid)</strong> ' + r.toFixed(3) + '</span>' +
'<span><strong>regression</strong> slope ' + reg.slope.toFixed(4) + ', r2 ' + reg.r2.toFixed(3) + '</span>' +
'<span><strong>vol-weighted price</strong> ' + wa.toFixed(4) + '</span>';
};
// Recompute whenever the grid is filtered, so the figures track the view.
grid.on('filter:changed', render);
render();
</script>