Modification of TOPSIS model based on entropy weight method

Last time we mentioned that using the analytic hierarchy process to determine the weight of indicators is too subjective. Using the entropy weight method can assign values more objectively. Let’s talk about the application of the entropy weight method.

From the definition, we can see two new terms, one is the degree of variation, and the other is the amount of information. The amount of information is as the name suggests, so what is the degree of variation? It can be simply understood as the degree of deviation of the data (i.e. variance or standard deviation).

Here we have introduced an important concept of the entropy weight method – information entropy. So, the greater the information entropy, the greater or the smaller the amount of information. We can read a highly praised answer on Zhihu.

We have already understood the basic concept of the entropy weight method, and then the calculation steps of the entropy weight method are given.

At this point, let’s discuss the entropy weight method again. Whether the entropy weight method can reduce the subjectivity of the analytic hierarchy process is still open to question. We can simply understand this method as the greater the variance, the greater the weight of the indicator. This is actually unscientific.

In fact, no matter what the evaluation index is, it is actually not objective because the parameters of the model are set by ourselves.

The entropy weight method does not require us to set the weight ourselves. The distribution of the sample data determines the weight, which may seem more objective. But to give a simple example, we have two indicators, X and Y, which are used to evaluate who is the best student in the class. According to common sense, we all think that X has a great influence on the evaluation, but for almost all students, X is 0, which means that the variance of Small, almost everyone’s ?

Finally, based on the code in the previous section, the matlab code implementation of the entropy weight method is given. .

%% Step 1: Copy the data to the workspace and name the matrix X
% (1) Right-click in the workspace, click New (Ctrl + N), and enter the variable name as X
% (2) Copy the data in Excel, right-click back to Matlab, and click Paste Excel data (Ctrl + Shift + V)
% (3) Close this window, click on the X variable, right-click and save as, save as a mat file (no need to copy and paste next time, just use the load command to load the data)
% (4) Note that the code and data must be placed in the same directory, and the current folder of Matlab must also be in this directory.
clear;clc
load data_water_quality.mat
%% Step 2: Determine whether forwarding is needed
[n,m]=size(X);
disp(['There are' num2str(n) 'evaluation objects,' num2str(m) 'evaluation indicators'])
Judge=input(['These' num2str(m) 'Whether the evaluation indicators need to be forwarded, please enter 1 if necessary. No need to enter 0: ']);

if Judge==1
    Position=input('Please enter the column where the indicator that needs to be forwarded is located. For example, columns 2, 3, and 6 need to be processed, then you need to enter [2,3,6]: ');
    disp('Please enter the indicator types of these columns that need to be processed (1: very small, 2: intermediate, 3: interval)')
    Type=input('For example: the 2nd column is extremely small, the 3rd column is interval type, and the 6th column is intermediate type, enter [1,3,2]: ');
    % Note that Position and Type are two row vectors of the same dimension.
    for i=1:size(Position,2) %Here we need to process these columns separately, so we need to know the total number of times to be processed, that is, the number of loops
        X(:,Position(i)) = Positivization(X(:,Position(i)),Type(i),Position(i));
    % Positivization is a function defined by ourselves. Its function is to perform forwarding. It receives a total of three parameters.
    % The first parameter is the column vector to be forwarded. X(:,Position(i)) X(:,n) means taking all the elements of the nth column.
    % The second parameter is the indicator type corresponding to this column (1: very small, 2: intermediate, 3: interval)
    % The third parameter tells the function which column in the original matrix we are processing
    % This function has a return value, which returns the forwarded indicator. We can directly assign it to the original column vector we want to process.
    end
    disp('Forwarded matrix X= ')
    disp(X)
