Horje
How to Perform Statistical Calculations with math.js?

Statistical Calculations means analyzing numerical data to uncover patterns, trends, and relationships. Using math.js, a powerful mathematics library for JavaScript, eases this process by providing a wide range of built-in functions for common statistical operations.

In this article, we will learn to Perform Statistical Calculations with Math.js.

Initialize a Node.js Project

Before you can install math.js, you need to have a Node.js project. If you don’t have one, you can create it using the following command:

npm init -y
Screenshot-2024-07-23-173725

Output

Setting Up Math.js

Before working with complex numbers, you need to set up Math.js in your JavaScript environment. You can include math.js in your project by using a CDN or installing it via npm.

Using a CDN

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/10.0.0/math.js"></script>
<script>
// Your code here
</script>

Using npm

npm install mathjs

Then, include it in your JavaScript file:

const math = require('mathjs');

1. Using Direct Functions

In this approach, we are using math.js’s built-in statistical functions like mean, median, and std directly to calculate the mean, median, and standard deviation of the data array.

Example: The below example uses Direct Functions to Perform Statistical Calculations with math.js.

JavaScript
// script1.js

const math = require("mathjs");

const data = [ 10, 20, 30, 40, 50 ];
const mean = math.mean(data);
console.log(`Mean: ${mean}`);
const median = math.median(data);
console.log(`Median: ${median}`);
const stdDev = math.std(data);
console.log(`Standard Deviation: ${stdDev}`);

Output

Mean: 30
Median: 30
Standard Deviation: 15.811388300841896

2. Using Aggregated Methods

In this approach, we are using aggregated methods from math.js to manually calculate the mean, median, and standard deviation by implementing the statistical formulas directly in the code.

Example: The below example uses Aggregated Methods to Perform Statistical Calculations with math.js.

JavaScript
// script1.js

const math = require('mathjs');
const data = [10, 20, 30, 40, 50];
const sum = math.sum(data);
const count = data.length;
const mean = sum / count;
console.log(`Mean: ${mean}`);
const sortedData = math.sort(data);
const middle = Math.floor(count / 2);
const median = count % 2 === 0 ? (sortedData[middle - 1] + sortedData[middle]) / 2 : sortedData[middle];
console.log(`Median: ${median}`);
const variance = math.sum(data.map(value => math.pow(value - mean, 2))) / (count - 1);
const stdDev = math.sqrt(variance);
console.log(`Standard Deviation: ${stdDev}`);

Note: Run node script.js command, to see output.

Output

Mean: 30
Median: 30
Standard Deviation: 15.811388300841896

3. Using Custom Functions

In this approach, we are defining custom functions for calculating the mean, median, and standard deviation by using basic operations and methods from math.js to perform the necessary calculations.

Example: The below example uses Custom Functions to Perform Statistical Calculations with math.js.

JavaScript
// script1.js
const math = require("mathjs");
const data = [ 10, 20, 30, 40, 50 ];
function calculateMean(array)
{
    return math.sum(array) / array.length;
}
function calculateMedian(array)
{
    const sortedArray = math.sort(array);
    const count = sortedArray.length;
    const middle = Math.floor(count / 2);
    return count % 2 === 0 ? (sortedArray[middle - 1]
                              + sortedArray[middle])
                                 / 2
                           : sortedArray[middle];
}
function calculateStdDev(array)
{
    const mean = calculateMean(array);
    const variance
        = math.sum(
              array.map(value => math.pow(value - mean, 2)))
          / (array.length - 1);
    return math.sqrt(variance);
}
const mean = calculateMean(data);
const median = calculateMedian(data);
const stdDev = calculateStdDev(data);
console.log(`Mean: ${mean}`);
console.log(`Median: ${median}`);
console.log(`Standard Deviation: ${stdDev}`);

Note: Run node script.js command, to see output.

Output

Mean: 30
Median: 30
Standard Deviation: 15.811388300841896



Reffered: https://www.geeksforgeeks.org


Web Technologies

Related
How to Install and Set Up math.js? How to Install and Set Up math.js?
How to Perform Basic Arithmetic Operations with math.js? How to Perform Basic Arithmetic Operations with math.js?
How to Add Meta Tags in WordPress? How to Add Meta Tags in WordPress?
How To Write Good API Documentation? How To Write Good API Documentation?
How to Evaluate Expressions with math.js? How to Evaluate Expressions with math.js?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19