Lattice Grid Buy a licence

demo D182

Correlation and weighted averages

Two-column figures from the API, over the filtered rows

grid.statistics.correlation(a, b)

Building…
Loading a live grid…

The configuration

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.27.0/lattice-grid.min.css">

<div id="grid" style="height: 460px"></div>
<div id="stats" style="margin-top: 12px; font: 13px system-ui; display: flex; flex-wrap: wrap; gap: 0.4rem 1.4rem"></div>

<script type="module">
  import { createGrid } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.27.0/modules/htmx.esm.js';

  const grid = createGrid(document.getElementById('grid'), {
    rowKey: 'id',
    toolPanel: { side: 'left', panels: ['filters', 'columns'], exportName: 'book' },
    columnDefaults: { filter: true },
    columns: [
      { field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
      { field: 'desk', title: 'Desk', 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' } },
    ],
    rows,  // e.g. [{ id: 1, symbol: 'ABC', desk: 'Rates', price: 101.2, bid: 101.1, volume: 4200 }, …]
  });

  // Two-column figures read straight from the grid over the rows that survive the
  // filters: how tightly two columns move together, a line fitted through them,
  // and an average that weights each price by its volume.
  const panel = document.getElementById('stats');
  const num = (v, d = 3) => (typeof v === 'number' ? v.toFixed(d) : String(v));
  const report = () => {
    const s = grid.statistics;
    const r = s.correlation('price', 'bid');
    const reg = s.regression('price', 'bid');
    const wa = s.weightedAverage('price', 'volume');
    panel.innerHTML =
      '<span><strong>corr(price, bid)</strong> ' + num(r) + '</span>' +
      '<span><strong>fit</strong> slope ' + num(reg && reg.slope, 4) + ', r&sup2; ' + num(reg && reg.r2) + '</span>' +
      '<span><strong>volume-weighted price</strong> ' + num(wa, 4) + '</span>';
  };
  grid.on('filter:changed', report);  // the figures move as a desk is filtered
  report();
</script>