In JavaScript, the standard deviation of an array can be calculated by finding the mean, variance, and then the square root of the variance.
- Mean is the average of all elements in the array.
- Variance is the average of the squared differences between each element and the mean.
- Standard deviation is the square root of the variance.
- reduce() can be used to calculate the sum of array elements.
- map() can be used to calculate the squared difference from the mean.
- A JavaScript statistics library can also be used to calculate standard deviation directly.
For a population of n values:
Mean:
Mean = Σx / nVariance:
Variance = Σ(x - Mean)² / nStandard Deviation:
Standard Deviation = āVarianceThe following approaches can be used to calculate the standard deviation of an array in JavaScript.
Approach 1: Using reduce() and map()
In this approach, first calculate the mean using reduce(). Then use map() to calculate the squared difference of each value from the mean. Finally, calculate the variance and its square root.
Working:
- Calculate the sum of all elements using reduce().
- Divide the sum by the array length to get the mean.
- Use map() to calculate (value - mean)² for every element.
- Calculate the average of these squared differences to get the variance.
- Use Math.sqrt() to calculate the standard deviation.
Syntax:
const mean = arr.reduce((sum, value) => sum + value, 0) / arr.length;
const variance = arr
.map(value => (value - mean) ** 2)
.reduce((sum, value) => sum + value, 0) / arr.length;
const standardDeviation = Math.sqrt(variance);
function standardDeviation(arr) {
const mean =
arr.reduce((sum, value) => sum + value, 0) /
arr.length;
const squaredDifferences = arr.map(
value => (value - mean) ** 2
);
const variance =
squaredDifferences.reduce(
(sum, value) => sum + value,
0
) / arr.length;
return Math.sqrt(variance);
}
console.log(standardDeviation([1, 2, 3, 4, 5]));
console.log(
standardDeviation([23, 4, 6, 457, 65, 7, 45, 8])
);
Output
1.4142135623730951 145.13565852332775
Note: This approach calculates the population standard deviation, where the variance is divided by n. For sample standard deviation, divide the variance by n - 1.
Approach 2: Using reduce() Without Creating an Intermediate Array
The standard deviation can also be calculated using reduce() to directly accumulate the squared differences. This avoids creating a separate array using map().
Working:
- Calculate the mean using reduce().
- Use reduce() again to calculate the sum of squared differences.
- Divide the sum by the number of elements to get the variance.
- Use Math.sqrt() to obtain the standard deviation.
function standardDeviation(arr) {
const mean =
arr.reduce((sum, value) => sum + value, 0) /
arr.length;
const squaredDifferenceSum = arr.reduce(
(sum, value) => sum + (value - mean) ** 2,
0
);
const variance =
squaredDifferenceSum / arr.length;
return Math.sqrt(variance);
}
const numbers = [1, 2, 3, 4, 5];
console.log(standardDeviation(numbers));
Output
1.4142135623730951
- The simple-statistics library is installed to provide statistical functions.
- The library is imported using require().
- An array of numbers is created as input data.
- The standardDeviation() function is called with the array to compute the result.
- The calculated standard deviation is printed to the console.
Approach 3: Using a Statistics Library
A statistics library can be used when multiple statistical calculations are required. For example, the simple-statistics library provides a standardDeviation() method.
Working:
- Install the simple-statistics package.
- Import the library into the JavaScript program.
- Pass the array to standardDeviation().
- The method returns the standard deviation
Installation:
npm install simple-statisticsconst ss = require("simple-statistics");
const numbers = [1, 2, 3, 4, 5];
const result = ss.standardDeviation(numbers);
console.log(result);
Output:
1.4142135623730951Note: Make sure to distinguish between population and sample standard deviation when using a statistics library, as the result depends on which definition the library method uses.