<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>KoliStat blog</title>
  <id>https://kolistat.com/blog/</id>
  <link href="https://kolistat.com/blog/" />
  <link rel="self" href="https://kolistat.com/feed.xml" />
  <updated>2026-07-06T00:00:00.000Z</updated>
  <subtitle>Notes from the founder on building modern tooling for clinical and tabular data — SAS, DuckDB, WebAssembly, and the craft of byte-parity engineering.</subtitle>
  <author><name>Massimo Meneghello</name></author>
  <entry>
    <title>the-stats-duck v0.7 — a regression in every GROUP BY</title>
    <id>https://kolistat.com/blog/the-stats-duck-v0-7/</id>
    <link href="https://kolistat.com/blog/the-stats-duck-v0-7/" />
    <updated>2026-07-06T00:00:00.000Z</updated>
    <published>2026-07-06T00:00:00.000Z</published>
    <author><name>Massimo Meneghello</name></author>
    <category term="the-stats-duck" />
    <category term="DuckDB" />
    <category term="statistics" />
    <category term="regression" />
    <summary>v0.7 of our open-source DuckDB statistics extension adds lm_fit: an OLS aggregate that fits one regression per GROUP BY group, with heteroskedasticity- and cluster-robust standard errors (HC0–HC3, CR0/CR1). Plus a portable bootstrap and a new linear-algebra kernel underneath.</summary>
    <content type="html">&lt;p&gt;&lt;code&gt;the-stats-duck&lt;/code&gt; — our open-source DuckDB extension for statistics — just shipped &lt;strong&gt;&lt;a href=&quot;https://github.com/KoliStat/the-stats-duck/releases/tag/v0.7.0&quot;&gt;v0.7.0&lt;/a&gt;&lt;/strong&gt;, and it&apos;s now a one-line install from the &lt;a href=&quot;https://duckdb.org/community_extensions/extensions/stats_duck&quot;&gt;DuckDB community extensions&lt;/a&gt; (see below).&lt;/p&gt;
&lt;p&gt;v0.6 taught DuckDB to fit a regression: &lt;a href=&quot;/blog/the-stats-duck-v0-6-0/&quot;&gt;&lt;code&gt;lm&lt;/code&gt;&lt;/a&gt; took an R-style formula and gave you back a coefficient table for the whole query. Good for one model. But clinical trial work is rarely just one model — it&apos;s usually &lt;em&gt;one model per site&lt;/em&gt;, &lt;em&gt;per arm&lt;/em&gt;, &lt;em&gt;per cohort&lt;/em&gt; — and the standard errors you report had better survive a reviewer who knows the residuals aren&apos;t textbook-tidy.&lt;/p&gt;
&lt;p&gt;So v0.7&apos;s headline is &lt;strong&gt;&lt;code&gt;lm_fit&lt;/code&gt;&lt;/strong&gt;: ordinary least squares as a DuckDB &lt;strong&gt;aggregate&lt;/strong&gt;. One regression per &lt;code&gt;GROUP BY&lt;/code&gt; group, with robust and cluster-robust standard errors on request.&lt;/p&gt;
&lt;h2 id=&quot;one-regression-per-group&quot;&gt;&lt;a href=&quot;#one-regression-per-group&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to One regression per group&quot;&gt;One regression per group&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Because &lt;code&gt;lm_fit&lt;/code&gt; is an aggregate, &lt;code&gt;GROUP BY&lt;/code&gt; does the fanning-out for free. Here it fits &lt;code&gt;body_mass_g ~ flipper_length_mm + bill_length_mm&lt;/code&gt; &lt;strong&gt;separately for each penguin species&lt;/strong&gt;, in a single query — live, in your browser:&lt;/p&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;lm_fit per-species regression running live in Bedevere&quot;
  style=&quot;width: 100%; height: 640px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#dataset=https://kolistat.com/data/penguins.parquet&amp;autorun=1&amp;query=WITH%20fits%20AS%20(%0A%20%20SELECT%20species%2C%20lm_fit(body_mass_g%2C%20%5Bflipper_length_mm%2C%20bill_length_mm%5D)%20AS%20m%0A%20%20FROM%20penguins%0A%20%20WHERE%20body_mass_g%20IS%20NOT%20NULL%0A%20%20%20%20AND%20flipper_length_mm%20IS%20NOT%20NULL%0A%20%20%20%20AND%20bill_length_mm%20IS%20NOT%20NULL%0A%20%20GROUP%20BY%20species%0A)%2C%0Acoefs%20AS%20(SELECT%20species%2C%20unnest(m.coefficients)%20AS%20c%20FROM%20fits)%0ASELECT%20species%2C%20(c).term%20AS%20term%2C%0A%20%20%20%20%20%20%20round((c).estimate%2C%202)%20AS%20estimate%2C%0A%20%20%20%20%20%20%20round((c).std_error%2C%202)%20AS%20std_error%2C%0A%20%20%20%20%20%20%20round((c).p_value%2C%204)%20AS%20p_value%0AFROM%20coefs%0AORDER%20BY%20species%2C%20term%3B&quot;
&gt;&lt;/iframe&gt;
&lt;p&gt;Three species, three fitted models, one pass over the data. Change &lt;code&gt;GROUP BY species&lt;/code&gt; to &lt;code&gt;GROUP BY island&lt;/code&gt; and you get a model per island instead — the aggregate doesn&apos;t care.&lt;/p&gt;
&lt;h2 id=&quot;the-shape-of-it&quot;&gt;&lt;a href=&quot;#the-shape-of-it&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to The shape of it&quot;&gt;The shape of it&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;lm_fit&lt;/code&gt; takes the response and the predictor row, and returns a &lt;code&gt;STRUCT&lt;/code&gt;:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;lm_fit(y, x [, vcov [, cluster [, add_intercept]]])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;y&lt;/code&gt;&lt;/strong&gt; — the response value (one per row).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;x&lt;/code&gt;&lt;/strong&gt; — the predictor row as a &lt;code&gt;LIST(DOUBLE)&lt;/code&gt;, e.g. &lt;code&gt;[flipper_length_mm, bill_length_mm]&lt;/code&gt;. An intercept is prepended automatically unless &lt;code&gt;add_intercept&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The result &lt;code&gt;STRUCT&lt;/code&gt; carries a &lt;strong&gt;&lt;code&gt;coefficients&lt;/code&gt;&lt;/strong&gt; list — one &lt;code&gt;{term, estimate, std_error, t_statistic, p_value}&lt;/code&gt; per predictor — alongside &lt;code&gt;n&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;, &lt;code&gt;df_residual&lt;/code&gt;, &lt;code&gt;r_squared&lt;/code&gt;, &lt;code&gt;adj_r_squared&lt;/code&gt;, &lt;code&gt;sigma&lt;/code&gt;, &lt;code&gt;f_statistic&lt;/code&gt;, &lt;code&gt;f_p_value&lt;/code&gt;, and the &lt;code&gt;vcov_type&lt;/code&gt; actually used.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Predictors come back &lt;strong&gt;by position&lt;/strong&gt; — &lt;code&gt;(Intercept)&lt;/code&gt;, then &lt;code&gt;x1&lt;/code&gt;, &lt;code&gt;x2&lt;/code&gt;, … in list order — so you &lt;code&gt;unnest&lt;/code&gt; the coefficients to get a tidy row per term. On a tiny dataset that looks like this:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (u).term, (u).estimate, (u).std_error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;  SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; unnest((lm_fit(y, [x1])).coefficients) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; u&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;      (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;), (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;), (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;), (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;      (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;), (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;), (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;13&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;), (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  ) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; t(y, x1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;--   term         estimate  std_error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;--   (Intercept)  0.0357    0.1404&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;--   x1           1.9976    0.0278&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;&lt;code&gt;lm_fit&lt;/code&gt; is the aggregate, positional sibling of v0.6&apos;s formula-driven &lt;code&gt;lm&lt;/code&gt;/&lt;code&gt;lm_summary&lt;/code&gt;. Reach for &lt;code&gt;lm&lt;/code&gt; when you want a formula over one table; reach for &lt;code&gt;lm_fit&lt;/code&gt; when you want a model &lt;strong&gt;per group&lt;/strong&gt;, robust standard errors, or both.&lt;/p&gt;
&lt;h2 id=&quot;standard-errors-that-survive-review&quot;&gt;&lt;a href=&quot;#standard-errors-that-survive-review&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Standard errors that survive review&quot;&gt;Standard errors that survive review&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The point estimates are just OLS. What changes with the &lt;code&gt;vcov&lt;/code&gt; argument is the &lt;strong&gt;standard errors&lt;/strong&gt; — and with them the &lt;em&gt;t&lt;/em&gt;-statistics and &lt;em&gt;p&lt;/em&gt;-values. That&apos;s usually the number a methodologist actually argues about.&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;-- Heteroskedasticity-consistent (HC3) — the conservative small-sample default&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (u).term, (u).std_error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; unnest((lm_fit(y, [x1, x2], &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;HC3&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)).coefficients) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; u &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; mytable);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;--   term         std_error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;--   (Intercept)  3.2802&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;--   x1           0.4749&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;--   x2           0.3839&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;The full menu:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;&apos;const&apos;&lt;/code&gt;&lt;/strong&gt; — classical OLS (the default).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;&apos;HC0&apos;&lt;/code&gt;–&lt;code&gt;&apos;HC3&apos;&lt;/code&gt;&lt;/strong&gt; — heteroskedasticity-consistent. &lt;code&gt;&apos;HC1&apos;&lt;/code&gt; is Stata&apos;s &lt;code&gt;, robust&lt;/code&gt;; &lt;code&gt;&apos;HC3&apos;&lt;/code&gt; is the conservative choice when &lt;em&gt;n&lt;/em&gt; is small.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;&apos;CR0&apos;&lt;/code&gt; / &lt;code&gt;&apos;CR1&apos;&lt;/code&gt;&lt;/strong&gt; — cluster-robust, for data with repeated measurement inside a subject, site, or center. Pass a per-row cluster key (&lt;code&gt;&apos;cluster&apos;&lt;/code&gt; is accepted as an alias for &lt;code&gt;&apos;CR1&apos;&lt;/code&gt;, the Stata / statsmodels default), and inference uses a &lt;em&gt;t(G − 1)&lt;/em&gt; reference with &lt;em&gt;G&lt;/em&gt; clusters:&lt;/li&gt;
&lt;/ul&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (u).term, (u).estimate, (u).std_error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;  SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; unnest(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    (lm_fit(response, [dose], &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;CR1&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, subject_id::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)).coefficients&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  ) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; u&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; observations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;Only the covariance estimator moves — the coefficients are identical across all of them. Every estimator is validated against statsmodels&apos; &lt;code&gt;cov_type=&apos;HC*&apos;&lt;/code&gt; and &lt;code&gt;&apos;cluster&apos;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;One nicety for the per-group case: a &lt;strong&gt;degenerate group&lt;/strong&gt; — fewer rows than predictors, a singular design, or fewer than two clusters — returns &lt;code&gt;NULL&lt;/code&gt; for that group instead of aborting the whole query. You get every model that &lt;em&gt;can&lt;/em&gt; be fit and NULLs where one can&apos;t, which is exactly what you want when you&apos;ve just fanned out across 200 sites.&lt;/p&gt;
&lt;h2 id=&quot;bootstrap-now-identical-on-every-machine&quot;&gt;&lt;a href=&quot;#bootstrap-now-identical-on-every-machine&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to bootstrap(), now identical on every machine&quot;&gt;bootstrap(), now identical on every machine&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A quieter but important fix: &lt;strong&gt;a seeded &lt;code&gt;bootstrap()&lt;/code&gt; now returns the exact same resamples on every operating system.&lt;/strong&gt; Before v0.7, the same seed could draw a &lt;em&gt;different&lt;/em&gt; set of resamples on Windows, macOS, and Linux — so a bootstrap result you shared with a colleague wouldn&apos;t reproduce number-for-number on their machine. The estimates were always statistically sound; they just weren&apos;t bit-for-bit identical across platforms.&lt;/p&gt;
&lt;p&gt;Now they are: give &lt;code&gt;bootstrap()&lt;/code&gt; a seed and anyone who reruns it, on any OS, gets the same resamples and the same interval. If you like this kind of bug, we &lt;a href=&quot;/blog/claude-code-rng-bug/&quot;&gt;wrote up two related RNG traps&lt;/a&gt; from the sampler work, and the full reasoning behind this one is in the &lt;a href=&quot;https://github.com/KoliStat/the-stats-duck/blob/main/notes/engineering/2026-06-bootstrap-rng-portability.md&quot;&gt;engineering note&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;under-the-hood-a-shared-numerical-core&quot;&gt;&lt;a href=&quot;#under-the-hood-a-shared-numerical-core&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Under the hood: a shared numerical core&quot;&gt;Under the hood: a shared numerical core&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;lm_fit&lt;/code&gt; is built on a new, well-tested numerical core — the matrix machinery that regression and robust standard errors actually run on (the least-squares solve, the matrix inverses, and the &quot;sandwich&quot; that turns residuals into robust standard errors). It&apos;s deliberately self-contained and reusable, so the same carefully-checked code can back more of the extension over time: &lt;code&gt;lm_fit&lt;/code&gt; is the first to use it, and the existing &lt;code&gt;lm&lt;/code&gt; / &lt;code&gt;lm_summary&lt;/code&gt; move onto it next. More of the statistics now stands on one foundation, validated the same way.&lt;/p&gt;
&lt;h2 id=&quot;go-play&quot;&gt;&lt;a href=&quot;#go-play&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Go play&quot;&gt;Go play&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;the-stats-duck&lt;/code&gt; is MIT-licensed and now lives in the &lt;strong&gt;&lt;a href=&quot;https://duckdb.org/community_extensions/extensions/stats_duck&quot;&gt;DuckDB community extensions&lt;/a&gt;&lt;/strong&gt; — so installing it is one line in any DuckDB session, no building from source:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;INSTALL stats_duck &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; community;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;LOAD&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; stats_duck;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;It&apos;s the statistics engine under &lt;a href=&quot;https://bedeverewise.app&quot;&gt;Bedevere&lt;/a&gt; and &lt;a href=&quot;https://editor.kolistat.com&quot;&gt;KoliLang&lt;/a&gt;; the code and full &lt;a href=&quot;https://github.com/KoliStat/the-stats-duck/releases/tag/v0.7.0&quot;&gt;v0.7.0 release notes&lt;/a&gt; live on &lt;a href=&quot;https://github.com/KoliStat/the-stats-duck&quot;&gt;GitHub&lt;/a&gt;. Drop it into any DuckDB session and your &lt;code&gt;GROUP BY&lt;/code&gt; grows a regression department.&lt;/p&gt;
&lt;p&gt;And if there&apos;s an estimator or test you need that isn&apos;t there yet, &lt;a href=&quot;/contact&quot;&gt;get in touch&lt;/a&gt; — commissioned functions ship in the open-source extension, so everyone gets them.&lt;/p&gt;
&lt;p&gt;Not dead. &lt;em&gt;What?&lt;/em&gt; Doing your regressions.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Story of an interesting bug, found (and solved) by Claude Code</title>
    <id>https://kolistat.com/blog/claude-code-rng-bug/</id>
    <link href="https://kolistat.com/blog/claude-code-rng-bug/" />
    <updated>2026-07-03T00:00:00.000Z</updated>
    <published>2026-07-03T00:00:00.000Z</published>
    <author><name>Massimo Meneghello</name></author>
    <category term="the-stats-duck" />
    <category term="DuckDB" />
    <category term="C++" />
    <category term="Claude Code" />
    <summary>Two RNG bugs, one debugging session: DuckDB broadcasting a single random draw across 2,048 rows, and a standard-library uniform distribution with a clipped tail. Both produced plausible-looking numbers, both are invisible in code review — and both fell to a Monte Carlo checklist, run by Claude Code.</summary>
    <content type="html">&lt;p&gt;Most of &lt;a href=&quot;https://github.com/KoliStat/the-stats-duck&quot;&gt;the-stats-duck&lt;/a&gt; — our open-source DuckDB extension for statistics — is written with &lt;a href=&quot;https://claude.com/claude-code&quot;&gt;Claude Code&lt;/a&gt; doing the typing. This post is the story of the most interesting bug of the &lt;a href=&quot;/blog/the-stats-duck-v0-6-0/&quot;&gt;v0.6.0&lt;/a&gt; release: the kind that returns &lt;em&gt;plausible-looking output&lt;/em&gt; while being statistically wrong, the kind that slips past every code review and could sit in production for months.&lt;/p&gt;
