How to use a Library (e.g., simple-statistics) In Javascript

To calculate the standard deviation of an array of numbers in JavaScript using a library like simple-statistics, import the library, then call its standardDeviation function with the array as an argument.

Example: In this example we installs and uses the simple-statistics library to calculate the standard deviation of an array of numbers. It imports the library, computes the standard deviation, and logs the result.

JavaScript
// Install the library: npm install simple-statistics

// Import the library
const ss = require('simple-statistics');

// Example array of numbers
const numbers = [1, 2, 3, 4, 5];

// Calculate the standard deviation
const standardDeviation = ss.standardDeviation(numbers);

console.log(standardDeviation);

Output:

1.5811388300841898


How to get the standard deviation of an array of numbers using JavaScript ?

Given an array and the task is to calculate the standard deviation using JavaScript.

Example:

Input:  [1, 2, 3, 4, 5]
Output: 1.4142135623730951

Input: [23, 4, 6, 457, 65, 7, 45, 8]
Output: 145.13565852332775

Please refer to Mean, Variance, and Standard Deviation for details.

Mean is average of element. Where 0 <= i < n
Mean of arr[0..n-1] = ∑(arr[i]) / n
Variance is the sum of squared differences from the mean divided by a number of elements.
Variance = ∑(arr[i] – mean)2 / n
Standard Deviation is the square root of the variance.
Standard Deviation = variance ^ 1/2

Approach: To get the standard deviation of an array, first we calculate the mean and then the variance, and then the deviation. To calculate the mean we use Array.reduce() method and calculate the sum of all the array items and then divide the array by the length of the array.

To calculate the variance we use the map() method and mutate the array by assigning (value – mean) ^ 2 to every array item, and then we calculate the sum of the array, and then we divide the sum by the length of the array. To calculate the standard deviation we calculate the square root of the array.

Example: In this example, we will be calculating the standard deviation of the given array of elements.

Javascript
// Javascript program to calculate the 
// standard deviation of an array
function StandardDeviation(arr) {

    // Creating the mean with Array.reduce
    let mean = arr.reduce((acc, curr) => {
        return acc + curr
    }, 0) / arr.length;

    // Assigning (value - mean) ^ 2 to
    // every array item
    arr = arr.map((k) => {
        return (k - mean) ** 2
    });

    // Calculating the sum of updated array 
    let sum = arr.reduce((acc, curr) => acc + curr, 0);

    // Calculating the variance
    let variance = sum / arr.length

    // Returning the standard deviation
    return Math.sqrt(sum / arr.length)
}

console.log(StandardDeviation([1, 2, 3, 4, 5]))
console.log(StandardDeviation([23, 4, 6, 457, 65, 7, 45, 8]))

Output
1.4142135623730951
145.13565852332775

Similar Reads

Using a Library (e.g., simple-statistics)

To calculate the standard deviation of an array of numbers in JavaScript using a library like simple-statistics, import the library, then call its standardDeviation function with the array as an argument....