Download grader/facts.mjs from PulseOS/budgetbench: direct link, hf CLI and curl.
- Browser
- Download file 9.37 kB
-
https://huggingface.co/datasets/PulseOS/budgetbench/resolve/main/grader/facts.mjs
- Command line
-
hf download hf://datasets/PulseOS/budgetbench/grader/facts.mjs
-
curl -L -o facts.mjs https://huggingface.co/datasets/PulseOS/budgetbench/resolve/main/grader/facts.mjs
9.37 kB
| // THE TRUE FIGURES OF A HOUSEHOLD, for the grader's audit of every money figure an answer prints. Adapted from | |
| // eval-chat/facts.mjs (grader v3's table): computed from truth/ledger.csv and truth/setup.json alone, never from a | |
| // system's output and never from app code; the category-to-group map is oracle/categories.json (data). | |
| // | |
| // A fact is {value, kind, entity, months}: `months` is the set of months the figure is about (null for today's | |
| // balances and yearly estimates). Facts are per CURRENCY: a figure is held only against the facts of the currency | |
| // it is written in. `forbiddenOf` lists the figures that ADD two currencies (no right answer prints one). | |
| import fs from "node:fs"; | |
| import {r2} from "../oracle/oracle.mjs"; | |
| const CATS = JSON.parse(fs.readFileSync(new URL("../oracle/categories.json", import.meta.url), "utf8")); | |
| const NOT_SPEND = new Set(["Income", "Transfers", "Debt Payments"]); | |
| /** leaf -> group (a group maps to itself) */ | |
| export function groupMap() { | |
| const m = new Map(Object.entries(CATS.leaves)); | |
| for (const g of CATS.groups) if (!m.has(g)) m.set(g, g); | |
| return m; | |
| } | |
| /** every true figure of the household in one currency */ | |
| export function factsOf(hh, currency) { | |
| const rows = hh.rows.filter((r) => r.currency === currency); | |
| const months = [...new Set(rows.map((r) => r.month))].sort(); | |
| const facts = []; | |
| const add = (value, kind, entity, ms) => { if (Number.isFinite(value) && Math.abs(value) >= 0.005) facts.push({value: r2(Math.abs(value)), kind, entity, months: ms}); }; | |
| const spendRows = rows.filter((r) => !NOT_SPEND.has(r.group)); | |
| const cellOf = (pred) => { const m = new Map(); for (const r of spendRows) if (pred(r)) { const k = `${r.category}|${r.month}`; m.set(k, (m.get(k) ?? 0) + r.amount); } return m; }; | |
| const spendFrom = (cells, month) => { let t = 0; for (const [k, v] of cells) if (k.endsWith(`|${month}`)) t += Math.max(0, -v); return r2(t); }; | |
| const all = cellOf(() => true); | |
| const spendOf = (m, pred) => (pred == null ? spendFrom(all, m) : spendFrom(cellOf(pred), m)); | |
| const spans = []; | |
| for (let i = 0; i < months.length; i++) for (let j = i; j < months.length; j++) spans.push(months.slice(i, j + 1)); | |
| const leaves = [...new Set(spendRows.map((r) => r.category))]; | |
| const groups = [...new Set(spendRows.map((r) => r.group))]; | |
| const merchants = [...new Set(rows.map((r) => r.merchant))]; | |
| const accounts = [...new Set(rows.map((r) => r.account))]; | |
| const incomeOf = (m) => r2(rows.filter((r) => r.month === m && r.group === "Income").reduce((a, r) => a + r.amount, 0)); | |
| const loansOf = (m) => r2(-rows.filter((r) => r.month === m && r.group === "Debt Payments" && r.category !== "Credit Card Payment").reduce((a, r) => a + r.amount, 0)); | |
| const creditOf = (m) => { let t = 0; for (const [k, v] of all) if (k.endsWith(`|${m}`) && v > 0) t += v; return r2(t); }; | |
| const grossOf = (m, pred) => r2(-spendRows.filter((r) => r.month === m && r.amount < 0 && pred(r)).reduce((a, r) => a + r.amount, 0)); | |
| const cache = new Map(); | |
| const spendCached = (key, pred, m) => { const k = `${key}|${m}`; if (!cache.has(k)) cache.set(k, spendOf(m, pred)); return cache.get(k); }; | |
| for (const span of spans) { | |
| const sum = (f) => r2(span.reduce((a, m) => a + f(m), 0)); | |
| const spend = sum((m) => spendCached("*", null, m)), income = sum(incomeOf), out = r2(spend + sum(loansOf)), credit = sum(creditOf); | |
| add(spend, "spend", "*", span); add(income, "income", "*", span); add(out, "money_out", "*", span); | |
| add(income - spend, "kept", "*", span); add(income - out, "kept", "*", span); add(income + credit - spend, "kept", "*", span); add(income + credit - out, "kept", "*", span); | |
| add(credit, "refund_credit", "*", span); | |
| add(sum((m) => grossOf(m, () => true)), "gross_purchases", "*", span); | |
| for (const c of leaves) { add(sum((m) => spendCached(`c:${c}`, (r) => r.category === c, m)), "spend", c, span); add(sum((m) => grossOf(m, (r) => r.category === c)), "gross_purchases", c, span); } | |
| for (const g of groups) add(sum((m) => spendCached(`g:${g}`, (r) => r.group === g, m)), "spend", g, span); | |
| for (const mer of merchants) { | |
| add(sum((m) => spendCached(`m:${mer}`, (r) => r.merchant === mer, m)), "merchant_net", mer, span); | |
| const rs = rows.filter((r) => span.includes(r.month) && r.merchant === mer); | |
| add(-rs.filter((r) => r.amount < 0).reduce((a, r) => a + r.amount, 0), "merchant_out", mer, span); | |
| add(rs.filter((r) => r.amount > 0).reduce((a, r) => a + r.amount, 0), "merchant_in", mer, span); | |
| } | |
| for (const a of accounts) add(sum((m) => spendCached(`a:${a}`, (r) => r.account === a, m)), "spend", a, span); | |
| const complete = span.filter((m) => m !== hh.cur); | |
| for (const set of [span, complete]) if (set.length > 1) { | |
| add(r2(set.reduce((a, m) => a + spendCached("*", null, m), 0) / set.length), "average", "*", set); | |
| add(r2(set.reduce((a, m) => a + incomeOf(m), 0) / set.length), "average", "*", set); | |
| for (const c of leaves) add(r2(set.reduce((a, m) => a + spendCached(`c:${c}`, (r) => r.category === c, m), 0) / set.length), "average", c, set); | |
| for (const mer of merchants) add(r2(set.reduce((a, m) => a + spendCached(`m:${mer}`, (r) => r.merchant === mer, m), 0) / set.length), "average", mer, set); | |
| } | |
| } | |
| for (const r of rows) { add(r.amount, "row", r.merchant, [r.month]); add(r.amount, "row", r.category, [r.month]); } | |
| for (const mer of merchants) for (const a of new Set(rows.filter((r) => r.merchant === mer && r.amount < 0).map((r) => r2(-r.amount)))) add(a * 12, "yearly_estimate", mer, null); | |
| for (let i = 0; i < months.length; i++) for (let j = 0; j < months.length; j++) if (i !== j) { | |
| const [a, b] = [months[i], months[j]]; | |
| add(spendCached("*", null, a) - spendCached("*", null, b), "difference", "*", [a, b]); | |
| for (const c of leaves) add(spendCached(`c:${c}`, (r) => r.category === c, a) - spendCached(`c:${c}`, (r) => r.category === c, b), "difference", c, [a, b]); | |
| for (const g of groups) add(spendCached(`g:${g}`, (r) => r.group === g, a) - spendCached(`g:${g}`, (r) => r.group === g, b), "difference", g, [a, b]); | |
| for (const mer of merchants) add(spendCached(`m:${mer}`, (r) => r.merchant === mer, a) - spendCached(`m:${mer}`, (r) => r.merchant === mer, b), "difference", mer, [a, b]); | |
| } | |
| let cash = 0, debt = 0; | |
| for (const a of hh.accounts ?? []) if ((a.currency ?? hh.home) === currency && a.balance != null) { | |
| add(a.balance, "balance", a.name, null); | |
| if (a.kind === "checking" || a.kind === "savings") cash += a.balance; else debt += a.balance; | |
| } | |
| add(cash, "cash", "*", null); add(cash - debt, "net_worth", "*", null); add(debt, "debt", "*", null); | |
| return facts; | |
| } | |
| /** figures that add two currencies: spending (whole, per category, per group) over every span, summed across currencies */ | |
| export function forbiddenOf(hh) { | |
| const curs = [...new Set(hh.rows.map((r) => r.currency))]; | |
| if (curs.length < 2) return []; | |
| const per = curs.map((c) => factsOf(hh, c).filter((f) => f.kind === "spend" || f.kind === "income" || f.kind === "gross_purchases")); | |
| const key = (f) => `${f.kind}|${f.entity}|${(f.months ?? []).join("+")}`; | |
| const maps = per.map((fs_) => new Map(fs_.map((f) => [key(f), f]))); | |
| const out = []; | |
| for (const [k, f] of maps[0]) { | |
| let total = f.value, n = 1; | |
| for (const m of maps.slice(1)) if (m.has(k)) { total += m.get(k).value; n++; } | |
| if (n > 1) out.push({value: r2(total), kind: `${f.kind} across currencies`, entity: f.entity, months: f.months}); | |
| } | |
| return out; | |
| } | |
| /** facts whose value is this figure (to the cent, or the figure is the fact rounded to the dollar) */ | |
| export function factsMatching(facts, value) { | |
| const whole = Number.isInteger(value); | |
| return facts.filter((f) => Math.abs(f.value - value) < 0.005 || (whole && Math.round(f.value) === value)); | |
| } | |
| /** THE GROUP AT A RANK (grader v3 revision 2026-10-02): see eval-chat/facts.mjs; rows are one currency's */ | |
| export function groupAtRank(fx, months, leaf, rank, smallest) { | |
| if (!Array.isArray(months) || months.length === 0 || !(rank >= 1)) return null; | |
| const spend = new Map(), lm = new Map(); | |
| for (const r of fx.rows) { | |
| if (NOT_SPEND.has(r.group) || !months.includes(r.month)) continue; | |
| const k = `${r.category}|${r.month}`; | |
| lm.set(k, (lm.get(k) ?? 0) + r.amount); | |
| } | |
| for (const [k, v] of lm) { const cat = k.split("|")[0]; spend.set(cat, r2((spend.get(cat) ?? 0) + Math.max(0, -v))); } | |
| const leaves = [...spend.entries()].filter(([, v]) => v > 0.005).sort((a, b) => (smallest ? a[1] - b[1] : b[1] - a[1]) || a[0].localeCompare(b[0])); | |
| const p = leaves.findIndex(([c]) => c === leaf) + 1; | |
| if (p < rank) return null; | |
| const left = leaves.slice(p - rank); | |
| const gm = groupMap(); | |
| const groups = new Map(); | |
| for (const [c, v] of left) groups.set(gm.get(c) ?? c, r2((groups.get(gm.get(c) ?? c) ?? 0) + v)); | |
| const ordered = [...groups.entries()].filter(([, v]) => v > 0.005).sort((a, b) => (smallest ? a[1] - b[1] : b[1] - a[1]) || a[0].localeCompare(b[0])); | |
| return ordered[rank - 1]?.[0] ?? null; | |
| } | |