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