npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ahp-calc

v1.2.7

Published

AHP (Analytical Hierarchy Process) is a decision-making method that helps break down complex problems into a hierarchy of simpler comparisons. It uses pairwise comparisons and mathematical calculations to rank alternatives based on criteria.

Readme

AHP-CALC

License: MIT NPM Version Downloads

A TypeScript module implementing the Analytical Hierarchy Process (AHP) for decision support systems.
It supports pairwise comparison matrices, calculates priority weights, and evaluates consistency.
Ideal for multi-criteria decision analysis (MCDA).

⚠️ Note: This library does not support sub-criteria (yet).


📋 Requirements

This library requires:

  • A pairwise comparison matrix for the criteria.
  • A pairwise comparison matrix for the alternatives with respect to each criterion.

📦 Installation

npm install ahp-calc

Documentation

Documentation

Main Feature

  • Support for Pairwise Comparison Matrices: The library allows you to create and process pairwise comparison matrices for both criteria and alternatives.

  • Matrix Normalization: Supports normalization of both criteria and alternatives matrices to calculate relative priorities.

  • Weight Calculation (bobot priority): It calculates local priority weights (eigenvectors) for criteria and alternatives using the normalized matrices, and it can compute the global weights across all levels.

  • Consistency Evaluation: Includes functionality for consistency checking using λmax (Lamda Max), Consistency Index (CI), and Consistency Ratio (CR) to evaluate whether the matrix comparisons are consistent.

  • Converts Nested String Matrices to Numeric Matrices: The library supports converting string-based matrices (nested) into numeric matrices, making it easier to process data in AHP.

  • Priority Weights Calculation for Alternatives: For multi-alternative problems, the library supports calculating priority weights for alternatives against each criterion.

  • Consistency Check for Alternatives: The library can evaluate the consistency of matrices for alternatives as well, using the same λmax, CI, and CR methods.

No Support for Sub-criteria: ⚠️ Currently, the library does not support sub-criteria for further hierarchical decision-making.

Example Usage

you can start directly using function to calculate the pairwaise

for example to calculate Criteria Pairwise Comparison Matrices:

import { calculcateCritMatrix } from "ahp-calc";

const critMatriks: string[][] = [
  ["1", "3", "5", "7", "7"],
  ["1/3", "1", "3", "5", "5"],
  ["1/5", "1/3", "1", "3", "3"],
  ["1/7", "1/5", "1/3", "1", "2"],
  ["1/7", "1/5", "1/3", "1/2", "1"],
];

const {
  normalizedMatrix,
  CI,
  CR,
  RI,
  originalMatrix,
  konsistensi,
  lamdaMax,
  sumCrit,
  n,
  weightsCriteria,
} = calculcateCritMatrix(critMatriks);
console.log({
  sumCrit,
  normalizedMatrix,
  CI,
  CR,
  RI,
  originalMatrix,
  lamdaMax,
  konsistensi,
  n,
  weightsCriteria,
});

for example to calculate alternatif Pairwise Comparison Matrices and if you just wan to take normalize you can do like this:

import { calculateAltMatrix } from "ahp-calc";
const altMatrix: string[][][] = [
  [
    ["1", "3", "2", "1/3"],
    ["1/3", "1", "1/3", "1/5"],
    ["1/2", "3", "1", "1/3"],
    ["3", "5", "3", "1"],
  ],
  [
    ["1", "3", "5", "2"],
    ["1/3", "1", "3", "1/3"],
    ["1/5", "1/3", "1", "1/5"],
    ["1/2", "3", "5", "1"],
  ],
  [
    ["1", "1/3", "1/5", "1/5"],
    ["3", "1", "1/3", "1/3"],
    ["5", "3", "1", "2"],
    ["5", "3", "1/2", "1"],
  ],
  [
    ["1", "1/3", "2", "1/3"],
    ["3", "1", "3", "2"],
    ["1/2", "1/3", "1", "1/3"],
    ["3", "1/2", "3", "1"],
  ],
];

const { normalized } = calculateAltMatrix(altMatrix);

console.log(normalized);

Or you can start using the library by first creating an instance of the AHPCrit or AHPAlt class depending on whether you're working with criteria or alternatives.

import { AHPCrit } from "ahp-calc";

const critMatrix = [
  [
    [1.0, 3.0, 0.2],
    [0.3333, 1.0, 0.1429],
    [5.0, 7.0, 1.0],
  ],
];

