demo D182
Correlation and weighted averages in ESM
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.
This is the ES module version. The grid is imported straight from a CDN as a module, the same configuration as the vanilla tab without a global script tag, so it drops into any bundler or a bare module script.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<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 type="module">
import { createGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.esm.min.js';
const grid = 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>