Bookmark this: tool.io/statistics/confidence-interval/source-code
Z Confidence Interval Calculator Source Code
Browse the implementation files for Z Confidence Interval Calculator.tool.ts typescript
// Tool: Z Confidence Interval Calculator
//
// Calculates a two-sided z confidence interval for a population mean using a
// known population standard deviation. Returns lower,upper with four decimals.
const Z_SCORES: Record<number, number> = {
80: 1.2815515655446004,
85: 1.4395314709384563,
90: 1.6448536269514722,
95: 1.959963984540054,
98: 2.3263478740408408,
99: 2.5758293035489004,
};
const parseFiniteNumber = (input: unknown): number | null => {
if (
input === undefined ||
input === null ||
(typeof input === 'string' && input.trim() === '')
) {
return null;
}
if (typeof input !== 'string' && typeof input !== 'number') return null;
const value = Number(input);
return Number.isFinite(value) ? value : null;
};
export const zConfidenceInterval = (
sampleSizeInput: unknown,
meanInput: unknown,
populationStandardDeviationInput: unknown,
confidenceLevelInput: unknown,
): string => {
const sampleSize = parseFiniteNumber(sampleSizeInput);
const mean = parseFiniteNumber(meanInput);
const populationStandardDeviation = parseFiniteNumber(
populationStandardDeviationInput,
);
const confidenceLevel = parseFiniteNumber(confidenceLevelInput);
if (
sampleSize === null ||
mean === null ||
populationStandardDeviation === null ||
confidenceLevel === null ||
!Number.isSafeInteger(sampleSize) ||
sampleSize < 1 ||
populationStandardDeviation < 0
) {
return '';
}
const zScore = Z_SCORES[confidenceLevel];
if (zScore === undefined) return '';
const margin =
(zScore * populationStandardDeviation) / Math.sqrt(sampleSize);
const lower = mean - margin;
const upper = mean + margin;
return Number.isFinite(lower) && Number.isFinite(upper)
? `${lower.toFixed(4)},${upper.toFixed(4)}`
: '';
};
export const Tool: TypeTool = {
name: 'Z Confidence Interval Calculator',
slug: 'statistics/confidence-interval',
intro: 'Calculate a two-sided z confidence interval for a population mean.',
tool: zConfidenceInterval,
i: [
{
example: '30',
help: 'Enter a positive whole-number sample size.',
label: 'Sample Size',
placeholder: 'Sample size',
type: 'number',
},
{
example: '5.4',
help: 'Enter the sample mean.',
label: 'Sample Mean',
placeholder: 'Sample mean',
type: 'number',
},
{
example: '1.2',
help: 'Enter the known population standard deviation.',
label: 'Population Standard Deviation',
placeholder: 'Population standard deviation',
type: 'number',
},
{
label: 'Confidence Level',
placeholder: 'Select a confidence level',
type: 'select',
options: [
{ value: '80', text: '80%' },
{ value: '85', text: '85%' },
{ value: '90', text: '90%' },
{ value: '95', text: '95%', selected: true },
{ value: '98', text: '98%' },
{ value: '99', text: '99%' },
],
},
],
o: [
{
example: '4.9706,5.8294',
help: 'Lower and upper confidence bounds.',
label: 'Confidence Interval',
placeholder: 'Confidence interval will appear here...',
type: 'text',
render: 'textarea',
},
],
equation:
'$$CI=\\bar{x}\\pm z_{\\alpha/2}\\frac{\\sigma}{\\sqrt{n}}$$',
};
export default Tool;
Description
Browse the implementation files for Z Confidence Interval Calculator.