const weights = AHPCrit.calculateCriteriaWeight(critMatrix);
console.log(weights);

To count each column in all matriks for alternatives:

import { AHPAlt } from "ahp-calc";

const originalMatrix = [
  [
    [1.0, 3.0, 0.5],
    [0.3333, 1.0, 0.1429],
    [2.0, 7.0, 1.0],
  ],
  [
    [1.0, 3.0, 0.5],
    [0.3333, 1.0, 0.1429],
    [2.0, 7.0, 1.0],
  ],
  [
    [1.0, 3.0, 0.5],
    [0.3333, 1.0, 0.1429],
    [2.0, 7.0, 1.0],
  ],
];

const sumAlt = AHPAlt.countTotalAlterEachColumn(originalMatrix);
console.log(sumAlt);

Ranks alternatif

const { weightsCriteria } = calculcateCritMatrix(critMatriks);
const { weightAlt } = calculateAltMatrix(altMatrix);
const res = calculateCompositeWeights(weightAlt, weightsCriteria);
console.log(res);

📊 Example AHP Calculation Output

This is an example output from running a criteria comparison using the AHP (Analytic Hierarchy Process) method:

🔢 Input Matrix (Pairwise Comparisons)

| 0 | 1 | 2 | 3 | 4 | | ----- | ----- | ----- | --- | --- | | 1 | 3 | 5 | 7 | 7 | | 0.333 | 1 | 3 | 5 | 5 | | 0.2 | 0.333 | 1 | 3 | 3 | | 0.143 | 0.2 | 0.333 | 1 | 2 | | 0.143 | 0.2 | 0.333 | 0.5 | 1 |

📉 Normalized Matrix

| 0 | 1 | 2 | 3 | 4 | | ------ | ------ | ------ | ------ | ------ | | 0.5498 | 0.6338 | 0.5173 | 0.4242 | 0.3889 | | 0.1831 | 0.2113 | 0.3104 | 0.3030 | 0.2778 | | 0.1100 | 0.0704 | 0.1035 | 0.1818 | 0.1667 | | 0.0786 | 0.0423 | 0.0345 | 0.0606 | 0.1111 | | 0.0786 | 0.0423 | 0.0345 | 0.0303 | 0.0556 |

⚖️ Calculated Weights

These represent the relative importance of each criterion:

| Weight | | ------ | | 0.4535 | | 0.2826 | | 0.1486 | | 0.0725 | | 0.0429 |

📈 Summary

  • Column Totals (for normalization): [1.819, 4.733, 9.666, 16.5, 18]
  • λ max: 5.277
  • Consistency Index (CI): 0.069
  • Consistency Ratio (CR): 0.062
  • Random Index (RI): 1.12
  • Consistency Check: Matrix is consistent (CR ≤ 0.1)
  • Number of criteria: 5

AHP Alternative Calculation Results

Matrix Order (n):

[ 4, 4, 4, 4, 4 ]

Original Matrices:

  • Matrix [0]:

| (index) | 0 | 1 | 2 | 3 | | ------- | ----- | --- | ----- | ----- | | 0 | 1 | 3 | 2 | 0.333 | | 1 | 0.333 | 1 | 0.333 | 0.2 | | 2 | 0.5 | 3 | 1 | 0.333 | | 3 | 3 | 5 | 3 | 1 |

  • Matrix [1]:

| (index) | 0 | 1 | 2 | 3 | | ------- | ----- | ----- | --- | ----- | | 0 | 1 | 3 | 5 | 2 | | 1 | 0.333 | 1 | 3 | 0.333 | | 2 | 0.2 | 0.333 | 1 | 0.2 | | 3 | 0.5 | 3 | 5 | 1 |

  • Matrix [2]:

| (index) | 0 | 1 | 2 | 3 | | ------- | --- | ----- | ----- | ----- | | 0 | 1 | 0.333 | 0.2 | 0.2 | | 1 | 3 | 1 | 0.333 | 0.333 | | 2 | 5 | 3 | 1 | 2 | | 3 | 5 | 3 | 0.5 | 1 |

  • Matrix [3]:

| (index) | 0 | 1 | 2 | 3 | | ------- | --- | ----- | --- | ----- | | 0 | 1 | 0.333 | 2 | 0.333 | | 1 | 3 | 1 | 3 | 2 | | 2 | 0.5 | 0.333 | 1 | 0.333 | | 3 | 3 | 0.5 | 3 | 1 |

  • Matrix [4]:

