pymcdm.weights

pymcdm.weights.angle_weights(matrix, *args, **kwargs)

Calculate weights for given matrix using angle method.

Parameters:

matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.cilos_weights(matrix, types, *args, **kwargs)

Calculate weights for given matrix using CILOS method.

Parameters:
  • matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

  • types (ndarray) – Array with definitions of criteria types: 1 if criteria is profit and -1 if criteria is cost for each criteria in matrix.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.critic_weights(matrix, *args, **kwargs)

Calculate weights for given matrix using CRITIC method.

Parameters:

matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.entropy_weights(matrix, *args, **kwargs)

Calculate weights for given matrix using entropy method.

Parameters:

matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.equal_weights(matrix, *args, **kwargs)

Calculate equal weights for given matrix.

Parameters:

matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.gini_weights(matrix, *args, **kwargs)

Calculate weights for given matrix using gini method.

Parameters:

matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.idocriw_weights(matrix, types, *args, **kwargs)

Calculate weights for given matrix using IDOCRIW method.

Parameters:
  • matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

  • types (ndarray) – Array with definitions of criteria types: 1 if criteria is profit and -1 if criteria is cost for each criteria in matrix.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.merec_weights(matrix, types, *args, **kwargs)

Calculate weights for given matrix using MEREC method.

Parameters:
  • matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

  • types (ndarray) – Array with definitions of criteria types: 1 if criteria is profit and -1 if criteria is cost for each criteria in matrix.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.standard_deviation_weights(matrix, *args, **kwargs)

Calculate weights for given matrix using std method.

Parameters:

matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.variance_weights(matrix, *args, **kwargs)

Calculate weights for given matrix using std method.

Parameters:

matrix (ndarray) – Decision matrix / alternatives data. Alternatives are in rows and Criteria are in columns.

Returns:

Vector of weights.

Return type:

ndarray

pymcdm.weights.subjective

class pymcdm.weights.subjective.AHP(ranking: ndarray | list | tuple = None, scoring: ndarray | list | tuple = None, object_names: list[str] = None, matrix: ndarray | list | tuple = None, filename: str = None)

Bases: PairwiseWeightsBase

A subclass of PairwiseWeightsBase implementing the AHP (Analytic Hierarchy Process) method [1].

RI values for determination of the consistency are taken from [2].

The AHP class computes weights for pairwise comparisons based on rankings or user-provided input.

Parameters:
  • ranking (np.ndarray | list | tuple, optional) – Array representing the ranking of objects. Only one of ranking, scoring, object_names, matrix, or filename must be provided.

  • scoring (np.ndarray | list | tuple, optional) – Array representing the scoring of objects.

  • object_names (list of str, optional) – List of names corresponding to the objects being compared. This triggers manual pairwise comparison.

  • matrix (np.ndarray | list | tuple, optional) – Predefined pairwise comparison matrix.

  • filename (str, optional) – Path to a CSV file containing a pairwise comparison matrix.

Examples

>>> from pymcdm.weights.subjective import AHP
>>> ahp = AHP(ranking=[1, 2, 4, 3]) # Identify weights for four criteria based on ranking
>>> weights = ahp()
[0.48144852 0.21998235 0.13073309 0.16783605]
>>> ahp = AHP(object_names=['Price', 'Mileage', 'HP', 'Year']) # Identify weights based on manual comparisons
>>> weights = ahp()
>>> print(weights)
[0.52407767 0.26268171 0.03742414 0.17581648]
>>> print(ahp.get_cr())  # To calculate CR use get_cr() method
0.5021601824872032

References

_answer_mapper(ans: float) float

Maps a numerical answer value to its corresponding inverse value.

In case of AHP the mapping is as follows: 1 -> 1 2 -> 1/2 1/2 -> 2 and so on.

Parameters:

ans (float) – The numerical value to map.

Returns:

The inverse value of the input, calculated as 1 / ans.

Return type:

float

_compare_ranking(i: int, j: int) float

Compares two objects based on their ranking values.

This function takes into account differences between values in the ranking/scoring. If one criterion A has value 9 and criterion B 1 it means that B is nine times better than A. Therefore result of the comparison will be A and B will be 1/9. In case of A (1) and B (9) the result will be 9, because B is nine times worse than A.

