Simon-Pierre Boucher
All apps & projects
Metrika icon
Open Source
Swift

Metrika

Stata-class statistics, GPU-accelerated by Apple Silicon

A native Swift 6 econometrics app with a DuckDB engine, invisible Metal/MLX compute, and every estimator validated against R to 1e-10.

116

R-validated tests

1e-10

relative tolerance vs R

0.2 s

to load 10M rows

2M

points in the Metal renderer

38

built-in commands

Overview

Metrika brings the Stata mental model to a fully native Mac app: one line like `reg log_rev price i.region, cluster(firm_id)` yields publication-ready output with factor variables, if/in qualifiers, and robust or cluster-robust inference. No Electron, no Python runtime — Swift 6, SwiftUI, and Accelerate all the way down.

The GPU is invisible: a planner dispatches every command to CPU (LAPACK) or GPU (MLX) automatically, and large bootstrap runs execute as batched Metal solves. All randomness flows through a counter-based Philox4x32 generator, so `set seed 42` produces bit-identical resamples on CPU and GPU, in any chunk order, across any parallelism.

Under the hood, a DuckDB columnar engine loads 10 million rows in 0.2 seconds, and a Metal point-sprite renderer takes over scatter plots past 100k points and shrugs at 2,000,000. Estimation spans OLS, GLMs, 2SLS, panel fixed effects, GPU bootstrap, permutation tests, Bayesian Gibbs sampling, lasso/elastic net, and gradient boosting.

Key Features

The Stata mental model

Familiar one-line syntax with factor variables, if/in qualifiers, robust and cluster-robust inference — console, do-files, or headless CLI.

Invisible GPU dispatch

A planner routes each command to LAPACK or MLX automatically; large bootstraps run as batched Metal solves without you choosing a backend.

Bit-identical reproducibility

Counter-based Philox4x32 RNG makes seeded resamples bit-identical on CPU and GPU, across any chunk order or parallelism.

R-validated to 1e-10

Coefficients, HC0-HC3 and cluster SEs, p-values into the far tails, and delta-method marginal effects all match R golden values.

DuckDB columnar engine

Bulk C-API extraction loads 10 million rows in 0.2 s, summarizes in ~0.3 s, and regresses in ~0.2 s — on a laptop.

Full inference toolbox

GPU pairs bootstrap, exact permutation tests, and Bayesian regression via Gibbs sampling, all seed-reproducible.

glmnet- and xgboost-exact ML

Lasso and elastic net match glmnet including its y-standardization convention; boosted-tree predictions match xgboost observation-by-observation.

Charts that scale

Swift Charts for scatter, line, histogram, and kdensity, with a Metal point-sprite renderer taking over beyond 100k points.

Extensible by design

Drop-in .zyq script commands with args macros, or native Swift plugins with syntax validation and gated dataset mutation.

How It Works

  1. ZQParser + ZQPlanner

    A command grammar parses Stata-style input into a typed AST, which the planner dispatches to CPU, GPU, or hybrid execution paths.

  2. ZQData over DuckDB

    A DataFrame facade over the DuckDB columnar engine plus native .dta support, with explicit missing-value semantics and listwise-deletion reporting.

  3. ZQStats and ZQGPU

    LAPACK estimators (OLS via QR, never X'X) live in ZQStats; ZQGPU is the only module touching MLX/Metal — backends stay swappable.

  4. ZQGraphics

    Plot specs render through Swift Charts, with a Metal point-sprite renderer taking over automatically past 100k points.

  5. MetrikaKit package

    The entire engine has zero UI dependencies and is fully testable with swift test; the SwiftUI app is a thin shell over console, data browser, editor, and manual.

Tech Stack

Core

Swift 6
SwiftUI
Swift Charts
macOS 14+ (Apple Silicon)

Compute

MLX
Metal
Accelerate (LAPACK)
Philox4x32 RNG

Data

DuckDB
Parquet / CSV / JSON / Arrow
Native Stata .dta (read 117-119, write 118)

Validation

R golden fixtures
glmnet
xgboost
Random123 known-answer vectors

Highlights

  • Every CPU estimator validated against R to 1e-10 relative tolerance — a p-value of 4x10^-22 matches R exactly
  • GPU bootstrap resample indices asserted bit-identical to the CPU Philox reference, itself pinned to Random123 known-answer vectors
  • 10 million rows: load 0.2 s, summarize ~0.3 s, regress ~0.2 s
  • Reads and writes native Stata .dta files (formats 117-119)
  • Bayesian posteriors with diffuse priors reproduce the frequentist answer within Monte-Carlo error — asserted, not assumed
  • Signed, notarized, and stapled DMG; documentation regenerated from the command registry so it can never drift from the app

Explore Metrika

GPU-accelerated statistics for macOS — the full source is on GitHub.