1 | package de.ugoe.cs.quest.usageprofiles;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Random;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.quest.eventcore.Event;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * <p>
|
---|
12 | * Implements Prediction by Partial Match (PPM) based on the following formula (LaTeX-style
|
---|
13 | * notation):<br>
|
---|
14 | * P_{PPM}(X_n|X_{n-1},...,X_{n-k}) = \sum_{i=k}^min escape^{k-i} P_{MM^i}(X_n
|
---|
15 | * |X_{n-1},...,X_{n-i})(1-escape)+escape^(k-min)P(X_n|X_{n-i},... ,X_{n-min})<br>
|
---|
16 | * P_{MM^i} denotes the probability in an i-th order Markov model.
|
---|
17 | * </p>
|
---|
18 | *
|
---|
19 | * @author Steffen Herbold
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public class PredictionByPartialMatch extends TrieBasedModel {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * <p>
|
---|
26 | * Id for object serialization.
|
---|
27 | * </p>
|
---|
28 | */
|
---|
29 | private static final long serialVersionUID = 1L;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * <p>
|
---|
33 | * Minimum order of the Markov model.
|
---|
34 | * </p>
|
---|
35 | */
|
---|
36 | protected int minOrder;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * <p>
|
---|
40 | * Probability to use a lower-order Markov model
|
---|
41 | * </p>
|
---|
42 | */
|
---|
43 | protected double probEscape;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>
|
---|
47 | * Constructor. Creates a new PredictionByPartialMatch model with a given Markov order and a
|
---|
48 | * default escape probability of 0.1.
|
---|
49 | * </p>
|
---|
50 | *
|
---|
51 | * @param markovOrder
|
---|
52 | * Markov order of the model
|
---|
53 | * @param r
|
---|
54 | * random number generator used by probabilistic methods of the class
|
---|
55 | */
|
---|
56 | public PredictionByPartialMatch(int markovOrder, Random r) {
|
---|
57 | this(markovOrder, r, 0.1);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * <p>
|
---|
62 | * Creates a new PredictionByPartialMatch model with a given Markov order and escape
|
---|
63 | * probability.
|
---|
64 | * </p>
|
---|
65 | *
|
---|
66 | * @param markovOrder
|
---|
67 | * Markov order of the model
|
---|
68 | * @param r
|
---|
69 | * random number generator used by probabilistic methods of the class
|
---|
70 | * @param probEscape
|
---|
71 | * escape probability used by the model
|
---|
72 | */
|
---|
73 | public PredictionByPartialMatch(int markovOrder, Random r, double probEscape) {
|
---|
74 | this(markovOrder, 0, r, probEscape);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * <p>
|
---|
79 | * Creates a new PredictionByPartialMatch model with a given Markov order and escape
|
---|
80 | * probability.
|
---|
81 | * </p>
|
---|
82 | *
|
---|
83 | * @param markovOrder
|
---|
84 | * Markov order of the model
|
---|
85 | * @param minOrder
|
---|
86 | * minimum order of the model; if this order is reached, there is no escape
|
---|
87 | * @param r
|
---|
88 | * random number generator used by probabilistic methods of the class
|
---|
89 | * @param probEscape
|
---|
90 | * escape probability used by the model
|
---|
91 | * @throws IllegalArgumentException
|
---|
92 | * thrown if minOrder is less than 0 or greater than markovOrder or probEscape is
|
---|
93 | * not in the interval (0,1)
|
---|
94 | */
|
---|
95 | public PredictionByPartialMatch(int markovOrder, int minOrder, Random r, double probEscape) {
|
---|
96 | super(markovOrder, r);
|
---|
97 | if (minOrder < 0) {
|
---|
98 | throw new IllegalArgumentException("minOrder must be greather than or equal to 0");
|
---|
99 | }
|
---|
100 | if (minOrder > markovOrder) {
|
---|
101 | throw new IllegalArgumentException(
|
---|
102 | "minOrder must be less than or equal to markovOrder");
|
---|
103 | }
|
---|
104 | if (probEscape <= 0.0 || probEscape >= 1.0) {
|
---|
105 | throw new IllegalArgumentException("probEscape must be in the interval (0,1)");
|
---|
106 | }
|
---|
107 | this.probEscape = probEscape;
|
---|
108 | this.minOrder = minOrder;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * <p>
|
---|
113 | * Sets the escape probability of the model.
|
---|
114 | * </p>
|
---|
115 | *
|
---|
116 | * @param probEscape
|
---|
117 | * new escape probability
|
---|
118 | */
|
---|
119 | public void setProbEscape(double probEscape) {
|
---|
120 | this.probEscape = probEscape;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * <p>
|
---|
125 | * Returns the escape probability of the model.
|
---|
126 | * </p>
|
---|
127 | *
|
---|
128 | * @return escape probability of the model
|
---|
129 | */
|
---|
130 | public double getProbEscape() {
|
---|
131 | return probEscape;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * <p>
|
---|
136 | * Calculates the probability of the next event based on the formula:<br>
|
---|
137 | * P_{PPM}(X_n|X_{n-1},...,X_{n-k}) = \sum_{i=k}^min escape^{k-i} P_{MM^i}(X_n
|
---|
138 | * |X_{n-1},...,X_{n-i})(1-escape)+escape^(k-min)P(X_n|X_{n-i},... ,X_{n-min})<br>
|
---|
139 | * P_{MM^i} denotes the probability in an i-th order Markov model.
|
---|
140 | * </p>
|
---|
141 | *
|
---|
142 | * @see de.ugoe.cs.quest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
|
---|
143 | * de.ugoe.cs.quest.eventcore.Event)
|
---|
144 | */
|
---|
145 | @Override
|
---|
146 | public double getProbability(List<Event> context, Event symbol) {
|
---|
147 | if (context == null) {
|
---|
148 | throw new IllegalArgumentException("context must not be null");
|
---|
149 | }
|
---|
150 | if (symbol == null) {
|
---|
151 | throw new IllegalArgumentException("symbol must not be null");
|
---|
152 | }
|
---|
153 | double result = 0.0d;
|
---|
154 | double resultCurrentContex = 0.0d;
|
---|
155 | double resultShorterContex = 0.0d;
|
---|
156 |
|
---|
157 | List<Event> contextCopy;
|
---|
158 | if (context.size() >= trieOrder) {
|
---|
159 | contextCopy =
|
---|
160 | new LinkedList<Event>(context.subList(context.size() - trieOrder + 1,
|
---|
161 | context.size()));
|
---|
162 | }
|
---|
163 | else {
|
---|
164 | contextCopy = new LinkedList<Event>(context);
|
---|
165 | }
|
---|
166 |
|
---|
167 | Collection<Event> followers = trie.getFollowingSymbols(contextCopy); // \Sigma'
|
---|
168 | int sumCountFollowers = 0; // N(s\sigma')
|
---|
169 | for (Event follower : followers) {
|
---|
170 | sumCountFollowers += trie.getCount(contextCopy, follower);
|
---|
171 | }
|
---|
172 |
|
---|
173 | int countSymbol = trie.getCount(contextCopy, symbol); // N(s\sigma)
|
---|
174 | if (sumCountFollowers == 0) {
|
---|
175 | resultCurrentContex = 0.0;
|
---|
176 | }
|
---|
177 | else {
|
---|
178 | resultCurrentContex = (double) countSymbol / sumCountFollowers;
|
---|
179 | }
|
---|
180 | if (contextCopy.size() > minOrder) {
|
---|
181 | resultCurrentContex *= (1 - probEscape);
|
---|
182 | contextCopy.remove(0);
|
---|
183 | if (contextCopy.size() >= minOrder) {
|
---|
184 | double probSuffix = getProbability(contextCopy, symbol);
|
---|
185 |
|
---|
186 | if (followers.size() == 0) {
|
---|
187 | resultShorterContex = probSuffix;
|
---|
188 | }
|
---|
189 | else {
|
---|
190 | resultShorterContex = probEscape * probSuffix;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | result = resultCurrentContex + resultShorterContex;
|
---|
195 |
|
---|
196 | return result;
|
---|
197 | }
|
---|
198 | }
|
---|