src/market.js
// TAM/SAM/SOM calculator. Uses only numbers the user supplied; every result carries its formula.
// Missing inputs are reported (or the tool errors); nothing is filled in or estimated.
import { ToolError } from "./errors.js";
import { md } from "./text.js";
import { normalizeAssumption, logMarkdown } from "./assumptions.js";
const nf = new Intl.NumberFormat("en-US", { maximumFractionDigits: 6 });
const round2 = (x) => Math.round(x * 100) / 100;
const fmt = (x) => nf.format(x);
const pct = (x) => `${nf.format(Math.round(x * 1e6) / 1e4)}%`;
export function marketSize(input) {
const cur = input.currency;
const period = input.period ?? "year";
const warnings = [];
const money = (x) => `${fmt(round2(x))} ${cur}`;
if (!input.top_down && !input.bottom_up) throw new ToolError("invalid_input", "Give top_down and/or bottom_up numbers. This tool does not supply or estimate market figures.");
const assumptions = [];
const addAssumption = (id, statement, n, category) =>
assumptions.push(
normalizeAssumption(
{
id,
statement,
category,
impact: "high",
confidence: n.confidence,
evidence: n.source_url ? [n.source_url] : [],
test: n.source_url ? "Re-check the cited source, its date and its definition of the market." : "Find a source, or validate with customer interviews, a survey or a pricing test.",
value: n.value,
basis: n.basis ?? null,
source_url: n.source_url ?? null,
},
id,
null,
),
);
let td = null;
if (input.top_down) {
const t = input.top_down;
const tam = t.total_market.value;
const sam = tam * t.serviceable_share.value;
const som = sam * t.obtainable_share.value;
td = {
tam: round2(tam),
sam: round2(sam),
som: round2(som),
formulas: {
tam: `TAM = total_market = ${money(tam)}`,
sam: `SAM = TAM \u00d7 serviceable_share = ${fmt(tam)} \u00d7 ${fmt(t.serviceable_share.value)} = ${money(sam)}`,
som: `SOM = SAM \u00d7 obtainable_share = ${fmt(round2(sam))} \u00d7 ${fmt(t.obtainable_share.value)} = ${money(som)}`,
},
};
addAssumption("top_down.total_market", `The total market is ${money(tam)} per ${period}.`, t.total_market, "market");
addAssumption("top_down.serviceable_share", `${pct(t.serviceable_share.value)} of the total market is serviceable by the product.`, t.serviceable_share, "market");
addAssumption("top_down.obtainable_share", `The company can win ${pct(t.obtainable_share.value)} of the serviceable market.`, t.obtainable_share, "go_to_market");
}
let bu = null;
if (input.bottom_up) {
const b = input.bottom_up;
if (b.obtainable_share && b.obtainable_customers) throw new ToolError("invalid_input", "bottom_up: give obtainable_share or obtainable_customers, not both.");
const arpc = b.annual_revenue_per_customer.value;
const tam = b.customers_total ? b.customers_total.value * arpc : null;
const sam = b.customers_serviceable.value * arpc;
let som = null;
let somFormula = null;
if (b.obtainable_customers) {
som = b.obtainable_customers.value * arpc;
somFormula = `SOM = obtainable_customers \u00d7 annual_revenue_per_customer = ${fmt(b.obtainable_customers.value)} \u00d7 ${fmt(arpc)} = ${money(som)}`;
} else if (b.obtainable_share) {
som = sam * b.obtainable_share.value;
somFormula = `SOM = SAM \u00d7 obtainable_share = ${fmt(round2(sam))} \u00d7 ${fmt(b.obtainable_share.value)} = ${money(som)}`;
} else warnings.push("bottom_up: no obtainable_share or obtainable_customers, so bottom-up SOM was not computed.");
if (!b.customers_total) warnings.push("bottom_up: no customers_total, so bottom-up TAM was not computed.");
if (b.customers_total && b.customers_serviceable.value > b.customers_total.value) warnings.push("bottom_up: customers_serviceable is larger than customers_total; check both numbers.");
if (b.obtainable_customers && b.obtainable_customers.value > b.customers_serviceable.value) warnings.push("bottom_up: obtainable_customers is larger than customers_serviceable; check both numbers.");
bu = {
tam: tam === null ? null : round2(tam),
sam: round2(sam),
som: som === null ? null : round2(som),
formulas: {
tam: tam === null ? "TAM not computed: customers_total not given." : `TAM = customers_total \u00d7 annual_revenue_per_customer = ${fmt(b.customers_total.value)} \u00d7 ${fmt(arpc)} = ${money(tam)}`,
sam: `SAM = customers_serviceable \u00d7 annual_revenue_per_customer = ${fmt(b.customers_serviceable.value)} \u00d7 ${fmt(arpc)} = ${money(sam)}`,
som: somFormula ?? "SOM not computed: obtainable_share or obtainable_customers not given.",
},
};
if (b.customers_total) addAssumption("bottom_up.customers_total", `There are ${fmt(b.customers_total.value)} potential customers in total.`, b.customers_total, "customer");
addAssumption("bottom_up.customers_serviceable", `${fmt(b.customers_serviceable.value)} customers are serviceable.`, b.customers_serviceable, "customer");
addAssumption("bottom_up.annual_revenue_per_customer", `Revenue per customer is ${money(arpc)} per ${period}.`, b.annual_revenue_per_customer, "pricing");
if (b.obtainable_customers) addAssumption("bottom_up.obtainable_customers", `The company can win ${fmt(b.obtainable_customers.value)} customers.`, b.obtainable_customers, "go_to_market");
if (b.obtainable_share) addAssumption("bottom_up.obtainable_share", `The company can win ${pct(b.obtainable_share.value)} of serviceable revenue.`, b.obtainable_share, "go_to_market");
}
for (const r of [td, bu]) if (r && r.som !== null && r.som > r.sam) warnings.push("SOM is larger than SAM; check the inputs.");
let comparison = null;
if (td && bu) {
comparison = {};
for (const k of ["tam", "sam", "som"]) {
const a = td[k];
const b = bu[k];
if (a === null || b === null) {
comparison[k] = { ratio: null, note: "Not computed by both approaches." };
continue;
}
const lo = Math.min(a, b);
const ratio = lo > 0 ? Math.round((Math.max(a, b) / lo) * 100) / 100 : null;
const note = ratio === null ? "One approach gives 0." : ratio > 3 ? `Top-down and bottom-up differ by ${ratio}\u00d7: revisit the assumptions behind the larger one.` : `Within ${ratio}\u00d7 of each other.`;
if (ratio !== null && ratio > 3) warnings.push(`${k.toUpperCase()}: ${note}`);
comparison[k] = { ratio, note };
}
}
const unsourced = assumptions.filter((a) => !a.evidence.length).length;
if (unsourced) warnings.push(`${unsourced} input(s) have no source_url; they are logged as unsupported assumptions.`);
const cellOf = (r, k) => (r ? (r[k] === null ? "n/a" : md(`${fmt(r[k])} ${cur}`)) : "\u2013");
const lines = [
`# Market size${input.market ? `: ${md(input.market)}` : ""}`,
"",
`_Computed only from the numbers you supplied (${md(cur)} per ${md(period)}). Nothing was looked up or estimated._`,
"",
"| | Top-down | Bottom-up |",
"| --- | --- | --- |",
...["tam", "sam", "som"].map((k) => `| ${k.toUpperCase()} | ${cellOf(td, k)} | ${cellOf(bu, k)} |`),
"",
"## Formulas",
"",
...(td ? Object.values(td.formulas).map((f) => `- Top-down: ${md(f)}`) : []),
...(bu ? Object.values(bu.formulas).map((f) => `- Bottom-up: ${md(f)}`) : []),
"",
logMarkdown(assumptions, "Inputs as assumptions"),
];
if (warnings.length) lines.push("", "## Warnings", "", ...warnings.map((w) => `- ${md(w)}`));
return { currency: cur, period, top_down: td, bottom_up: bu, comparison, assumptions, markdown: lines.join("\n") + "\n", warnings };
}