end
%% Step 4: Let the user judge whether the weight needs to be increased
disp('Please enter whether you need to increase the weight vector, you need to enter 1, you do not need to enter 0')
Judge = input('Please enter whether you need to increase the weight: ');
if Judge == 1
    Judge = input('Please enter 1 to determine the weight using the entropy weight method, otherwise enter 0: ');
    if Judge == 1
        if sum(sum(Z<0)) > 0 %If there are negative numbers in the previously standardized Z matrix, re-standardize X
            disp('There are negative numbers in the original Z matrix obtained by normalization, so X needs to be re-normalized')
            for i = 1:n
                for j = 1:m
                    Z(i,j) = [X(i,j) - min(X(:,j))] / [max(X(:,j)) - min(X(:,j))];
                end
            end
            disp('X is re-normalized and the normalized matrix Z obtained is:')
            disp(Z)
        end
        weight = Entropy_Method(Z);
        disp('The weight determined by the entropy weight method is: ')
        disp(weight)
    else
        disp(['If you have 3 indicators, you need to enter 3 weights, for example, they are 0.25, 0.25, 0.5, then you need to enter [0.25, 0.25, 0.5]']);
        weigh = input(['You need to enter' num2str(m) ' weights.' 'Please enter these ' num2str(m) ' weights in the form of row vectors:' ]);
        OK = 0; % is used to determine whether the user's input format is correct
        while OK == 0
            if abs(sum(weigh) - 1)<0.000001 & amp; & amp; size(weigh,1) == 1 & amp; & amp; size(weigh,2) == m % Note here that the operation of floating point numbers is not accurate
                OK = 1;
            else
                weigh = input('You entered something wrong, please re-enter the weight row vector:');
            end
        end
    end
else
    weigh = ones(1,m) ./ m; %If no weighting is required, the default weights will be the same, that is, they will all be 1/m
end

%% Step 3: Standardize the forwarded matrix
Z=X ./ repmat(sum(X .*X) .^0.5, n, 1);
disp('normalized matrix Z = ')
disp(Z)

%% Step 5: Calculate the distance from the maximum value and the minimum value, and calculate the score
D_P=sum([(Z - repmat(max(Z),n,1)) .^ 2] .* repmat(weight,n,1) ,2) .^ 0.5; %D + distance vector from the maximum value
D_N=sum([(Z - repmat(min(Z),n,1)) .^ 2] .* repmat(weight,n,1) ,2) .^ 0.5; %D-distance vector from the minimum value
S = D_N ./ (D_P + D_N); % unnormalized score
disp('The final score is:')
stand_S = S / sum(S);
[sorted_S,index] = sort(stand_S ,'descend');

% sort(A) If A is a vector, whether it is a column or row vector, A will be sorted in ascending order by default. sort(A) is the default ascending order, and sort(A,'descend') is the descending order.
% sort(A) If A is a matrix, the columns of A will be sorted in ascending order by default.
% sort(A,dim)
% Equivalent to sort(A) when dim=1
% When dim=2, it means to sort the elements of each row in A in ascending order.
% A = [2,1,3,8]
% To sort one-dimensional vectors in Matlab, use the sort function: sort(A). The sorting is performed in ascending order, where A is the vector to be sorted;
% If you want to retain the index before sorting, you can use [sA,index] = sort(A,'descend'). After sorting, sA is the sorted vector, and index is the index of A in the vector sA.
% sA = 8 3 2 1
% index = 4 3 1 2

In this code, we newly define two functions my_log() and Entropy_Method(), and also give the corresponding code.

% Redefine a mylog function to return 0 when the element in the input p is 0
function [lnp] = mylog(p)
n= length(p); % length of vector
lnp = zeros(n,1); %Initialize the final result
    for i = 1:n % starts looping
        if p(i) == 0 % if the i-th element is 0
            lnp(i) = 0; % Then the i-th result returned is also 0
        else
            lnp(i) = log(p(i));
        end
    end
end
function [W] = Entropy_Method(Z)
% Calculate the entropy weight corresponding to the samples with n samples and m indicators
% input
% Z: n*m matrix (needs to be normalized and standardized, and there are no negative numbers in the elements)
% output
% W: entropy weight, 1*m row vector
% Calculate entropy weight
    [n,m] = size(Z);
    D = zeros(1,m); %Initialize the row vector that holds the information utility value
    for i = 1:m
        x = Z(:,i); %Get the index of column i
        p = x / sum(x);
        % Note that p may be 0. When calculating ln(p)*p, Matlab will return NaN, so here we define a function ourselves.
        e = -sum(p .* mylog(p)) / log(n); %Calculate information entropy
        D(i) = 1 - e; % Calculate information utility value
    end
    W= D ./ sum(D); % Normalize the information utility value to obtain the weight
end