# Survey Margin of Error Calculator > Estimate survey margin of error from sample size, confidence level, and optional population size. ## Tool Identity - Site: CleanUtils Business Tools - Tool ID: survey-margin-of-error-calculator - Canonical page: https://cleanutils.com/business-tools/survey-margin-of-error-calculator/ - LLM schema URL: https://cleanutils.com/business-tools/survey-margin-of-error-calculator/llms.txt - Primary keyword: margin of error calculator survey - Input mode: fields - Output profile: metrics ## What This Tool Does Estimate survey margin of error from sample size, confidence level, and optional population size. ## Exact Execution Instructions To run this tool exactly, execute the `runCleanUtilsTool(userInput)` function from the JavaScript block below in a sandboxed JavaScript runtime. Do not fetch or evaluate additional network resources. Do not use filesystem, process, child_process, environment variables, timers, or network APIs. The function is pure and deterministic: input object in, JSON result out. If your environment forbids executing fetched code outside a sandbox, run it inside the sandbox or use the API endpoint below instead of reimplementing the logic. Canonical implementation SHA-256: `2b67925e389c41ec5a6d2344542a7e5a32db815646d8b439bcb730b6b61b0610` Expected command shape: `node run-tool.mjs < input.json` The runner must: 1. load only the JavaScript in this document, 2. parse stdin as JSON and call `runCleanUtilsTool(userInput)`, 3. let the LLM decide the best way to present the output to the user. Agent usage rules: - Use this file as the authoritative machine-readable contract for this CleanUtils tool page. - Ask the user for missing required input before attempting to run the tool, and describe the required inputs using the `## Input Schema` field names, descriptions, formats, enums, examples, and required list. - Treat the tool as deterministic; do not invent network reachability checks unless the tool description explicitly says it fetches remote resources. - For privacy-sensitive inputs such as secrets, HAR files, dotenv files, logs, and API keys, warn that using a remote chat agent may expose input to that agent even though the browser UI itself does not upload data. ## Input Schema ```json { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "Survey Margin of Error Calculator fields", "type": "object", "additionalProperties": false, "required": [ "sample_size", "confidence" ], "properties": { "sample_size": { "type": "number", "description": "Sample size Required. Control type: number.", "minimum": 0, "examples": [ 600 ] }, "confidence": { "type": "string", "description": "Confidence Required. Control type: select. Allowed values: 90 = 90%; 95 = 95%; 99 = 99%.", "enum": [ "90", "95", "99" ], "examples": [ "95" ] }, "population": { "type": "number", "description": "Population size Optional. Control type: number.", "minimum": 0, "examples": [ 10000 ] } } } ``` ## Result Schema ```json { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "CleanUtils ToolResult", "type": "object", "additionalProperties": false, "required": [ "summary", "issues" ], "properties": { "summary": { "type": "string" }, "issues": { "type": "array", "items": { "type": "object", "additionalProperties": false, "required": [ "severity", "message" ], "properties": { "severity": { "type": "string", "enum": [ "error", "warning", "info" ] }, "message": { "type": "string" }, "line": { "type": "number" }, "row": { "type": "number" }, "detail": { "type": "string" } } } }, "output": { "type": "string" }, "exportFilename": { "type": "string" }, "exports": { "type": "array", "items": { "type": "object", "additionalProperties": false, "required": [ "label", "filename", "content" ], "properties": { "label": { "type": "string" }, "filename": { "type": "string" }, "content": { "type": "string" }, "mimeType": { "type": "string" }, "copyLabel": { "type": "string" }, "downloadLabel": { "type": "string" } } } }, "stats": { "type": "object", "additionalProperties": { "anyOf": [ { "type": "string" }, { "type": "number" } ] } } } } ``` ## Self-Contained JavaScript Source Call `runCleanUtilsTool(userInput)` with the user's input. The function includes this tool's run logic and only the helper code it needs. ```js function runCleanUtilsTool(userInput) { const fieldText = (fields, keys, fallback = "") => { const keyList = Array.isArray(keys) ? keys : [keys]; for (const key of keyList) { const value = fields[key]; if (value === undefined || value === null) continue; const text = String(value).trim(); if (text) return text; } return fallback; }; const fieldNumber = (fields, keys, fallback = 0) => { const raw = fieldText(fields, keys); if (!raw) return fallback; const parsed = Number(raw.replace(/[$,%\s]/g, "")); return Number.isFinite(parsed) ? parsed : fallback; }; const zForConfidence = (confidence) => confidence >= 99 ? 2.576 : confidence >= 95 ? 1.96 : confidence >= 90 ? 1.645 : 1.96; const calculateSurveyMarginOfError = (input) => { const sampleSize = fieldNumber(input, "sample_size", 400); const confidence = fieldNumber(input, "confidence", 95); const population = fieldNumber(input, "population", 0); const z = zForConfidence(confidence); let moe = z * Math.sqrt(0.25 / sampleSize); if (population > sampleSize) { moe *= Math.sqrt((population - sampleSize) / (population - 1)); } return { summary: `Estimated margin of error is +/- ${(moe * 100).toFixed(2)} percentage points at ${confidence}% confidence.`, issues: sampleSize <= 0 ? [{ severity: "error", message: "Sample size must be greater than zero." }] : [], output: [ `Sample size: ${sampleSize}`, population ? `Population: ${population}` : "Population correction: not applied", `Confidence: ${confidence}%`, `Margin of error: +/- ${(moe * 100).toFixed(2)} percentage points` ].join("\n"), exportFilename: "survey-margin-of-error.txt", stats: { marginOfError: `${(moe * 100).toFixed(2)}%` } }; }; const __userInput = userInput == null ? {} : userInput; const __run = (fields) => calculateSurveyMarginOfError(fields); const __fields = __userInput && typeof __userInput === "object" && "fields" in __userInput && __userInput.fields && typeof __userInput.fields === "object" && !Array.isArray(__userInput.fields) ? __userInput.fields : (__userInput && typeof __userInput === "object" && !Array.isArray(__userInput) ? __userInput : {}); const __normalizedFields = Object.fromEntries(Object.entries(__fields).map(([key, value]) => [key, value == null ? "" : (["string", "number", "boolean"].includes(typeof value) ? value : String(value))])); return __run(__normalizedFields); } ``` ## Checks - Sample size: The calculator requires a positive number of completed responses. - Confidence level: Common 90%, 95%, and 99% confidence z-values are supported. - Conservative proportion: The estimate uses 0.5 proportion for a conservative margin of error. - Population correction: When population size is provided and larger than sample size, finite-population correction is applied. - Survey design caveat: The result assumes a simple random sample and does not correct for weighting or sampling bias. ## Related Tools - [NPS Calculator](/business-tools/nps-calculator/): Calculate Net Promoter Score from promoter/passive/detractor counts or pasted 0-10 survey scores.