Lattice Grid Buy a licence

demo D183

Statistical values in a column: JavaScript Data Grid Demo

A column's median, deviation and percentile, read into computed columns

value.compute

This grid reads a column's median, deviation and percentile into computed columns. It lets a per-row calculation reference a whole-column statistic, so a value can be compared against its column's own figures inline.

Building…
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: 460px"></div>

<script>
  // Read the column's shape once over the data, then feed those figures into
  // computed columns. "vs median" and a z-score become columns like any other:
  // sortable, filterable, exportable.
  const prices = rows.map((r) => r.price).sort((a, b) => a - b);
  const n = prices.length;
  const median = n % 2 ? prices[(n - 1) / 2] : (prices[n / 2 - 1] + prices[n / 2]) / 2;
  const mean = prices.reduce((s, x) => s + x, 0) / n;
  const sd = Math.sqrt(prices.reduce((s, x) => s + (x - mean) ** 2, 0) / n) || 1;
  const p90 = prices[Math.floor(0.9 * (n - 1))];

  const grid = LatticeGrid.createGrid(document.getElementById('grid'), {
    rowKey: 'id',
    columns: [
      { field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
      { field: 'price', title: 'Price', type: 'number', format: { decimals: 4 } },
      // pure lets the grid cache the result; a compute reads only its deps.
      { id: 'vsMedian', title: 'vs median', type: 'number', format: { decimals: 4, signed: true },
        value: { deps: ['price'], pure: true, compute: (d) => d.price - median } },
      { id: 'z', title: 'Z-score', type: 'number', format: { decimals: 2, signed: true },
        value: { deps: ['price'], pure: true, compute: (d) => (d.price - mean) / sd } },
      { id: 'p90', title: 'over p90', type: 'boolean',
        value: { deps: ['price'], pure: true, compute: (d) => d.price > p90 } },
    ],
    rows,  // trading book: symbol, price
  });
</script>