The differences are limited to 9, to fit in the AHP dictionary.

Parameters:
  • i (int) – Index of the first object in the ranking.

  • j (int) – Index of the second object in the ranking.

Returns:

The result of the comparison.

Return type:

float

_matrix_to_weights() ndarray

Converts the pairwise comparison matrix into weights.

Returns:

The normalized weights derived from the pairwise comparison matrix.

Return type:

np.ndarray

static _question(a: str, b: str) str

Generates a question string for comparing two objects.

The question prompts the user to compare the importance of two objects and choose one of the options for two criteria a anb b.

Parameters:
  • a (str) – The name of the first object.

  • b (str) – The name of the second object.

Returns:

A formatted question string prompting the user to compare the two objects.

Return type:

str

check_cr(cr_threshold: float = 0.1)

Calculate Consistency Ratio (CR) coefficient based on the created pairwise comparison matrix and return True if calculated CR is lower than cr_threshold parameter (default is 0.1), or False if bigger, indicating if the created matrix should be considered consistent.

Parameters:

cr_threshold (float, optional) – CR threshold which determines if matrix with given CR is consistent (calculated CR is lower than threshold) or not (calculated CR is larger than threshold). Default is 0.1.

Raises:

ValueError – If matrix is not existed (model is not identified) or if matrix is too big.

get_cr()

Calculate Consistency Ratio (CR) coefficient based on the created pairwise comparison matrix.

Raises:

ValueError – If matrix is not existed (model is not identified) or if matrix is too big.

class pymcdm.weights.subjective.RANCOM(ranking: ndarray | list | tuple = None, scoring: ndarray | list | tuple = None, object_names: list[str] = None, matrix: ndarray | list | tuple = None, filename: str = None)

Bases: PairwiseWeightsBase

A subclass of PairwiseWeightsBase implementing the RANCOM (RANking COMparison) method [3].

The RANCOM class computes weights for pairwise comparisons based on rankings or user-provided input.

Parameters:
  • ranking (np.ndarray | list | tuple, optional) – Array representing the ranking of objects. Only one of ranking, scoring, object_names, matrix, or filename must be provided.

  • scoring (np.ndarray | list | tuple, optional) – Array representing the scoring of objects.

  • object_names (list of str, optional) – List of names corresponding to the objects being compared. This triggers manual pairwise comparison.

  • matrix (np.ndarray | list | tuple, optional) – Predefined pairwise comparison matrix.

  • filename (str, optional) – Path to a CSV file containing a pairwise comparison matrix.

Examples

>>> from pymcdm.weights.subjective import RANCOM
>>> rancom = RANCOM(ranking=[1, 2, 4, 3]) # Identify weights for four criteria based on ranking
>>> weights = rancom()
[0.4375 0.3125 0.0625 0.1875]
>>> rancom = RANCOM(object_names=['Price', 'Mileage', 'HP', 'Year']) # Identify weights based on manual comparisons
>>> weights = rancom()
[0.4375 0.3125 0.0625 0.1875]

References

_answer_mapper(ans: float) float

Maps a numerical answer value to its corresponding inverse value.

In case of RANCOM the mapping is as follows: 0 -> 1 0.5 -> 0.5 1 -> 0

Parameters:

ans (float) – The numerical value to map.

Returns:

The inverse value of the input, calculated as 1 - ans.

Return type:

float

_compare_ranking(i: int, j: int) float

Compares two objects based on their ranking values.

In the ranking, smaller values represent better options. The comparison returns: - 1 if the first object is ranked better than the second. - 0 if the second object is ranked better than the first. - 0.5 if the two objects are equally ranked.

Parameters:
  • i (int) – Index of the first object in the ranking.

  • j (int) – Index of the second object in the ranking.

Returns:

The result of the comparison: 1, 0.5, or 0.

Return type:

float

_matrix_to_weights() ndarray

Converts the pairwise comparison matrix into weights.

Returns:

The normalized weights derived from the pairwise comparison matrix.

Return type:

np.ndarray

static _question(a: str, b: str) str

Generates a question string for comparing two objects.

The question prompts the user to compare the importance of two objects and choose one of the following options: - 1: The first object is more important than the second. - 1/2: The two objects are equally important. - 0: The second object is more important than the first.