| (index) | 0 | 1 | 2 | 3 | | ------- | ----- | --- | --- | --- | | 0 | 1 | 3 | 3 | 3 | | 1 | 0.333 | 1 | 2 | 2 | | 2 | 0.333 | 0.5 | 1 | 2 | | 3 | 0.333 | 0.5 | 0.5 | 1 |

Normalized Matrices:

  • Matrix [0]:

| (index) | 0 | 1 | 2 | 3 | | ------- | ----- | ----- | ----- | ----- | | 0 | 0.207 | 0.25 | 0.316 | 0.178 | | 1 | 0.069 | 0.083 | 0.053 | 0.107 | | 2 | 0.103 | 0.25 | 0.158 | 0.178 | | 3 | 0.621 | 0.417 | 0.474 | 0.536 |

  • Matrix [1]:

| (index) | 0 | 1 | 2 | 3 | | ------- | ----- | ----- | ----- | ----- | | 0 | 0.492 | 0.409 | 0.357 | 0.566 | | 1 | 0.164 | 0.136 | 0.214 | 0.094 | | 2 | 0.098 | 0.045 | 0.071 | 0.057 | | 3 | 0.246 | 0.409 | 0.357 | 0.283 |

  • Matrix [2]:

| (index) | 0 | 1 | 2 | 3 | | ------- | ----- | ----- | ----- | ----- | | 0 | 0.071 | 0.045 | 0.098 | 0.057 | | 1 | 0.214 | 0.136 | 0.164 | 0.094 | | 2 | 0.357 | 0.409 | 0.492 | 0.566 | | 3 | 0.357 | 0.409 | 0.246 | 0.283 |

  • Matrix [3]:

| (index) | 0 | 1 | 2 | 3 | | ------- | ----- | ----- | ----- | ----- | | 0 | 0.133 | 0.154 | 0.222 | 0.091 | | 1 | 0.4 | 0.462 | 0.333 | 0.546 | | 2 | 0.067 | 0.154 | 0.111 | 0.091 | | 3 | 0.4 | 0.231 | 0.333 | 0.273 |

  • Matrix [4]:

| (index) | 0 | 1 | 2 | 3 | | ------- | ----- | --- | ----- | ----- | | 0 | 0.5 | 0.6 | 0.462 | 0.375 | | 1 | 0.167 | 0.2 | 0.308 | 0.25 | | 2 | 0.167 | 0.1 | 0.154 | 0.25 | | 3 | 0.167 | 0.1 | 0.077 | 0.125 |

Column Totals for Each Alternative:

  • Column Total [0]: [ '4.833', '12.000', '6.333', '1.866' ]
  • Column Total [1]: [ '2.033', '7.333', '14.000', '3.533' ]
  • Column Total [2]: [ '14.000', '7.333', '2.033', '3.533' ]
  • Column Total [3]: [ '7.500', '2.166', '9.000', '3.666' ]
  • Column Total [4]: [ '1.999', '5.000', '6.500', '8.000' ]

Weights from Alternative Matrices:

  • Weight [0]: [ '0.253', '0.075', '0.193', '0.479' ]
  • Weight [1]: [ '0.409', '0.173', '0.064', '0.353' ]
  • Weight [2]: [ '0.064', '0.173', '0.409', '0.353' ]
  • Weight [3]: [ '0.164', '0.403', '0.097', '0.336' ]
  • Weight [4]: [ '0.465', '0.248', '0.178', '0.109' ]

Lambda Max:

[ '4.132', '4.148', '4.148', '4.152', '4.140' ]

Consistency Index (CI):

[ '0.044', '0.049', '0.049', '0.051', '0.047' ]

Random Index (RI): 0.9

Consistency Ratio (CR):

  • CR [0]: 0.049 → Consistent? ✅ Yes
  • CR [1]: 0.054 → Consistent? ✅ Yes
  • CR [2]: 0.054 → Consistent? ✅ Yes
  • CR [3]: 0.057 → Consistent? ✅ Yes
  • CR [4]: 0.052 → Consistent? ✅ Yes

Overall Consistency Status: ✅ All Consistent

CONTRIBUTING

please read our to start contribute Contributing Guidelines.