&lt;p&gt;It turned out to be two bugs, stacked. One was ours. One was the C++ standard library&apos;s. Claude Code wrote the first one, then found and fixed both — and the diagnostic trail is a nice little detective story, so we wrote it down.&lt;/p&gt;
&lt;h2 id=&quot;the-setup&quot;&gt;&lt;a href=&quot;#the-setup&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to The setup&quot;&gt;The setup&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;v0.6.0 added the random-sampling family: &lt;code&gt;rnorm&lt;/code&gt;, &lt;code&gt;rt&lt;/code&gt;, &lt;code&gt;rchisq&lt;/code&gt;, &lt;code&gt;rf&lt;/code&gt;, &lt;code&gt;rgamma&lt;/code&gt;, &lt;code&gt;rbeta&lt;/code&gt;, &lt;code&gt;rexp&lt;/code&gt;, &lt;code&gt;rweibull&lt;/code&gt;, &lt;code&gt;rlnorm&lt;/code&gt;, &lt;code&gt;rpois&lt;/code&gt;. The design is classic inverse-CDF sampling: draw a uniform &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;U&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;n&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;i&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;f&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;o&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;r&lt;/mi&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;m&lt;/mi&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;u \sim \mathrm{Uniform}(0,1)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.4306em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;∼&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathrm&quot;&gt;Uniform&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.1667em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, push it through the distribution&apos;s quantile function, done. The quantile functions already existed and were cross-checked against R — the &lt;code&gt;r*&lt;/code&gt; family just had to compose them with a uniform generator.&lt;/p&gt;
&lt;p&gt;The first draft of &lt;code&gt;rexp(rate)&lt;/code&gt; looked like every other distribution scalar in the codebase:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; RExp1Exec&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;DataChunk&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;ExpressionState&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;Vector&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;    UnaryExecutor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;ExecuteWithNulls&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        args.data[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;], result, args.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        [](&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt; rate&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;ValidityMask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt;mask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;idx_t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt; idx&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; SafeSample&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;                [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;](&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) { &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; stats_duck&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;ExponentialQuantile&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(u, rate); },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;                mask, idx);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;If you have ever written a DuckDB scalar function, this is exactly what you would write. &lt;code&gt;UnaryExecutor&lt;/code&gt; is the officially sanctioned helper that deals with all the vector plumbing for you. The function was marked &lt;code&gt;VOLATILE&lt;/code&gt;, the flag that tells DuckDB &quot;this is not a pure function, don&apos;t optimise calls away.&quot; Every box ticked.&lt;/p&gt;
&lt;p&gt;It compiled. It ran. It returned random-looking numbers.&lt;/p&gt;
&lt;h2 id=&quot;a-mean-that-wont-sit-still&quot;&gt;&lt;a href=&quot;#a-mean-that-wont-sit-still&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to A mean that won&amp;#x27;t sit still&quot;&gt;A mean that won&apos;t sit still&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Then Claude Code ran the smoke test it runs on every sampler — check the moments against theory:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; round&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;avg&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(rexp(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)), &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; mean,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;       round&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(rexp(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)), &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; mx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;500000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;An exponential with rate λ = 1 has mean 1.0, and the expected maximum of 500,000 independent draws is a textbook extreme-value fact: about &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;ln&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;500,000&lt;/mn&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;γ&lt;/mi&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;mn&gt;13.7&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\ln(500{,}000) + \gamma \approx 13.7&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mop&quot;&gt;ln&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mpunct&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;000&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.6776em;vertical-align:-0.1944em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord mathnormal&quot; style=&quot;margin-right:0.0556em;&quot;&gt;γ&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;≈&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.6444em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;13.7&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;We got a mean drifting between &lt;strong&gt;0.92 and 1.1&lt;/strong&gt; across runs, and a max of about &lt;strong&gt;8&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Not absurd numbers. Numbers a tired human would wave through — &lt;em&gt;random is random, right?&lt;/em&gt; But the mean of 500k draws should sit within a hair of 1.00, and a max of 8 is a right tail that has been amputated. Something was wrong, and one more observation made it interesting: &lt;code&gt;rnorm()&lt;/code&gt; — the zero-argument form — had perfect moments, while &lt;code&gt;rgamma(2, 3)&lt;/code&gt; was slightly off. The error correlated with &lt;strong&gt;which executor pattern the function used&lt;/strong&gt;, not with which distribution it sampled.&lt;/p&gt;
&lt;h2 id=&quot;500000-rows-245-random-numbers&quot;&gt;&lt;a href=&quot;#500000-rows-245-random-numbers&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to 500,000 rows, 245 random numbers&quot;&gt;500,000 rows, 245 random numbers&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The killer diagnostic was one query:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; total, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;DISTINCT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; v) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; distinct_cnt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; rexp(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; v &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;500000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;total: 500000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;distinct_cnt: 245&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;Half a million rows. &lt;strong&gt;245 unique values.&lt;/strong&gt; And the ratio gives the whole game away: 500,000 / 245 ≈ 2041 — which is DuckDB&apos;s vector size of &lt;strong&gt;2048&lt;/strong&gt;. Each chunk of 2048 rows was getting exactly one random draw, copied 2048 times.&lt;/p&gt;
&lt;p&gt;Here is why. DuckDB executes queries in chunks of up to 2048 rows, and &lt;code&gt;UnaryExecutor&lt;/code&gt; has a fast path for constant inputs: when the argument is the literal &lt;code&gt;1.0&lt;/code&gt;, the executor reads the constant once, invokes your lambda &lt;strong&gt;once&lt;/strong&gt;, and broadcasts the single result across the chunk. For a pure function, that is a perfectly sound optimisation. For a sampling function, it is catastrophic — and it also explains the clue above: &lt;code&gt;rnorm()&lt;/code&gt; takes no arguments, so it never went through &lt;code&gt;UnaryExecutor&lt;/code&gt; and had a hand-written per-row loop all along.&lt;/p&gt;
&lt;h3 id=&quot;but-we-marked-it-volatile&quot;&gt;&lt;a href=&quot;#but-we-marked-it-volatile&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to But we marked it VOLATILE!&quot;&gt;But we marked it VOLATILE!&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;We did. And this is the part worth writing on a wall:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;FunctionStability::VOLATILE&lt;/code&gt; instructs the &lt;strong&gt;planner&lt;/strong&gt; — don&apos;t constant-fold, don&apos;t common-subexpression-eliminate. It says nothing to the &lt;strong&gt;executor&lt;/strong&gt;, whose constant-input fast path is a runtime optimisation that fires whether the function is volatile or not.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So &lt;code&gt;VOLATILE&lt;/code&gt; correctly stops &lt;code&gt;rnorm() + rnorm()&lt;/code&gt; from being folded into &lt;code&gt;2 * rnorm()&lt;/code&gt; at plan time, and then the executor cheerfully replicates one draw across each vector at run time anyway. Two different layers, two different notions of &quot;don&apos;t cache this.&quot;&lt;/p&gt;
&lt;p&gt;The fix: drop the helper and write the per-row loop explicitly via &lt;code&gt;UnifiedVectorFormat&lt;/code&gt;, which still handles constant / flat / dictionary input encodings — but in a loop we control:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; Quantile&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; inline&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; RunPerRow1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;DataChunk&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;Vector&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;Quantile&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt;q&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;    idx_t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; count &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; args.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    UnifiedVectorFormat a0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    args.data[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;].&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;ToUnifiedFormat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(count, a0);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; v0 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; UnifiedVectorFormat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;GetData&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt;(a0);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    result.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;SetVectorType&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;VectorType&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::FLAT_VECTOR);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; out &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; FlatVector&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;GetData&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt;(result);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;mask &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; FlatVector&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;Validity&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(result);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;idx_t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; count; i&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;        auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; i0 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; a0.sel-&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;get_index&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(i);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;a0.validity.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;RowIsValid&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(i0)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            mask.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;SetInvalid&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(i);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;NextUniform&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt; // keep RNG advance independent of NULLs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            out[i] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 0.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;            continue&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        out[i] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; SafeSampleOne&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;            [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;](&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt; u&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) { &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; q&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(u, v0[i0]); }, mask, i);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;(Note the small courtesy in the NULL branch: the RNG advances anyway, so the stream of draws doesn&apos;t depend on where the NULLs happen to sit.)&lt;/p&gt;
&lt;p&gt;After the fix: 500,000 rows, 500,000 distinct values, mean ≈ 0.997. Bug one down.&lt;/p&gt;
&lt;h2 id=&quot;the-clipped-tail&quot;&gt;&lt;a href=&quot;#the-clipped-tail&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to The clipped tail&quot;&gt;The clipped tail&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Except the story wasn&apos;t over. The distribution&apos;s broad shape was now right, but the right tail of &lt;code&gt;rexp(1.0)&lt;/code&gt; still came up light. Claude Code compared tail counts against what Exp(1) theory demands for 500k draws:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Statistic&lt;/th&gt;
&lt;th&gt;Expected (Exp(1), n=500k)&lt;/th&gt;
&lt;th&gt;Observed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;count(v &gt; 5)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;≈ 3,370&lt;/td&gt;
&lt;td&gt;4,096&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;count(v &gt; 8)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;≈ 167&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;count(v &gt; 10)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;≈ 23&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implied max of &lt;code&gt;u&lt;/code&gt; (= 1 − exp(−max v))&lt;/td&gt;
&lt;td&gt;≈ 1 − 1.5×10⁻⁶&lt;/td&gt;
&lt;td&gt;≈ 0.999665&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The largest uniform draw in half a million samples was about 0.999665. The probability of that happening by chance is &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0.999665&lt;/mn&gt;&lt;msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mn&gt;500,000&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;167&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;msup&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;72&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;(0.999665)^{500{,}000} \approx e^{-167} \approx 10^{-72}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1.0641em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.999665&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em;&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mpunct mtight&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;000&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;≈&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8141em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord mathnormal&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em;&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;167&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mrel&quot;&gt;≈&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2778em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8141em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8141em;&quot;&gt;&lt;span style=&quot;top:-3.063em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;72&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. That is not bad luck. Something was clipping &lt;code&gt;u&lt;/code&gt; from above.&lt;/p&gt;
&lt;p&gt;The uniform generator was this:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;uniform_real_distribution&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt; &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;dist&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;nextafter&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;nextafter&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; u &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; dist&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(rng);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;Textbook C++. Deliberately an open interval (0, 1), so the inverse-CDF transforms downstream — &lt;code&gt;log(1-u)&lt;/code&gt;, &lt;code&gt;qnorm(u)&lt;/code&gt; — can never hit the endpoints. A code reviewer nods and moves on.&lt;/p&gt;
&lt;p&gt;But on the MSVC STL our Windows builds ship against, that distribution does not deliver uniform draws across its declared support — the upper tail is squeezed in. Same story for &lt;code&gt;std::generate_canonical&amp;#x3C;double, 53&gt;&lt;/code&gt;. We didn&apos;t excavate the implementation to pin the exact mechanism; there are years-old reports against MSVC&apos;s &lt;code&gt;&amp;#x3C;random&gt;&lt;/code&gt; with similar symptoms, and we needed a working generator more than a culprit.&lt;/p&gt;
&lt;p&gt;So the fix sidesteps the library entirely: take one 64-bit draw from &lt;code&gt;mt19937_64&lt;/code&gt;, keep the top 53 bits, scale. Every representable double in [0, 1) at 53-bit precision gets equal probability mass:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; inline&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; NextUniform&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    auto&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;rng &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; TLSRng&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    uint64_t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; bits &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; rng&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 11&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;            // keep the top 53 bits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; u &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; static_cast&amp;#x3C;double&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(bits) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; static_cast&amp;#x3C;double&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;ULL&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 53&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (u &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&amp;#x3C;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 0.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;        u &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;numeric_limits&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;&gt;::&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; u;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;The upper bound is 1 − 2⁻⁵³ by construction, so no nudging needed there; the lower side is bumped off exact zero to keep &lt;code&gt;log(0)&lt;/code&gt; and &lt;code&gt;qnorm(0)&lt;/code&gt; out of reach. After this, the tail counts match Exp(1) theory to three significant figures out to the 10⁻⁵ quantile — and the smoke-test max lands right where extreme-value theory says it should, around 13.7.&lt;/p&gt;
&lt;p&gt;(In hindsight, that very first &lt;code&gt;max ≈ 8&lt;/code&gt; was both bugs compressed into a single number: 245 effective draws would put the expected maximum near 6, and the clipped generator couldn&apos;t produce anything above 8.0 regardless.)&lt;/p&gt;
&lt;h2 id=&quot;both-bugs-pass-code-review&quot;&gt;&lt;a href=&quot;#both-bugs-pass-code-review&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Both bugs pass code review&quot;&gt;Both bugs pass code review&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;What makes this pair worth a blog post is that &lt;strong&gt;neither bug is visible in the code&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The first draft of &lt;code&gt;rexp&lt;/code&gt; is idiomatic DuckDB — it uses the recommended executor helper and the documented volatility flag. The uniform generator is idiomatic C++ — &lt;code&gt;std::uniform_real_distribution&lt;/code&gt; is the API every textbook tells you to use. You could put both snippets in front of ten strong engineers and collect ten approvals.&lt;/p&gt;
&lt;p&gt;The bugs only exist &lt;em&gt;statistically&lt;/em&gt;. And statistics is exactly the thing a review diff doesn&apos;t show. Worse: checking moments alone isn&apos;t enough either — a generator that clips its upper tail keeps the mean and standard deviation close to theory while silently ruining every Monte Carlo workload that cares about tails, which is roughly all of them.&lt;/p&gt;
&lt;p&gt;The thing that caught both was a boring, mechanical verification loop: after implementing, check the output against theory — moments, then distinct counts, then tail counts — and refuse to shrug when 500k draws average 0.92 instead of 1.00. This is precisely the kind of discipline an agent is good at, because it never gets bored and never decides &quot;eh, random is random.&quot; Claude Code wrote the naive first draft, like most of us would have. It also ran the checks that most of us, honestly, would have skipped — then chased the anomaly down through DuckDB&apos;s executor internals into the standard library, and wrote &lt;a href=&quot;https://github.com/KoliStat/the-stats-duck/blob/main/notes/engineering/2026-06-volatile-and-rng-bias.md&quot;&gt;the engineering note&lt;/a&gt; so the next contributor — human or agent — starts from the pattern that works.&lt;/p&gt;
&lt;h2 id=&quot;the-checklist&quot;&gt;&lt;a href=&quot;#the-checklist&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to The checklist&quot;&gt;The checklist&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Distilled from the wreckage, for anyone shipping sampling functions anywhere:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;count(*)&lt;/code&gt; vs &lt;code&gt;count(DISTINCT v)&lt;/code&gt;.&lt;/strong&gt; If a function is supposed to vary per row and these disagree, you have an executor caching problem. Full stop.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tail counts against theoretical probabilities.&lt;/strong&gt; &lt;code&gt;count(v &gt; threshold)&lt;/code&gt; vs &lt;code&gt;n · P(V &gt; threshold)&lt;/code&gt;. Catches uniform-generator bias that moment checks can&apos;t see.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inverse round-trip.&lt;/strong&gt; &lt;code&gt;pnorm(rnorm())&lt;/code&gt; must be Uniform(0, 1): mean 0.5, stddev &lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;/&lt;/mi&gt;&lt;msqrt&gt;&lt;mn&gt;12&lt;/mn&gt;&lt;/msqrt&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;1/\sqrt{12}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1.1572em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1/&lt;/span&gt;&lt;span class=&quot;mord sqrt&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.9072em;&quot;&gt;&lt;span class=&quot;svg-align&quot; style=&quot;top:-3em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot; style=&quot;padding-left:0.833em;&quot;&gt;&lt;span class=&quot;mord&quot;&gt;12&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-2.8672em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;hide-tail&quot; style=&quot;min-width:0.853em;height:1.08em;&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;400em&quot; height=&quot;1.08em&quot; viewBox=&quot;0 0 400000 1080&quot; preserveAspectRatio=&quot;xMinYMin slice&quot;&gt;&lt;path d=&quot;M95,702
c-2.7,0,-7.17,-2.7,-13.5,-8c-5.8,-5.3,-9.5,-10,-9.5,-14
c0,-2,0.3,-3.3,1,-4c1.3,-2.7,23.83,-20.7,67.5,-54
c44.2,-33.3,65.8,-50.3,66.5,-51c1.3,-1.3,3,-2,5,-2c4.7,0,8.7,3.3,12,10
s173,378,173,378c0.7,0,35.3,-71,104,-213c68.7,-142,137.5,-285,206.5,-429
c69,-144,104.5,-217.7,106.5,-221
l0 -0
c5.3,-9.3,12,-14,20,-14
H400000v40H845.2724
s-225.272,467,-225.272,467s-235,486,-235,486c-2.7,4.7,-9,7,-19,7
c-6,0,-10,-1,-12,-3s-194,-422,-194,-422s-65,47,-65,47z
M834 80h400000v40h-400000z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.1328em;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Run it more than once.&lt;/strong&gt; Thread-local RNGs seed per thread, so symptoms drift between runs. One weird number might be sampling noise; a moving target across reruns is a strong signal.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;trust-but-countdistinct&quot;&gt;&lt;a href=&quot;#trust-but-countdistinct&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Trust, but count(DISTINCT)&quot;&gt;Trust, but count(DISTINCT)&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The fixes landed before &lt;a href=&quot;/blog/the-stats-duck-v0-6-0/&quot;&gt;v0.6.0&lt;/a&gt; shipped, so every released build samples correctly. Don&apos;t take our word for it — this is the shipped build, running in your browser right now:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;  count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; total&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;DISTINCT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; v) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; distinct_cnt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;avg&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(v), &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; mean&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(v), &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; mx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; rexp(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; v &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;500000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;The fixed rexp() running live in Bedevere&quot;
  style=&quot;width: 100%; height: 340px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#autorun=1&amp;query=SELECT%0A%20%20count(*)%20AS%20total%0A%20%20%2C%20count(DISTINCT%20v)%20AS%20distinct_cnt%0A%20%20%2C%20round(avg(v)%2C%203)%20AS%20mean%0A%20%20%2C%20round(max(v)%2C%201)%20AS%20mx%0AFROM%20(SELECT%20rexp(1.0)%20AS%20v%20FROM%20range(500000))%3B&quot;
