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