1. Software version
matlab2013b。
2. Theoretical knowledge of this algorithm
As can be seen from the above introduction, the data format of the paper is as follows:
Time1 |
Time2 |
Time3 |
Time4 |
Time5 |
Time6 |
...... |
|
USER1 |
Pos1(1) |
Pos1(2) |
Pos1(3) |
Pos1(4) |
Pos1(5) |
Pos1(6) |
...... |
USER2 |
Pos2(1) |
Pos2(2) |
Pos2(3) |
Pos2(4) |
Pos2(5) |
Pos2(6) |
...... |
USER3 |
Pos3(1) |
Pos3(2) |
Pos3(3) |
Pos3(4) |
Pos3(5) |
Pos3(6) |
...... |
...... |
...... |
...... |
...... |
...... |
...... |
...... |
...... |
USERS |
Posn(1) |
Posn (2) |
Posn (3) |
Posn (4) |
Posn (5) |
Posn (6) |
...... |
That is, by collecting the positions of multiple users at different times, and using algorithms to predict the following positions (the paper predicts the next 6 hours, here, we set the parameters to be configurable to predict the position at any time)
In the first paper, it is stated that:
The same is true for services, the same type of data (services are not covered in this paper)
That is, the input data type of the model we are using should be in the following format:
Time1 |
Time2 |
Time3 |
Time4 |
...... |
|
USER1 |
Pos1, Ser1 (1) |
Pos1, Ser1 (2) |
Pos1, Ser1 (3) |
Pos1, Ser1 (4) |
...... |
USER2 |
Pos2,Ser2 (1) |
Pos2,Ser2 (2) |
Pos2,Ser2 (3) |
Pos2,Ser2 (4) |
...... |
USER3 |
Pos3,Ser3 (1) |
Pos3,Ser3 (2) |
Pos3,Ser3 (3) |
Pos3,Ser3 (4) |
...... |
...... |
...... |
...... |
...... |
...... |
...... |
USERS |
Posn,Sern (1) |
Posn ,Sern (2) |
Posn,Sern (3) |
Posn,Sern (4) |
...... |
Therefore, in the request, you need the input of the model, that is, the data type of the above format.
The specific data are:
3. Core code
clc;
clear;
close all;
warning off;
pack;
addpath 'func\'
load locs_realitymining2.mat
N1 = 100;%为了防止出现连续状态不变得情况,这里N1设置大点
N2 = 6;
N = N1 + N2; %前N1个用于训练,后N2个用于预测
Times = 1000;
%通过多次循环,计算正确率
for Nu = 1:length(Locaiton_id3)
UNo = Nu;%用户标号
for tim = 1:Times
Dat = Locaiton_id3{1,UNo}(1+tim:N+tim);
State = unique(Dat);
%Counting the User Behavior Patterns
%Counting the User Behavior Patterns
Alpha = [];
maps = [];
MAP = [];
[Alpha,maps,MAP] = func_find_alpha_table(Dat,Locaiton_id3,State);
%Modeling the User Behaviors,the user’s behavior model can be built based on the resulting counting tables
%状态转移概率%释放概率
%对应算法步骤中计算STATE的步骤,计算moving or steady??
[seq,states] = func_cal_moving_steady(maps(1:N));%这里需要地址映射为自然数
[TRANS_EST,EMIS_EST] = hmmestimate(seq,states);
[r,c] = size(TRANS_EST);
for p1 = 1:r
for p2 = 1:c
if TRANS_EST(p1,p2) == 0
TRANS_EST(p1,p2) = eps;
end
if TRANS_EST(p1,p2) == 1
TRANS_EST(p1,p2) = 1-eps;
end
end
end
%通过vertiber算法计算概率
likelystates = hmmviterbi(seq,TRANS_EST,EMIS_EST);
Ps = length(find(likelystates==1))/N;
Pm = length(find(likelystates==2))/N;
%预测后面时刻的位置
likelihood_next_node = zeros(length(State),length(State));
for i = 1:length(State)
for j = 1:length(State)
if i == j
likelihood_next_node(i,j) = Ps*Alpha{i,j}(1);
else
likelihood_next_node(i,j) = Pm*Alpha{i,j}(1);
end
end
end
Plikelihood_next_node = zeros(length(State),N2);
for k = 1:N2
for i = 1:length(State)
Plikelihood_next_node(i,k) = likelihood_next_node(maps(N1+k-1),i)/(sum(likelihood_next_node(:,i))+eps);
end
[V,I] = max(Plikelihood_next_node(:,k));
for j = 1:size(MAP,1)
if I == MAP(j,2);
POS(k) = MAP(j,1);
end
end
end
NNN(:,tim) = (Dat(N1+1:N)==POS(1:N2)')';
end
for k = 1:N2
Precision1m(k,Nu) = sum(NNN(k,:))/Times;
end
end
save tmps2.mat Precision1m
clear all;
load locs_realitymining2.mat
N1 = 100;
N2 = 6;
N = N1 + N2;
Times = 200;
for i = 1:length(Locaiton_id3)
L(i) = length(Locaiton_id3{i});
end
Len = min(L);
%在我们划定的,都存在用户行为的时间段内进行对比
Therehold = 20;
Flag = 0;
ASS = cell(length(Locaiton_id3),length(Locaiton_id3));
for k1 = 1:length(Locaiton_id3)
for k2 = k1+1:length(Locaiton_id3)
Flag = 0;
for i = 1:Len
if Locaiton3{k1}(i) == Locaiton3{k2}(i)%假设持续时间大于20为关联
Flag = Flag + 1;
else
Flag = 0;
end
if Flag > Therehold
ASS{k1,k2} = [ASS{k1,k2},i];
Flag = 0;
end
end
end
end
for Nu = 1:length(Locaiton_id3)
UNo = Nu;%用户标号
for tim = 1:Times
UNo
tim
%计算状态数
Dat = Locaiton_id3{1,UNo}(1+tim:N+tim);
State = unique(Dat);
%计算状态概论
P = func_trans_prob(Dat,State,N);
%计算状态转移概论
[P_tra,Map] = func_transition_matrix(Dat,State);
for j = 1:size(Map,1)
if Dat(N1) == Map(j,1);
Initial = Map(j,2);
end
end
%开始预测
TT = zeros(1,length(State));
TT(Initial) = 1;
PState{1}= TT;
for i = 2:N2+1
PState{i} = PState{i-1}*P_tra;
[V,I] = max(PState{i});
for j = 1:size(Map,1)
if I == Map(j,2);
Pos(i-1) = Map(j,1);
end
end
end
NNN(:,tim) = (Dat(N1+1:N)==Pos(1:N2)')';
end
for k = 1:N2
Precision1(k,Nu) = sum(NNN(k,:))/Times;
end
end
save tmps1.mat Precision1
load tmps1.mat
load tmps2.mat
%结合CPB结果最后的概率
Precision2 = Precision1;
for Nu1 = 1:length(Locaiton_id3)
for Nu2 = 1:length(Locaiton_id3)
for tim = 1:Times
if isempty(ASS{Nu1,Nu2}) == 0
for k = 1:length(ASS{Nu1,Nu2})
if ASS{Nu1,Nu2}(k) == tim
for k2 = 1:N2
Precision2(k2,Nu1) = max(max(Precision1(k2,Nu1),Precision1(k2,Nu2)),max(Precision1m(k2,Nu1),Precision1m(k2,Nu2)));
end
end
end
end
end
end
end
save Result.mat Precision2
figure;
load Result1.mat
Views1 = [mean(Precision1(:,:),2)];
Views2 = [mean(Precision2(:,:),2)];
Views = [Views1,Views2];
bar(Views);
axis([0,N2+1,0.4,0.8]);
xlabel('Times');
ylabel('Precision');
legend('Markov','MMUB CPB Markov');
4. Operation steps and simulation conclusion
In this algorithm, we will combine CBP+MMUB+MarkovChain to construct the prediction algorithm.
It can be seen from the simulation results that the improved algorithm has a certain performance improvement compared with other algorithms.
5. References
A05-14
6. How to obtain the complete source code
Method 1: Wechat or QQ to contact the blogger
Method 2: Subscription , free access to the tutorial case code and any 2 complete source code of this blog