&gt;&lt;/iframe&gt;
&lt;p&gt;Half a million rows, half a million distinct values, mean within a hair of 1, max where the theory puts it.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;the-stats-duck&lt;/code&gt; is MIT-licensed and runs anywhere DuckDB runs — it&apos;s the statistics engine under &lt;a href=&quot;https://bedeverewise.app&quot;&gt;Bedevere&lt;/a&gt; and &lt;a href=&quot;https://editor.kolistat.com&quot;&gt;KoliLang&lt;/a&gt;. The full engineering note, with the complete diagnostic trail, lives &lt;a href=&quot;https://github.com/KoliStat/the-stats-duck/blob/main/notes/engineering/2026-06-volatile-and-rng-bias.md&quot;&gt;in the repo&lt;/a&gt;. And if there&apos;s a distribution or test you need that isn&apos;t there yet, &lt;a href=&quot;/contact&quot;&gt;get in touch&lt;/a&gt; — commissioned functions ship in the open-source extension, so everyone gets them. Carefully sampled.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>the-stats-duck v0.6.0 — statistics that live in your SQL</title>
    <id>https://kolistat.com/blog/the-stats-duck-v0-6-0/</id>
    <link href="https://kolistat.com/blog/the-stats-duck-v0-6-0/" />
    <updated>2026-06-22T00:00:00.000Z</updated>
    <published>2026-06-22T00:00:00.000Z</published>
    <author><name>Massimo Meneghello</name></author>
    <category term="the-stats-duck" />
    <category term="DuckDB" />
    <category term="statistics" />
    <category term="Bedevere" />
    <summary>A tour of the-stats-duck v0.6.0: a one-shot data profiler, regression and bootstrap CIs in plain SQL, ten new distributions, and SAS/XPT imports that got 52x faster — with live Bedevere demos you can poke at.</summary>
    <content type="html">&lt;p&gt;&lt;code&gt;the-stats-duck&lt;/code&gt; — our open-source DuckDB extension — just shipped &lt;strong&gt;&lt;a href=&quot;https://github.com/KoliStat/the-stats-duck/releases/tag/v0.6.0&quot;&gt;v0.6.0&lt;/a&gt;&lt;/strong&gt;, and in keeping with the whole operation, the release is named &lt;em&gt;i-m-not-dead&lt;/em&gt;. It very much isn&apos;t!&lt;/p&gt;