Parameters:
  • a (str) – The name of the first object.

  • b (str) – The name of the second object.

Returns:

A formatted question string prompting the user to compare the two objects.

Return type:

str

class pymcdm.weights.subjective.PairwiseWeightsBase(ranking: ndarray | list | tuple = None, scoring: ndarray | list | tuple = None, object_names: list[str] = None, matrix: ndarray | list | tuple = None, filename: str = None)

Bases: ABC

A base class for managing pairwise comparison weighting methods using different input formats.

This abstract base class supports the initialization, validation, and processing of pairwise comparison data using one of several input options: ranking, scoring, object names, pairwise comparison matrices, or a file. It is designed for extension in derived classes, which must override its abstract methods.

Parameters:
  • ranking (np.ndarray | list | tuple, optional) – Array representing the ranking of objects. Only one of ranking, scoring, object_names, matrix, or filename must be provided.

  • scoring (np.ndarray | list | tuple, optional) – Array representing the scoring of objects.

  • object_names (list of str, optional) – List of names corresponding to the objects being compared. This triggers manual pairwise comparison.

  • matrix (np.ndarray | list | tuple, optional) – Predefined pairwise comparison matrix.

  • filename (str, optional) – Path to a CSV file containing a pairwise comparison matrix.

Raises:

ValueError – If none or more than one of ranking, scoring, object_names, matrix, or filename are provided.

__call__() ndarray

Return weights if already calculated, or calculate them based on matrix (if matrix is present). If there are no matrix available, it will be calculated based on provided input and then weights will be calculated and returned.

Returns:

Weights computed based on input.

Return type:

np.ndarray

abstractmethod _answer_mapper(ans: float) float

Maps a user-provided answer value to its corresponding inverse or paired value.

For example, if we have values {0, 0.5, 1} as possible answers, it should map 0 to 1, 1 to 0 and so on.

Parameters:

ans (float) – The numerical value to map.

Returns:

The mapped numerical value (usually reversed value for matrix).

Return type:

float

Notes

This method must be implemented in subclasses.

_compare_pairwise(i: int, j: int) float

Performs a pairwise comparison between two objects based on user input.

Parameters:
  • i (int) – Index of the first object in the pair.

  • j (int) – Index of the second object in the pair.

Returns:

The user’s response, mapped to a numerical value.

Return type:

float

Raises:

KeyError – If the user’s input is not found in user_answer_map.

abstractmethod _compare_ranking(i: int, j: int) float

Compares two objects based on their positions in the ranking.

Parameters:
  • i (int) – Index of the first object in the ranking.

  • j (int) – Index of the second object in the ranking.

Returns:

The result of the comparison.

Return type:

float

Notes

This method must be implemented in subclasses.

_identify(objects: list, comparison_func: Callable) ndarray

Constructs a pairwise comparison matrix using a list of objects and a comparison function. Comparing function is either _compare_pariwise() or _compare_ranking(). This function will be applied to objects from objects.

Parameters:
  • objects (list) – A list of objects to compare.

  • comparison_func (Callable) – A function to perform pairwise comparisons between objects.

Returns:

The constructed pairwise comparison matrix.

Return type:

np.ndarray

abstractmethod _matrix_to_weights() ndarray

Converts the pairwise comparison matrix into weights.

Returns:

The calculated weights based on the pairwise comparison matrix.

Return type:

np.ndarray

Notes

This method must be implemented in subclasses.

abstractmethod static _question(a: str, b: str) str

Generates a question string for comparing two objects. This is used during manual identification process.

Parameters:
  • a (str) – The name of the first object.

  • b (str) – The name of the second object.

Returns:

A question string prompting the user to compare the two objects.

Return type:

str

Notes

This method must be implemented in subclasses.

to_csv(filename: str, allow_overwrite: bool = False)

Saves the pairwise comparison matrix to a CSV file.

Parameters:
  • filename (str) – The name of the file where the matrix will be saved.

  • allow_overwrite (bool, optional) – If True, allows overwriting of existing files. Default is False.

Raises:
  • ValueError – If the pairwise comparison matrix is not identified yet.

  • FileExistsError – If the specified file already exists and allow_overwrite is False.