層次分析法
一、核心
在層次分析法中,通過 算術平均法、幾何平均法、特征值法 計算指標權重,再通過 一致性檢驗 確保判斷矩陣邏輯合理,為多準則決策提供量化依據。
二、代碼
(一)一致性檢驗(判斷矩陣合理性)
import numpy as np# 1. 定義判斷矩陣
A = np.array([[1, 2, 3, 5], [1/2, 1, 1/2, 2], [1/3, 2, 1, 1/2], [1/5, 1/2, 1/2, 1]])# 2. 獲取矩陣階數(指標數量)
n = A.shape[0] # 3. 計算特征值與特征向量
eig_val, eig_vec = np.linalg.eig(A)
Max_eig = max(eig_val) # 提取最大特征值# 4. 計算一致性指標
CI = (Max_eig - n) / (n - 1) # 5. 平均隨機一致性指標(查表值)
RI = [0, 0.0001, 0.52, 0.89, 1.12, 1.26, 1.36, 1.41, 1.46, 1.49, 1.52, 1.54, 1.56, 1.58, 1.59]
CR = CI / RI[n-1] # 一致性比率# 6. 輸出結果與判斷
print(f'一致性指標CI={CI}')
print(f'一致性比例CR={CR}')
if CR < 0.1:print('CR<0.1,判斷矩陣一致,可繼續計算權重!')
else:print('CR≥0.1,矩陣需調整!')
- 關鍵邏輯:通過最大特征值與
n
的關系,量化矩陣一致性。CR<0.1
是判斷矩陣合理的標準。
(二)算術平均法求權重
import numpy as np# 1. 定義判斷矩陣
A = np.array([[1, 2, 3, 5], [1/2, 1, 1/2, 2], [1/3, 2, 1, 1/2], [1/5, 1/2, 1/2, 1]])# 2. 計算每列和(按列求和)
ASum = np.sum(A, axis=0) # 3. 列歸一化(矩陣元素÷對應列和)
Stand_A = A / ASum # 4. 按行求和(歸一化后行累加)
ASumr = np.sum(Stand_A, axis=1) # 5. 計算權重(行和÷指標數)
weights = ASumr / A.shape[0] print('算術平均法權重:', weights)
- 步驟解析:先歸一化消除量綱,再通過行和平均分配權重,計算簡單、直觀,適合初步權重分配。
(三)幾何平均法求權重
import numpy as np# 1. 定義判斷矩陣
A = np.array([[1, 2, 3, 5], [1/2, 1, 1/2, 2], [1/3, 2, 1, 1/2], [1/5, 1/2, 1/2, 1]])# 2. 按行求元素乘積
prod_A = np.prod(A, axis=1) # 3. 計算行乘積的 n 次根(n 是指標數)
prod_n_A = np.power(prod_A, 1/A.shape[0]) # 4. 歸一化(根值÷所有根值的和)
weights = prod_n_A / np.sum(prod_n_A) print('幾何平均法權重:', weights)
- 核心思想:通過行元素乘積的開方,平衡指標間的相對重要性,削弱極端值影響
(四)特征值法求權重
import numpy as np# 1. 定義判斷矩陣
A = np.array([[1, 2, 3, 5], [1/2, 1, 1/2, 2], [1/3, 2, 1, 1/2], [1/5, 1/2, 1/2, 1]])# 2. 計算特征值與特征向量
eig_values, eig_vectors = np.linalg.eig(A) # 3. 找到最大特征值的索引
max_index = np.argmax(eig_values) # 4. 提取對應特征向量并歸一化
max_vector = eig_vectors[:, max_index]
weights = max_vector / np.sum(max_vector) print('特征值法權重:', weights)
- 理論依據:一致矩陣的特征向量對應權重,通過最大特征值對應的特征向量計算