&lt;p&gt;In case you haven&apos;t met it before: &lt;code&gt;the-stats-duck&lt;/code&gt; is the statistics engine that &lt;a href=&quot;https://bedeverewise.app&quot;&gt;Bedevere&lt;/a&gt; and &lt;a href=&quot;https://editor.kolistat.com&quot;&gt;KoliLang&lt;/a&gt; lean on. It allows DuckDB to do real statistics — distributions, tests, regression, even plots — without ever leaving SQL. MIT-licensed, and it runs anywhere DuckDB runs — including a browser!&lt;/p&gt;
&lt;p&gt;Which is quite handy, because &lt;strong&gt;every demo below is a live &lt;a href=&quot;https://bedeverewise.app&quot;&gt;Bedevere&lt;/a&gt; instance&lt;/strong&gt; running &lt;code&gt;the-stats-duck&lt;/code&gt; right here in your browser. The dataset is the famous &lt;a href=&quot;https://allisonhorst.github.io/palmerpenguins/&quot;&gt;Palmer Penguins&lt;/a&gt; — change the SQL and re-run it.&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt; &apos;penguins&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;The penguins dataset running live in Bedevere&quot;
  style=&quot;width: 100%; height: 440px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#dataset=https://kolistat.com/data/penguins.parquet&amp;autorun=1&amp;query=SELECT%20*%20FROM%20&apos;penguins&apos;%3B&quot;
&gt;&lt;/iframe&gt;
&lt;h2 id=&quot;profile-a-whole-table-in-one-shot-meta&quot;&gt;&lt;a href=&quot;#profile-a-whole-table-in-one-shot-meta&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Profile a whole table in one shot: meta()&quot;&gt;Profile a whole table in one shot: &lt;code&gt;meta()&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The first thing anyone would usually do with a new dataset is &lt;em&gt;squint&lt;/em&gt; at it. &lt;code&gt;meta()&lt;/code&gt; squints for you the full profile — one row per column:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  column_name, kind, n_missing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , n_distinct, mean, median, stddev, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;top&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; meta(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;penguins&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;meta() running live in Bedevere&quot;
  style=&quot;width: 100%; height: 440px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#dataset=https://kolistat.com/data/penguins.parquet&amp;autorun=1&amp;query=SELECT%0A%20%20column_name%2C%20kind%2C%20n_missing%0A%20%20%2C%20n_distinct%2C%20mean%2C%20median%2C%20stddev%2C%20top%0AFROM%20meta(&apos;penguins&apos;)%0A%3B&quot;
&gt;&lt;/iframe&gt;
&lt;p&gt;It overlaps with DuckDB&apos;s built-in &lt;code&gt;SUMMARIZE&lt;/code&gt;, but &lt;code&gt;meta()&lt;/code&gt; is a &lt;em&gt;table function&lt;/em&gt; — so you can join it, filter it, and compose it in CTEs.&lt;sup&gt;&lt;a href=&quot;#user-content-fn-summarize-cte&quot; id=&quot;user-content-fnref-summarize-cte&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; Do you need to know &quot;how many numeric columns, and how many missing values total?&quot; That&apos;s just aggregation over &lt;code&gt;meta()&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;regression-without-leaving-sql-lm-an-r-style-formula&quot;&gt;&lt;a href=&quot;#regression-without-leaving-sql-lm-an-r-style-formula&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Regression without leaving SQL: lm + an R-style formula&quot;&gt;Regression without leaving SQL: &lt;code&gt;lm&lt;/code&gt; + an R-style formula&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Yes, ordinary least squares, in a SQL query, with the formula syntax you already know:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; *&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; lm_summary(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;  &apos;penguins&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , formula :&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt; &apos;body_mass_g ~ flipper_length_mm + bill_length_mm&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;lm_summary() running live in Bedevere&quot;
  style=&quot;width: 100%; height: 360px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#dataset=https://kolistat.com/data/penguins.parquet&amp;autorun=1&amp;query=SELECT%20*%0AFROM%20lm_summary(%0A%20%20&apos;penguins&apos;%0A%20%20%2C%20formula%20%3A%3D%20&apos;body_mass_g%20~%20flipper_length_mm%20%2B%20bill_length_mm&apos;%0A)%3B&quot;
&gt;&lt;/iframe&gt;
&lt;p&gt;&lt;code&gt;lm()&lt;/code&gt; gives you the coefficient table (&lt;code&gt;estimate&lt;/code&gt;, &lt;code&gt;std_error&lt;/code&gt;, &lt;code&gt;t_statistic&lt;/code&gt;, &lt;code&gt;p_value&lt;/code&gt; per term); &lt;code&gt;lm_summary()&lt;/code&gt; gives the model-level line (R², adjusted R², F and its p-value, residual df, sigma). The formula language handles additive predictors and intercept removal (&lt;code&gt;- 1&lt;/code&gt;); interactions and inline transforms aren&apos;t in v0.6 yet, but we might add them soon!&lt;/p&gt;
&lt;p&gt;Under the hood it&apos;s a Cholesky solve of &lt;code&gt;X&apos;X&lt;/code&gt;, complete-case filtered, and cross-verified against R on &lt;code&gt;cars&lt;/code&gt; and &lt;code&gt;mtcars&lt;/code&gt; to four decimals.&lt;/p&gt;
&lt;h2 id=&quot;confidence-intervals-by-brute-force-bootstrap&quot;&gt;&lt;a href=&quot;#confidence-intervals-by-brute-force-bootstrap&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Confidence intervals by brute force: bootstrap()&quot;&gt;Confidence intervals by brute force: &lt;code&gt;bootstrap()&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Don&apos;t want to assume normality? Resample:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; b &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;  SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; bootstrap(body_mass_g, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;mean&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;2000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; samples&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; penguins&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  list_aggregate(samples, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;quantile_cont&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;025&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; lo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , list_aggregate(samples, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;quantile_cont&apos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;975&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; hi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;bootstrap() running live in Bedevere&quot;
  style=&quot;width: 100%; height: 320px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#dataset=https://kolistat.com/data/penguins.parquet&amp;autorun=1&amp;query=WITH%20b%20AS%20(%0A%20%20SELECT%20bootstrap(body_mass_g%2C%20&apos;mean&apos;%2C%202000%2C%2042)%20AS%20samples%0A%20%20FROM%20penguins%0A)%0ASELECT%0A%20%20list_aggregate(samples%2C%20&apos;quantile_cont&apos;%2C%200.025)%20AS%20lo%0A%20%20%2C%20list_aggregate(samples%2C%20&apos;quantile_cont&apos;%2C%200.975)%20AS%20hi%0AFROM%20b%0A%3B&quot;
&gt;&lt;/iframe&gt;
&lt;p&gt;&lt;code&gt;bootstrap(value, statistic, n_iters [, seed])&lt;/code&gt; resamples with replacement and hands back a &lt;code&gt;LIST&amp;#x3C;DOUBLE&gt;&lt;/code&gt; of the statistic from each resample (&lt;code&gt;mean&lt;/code&gt;, &lt;code&gt;median&lt;/code&gt;, &lt;code&gt;stddev&lt;/code&gt;, …). Pair it with &lt;code&gt;list_aggregate(list, &apos;quantile_cont&apos;, p)&lt;/code&gt; for a percentile interval. Pass a seed and it&apos;s reproducible — even across &lt;code&gt;GROUP BY&lt;/code&gt; groups.&lt;/p&gt;
&lt;h2 id=&quot;charts-straight-from-sql-visualize-draw&quot;&gt;&lt;a href=&quot;#charts-straight-from-sql-visualize-draw&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Charts, straight from SQL: VISUALIZE … DRAW&quot;&gt;Charts, straight from SQL: &lt;code&gt;VISUALIZE … DRAW&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;the-stats-duck also speaks a small — for now — plot grammar (ggsql) that compiles to Vega-Lite, which Bedevere renders. v0.6.0 adds &lt;strong&gt;violin&lt;/strong&gt; marks, &lt;strong&gt;2-D facets&lt;/strong&gt; (&lt;code&gt;FACET BY row, col&lt;/code&gt;), and a per-layer &lt;strong&gt;&lt;code&gt;STAT smooth&lt;/code&gt;&lt;/strong&gt; (LOESS) — so &lt;code&gt;DRAW point DRAW line STAT smooth&lt;/code&gt; is your scatter-with-trend in one line.&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;VISUALIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  species &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , body_mass_g &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; penguins&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;DRAW violin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;TITLE &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;Body Mass (g) by species&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;VISUALIZE violin running live in Bedevere&quot;
  style=&quot;width: 100%; height: 520px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#dataset=https://kolistat.com/data/penguins.parquet&amp;autorun=1&amp;query=VISUALIZE%0A%20%20species%20AS%20x%0A%20%20%2C%20body_mass_g%20AS%20y%0AFROM%20penguins%0ADRAW%20violin%0ATITLE%20&apos;Body%20Mass%20(g)%20by%20species&apos;%0A%3B&quot;
