A和B之间的相似度计算函数
function SIM = SIMcalc(A,B)
%// Get joint unique events for A and B
unq_events = unique([A(:,1);B(:,1)]).'; %//'
%// Presence of events across joint unique events
event_tagA = bsxfun(@eq,A(:,1),unq_events);
event_tagB = bsxfun(@eq,B(:,1),unq_events);
%// Probabilities corresponding to each joint event
tagged_probA = sum(bsxfun(@times,A(:,2),event_tagA));
tagged_probB = sum(bsxfun(@times,B(:,2),event_tagB));
%// Set not-shared events as NaN
tagged_probA(~any(event_tagA))=nan;
tagged_probB(~any(event_tagB))=nan;
%// Get the similarity factors for each shared event. This is based on the
%// assumption that probabilities far apart must have a low shared
%// similarity factor. This factor would be later on used to scale the
%// individual probabilties for A and B.
sim_factor = 1-abs(tagged_probA-tagged_probB);
tagged_probA_sim_scaled = tagged_probA.*sim_factor;
tagged_probB_sim_scaled = tagged_probB.*sim_factor;
%// Get a concatenated matrix of scaled probabilities
tagged_probAB_sim_scaled = [tagged_probA_sim_scaled;tagged_probB_sim_scaled];
%// Get a hybrid array of probabilities based on the mean of probabilities
%// across A and B. Notice that for cases with identical probabilities, the
%// hybrid values would stay the same.
hybrid_probAB = mean(tagged_probAB_sim_scaled);
%// Get the sum of hybrid values. Notice that the sum would result in a
%// value of 1 when we have identical probabilities for identical events
SIM = nansum(hybrid_probAB);
return;
用于测试相似度计算的样本输入
%// Case 1 - First exammple from the question with D replacing B.
%// The SIM value must be 1 as mentioned in the question
disp('------------- Case 1 -----------------')
A= [1, 0.6; 5, 0.3; 4, 0.1]
B = [1,0.6; 5,0.3; 4,0.1]
SIM = SIMcalc(A,B)
%// Case 2 - Slight change to the first example with event 5 being
%// replaced by event 2 in B
%// The SIM value must be lesser than 1 as mentioned in the question
disp('------------- Case 2 -----------------')
A= [1, 0.6; 5, 0.3; 4, 0.1]
B = [1,0.6; 2,0.3; 4,0.1]
SIM = SIMcalc(A,B)
%// Case 3 - As presented in the comments by OP, that the SIM value must be 0
disp('------------- Case 3 -----------------')
A =[3,1;2,0;1,0]
B =[2,1;1,0;4,0]
SIM = SIMcalc(A,B)
%// Case 4 - As asked by me and replied by OP that SIM must be 1
disp('------------- Case 4 -----------------')
A =[3,1;2,0;1,0]
B =[3,1;2,0;1,0]
SIM = SIMcalc(A,B)
%// Case 5 - Random case added on my own.
%// As can be seen event 3 is common between A and B. Apart from event3,
%// only event 2 is common, but the probabilities arew far apart, so the
%// net SIM value must be slightly more than the identical probability of
%// event 3, i.e. slightly more than 0.55
disp('------------- Case 5 -----------------')
A =[3,0.55;2,0.95;1,0]
B =[3,0.55;2,0.05;4,0.4]
SIM = SIMcalc(A,B)
结果
------------- Case 1 -----------------
A =
1.0000 0.6000
5.0000 0.3000
4.0000 0.1000
B =
1.0000 0.6000
5.0000 0.3000
4.0000 0.1000
SIM =
1
------------- Case 2 -----------------
A =
1.0000 0.6000
5.0000 0.3000
4.0000 0.1000
B =
1.0000 0.6000
2.0000 0.3000
4.0000 0.1000
SIM =
0.7000
------------- Case 3 -----------------
A =
3 1
2 0
1 0
B =
2 1
1 0
4 0
SIM =
0
------------- Case 4 -----------------
A =
3 1
2 0
1 0
B =
3 1
2 0
1 0
SIM =
1
------------- Case 5 -----------------
A =
3.0000 0.5500
2.0000 0.9500
1.0000 0
B =
3.0000 0.5500
2.0000 0.0500
4.0000 0.4000
SIM =
0.6000
说明
让我们以case 5 来详细解释决定衡量A 和B 之间相似性的最终标量值的基本原理。建议运行此案例的代码并观察变量的值。
输入
A =
3.0000 0.5500
2.0000 0.9500
1.0000 0
B =
3.0000 0.5500
2.0000 0.0500
4.0000 0.4000
第 1 步
将A和B的事件对应的概率标记为NaNs。因此,我们将拥有tagged_probA 和tagged_probB,它们的值如下所示 -
Event 1 Event 2 Event 3 Event 4
0 0.95 0.55 NaN
NaN 0.05 0.55 0.4
第 2 步
计算概率之间的差,然后从1 中减去结果。因此,更接近1 的数字表示相似度。例如,在这个event 3 的示例中,我们将得到1 的结果。这构成了找到A 和B 之间相似性标准的基础,因为我们得到1 的相同概率和较小的值,因为概率在[0 1] 的范围内相距甚远。这存储到sim_factor -
sim_factor =
NaN 0.1000 1.0000 NaN
第 3 步
使用sim_factor 缩放A 和B 的标记概率。因此,我们根据A 和B 之间的相似性对标记概率进行了缩放。这些是——
tagged_probA_sim_scaled =
NaN 0.0950 0.5500 NaN
tagged_probB_sim_scaled =
NaN 0.0050 0.5500 NaN
第 4 步
由于最终值应该只是一个标量值,我们可以得到标记和缩放概率的平均值。结果值将具有与相同概率情况下的单个概率相同的值,如本例中的event 3。对于不相同的情况,它会根据A 和B 概率之间的差异来缩小概率。这是hybrid_probAB,如下图-
hybrid_probAB =
NaN 0.0500 0.5500 NaN
第 5 步
对来自hybrid_probAB 的非NaN 元素求和,得到最终的标量相似度值,对于这种特定情况,它小于1。对于具有相同概率的情况,它会给我们一个完美的1。
结束语
查看SIM 值,它们确实遵循预期趋势。因此,希望它适用于您的其他情况。要计算A 和其他数组之间的相似度值,请将它们作为输入运行函数。