[13] | 1 | package de.ugoe.cs.eventbench.models;
|
---|
[1] | 2 |
|
---|
[342] | 3 | import java.security.InvalidParameterException;
|
---|
[102] | 4 | import java.util.Collection;
|
---|
[5] | 5 | import java.util.LinkedList;
|
---|
[1] | 6 | import java.util.List;
|
---|
| 7 | import java.util.Random;
|
---|
| 8 |
|
---|
| 9 | import de.ugoe.cs.eventbench.data.Event;
|
---|
| 10 |
|
---|
[101] | 11 | /**
|
---|
| 12 | * <p>
|
---|
| 13 | * Implements Prediction by Partial Match (PPM) based on the following formula
|
---|
| 14 | * (LaTeX-style notation):<br>
|
---|
[115] | 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>
|
---|
[101] | 17 | * P_{MM^i} denotes the probability in an i-th order Markov model.
|
---|
| 18 | * </p>
|
---|
| 19 | *
|
---|
| 20 | * @author Steffen Herbold
|
---|
| 21 | *
|
---|
| 22 | */
|
---|
[12] | 23 | public class PredictionByPartialMatch extends TrieBasedModel {
|
---|
[101] | 24 |
|
---|
[86] | 25 | /**
|
---|
[101] | 26 | * <p>
|
---|
[86] | 27 | * Id for object serialization.
|
---|
[101] | 28 | * </p>
|
---|
[86] | 29 | */
|
---|
| 30 | private static final long serialVersionUID = 1L;
|
---|
[101] | 31 |
|
---|
| 32 | /**
|
---|
| 33 | * <p>
|
---|
[109] | 34 | * Minimum order of the Markov model.
|
---|
| 35 | * </p>
|
---|
| 36 | */
|
---|
[258] | 37 | protected int minOrder;
|
---|
[109] | 38 |
|
---|
| 39 | /**
|
---|
| 40 | * <p>
|
---|
[101] | 41 | * Probability to use a lower-order Markov model
|
---|
| 42 | * </p>
|
---|
| 43 | */
|
---|
[258] | 44 | protected double probEscape;
|
---|
[101] | 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 | */
|
---|
[16] | 58 | public PredictionByPartialMatch(int markovOrder, Random r) {
|
---|
| 59 | this(markovOrder, r, 0.1);
|
---|
[7] | 60 | }
|
---|
[101] | 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 | */
|
---|
[16] | 76 | public PredictionByPartialMatch(int markovOrder, Random r, double probEscape) {
|
---|
[109] | 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
|
---|
[342] | 96 | * @throws InvalidParameterException thrown if minOrder is less than 0 or greater than markovOrder or probEscape is not in the interval (0,1)
|
---|
[109] | 97 | */
|
---|
| 98 | public PredictionByPartialMatch(int markovOrder, int minOrder, Random r,
|
---|
| 99 | double probEscape) {
|
---|
[16] | 100 | super(markovOrder, r);
|
---|
[342] | 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 | }
|
---|
[7] | 110 | this.probEscape = probEscape;
|
---|
[109] | 111 | this.minOrder = minOrder;
|
---|
[7] | 112 | }
|
---|
[101] | 113 |
|
---|
| 114 | /**
|
---|
| 115 | * <p>
|
---|
| 116 | * Sets the escape probability of the model.
|
---|
| 117 | * </p>
|
---|
| 118 | *
|
---|
| 119 | * @param probEscape
|
---|
| 120 | * new escape probability
|
---|
| 121 | */
|
---|
[7] | 122 | public void setProbEscape(double probEscape) {
|
---|
| 123 | this.probEscape = probEscape;
|
---|
| 124 | }
|
---|
[101] | 125 |
|
---|
| 126 | /**
|
---|
| 127 | * <p>
|
---|
| 128 | * Returns the escape probability of the model.
|
---|
| 129 | * </p>
|
---|
| 130 | *
|
---|
| 131 | * @return escape probability of the model
|
---|
| 132 | */
|
---|
[7] | 133 | public double getProbEscape() {
|
---|
| 134 | return probEscape;
|
---|
| 135 | }
|
---|
[101] | 136 |
|
---|
| 137 | /**
|
---|
| 138 | * <p>
|
---|
| 139 | * Calculates the probability of the next event based on the formula:<br>
|
---|
[109] | 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>
|
---|
[101] | 144 | * P_{MM^i} denotes the probability in an i-th order Markov model.
|
---|
| 145 | * </p>
|
---|
[342] | 146 | *
|
---|
| 147 | * @see de.ugoe.cs.eventbench.models.IStochasticProcess#getProbability(java.util.List,
|
---|
| 148 | * de.ugoe.cs.eventbench.data.Event)
|
---|
[101] | 149 | */
|
---|
[12] | 150 | @Override
|
---|
[101] | 151 | public double getProbability(List<? extends Event<?>> context,
|
---|
| 152 | Event<?> symbol) {
|
---|
[342] | 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 | }
|
---|
[6] | 159 | double result = 0.0d;
|
---|
| 160 | double resultCurrentContex = 0.0d;
|
---|
| 161 | double resultShorterContex = 0.0d;
|
---|
[101] | 162 |
|
---|
[80] | 163 | List<Event<?>> contextCopy;
|
---|
[101] | 164 | if (context.size() >= trieOrder) {
|
---|
| 165 | contextCopy = new LinkedList<Event<?>>(context.subList(
|
---|
| 166 | context.size() - trieOrder + 1, context.size()));
|
---|
[80] | 167 | } else {
|
---|
| 168 | contextCopy = new LinkedList<Event<?>>(context);
|
---|
| 169 | }
|
---|
[6] | 170 |
|
---|
[102] | 171 | Collection<Event<?>> followers = trie.getFollowingSymbols(contextCopy); // \Sigma'
|
---|
[6] | 172 | int sumCountFollowers = 0; // N(s\sigma')
|
---|
[101] | 173 | for (Event<?> follower : followers) {
|
---|
[6] | 174 | sumCountFollowers += trie.getCount(contextCopy, follower);
|
---|
| 175 | }
|
---|
[101] | 176 |
|
---|
[6] | 177 | int countSymbol = trie.getCount(contextCopy, symbol); // N(s\sigma)
|
---|
[109] | 178 | if (sumCountFollowers == 0) {
|
---|
| 179 | resultCurrentContex = 0.0;
|
---|
[6] | 180 | } else {
|
---|
[109] | 181 | resultCurrentContex = (double) countSymbol / sumCountFollowers;
|
---|
| 182 | }
|
---|
[342] | 183 | if (contextCopy.size() > minOrder) {
|
---|
[109] | 184 | resultCurrentContex *= (1 - probEscape);
|
---|
[101] | 185 | contextCopy.remove(0);
|
---|
[109] | 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 | }
|
---|
[6] | 194 | }
|
---|
| 195 | }
|
---|
[101] | 196 | result = resultCurrentContex + resultShorterContex;
|
---|
| 197 |
|
---|
[6] | 198 | return result;
|
---|
| 199 | }
|
---|
[1] | 200 | }
|
---|