&gt;&lt;/iframe&gt;
&lt;h2 id=&quot;scatter-with-a-fitted-line-stat-smooth&quot;&gt;&lt;a href=&quot;#scatter-with-a-fitted-line-stat-smooth&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Scatter with a fitted line: STAT smooth&quot;&gt;Scatter with a fitted line: &lt;code&gt;STAT smooth&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The other half of v0.6.0&apos;s chart story is the per-layer &lt;strong&gt;&lt;code&gt;STAT&lt;/code&gt;&lt;/strong&gt; modifier — appended after a mark as &lt;code&gt;DRAW &amp;#x3C;mark&gt; STAT &amp;#x3C;name&gt;&lt;/code&gt;. It transforms &lt;em&gt;that layer only&lt;/em&gt;, so you can stack a raw mark under a statistical one. The canonical use is a scatter with a LOESS overlay:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;VISUALIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  bill_depth_mm &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , bill_length_mm &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , species &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; color&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; penguins&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;DRAW &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;point&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;DRAW &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;line&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; STAT smooth&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;SCALE x ZERO false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;SCALE x LABEL &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;Bill Depth (mm)&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;SCALE y ZERO false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;SCALE y LABEL &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;Bill Length (mm)&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;TITLE &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;Penguins Bill Depth VS Bill Length (Smooth)&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;STAT smooth running live in Bedevere&quot;
  style=&quot;width: 100%; height: 660px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#dataset=https://kolistat.com/data/penguins.parquet&amp;autorun=1&amp;query=VISUALIZE%0A%20%20bill_depth_mm%20AS%20x%0A%20%20%2C%20bill_length_mm%20AS%20y%0A%20%20%2C%20species%20AS%20color%0AFROM%20penguins%0ADRAW%20point%0ADRAW%20line%20STAT%20smooth%0ASCALE%20x%20ZERO%20false%0ASCALE%20x%20LABEL%20&apos;Bill%20Depth%20(mm)&apos;%0ASCALE%20y%20ZERO%20false%0ASCALE%20y%20LABEL%20&apos;Bill%20Length%20(mm)&apos;%0ATITLE%20&apos;Penguins%20Bill%20Depth%20VS%20Bill%20Length%20(Smooth)&apos;%0A%3B&quot;
&gt;&lt;/iframe&gt;
&lt;p&gt;Three modifiers ship. &lt;strong&gt;&lt;code&gt;smooth&lt;/code&gt;&lt;/strong&gt; injects a Vega-Lite LOESS transform on &lt;code&gt;(x, y)&lt;/code&gt;, grouped by &lt;code&gt;color&lt;/code&gt; when it&apos;s mapped — one fitted curve per species above. &lt;strong&gt;&lt;code&gt;summary&lt;/code&gt;&lt;/strong&gt; rewrites the layer&apos;s data SQL to &lt;code&gt;AVG(y) GROUP BY x [, color, facet, facet2] ORDER BY x&lt;/code&gt;, collapsing each cell to its mean. &lt;strong&gt;&lt;code&gt;identity&lt;/code&gt;&lt;/strong&gt; is the explicit no-op. &lt;code&gt;smooth&lt;/code&gt; is rejected on marks that already emit their own transform — &lt;code&gt;regression&lt;/code&gt;, &lt;code&gt;density&lt;/code&gt;, &lt;code&gt;violin&lt;/code&gt;, &lt;code&gt;histogram&lt;/code&gt; — which bake the statistic in themselves.&lt;/p&gt;
&lt;h2 id=&quot;a-straight-line-fit-draw-regression&quot;&gt;&lt;a href=&quot;#a-straight-line-fit-draw-regression&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to A straight-line fit: DRAW regression&quot;&gt;A straight-line fit: &lt;code&gt;DRAW regression&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Where &lt;code&gt;STAT smooth&lt;/code&gt; traces the data&apos;s local shape, the &lt;strong&gt;&lt;code&gt;regression&lt;/code&gt;&lt;/strong&gt; mark draws a single straight line — a least-squares &lt;code&gt;y ~ x&lt;/code&gt; fit, grouped by &lt;code&gt;color&lt;/code&gt;. Same axes as above, but each species gets a linear trend instead of a LOESS curve:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;VISUALIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  bill_depth_mm &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , bill_length_mm &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , species &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; color&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; penguins&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;DRAW &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;point&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;DRAW regression&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;SCALE x ZERO false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;SCALE x LABEL &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;Bill Depth (mm)&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;SCALE y ZERO false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;SCALE y LABEL &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;Bill Length (mm)&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;TITLE &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&apos;Penguins Bill Depth VS Bill Length (Regression)&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;DRAW regression running live in Bedevere&quot;
  style=&quot;width: 100%; height: 660px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#dataset=https://kolistat.com/data/penguins.parquet&amp;autorun=1&amp;query=VISUALIZE%0A%20%20bill_depth_mm%20AS%20x%0A%20%20%2C%20bill_length_mm%20AS%20y%0A%20%20%2C%20species%20AS%20color%0AFROM%20penguins%0ADRAW%20point%0ADRAW%20regression%0ASCALE%20x%20ZERO%20false%0ASCALE%20x%20LABEL%20&apos;Bill%20Depth%20(mm)&apos;%0ASCALE%20y%20ZERO%20false%0ASCALE%20y%20LABEL%20&apos;Bill%20Length%20(mm)&apos;%0ATITLE%20&apos;Penguins%20Bill%20Depth%20VS%20Bill%20Length%20(Regression)&apos;%0A%3B&quot;
