Skip to content

Main content

Pearson Correlation Calculator Source Code

Browse the implementation files for Pearson Correlation Calculator.
tool.ts typescript
import { parseFiniteNumberList } from '../../parse/numeric-dataset/tool';
import { formatFiniteNumber } from '../../parse/number/tool';
import { calculatePearsonCorrelation as calculatePortablePearsonCorrelation } from '@tool.io/tools/statistics/pearson-correlation';

export const calculatePearsonCorrelation = (
	firstInput: unknown,
	secondInput: unknown
): string => {
	const first = parseFiniteNumberList(firstInput);
	const second = parseFiniteNumberList(secondInput);
	if (
		!first ||
		!second ||
		first.length !== second.length ||
		first.length < 2
	)
		return '';
	const result = calculatePortablePearsonCorrelation(first, second);
	return result === null ? '' : formatFiniteNumber(result);
};

export const Tool: TypeTool = {
	name: 'Pearson Correlation Calculator',
	slug: 'statistics/pearson-correlation',
	intro:
		'Calculate Pearson’s linear correlation coefficient for two paired numeric series.',
	tool: calculatePearsonCorrelation,
	i: [
		{
			example: '1, 2, 3, 4, 5',
			label: 'X Values',
			placeholder: 'First numeric series',
			render: 'textarea',
			type: 'text'
		},
		{
			example: '2, 4, 5, 8, 10',
			label: 'Y Values',
			placeholder: 'Second numeric series',
			render: 'textarea',
			type: 'text'
		}
	],
	o: [
		{
			example: '0.990147542976674',
			label: 'Pearson Correlation',
			placeholder: 'Correlation will appear here...',
			type: 'number'
		}
	],
	equation:
		'$$r=\\frac{\\sum(x_i-\\bar{x})(y_i-\\bar{y})}{\\sqrt{\\sum(x_i-\\bar{x})^2\\sum(y_i-\\bar{y})^2}}$$'
};
export default Tool;

Description

Browse the implementation files for Pearson Correlation Calculator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools