python implements analytic hierarchy process (AHP)

Related articles:
python implements correspondence analysis (CA)
python implements principal component analysis (PCA)
python implements probability density matching method

1 Introduction to Analytic Hierarchy Process

Analytic Hierarchy Process (AHP) is a systematic and hierarchical analysis method that combines qualitative and quantitative methods.

The analytic hierarchy process decomposes the problem into different components according to the nature of the problem and the overall goal to be achieved , and aggregates and combines the factors at different levels according to the interrelated influences and affiliations between the factors to form a multi-level analysis structure model, so that the problem ultimately boils down to the determination of the relative importance of the lowest level (plans, measures, etc. for decision-making) relative to the highest level (overall goal) or the arrangement of relative priorities .

2 steps of AHP

The steps are shown in the figure below:

1) Establish a hierarchical structure model

Layer complex problems into layers:

  • The first is the goal layer, which is the ultimate goal of the problem;
  • Divide the factors that affect decision-making into several categories as the standard layer;
  • The last layer is the solution layer, which lists various solutions.

The problem is layered according to the affiliation relationship or interrelationship between various factors to form a top-down layer-by-layer dominance relationship, that is, a hierarchical hierarchical structure, as shown in the following figure:

2) Construct a judgment matrix

The 1-9 scaling method is used to form a judgment matrix, and the method is pairwise comparison.

The pairwise comparison matrix represents the comparison of the relative importance of all factors in this layer with respect to a certain *factor (accuracy or target)* in the upper layer . Pairwise comparison of elements aij a_{ij} of the matrixaijIt represents the comparison result of the i-th factor relative to the j-th factor. This value is given using Santy's 1-9 scale method.

For example, in the tourism example above, in the tourism problem, the results of pairwise comparison of the impact of various factors in the second layer A on the target layer Z are as follows:

analogy a 14 = 3 a_{14}=3a14=3 means that the scenery factor is slightly more important than the residential factor in choosing a tourist destination.

3) Calculate stratified weights and check their consistency

a. Calculate hierarchical weights (hierarchical single sorting)

Single-level sorting: It is to normalize the importance weight of factors on one level relative to a factor on the upper level. This weight corresponds to the normalized value of the eigenvector of the largest eigenvalue of the judgment matrix.

For example, the pairwise comparison matrix of solution layer B1/B2/B3 to A1\A2\A3\A4\A5:

  • The weight of B1/B2/B3 to A1 is: [0.595, 0.276, 0.128]
  • The weight of B1/B2/B3 to A2 is: [0.082, 0.236, 0.682]
  • The weight of B1/B2/B3 to A3 is: [0.429, 0.429, 0.143]
  • The weight of B1/B2/B3 to A4 is: [0.634, 0.192, 0.174]
  • The weight of B1/B2/B3 to A5 is: [0.167, 0.167, 0.667]
  • The weight of A: [0.264, 0.476, 0.054, 0.099, 0.109]

b. Consistency test

A direct and inverse matrix that satisfies the following relationship can be called a consistent matrix:

A consistent matrix has the following properties:

  1. is the positive and inverse matrix
  2. Transposition is also a consistent array;
  3. If each row is proportional, the matrix will be ranked 1;
  4. The largest characteristic root (value) is λ=n, and the remaining n−1 characteristic roots are all equal to 0;
  5. Any column (row) is the eigenvector corresponding to the eigenroot n, AW=nW;

When the maximum eigenvalue of the judgment matrix λ=n, it is a consistent matrix, and when the judgment matrix is ​​inconsistent, there must be a maximum eigenvalue λ>n. The more λ is larger than n, the more serious the inconsistency of the judgment matrix, then its The corresponding standardized feature vector is less able to reflect the influence of factors. Therefore, it is necessary to conduct a consistency check on the matrix. The steps are as follows:

  • Calculate the consistency index CI = λ max − nn − 1 CI=\frac{\lambda_{max}-n}{n-1}CI=n1lmaxn. When CI is equal to 0, there is complete consistency; when it is close to 0, there is satisfactory consistency; the larger it is, the more serious the inconsistency is.
  • Find the average random consistency index RI corresponding to CI:

For the value of RI, randomly select numbers from [1-9] and [1-1/9] to form a forward and inverse matrix. In this form, you will get 500 random sample matrices, and then calculate the maximum characteristic root. The mean value, and define RI=(mean-n)/(n-1).

  • Calculate the agreement ratio: CR = CIRI CR=\frac{CI}{RI}CR=RICI
  • When CR<0.1, the matrix consistency is acceptable.

4) Overall hierarchical ordering and consistency test

Calculating the weight of the relative importance of all factors at a certain level to the highest level (total goal) is called the overall ranking of levels.

The overall ordering of the hierarchy also requires consistency testing, layer by layer from upper to lower levels. Let AAThe weight vectors of layer A are a 1 , a 2 , . . . a_1,a_2,...a1,a2,...,B层对A 1 A_1A1CI of the judgment matrix CIC Iwa RIRIR ICI 1 , RI 1 CI_1,RI_1CI1,RI1,B层对A 2 A_2A2CI of the judgment matrix CIC Iwa RIRICI_2 ,RI_2, ...CI2,RI2,... , then:

C R = a 1 C I 1 + a 2 C I 2 + . . . a 1 R I 1 + a 2 R I 2 + . . . CR=\frac{a1CI1+a2CI2+...}{a1RI1+a2RI2+...} CR=a 1 R I 1+a 2 R I 2+...a 1 C I 1+a 2 C I 2+...

At this point, the final decision is made based on the overall ranking of the lowest layer (decision-making layer).

3 Advantages and Disadvantages of Analytical Hierarchy Process

advantage:

  • Fully consider the influence of qualitative factors, strengthen the reliability of the selection basis, and reduce judgment errors.
  • The traditional method model is complex, but AHP uses the 1-9 scaling method to form a judgment matrix, which is simple and easy to understand.
  • Integrate expert experience and judgment into a hierarchical structure to comprehensively evaluate problems, making up for the shortcomings of traditional methods that focus on quantitative information and lack flexibility.

shortcoming:

  • When there are many factors, it is difficult to achieve consistency in the judgment matrix, and it is difficult to adjust.
  • Uncertainty factors cannot be handled well.
  • Experts have different evaluations of the importance of indicators, which is prone to personal subjective one-sidedness.

4 Python implementation

# 参考:https://blog.csdn.net/lwq_0/article/details/107296446
def Eigenvalues_Feature_vector(phalanx):
    # 计算判断矩阵的特征值和特征向量
    a, b = np.linalg.eig(phalanx)
    # print("特征值是\n", a)
    print("特征值实部:", a.real)  # 显示特征值实部
    max_eigenvalue = max(a.real)
    print("最大特征值:", max_eigenvalue)
    num_shape = phalanx.shape
    CI = (max_eigenvalue - num_shape[0]) / (num_shape[0] - 1)
    print("---->> CI=", CI)
    if num_shape[0] == 2:
        RI = 0
    elif num_shape[0] == 3:
        RI = 0.52
    else:
        RI = 0.89

    if RI == 0 and CI == 0:
        CR = 0
        print("---->> CR=", CR)
    else:
        CR = CI / RI
        print("---->> CR=", CR)
    if CR < 0.1:
        print("---->> 一致性比例可接受!")
    else:
        print("---->> 一致性检验不通过!")
    print("\n")
    # print("特征向量是\n", b)  # numpy的特征向量是竖方向的,numpy输出的特征向量是单位化后的向量
    print("特征向量实部:", b.real)
    return b

def Weight_vector(phalanx):
    # 计算判断矩阵的权向量
    num = 0
    b = Eigenvalues_Feature_vector(phalanx)
    for i in range(len(b.real)):
        num += b.real[i][0]
    print("\n")
    print("指标权重是:")
    weight_list = []
    for j in range(len(b.real)):
        weight_num = b.real[j][0] / num
        weight_list.append(weight_num)
    print(weight_list)
    print("\n")
    return np.array([weight_list])

reference:

https://zhuanlan.zhihu.com/p/38207837

https://blog.csdn.net/lwq_0/article/details/107296446

Research on the comprehensive evaluation index system of study tours based on the analytic hierarchy process-Jin Miao.

Guess you like

Origin blog.csdn.net/mengjizhiyou/article/details/127759264