&gt;&lt;/iframe&gt;
&lt;h2 id=&quot;ten-new-distributions-and-the-full-dpqr&quot;&gt;&lt;a href=&quot;#ten-new-distributions-and-the-full-dpqr&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Ten new distributions (and the full d/p/q/r)&quot;&gt;Ten new distributions (and the full d/p/q/r)&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The distribution zoo grew: &lt;strong&gt;negative binomial&lt;/strong&gt;, &lt;strong&gt;hypergeometric&lt;/strong&gt;, &lt;strong&gt;Weibull&lt;/strong&gt;, &lt;strong&gt;log-normal&lt;/strong&gt;, and &lt;strong&gt;Poisson&lt;/strong&gt;, each with the R-style &lt;code&gt;d&lt;/code&gt; / &lt;code&gt;p&lt;/code&gt; / &lt;code&gt;q&lt;/code&gt; triple — plus a complete set of &lt;strong&gt;random samplers&lt;/strong&gt;: &lt;code&gt;rnorm&lt;/code&gt;, &lt;code&gt;rt&lt;/code&gt;, &lt;code&gt;rchisq&lt;/code&gt;, &lt;code&gt;rf&lt;/code&gt;, &lt;code&gt;rgamma&lt;/code&gt;, &lt;code&gt;rbeta&lt;/code&gt;, &lt;code&gt;rexp&lt;/code&gt;, &lt;code&gt;rweibull&lt;/code&gt;, &lt;code&gt;rlnorm&lt;/code&gt;, &lt;code&gt;rpois&lt;/code&gt;. Every one cross-checked against R to six decimals. Simulate, fit, and test without round-tripping to another language.&lt;/p&gt;
&lt;p&gt;Here&apos;s the Poisson(λ = 3) probability mass function — &lt;code&gt;dpois&lt;/code&gt; evaluated over &lt;code&gt;0…10&lt;/code&gt; and drawn as a bar chart, all in SQL:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; pois &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;  SELECT&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; k, dpois(k, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; pmf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; t(k)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;VISUALIZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  k &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;  , pmf &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; pois&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;DRAW bar&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;iframe
  class=&quot;bedevere-embed&quot;
  loading=&quot;lazy&quot;
  title=&quot;dpois() bar chart running live in Bedevere&quot;
  style=&quot;width: 100%; height: 420px; border: 1px solid var(--border, #ccc); border-radius: 8px&quot;
  src=&quot;https://bedeverewise.app/embed#autorun=1&amp;query=WITH%20pois%20AS%20(%0A%20%20SELECT%20k%2C%20dpois(k%2C%203)%20AS%20pmf%0A%20%20FROM%20range(0%2C%2011)%20AS%20t(k)%0A)%0AVISUALIZE%0A%20%20k%20AS%20x%0A%20%20%2C%20pmf%20AS%20y%0AFROM%20pois%0ADRAW%20bar%0A%3B&quot;
&gt;&lt;/iframe&gt;
&lt;h2 id=&quot;and-it-got-fast-readstat-is-52x-quicker&quot;&gt;&lt;a href=&quot;#and-it-got-fast-readstat-is-52x-quicker&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to And it got fast: read_stat is 52x quicker&quot;&gt;And it got fast: &lt;code&gt;read_stat&lt;/code&gt; is 52x quicker&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Last, and this one&apos;s for the &lt;a href=&quot;/blog/xpt-files&quot;&gt;XPT crowd&lt;/a&gt;: reading SAS / SPSS / Stata files used to be accidentally &lt;strong&gt;O(N²)&lt;/strong&gt;: the reader re-parsed the file from byte zero for every 2,048-row chunk! 😱&lt;/p&gt;
&lt;p&gt;v0.6.0 parses each file exactly once into a buffered, spillable column store and streams from it. Here are the numbers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a 200k-row, 7 MB XPT: &lt;strong&gt;~67 s → ~1.3 s&lt;/strong&gt; (52x)&lt;/li&gt;
&lt;li&gt;the real CDISC pilot &lt;code&gt;qs.xpt&lt;/code&gt; (122k rows): &lt;strong&gt;~39 s → ~1.1 s&lt;/strong&gt; (36x)&lt;/li&gt;
&lt;li&gt;1.6M rows: &lt;strong&gt;~70 min → ~15 s&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;go-play&quot;&gt;&lt;a href=&quot;#go-play&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Go play&quot;&gt;Go play&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;the-stats-duck&lt;/code&gt; is open source: &lt;strong&gt;&lt;a href=&quot;https://github.com/KoliStat/the-stats-duck&quot;&gt;github.com/KoliStat/the-stats-duck&lt;/a&gt;&lt;/strong&gt; (full &lt;a href=&quot;https://github.com/KoliStat/the-stats-duck/releases/tag/v0.6.0&quot;&gt;v0.6.0 notes here&lt;/a&gt;). It powers &lt;a href=&quot;https://bedeverewise.app&quot;&gt;Bedevere&lt;/a&gt; and &lt;a href=&quot;https://editor.kolistat.com&quot;&gt;KoliLang&lt;/a&gt;, but it&apos;s just a DuckDB extension: drop it into any DuckDB and your SQL grows a statistics department.&lt;/p&gt;
&lt;p&gt;Not dead. Doing your statistics.&lt;/p&gt;
&lt;section data-footnotes class=&quot;footnotes&quot;&gt;&lt;h2 class=&quot;sr-only&quot; id=&quot;footnotes&quot;&gt;&lt;a href=&quot;#footnotes&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Footnotes&quot;&gt;Footnotes&lt;/a&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li id=&quot;user-content-fn-summarize-cte&quot;&gt;
&lt;p&gt;A reader rightly points out that DuckDB&apos;s &lt;code&gt;SUMMARIZE&lt;/code&gt; can be used in a CTE too — you just wrap it in a &lt;code&gt;FROM&lt;/code&gt; subquery: &lt;code&gt;WITH some_table AS (FROM range(10)), x AS (FROM (SUMMARIZE some_table)) FROM x;&lt;/code&gt;. The practical difference is ergonomics: because &lt;code&gt;meta()&lt;/code&gt; is a table function, it composes (join, filter, CTE) without the &lt;code&gt;FROM (…)&lt;/code&gt; wrapper. &lt;a href=&quot;#user-content-fnref-summarize-cte&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 1&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</content>
  </entry>
  <entry>
    <title>You don&apos;t know XPT files</title>
    <id>https://kolistat.com/blog/xpt-files/</id>
    <link href="https://kolistat.com/blog/xpt-files/" />
    <updated>2026-06-17T00:00:00.000Z</updated>
    <published>2026-06-17T00:00:00.000Z</published>
    <author><name>Massimo Meneghello</name></author>
    <category term="XPT" />
    <category term="SAS" />
    <category term="clinical trials" />
    <category term="file formats" />
    <category term="Bedevere" />
    <summary>A friendly tour of the SAS Transport (XPT) format: everything you never wanted to know about it, and how to decode its IBM hexadecimal floats.</summary>
    <content type="html">&lt;p&gt;...unless you belong to the &lt;em&gt;magical&lt;/em&gt; world of &lt;strong&gt;clinical trials&lt;/strong&gt;! &lt;sup&gt;&lt;a href=&quot;#user-content-fn-mozilla&quot; id=&quot;user-content-fnref-mozilla&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; &lt;sup&gt;&lt;a href=&quot;#user-content-fn-economy&quot; id=&quot;user-content-fnref-economy&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Every clinical trial headed for the FDA produces a fresh pile of XPT files. It&apos;s nearly 40 years old, stores its numbers in IBM hexadecimal, and caps variable names at 8 characters. It is also not going anywhere, because regulators still mandate it.&lt;/p&gt;
&lt;h2 id=&quot;show-them-to-me&quot;&gt;&lt;a href=&quot;#show-them-to-me&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Show them to me!&quot;&gt;Show them to me!&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;First of all, would you like to open an XPT file? Well, it just happened that I launched a web app that does exactly that (among many other wonderful things, of course), and it happens that this might be the blog of the website that tries to promote it, in some way.
If you&apos;re not interested in the app but you stick around, you might still learn a thing or two from this post.&lt;/p&gt;
&lt;p&gt;The app is &lt;a href=&quot;https://bedeverewise.app/&quot;&gt;Bedevere Wise&lt;/a&gt;, named after the acute and science-fond knight of the Round Table.
Here is how it works: you take your XPT file, you drop it into the app, and &lt;em&gt;voilà&lt;/em&gt;, &lt;em&gt;magic&lt;/em&gt;!&lt;/p&gt;
&lt;p&gt;&lt;video src=&quot;/media/bedevere-import-xpt.mp4&quot; autoplay loop muted playsinline&gt;&lt;/video&gt;&lt;/p&gt;
&lt;p&gt;Well, you can also try to drop many other kinds of files used in the clinical trial world, like CSV, SAS7BDAT, Stata and even Excel! Or you can let the app browse a folder for you.
And everything stays in your browser! Even though, for the most picky ones, a desktop version of the app is on its way to be released soon — perfect even for the most &lt;em&gt;air-gapped&lt;/em&gt;, &lt;em&gt;strictly-secure&lt;/em&gt;, &lt;em&gt;highly-regulated&lt;/em&gt; environments!&lt;/p&gt;
&lt;p&gt;In case you are curious to try, but you don&apos;t have an XPT file handy, here is a GitHub &lt;a href=&quot;https://github.com/cdisc-org/sdtm-adam-pilot-project&quot;&gt;repo&lt;/a&gt; with a full clinical trial dataset and plenty of XPT files that you can download.&lt;/p&gt;
&lt;p&gt;Or, if you just want a couple of tiny files to poke at, grab these unimpressive, synthetic, test samples:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/files/xpt_test_num.xpt&quot;&gt;xpt_test_num.xpt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/files/xpt_test_mixed.xpt&quot;&gt;xpt_test_mixed.xpt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;so-what-are-these-damn-files&quot;&gt;&lt;a href=&quot;#so-what-are-these-damn-files&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to So, what are these damn files?&quot;&gt;So, what are these damn files?&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I&apos;d like to start by saying, &quot;According to Wikipedia...&quot;, but unfortunately, there&apos;s no page on Wikipedia about the topic.
&lt;em&gt;However&lt;/em&gt;, there is a &lt;a href=&quot;https://www.loc.gov/preservation/digital/formats/fdd/fdd000464.shtml&quot;&gt;page&lt;/a&gt; about the &lt;em&gt;SAS Transport File Format (XPORT) Family&lt;/em&gt; in the Digital Preservation section of The Library of Congress of the United States. The page says:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The SAS Transport File Format is an openly documented specification maintained by SAS, a commercial company with a variety of software products for statistics and business analytics, including the application now known as SAS/STAT, which originated in the late 1960s as SAS (an acronym for Statistical Analysis System) at North Carolina State University. The transport format was originally developed in the late 1980s when the corporate entity was known as SAS Institute, Inc. and the software as SAS, to support data transfers between statistical software systems, especially between SAS applications running on different operating systems. SAS considers it non-proprietary. &lt;sup&gt;&lt;a href=&quot;#user-content-fn-grabbing-docs&quot; id=&quot;user-content-fnref-grabbing-docs&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Basically, it&apos;s a &lt;em&gt;dinosaur&lt;/em&gt; of the computer age! An almost 40-year-old tabular data format, designed to store things that can be represented as tables, with headers and columns that are either text or numbers.&lt;/p&gt;
&lt;p&gt;And given the age, it comes as no surprise that XPT files are on the list, since over their long life they simply became &lt;em&gt;ubiquitous&lt;/em&gt; in the pharma/clinical trials world. Hence the reason to publicly preserve the format.&lt;/p&gt;
&lt;p&gt;And the reason they became — &lt;em&gt;and stay&lt;/em&gt; — so ubiquitous? &lt;em&gt;Regulation&lt;/em&gt;. If you want to submit the data of a clinical trial to the &lt;a href=&quot;https://www.fda.gov/regulatory-information/search-fda-guidance-documents/study-data-technical-conformance-guide-technical-specifications-document&quot;&gt;FDA&lt;/a&gt; (and it&apos;s a similar story for Japan&apos;s PMDA), you don&apos;t really get to choose your file format: submission datasets have to be provided as &lt;em&gt;SAS Transport Format Version 5&lt;/em&gt;. Yes, the &lt;code&gt;.xpt&lt;/code&gt; we&apos;ve been talking about. So every study headed for a regulatory submission produces a fresh pile of XPT files. And that pile isn&apos;t shrinking any time soon.&lt;/p&gt;
&lt;p&gt;It shouldn&apos;t surprise you, then, that many other, more familiar file formats are in the Library of Congress too, like PDF, JSON or ZIP. The reason is quite simple: those formats have become essential in our everyday lives, not only for people in IT or software engineers, but for &lt;em&gt;everyone&lt;/em&gt;. Even though most people don&apos;t know all the technical details and the specifics of the format, everyone relies on them, and there must be ways to know how to interact with those formats.
They became public protocols, like the ones we use to send internet packets or to open web pages.&lt;/p&gt;
&lt;p&gt;And XPT files are no less than that! You — &lt;em&gt;small pharma company&lt;/em&gt; — want to establish a formal communication with the FDA? Then, you speak their language and submit XPT files.&lt;/p&gt;
&lt;h2 id=&quot;wait-i-thought-it-was-all-ieee-754&quot;&gt;&lt;a href=&quot;#wait-i-thought-it-was-all-ieee-754&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Wait, I thought it was all IEEE 754!&quot;&gt;Wait, I thought it was all &lt;em&gt;IEEE 754&lt;/em&gt;!&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you managed to stick around up to this point, you might either be a bit of a masochist, an LLM crawler, or simply interested in some more technical details. And well, from a software engineering perspective, there is at least one worth mentioning: numeric values in the XPT format are stored using the &lt;em&gt;IBM float representation&lt;/em&gt; (yes, there is a &lt;a href=&quot;https://en.wikipedia.org/wiki/IBM_hexadecimal_floating-point&quot;&gt;page&lt;/a&gt; on Wikipedia about it). So, in order to be used by modern programming languages, they must be converted into the more convenient IEEE 754 floating point standard.
So no, even in 2026 it&apos;s not all IEEE 754. &lt;sup&gt;&lt;a href=&quot;#user-content-fn-not-all-ieee-754&quot; id=&quot;user-content-fnref-not-all-ieee-754&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;If you speak Python, here is the &lt;a href=&quot;https://github.com/pandas-dev/pandas/blob/aee9241b414707c381277b4392d5f0ef06956ba9/pandas/io/sas/sas_xport.py#L179&quot;&gt;code&lt;/a&gt; used by Pandas, which I first &lt;del&gt;copied&lt;/del&gt; re-implemented in one of my Go libraries. Since that implementation is a bit too long and detailed, I&apos;ll use a shorter version here:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;# A numeric whose first byte is one of these *and* whose remaining&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;# 7 bytes are all zero is a SAS missing value rather than a real number:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;#     &apos;.&apos;      -&gt; 0x2E         ordinary missing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;#     &apos;_&apos;      -&gt; 0x5F         special missing  ._&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;#     &apos;A&apos;..&apos;Z&apos; -&gt; 0x41..0x5A   special missings .A .. .Z&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;MISSING_FIRST&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;0x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;2E&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;0x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;5F&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;0x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;41&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;0x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;5A&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6F42C1;--shiki-dark:#B392F0&quot;&gt; ibm_to_double&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(raw):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;    Decode up to 8 bytes of IBM hex float.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;    Returns a Python float for real numbers, or None for a SAS missing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;    value. (The specific missing code, &apos;.&apos;/&apos;_&apos;/&apos;A&apos;..&apos;Z&apos;, is available&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;    as the byte `raw[0]` if you need to distinguish them.)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    # short numerics carry only the high-order bytes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    b &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; raw.ljust(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;\x00&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    # SAS missing value: a marker byte followed by nothing but zeros.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; b[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;:] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;\x00&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; and&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; b[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; MISSING_FIRST&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;      return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.from_bytes(b, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;big&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    # check for 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;      return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    sign &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 63&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 1.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    exponent &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 56&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;7F&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;            # excess-64, power of 16&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    fraction &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;00FFFFFFFFFFFFFF&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;      # low 56 bits = integer mantissa&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; sign &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; fraction &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 16.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (exponent &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 78&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;ibm1 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.fromhex(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;41 50 00 00 00 00 00 00&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(ibm_to_double(ibm1)) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;# 5.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;ibm2 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.fromhex(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;C1 50 00 00 00 00 00 00&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(ibm_to_double(ibm2)) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;# -5.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;ibm3 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.fromhex(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;41 13 c0 83 12 6e 97 8e&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(ibm_to_double(ibm3)) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;# 1.2345000000000002&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;ibm4 &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;.fromhex(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;3d 81 72 5b 67 2e e3 40&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;(ibm_to_double(ibm4)) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;# 0.00012345&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;Ok, even if you are familiar with Python, that might look a bit too &lt;em&gt;low-level&lt;/em&gt;, but that&apos;s how it is: moving from one numeric format to another means getting your hands dirty shifting bits around. Still, it&apos;s not as long and complicated as the Pandas implementation; here we are going straight to the point.&lt;/p&gt;
&lt;h2 id=&quot;decoding-a-number-bit-by-bit&quot;&gt;&lt;a href=&quot;#decoding-a-number-bit-by-bit&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Decoding a number, bit by bit&quot;&gt;Decoding a number, bit by bit&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;First of all, how is the numeric value actually represented? Looking at Wikipedia, we know that it can be represented with 4 bytes (32 bits), laid out like this:&lt;/p&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
  &lt;div class=&quot;bitfield-bar&quot;&gt;
    &lt;span class=&quot;bf-seg&quot; style=&quot;flex:1&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;bf-seg&quot; style=&quot;flex:7&quot;&gt;7&lt;/span&gt;
    &lt;span class=&quot;bf-seg&quot; style=&quot;flex:24&quot;&gt;24&lt;/span&gt;
  &lt;/div&gt;
  &lt;figcaption&gt;&lt;b&gt;Sign&lt;/b&gt; 1 bit · &lt;b&gt;Exponent&lt;/b&gt; 7 bits · &lt;b&gt;Fraction&lt;/b&gt; 24 bits&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;However, there might be cases in which the fraction uses 4 more bytes (56 bits in total), because the value is stored using the double precision format&lt;sup&gt;&lt;a href=&quot;#user-content-fn-truncation&quot; id=&quot;user-content-fnref-truncation&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;figure class=&quot;bitfield&quot;&gt;
  &lt;div class=&quot;bitfield-bar&quot;&gt;
    &lt;span class=&quot;bf-seg&quot; style=&quot;flex:1&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;bf-seg&quot; style=&quot;flex:7&quot;&gt;7&lt;/span&gt;
    &lt;span class=&quot;bf-seg&quot; style=&quot;flex:56&quot;&gt;56&lt;/span&gt;
  &lt;/div&gt;
  &lt;figcaption&gt;&lt;b&gt;Sign&lt;/b&gt; 1 bit · &lt;b&gt;Exponent&lt;/b&gt; 7 bits · &lt;b&gt;Fraction&lt;/b&gt; 56 bits&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Nothing really changes, since we read the most significant digits from left to right, and that&apos;s why we can just pad the bytes on the right with 0s until we get 8 bytes in total&lt;sup&gt;&lt;a href=&quot;#user-content-fn-lengths&quot; id=&quot;user-content-fnref-lengths&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;b &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; raw.ljust(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;\x00&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032F62;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;Next step: we check whether the number is a SAS &lt;em&gt;missing value&lt;/em&gt; (the format&apos;s take on &lt;em&gt;Not-a-Number&lt;/em&gt;). That&apos;s as simple as checking whether the first byte is one of the &lt;code&gt;MISSING_FIRST&lt;/code&gt; special values while the remaining 7 bytes are all 0s.
Then we check whether the number is exactly 0 — nothing special here either, all bits must be &lt;code&gt;0&lt;/code&gt;. To check that, we read the value as a big-endian integer, which will also come in handy for the next step.&lt;/p&gt;
&lt;p&gt;Now that we handled the special cases, we can do the actual conversion. From Wikipedia, we get:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mtext&gt;sign&lt;/mtext&gt;&lt;/msup&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;mtext&gt;significand&lt;/mtext&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msup&gt;&lt;mn&gt;16&lt;/mn&gt;&lt;mrow&gt;&lt;mtext&gt;exponent&lt;/mtext&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;64&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;(-1)^{\text{sign}} \times 0.\text{significand} \times 16^{\text{exponent} - 64}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:1.1305em;vertical-align:-0.25em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mopen&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mclose&quot;&gt;&lt;span class=&quot;mclose&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8805em;&quot;&gt;&lt;span style=&quot;top:-3.113em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord text mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;sign&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8889em;vertical-align:-0.1944em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;significand&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8641em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8641em;&quot;&gt;&lt;span style=&quot;top:-3.113em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord text mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;exponent&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;mbin mtight&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;64&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;which is &lt;em&gt;almost&lt;/em&gt; what we have in the last instruction of our &lt;code&gt;ibm_to_double&lt;/code&gt; function:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; sign &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; fraction &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 16.0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (exponent &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 78&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;Where does that &lt;code&gt;78&lt;/code&gt; come from, instead of the &lt;code&gt;64&lt;/code&gt; in the formula? It&apos;s the same thing, just accounted for differently. Wikipedia treats the 56 fraction bits as &lt;code&gt;0.significand&lt;/code&gt;, i.e. a binary &lt;em&gt;fraction&lt;/em&gt; between 0 and 1. We, on the other hand, are lazy and read those same 56 bits as a plain &lt;em&gt;integer&lt;/em&gt; (that&apos;s the big-endian &lt;code&gt;int.from_bytes&lt;/code&gt; from before). Reading 56 bits as an integer instead of a fraction means multiplying by &lt;code&gt;2^56&lt;/code&gt;, which is &lt;code&gt;16^14&lt;/code&gt;, so we have to compensate by lowering the exponent by 14: &lt;code&gt;64 + 14 = 78&lt;/code&gt;. No magic, just moving the same factor from one place to another.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sign.&lt;/strong&gt; If the first bit is &lt;code&gt;1&lt;/code&gt;, the number is negative, so we &lt;em&gt;shift&lt;/em&gt; away everything but that first bit and use it in an if/else.
Take the first example in the code, &lt;code&gt;41 50 00 00 00 00 00 00&lt;/code&gt;, which represents &lt;code&gt;5.0&lt;/code&gt;. The first byte &lt;code&gt;41&lt;/code&gt; is &lt;code&gt;0100 0001&lt;/code&gt; in binary. Shift everything 63 places to the right with the &lt;code&gt;&gt;&gt;&lt;/code&gt; operator, and we&apos;re left with 63 &lt;code&gt;0&lt;/code&gt;s plus that original first bit — still a &lt;code&gt;0&lt;/code&gt; — so the number is positive.
If we consider the second example instead, &lt;code&gt;C1 50 00 00 00 00 00 00&lt;/code&gt;, the first byte &lt;code&gt;C1&lt;/code&gt; in binary is &lt;code&gt;1100 0001&lt;/code&gt;, and since the only thing that changed was the first bit, we get &lt;code&gt;-5.0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Exponent.&lt;/strong&gt; We&apos;ve already seen the shift used to select the first bit of our value. This expression&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;exponent &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; (n &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt; 56&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;7F&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;selects the first &lt;em&gt;byte&lt;/em&gt; instead (&lt;code&gt;&gt;&gt; 56&lt;/code&gt;) and &lt;em&gt;ignores&lt;/em&gt; the first bit (&lt;code&gt;&amp;#x26; 0x7F&lt;/code&gt;), which is the &lt;code&gt;sign&lt;/code&gt;, of course, and we don&apos;t need it here.
If we select the first byte from the first and the second examples we have &lt;code&gt;0100 0001&lt;/code&gt; and &lt;code&gt;1100 0001&lt;/code&gt;, which, after the bitwise &lt;em&gt;and&lt;/em&gt; with &lt;code&gt;0x7F&lt;/code&gt; (&lt;code&gt;0111 1111&lt;/code&gt;), both give &lt;code&gt;0100 0001&lt;/code&gt;, which in base 10 is &lt;code&gt;65&lt;/code&gt;. Then &lt;code&gt;65 - 78&lt;/code&gt; is &lt;code&gt;-13&lt;/code&gt;, which doesn&apos;t really make sense on its own, since the number we want is &lt;code&gt;5.0&lt;/code&gt; or &lt;code&gt;-5.0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fraction.&lt;/strong&gt; That &lt;code&gt;-13&lt;/code&gt; only starts to make sense once we bring in the fraction — everything but the first byte, obtained with &lt;code&gt;&amp;#x26; 0x00FFFFFFFFFFFFFF&lt;/code&gt;: &lt;code&gt;00 50 00 00 00 00 00 00&lt;/code&gt;, which in base 10 is 22517998136852480.&lt;/p&gt;
&lt;p&gt;So, if we put everything together, we get&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1.0&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;22517998136852480&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msup&gt;&lt;mn&gt;16&lt;/mn&gt;&lt;mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;13&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;1.0 \times 22517998136852480 \times 16^{-13}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.7278em;vertical-align:-0.0833em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.7278em;vertical-align:-0.0833em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;22517998136852480&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mbin&quot;&gt;×&lt;/span&gt;&lt;span class=&quot;mspace&quot; style=&quot;margin-right:0.2222em;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:0.8641em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;msupsub&quot;&gt;&lt;span class=&quot;vlist-t&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:0.8641em;&quot;&gt;&lt;span style=&quot;top:-3.113em;margin-right:0.05em;&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:2.7em;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;sizing reset-size6 size3 mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;&lt;span class=&quot;mord mtight&quot;&gt;−&lt;/span&gt;&lt;span class=&quot;mord mtight&quot;&gt;13&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;which is exactly &lt;code&gt;5.0&lt;/code&gt;! The magic of floating point representation!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/my-brain-hurts.gif&quot; alt=&quot;My brain hurts&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;everything-else&quot;&gt;&lt;a href=&quot;#everything-else&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Everything else&quot;&gt;Everything else&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Good! Now that we&apos;ve sorted that out, everything else about the XPT format is quite straightforward.
There are a handful of version numbers floating around (5, 6, 8, 9), but really just two families: the classic &lt;strong&gt;v5/6&lt;/strong&gt; — 8-character variable names, which is what we&apos;ll parse here — and &lt;strong&gt;v8/9&lt;/strong&gt;, which mostly exists to allow longer variable names (through a few extra header records). Either way it&apos;s the same idea: a stack of fixed-layout records that look more or less like this (without the newline characters):&lt;/p&gt;
&lt;pre class=&quot;rec&quot;&gt;&lt;span class=&quot;rec-dim&quot;&gt;HEADER RECORD*******&lt;/span&gt;&lt;span class=&quot;rec-type&quot;&gt;LIBRARY&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt; HEADER RECORD!!!!!!!000000000000000000000000000000&lt;/span&gt;
SAS     SAS     SASLIB  9.4     X64_10PR                        25NOV23:15:18:17
25NOV23:15:18:17
&lt;span class=&quot;rec-dim&quot;&gt;HEADER RECORD*******&lt;/span&gt;&lt;span class=&quot;rec-type&quot;&gt;MEMBER&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt;  HEADER RECORD!!!!!!!000000000000000001600000000&lt;/span&gt;&lt;span class=&quot;rec-hl&quot;&gt;140&lt;/span&gt;
&lt;span class=&quot;rec-dim&quot;&gt;HEADER RECORD*******&lt;/span&gt;&lt;span class=&quot;rec-type&quot;&gt;DSCRPTR&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt; HEADER RECORD!!!!!!!000000000000000000000000000000&lt;/span&gt;
SAS     VALUES  SASDATA 9.4     X64_10PR                        25NOV23:15:18:17
25NOV23:15:18:17
&lt;span class=&quot;rec-dim&quot;&gt;HEADER RECORD*******&lt;/span&gt;&lt;span class=&quot;rec-type&quot;&gt;NAMESTR&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt; HEADER RECORD!!!!!!!000000000&lt;/span&gt;&lt;span class=&quot;rec-hl&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt;00000000000000000000&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Take that second record — the one starting with &lt;code&gt;SAS&lt;/code&gt; — it&apos;s the library&apos;s &lt;em&gt;real header&lt;/em&gt;, and it decodes into a row of fixed-width fields:&lt;/p&gt;
&lt;figure class=&quot;bytemap&quot;&gt;
  &lt;div class=&quot;bytemap-row&quot;&gt;
    &lt;span class=&quot;bm-cell&quot; style=&quot;flex:8&quot;&gt;&lt;b&gt;SAS&lt;/b&gt;&lt;small&gt;marker · 8&lt;/small&gt;&lt;/span&gt;
    &lt;span class=&quot;bm-cell&quot; style=&quot;flex:8&quot;&gt;&lt;b&gt;SAS&lt;/b&gt;&lt;small&gt;marker · 8&lt;/small&gt;&lt;/span&gt;
    &lt;span class=&quot;bm-cell&quot; style=&quot;flex:8&quot;&gt;&lt;b&gt;SASLIB&lt;/b&gt;&lt;small&gt;marker · 8&lt;/small&gt;&lt;/span&gt;
    &lt;span class=&quot;bm-cell&quot; style=&quot;flex:8&quot;&gt;&lt;b&gt;9.4&lt;/b&gt;&lt;small&gt;version · 8&lt;/small&gt;&lt;/span&gt;
    &lt;span class=&quot;bm-cell&quot; style=&quot;flex:8&quot;&gt;&lt;b&gt;X64_10PR&lt;/b&gt;&lt;small&gt;OS · 8&lt;/small&gt;&lt;/span&gt;
    &lt;span class=&quot;bm-cell&quot; style=&quot;flex:24&quot;&gt;&lt;b&gt;·····&lt;/b&gt;&lt;small&gt;padding · 24&lt;/small&gt;&lt;/span&gt;
    &lt;span class=&quot;bm-cell&quot; style=&quot;flex:16&quot;&gt;&lt;b&gt;25NOV23:15:18:17&lt;/b&gt;&lt;small&gt;created · 16&lt;/small&gt;&lt;/span&gt;
  &lt;/div&gt;
  &lt;figcaption&gt;The &lt;i&gt;real header&lt;/i&gt; record: 80 bytes total — most fields are 8 bytes wide, with 24 bytes of padding and a 16-byte timestamp.&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Apart from the dates (creation and last update), operating system, and SAS version, what matters here are the 4th and the last records:&lt;/p&gt;
&lt;pre class=&quot;rec&quot;&gt;&lt;span class=&quot;rec-dim&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;rec-dim&quot;&gt;HEADER RECORD*******&lt;/span&gt;&lt;span class=&quot;rec-type&quot;&gt;MEMBER&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt;  HEADER RECORD!!!!!!!000000000000000001600000000&lt;/span&gt;&lt;span class=&quot;rec-hl&quot;&gt;140&lt;/span&gt;
&lt;span class=&quot;rec-dim&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;rec-dim&quot;&gt;HEADER RECORD*******&lt;/span&gt;&lt;span class=&quot;rec-type&quot;&gt;NAMESTR&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt; HEADER RECORD!!!!!!!000000000&lt;/span&gt;&lt;span class=&quot;rec-hl&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt;00000000000000000000&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The number &lt;code&gt;140&lt;/code&gt; at the end of the 4th record tells us the number of bytes for the variable descriptor structure, while the last record tells us how many variables to expect (4 in this case). If that number &lt;code&gt;4&lt;/code&gt;, floating among a bunch of &lt;code&gt;0&lt;/code&gt;s, looks weird to you, you&apos;re not alone. Why did they decide to put it there, and not at the very end? Who knows...&lt;/p&gt;
&lt;p&gt;Ok, now that we know how many variables we need to read next, we can start parsing them into this structure:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; NAMESTR {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; ntype;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    /* VARIABLE TYPE: 1=NUMERIC, 2=CHAR */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; nhfun;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    /* HASH OF NNAME (always 0) */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; nlng;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;     /* LENGTH OF VARIABLE IN OBSERVATION */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; nvar0;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    /* VARNUM */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    char8 nname;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    /* NAME OF VARIABLE*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    char40 nlabel;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;  /* LABEL OF VARIABLE */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    char8 nform;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    /* NAME OF FORMAT */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; nfl;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;      /* FORMAT FIELD LENGTH OR 0 */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; nfd;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;      /* FORMAT NUMBER OF DECIMALS */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; nfj;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;      /* 0=LEFT JUSTIFICATION, 1=RIGHT JUST */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt; nfill&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;];&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;  /* (UNUSED, FOR ALIGNMENT AND FUTURE) */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    char8 niform;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;   /* NAME OF INPUT FORMAT */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; nifl;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;     /* INFORMAT LENGTH ATTRIBUTE */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    short&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; nifd;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;     /* INFORMAT NUMBER OF DECIMALS */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    long&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; npos;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;      /* POSITION OF VALUE IN OBSERVATION */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E36209;--shiki-dark:#FFAB70&quot;&gt; rest&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#005CC5;--shiki-dark:#79B8FF&quot;&gt;52&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;];&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;  /* remaining fields are irrelevant */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;I know, that&apos;s C, a language that&apos;s too scary for many programmers nowadays!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/media/this-is-fine.gif&quot; alt=&quot;This is fine&quot;&gt;&lt;/p&gt;
&lt;p&gt;But that&apos;s how it&apos;s reported in the &lt;a href=&quot;https://www.sas.com/content/dam/sasdam/documents/20260302/record-layout-of-a-sas-version-5-or-6-data-set-in-sas-transport-xport-format.pdf&quot;&gt;technical documents&lt;/a&gt;, and if you want to read an XPT file in a different language (or even C itself), you need to know how &lt;em&gt;short&lt;/em&gt; a &lt;code&gt;short&lt;/code&gt; is and how &lt;em&gt;long&lt;/em&gt; a &lt;code&gt;long&lt;/code&gt; is.&lt;/p&gt;
&lt;p&gt;After we read the variable descriptors, we get another header record:&lt;/p&gt;
&lt;pre class=&quot;rec&quot;&gt;&lt;span class=&quot;rec-dim&quot;&gt;HEADER RECORD*******&lt;/span&gt;&lt;span class=&quot;rec-type&quot;&gt;OBS&lt;/span&gt;&lt;span class=&quot;rec-dim&quot;&gt;     HEADER RECORD!!!!!!!000000000000000000000000000000&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and after that, if we got the type and size of each variable right, we can finally read the observations! (row by row)&lt;/p&gt;
&lt;h2 id=&quot;final-thoughts&quot;&gt;&lt;a href=&quot;#final-thoughts&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Final Thoughts&quot;&gt;Final Thoughts&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you made it this far, you now know more about IBM hexadecimal floats than you probably ever wanted to. The good news: the whole point of &lt;a href=&quot;https://bedeverewise.app/&quot;&gt;Bedevere Wise&lt;/a&gt; is that you don&apos;t have to. Drop your XPT file in and read it right away — no bit-shifting required. And it&apos;s about to get better: the next release makes reading XPT files faster and lets you &lt;em&gt;export&lt;/em&gt; to XPT as well, so you can round-trip them instead of just opening them.&lt;/p&gt;
&lt;p&gt;There are some well-known limitations though. If you pay attention to the variable descriptor structure, you might notice this:&lt;/p&gt;
&lt;figure class=&quot;code-block&quot;&gt;&lt;pre class=&quot;shiki shiki-themes github-light github-dark&quot; style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D73A49;--shiki-dark:#F97583&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt; NAMESTR {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    /* ... */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;    char8 nname;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    /* NAME OF VARIABLE*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#6A737D;--shiki-dark:#6A737D&quot;&gt;    /* ... */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#24292E;--shiki-dark:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;button type=&quot;button&quot; class=&quot;code-copy&quot; aria-label=&quot;Copy code&quot; data-copy=&quot;&quot;&gt;Copy&lt;/button&gt;&lt;/figure&gt;
&lt;p&gt;which means that variable names can&apos;t be longer than 8 characters — at least in the classic v5/6 files (the ones regulators want), since v8/9 lifts the limit, as we saw. Yes, that&apos;s quite a constraint, but that&apos;s how it was designed! And, if you&apos;re familiar with clinical trials, I&apos;m sure you&apos;ve seen cryptic names like these: &lt;code&gt;R2A1LO&lt;/code&gt;, &lt;code&gt;AENTMTFL&lt;/code&gt;, or &lt;code&gt;LBNRIND&lt;/code&gt;. Now you know why.&lt;/p&gt;
&lt;p&gt;Did I mention XPT files enough?&lt;/p&gt;
&lt;section data-footnotes class=&quot;footnotes&quot;&gt;&lt;h2 class=&quot;sr-only&quot; id=&quot;footnotes&quot;&gt;&lt;a href=&quot;#footnotes&quot; class=&quot;heading-anchor&quot; aria-label=&quot;Link to Footnotes&quot;&gt;Footnotes&lt;/a&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li id=&quot;user-content-fn-mozilla&quot;&gt;
&lt;p&gt;Actually, if &lt;code&gt;.xpt&lt;/code&gt; rings a bell and you&apos;ve &lt;em&gt;never&lt;/em&gt; been near a clinical trial, you might know it from Mozilla: XPCOM used &lt;code&gt;.xpt&lt;/code&gt; files (XPConnect Typelibs) to store compiled interface metadata. If that&apos;s you: I&apos;m sorry, this post really isn&apos;t for you. :( &lt;a href=&quot;#user-content-fnref-mozilla&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 1&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-economy&quot;&gt;
&lt;p&gt;Or maybe you belong to an economics department. &lt;a href=&quot;#user-content-fnref-economy&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 2&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-grabbing-docs&quot;&gt;
&lt;p&gt;I was quite amazed myself to discover how easily I could grab the technical documents that I needed when I first started implementing my first &lt;a href=&quot;https://github.com/caerbannogwhite/aargh/blob/bd6ef3b0ab8a6d52dba41b9a3d496fd2aba0540f/io/xpt.go#L1&quot;&gt;XPT parser in Go&lt;/a&gt;, before the LLM/agentic Age (&lt;a href=&quot;https://x.com/karpathy/status/1886192184808149383?lang=en&quot;&gt;2 BVC&lt;/a&gt;, &lt;em&gt;Before Vibe Coding&lt;/em&gt;, to be precise; here is the &lt;a href=&quot;https://github.com/caerbannogwhite/aargh/commit/feb9cc6e03e4ccdcc52bdacadab2ab8473ebb899&quot;&gt;commit&lt;/a&gt;). &lt;a href=&quot;#user-content-fnref-grabbing-docs&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 3&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-not-all-ieee-754&quot;&gt;
&lt;p&gt;Ok, I already know what to expect here: yes, for some applications, there might be more appropriate floating point representations. &lt;a href=&quot;#user-content-fnref-not-all-ieee-754&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 4&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-truncation&quot;&gt;
&lt;p&gt;Strictly speaking, XPT always uses the 8-byte IBM &lt;em&gt;double&lt;/em&gt; layout and just truncates it to fewer bytes. A truncated double happens to be bit-for-bit identical to IBM &lt;em&gt;single&lt;/em&gt; precision — both share the 1-bit sign and 7-bit exponent, and the double simply carries more fraction bits — which is exactly why right-padding the missing low bytes with zeros is lossless. &lt;a href=&quot;#user-content-fnref-truncation&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 5&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-lengths&quot;&gt;
&lt;p&gt;Numeric values aren&apos;t always 4 or 8 bytes: a variable can be any length from 2 to 8 bytes, set per variable by the &lt;code&gt;nlng&lt;/code&gt; field in the &lt;code&gt;NAMESTR&lt;/code&gt; struct you&apos;ll meet below. Shorter numerics keep only the high-order bytes, so padding back out to 8 recovers the full double. &lt;a href=&quot;#user-content-fnref-lengths&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 6&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</content>
  </entry>
  <entry>
    <title>Welcome to the KoliStat blog</title>
    <id>https://kolistat.com/blog/welcome/</id>
    <link href="https://kolistat.com/blog/welcome/" />
    <updated>2026-06-10T00:00:00.000Z</updated>
    <published>2026-06-10T00:00:00.000Z</published>
    <author><name>Massimo Meneghello</name></author>
    <category term="meta" />
    <summary>Notes from the founder on building modern tooling for clinical and tabular data — what we ship, how we build it, and the trade-offs that show up along the way.</summary>
    <content type="html">&lt;p&gt;KoliStat is the umbrella over three products built around the same question: what would clinical and tabular data tooling look like if it were built today, with the web as the runtime and DuckDB performance underneath?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;KoliLang&lt;/strong&gt; — the browser-native SAS engine: closed-source, compiled to WebAssembly, built to match SAS byte-for-byte, and live at &lt;a href=&quot;https://editor.kolistat.com&quot;&gt;editor.kolistat.com&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bedevere&lt;/strong&gt; — the tabular data visualizer and DuckDB SQL editor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;the-stats-duck&lt;/strong&gt; — the open-source DuckDB extension that supplies the statistics.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This blog is for the engineering notes that don&apos;t fit on a product page: pieces of the SAS dialect we had to model from scratch, DuckDB extension internals, WASM ABI design decisions, the macro evaluator, the PROC FREQ output spec, and why a particular byte-parity bug took two weeks to find.&lt;/p&gt;
&lt;p&gt;Cadence will be irregular. The work itself drives the topics: when something interesting lands in &lt;a href=&quot;/products/kolilang&quot;&gt;KoliLang&lt;/a&gt;, &lt;a href=&quot;/products/bedevere&quot;&gt;Bedevere&lt;/a&gt;, or &lt;a href=&quot;/products/the-stats-duck&quot;&gt;the-stats-duck&lt;/a&gt;, it gets a post. Subscribe via the &lt;a href=&quot;/feed.xml&quot;&gt;Atom feed&lt;/a&gt; or just check back — no newsletter, no comment section. That part is by design.&lt;/p&gt;</content>
  </entry>
</feed>
