Changeset 101 for trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/PredictionByPartialMatch.java
- Timestamp:
- 07/04/11 15:21:50 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/PredictionByPartialMatch.java
r93 r101 7 7 import de.ugoe.cs.eventbench.data.Event; 8 8 9 /** 10 * <p> 11 * Implements Prediction by Partial Match (PPM) based on the following formula 12 * (LaTeX-style notation):<br> 13 * P_{PPM}(X_n|X_{n-1},...,X_{n-k}) = \sum_{i=k}^1 escape^{i-1} 14 * P_{MM^i}(X_n|X_{n-1},...,X_{n-i})(1-escape)<br> 15 * P_{MM^i} denotes the probability in an i-th order Markov model. 16 * </p> 17 * 18 * @author Steffen Herbold 19 * 20 */ 9 21 public class PredictionByPartialMatch extends TrieBasedModel { 10 22 11 23 /** 24 * <p> 12 25 * Id for object serialization. 26 * </p> 13 27 */ 14 28 private static final long serialVersionUID = 1L; 15 29 30 /** 31 * <p> 32 * Probability to use a lower-order Markov model 33 * </p> 34 */ 16 35 double probEscape; 17 36 37 /** 38 * <p> 39 * Constructor. Creates a new PredictionByPartialMatch model with a given 40 * Markov order and a default escape probability of 0.1. 41 * </p> 42 * 43 * @param markovOrder 44 * Markov order of the model 45 * @param r 46 * random number generator used by probabilistic methods of the 47 * class 48 */ 18 49 public PredictionByPartialMatch(int markovOrder, Random r) { 19 50 this(markovOrder, r, 0.1); 20 51 } 21 52 53 /** 54 * <p> 55 * Creates a new PredictionByPartialMatch model with a given Markov order 56 * and escape probability. 57 * </p> 58 * 59 * @param markovOrder 60 * Markov order of the model 61 * @param r 62 * random number generator used by probabilistic methods of the 63 * class 64 * @param probEscape 65 * escape probability used by the model 66 */ 22 67 public PredictionByPartialMatch(int markovOrder, Random r, double probEscape) { 23 68 super(markovOrder, r); 24 69 this.probEscape = probEscape; 25 70 } 26 71 72 /** 73 * <p> 74 * Sets the escape probability of the model. 75 * </p> 76 * 77 * @param probEscape 78 * new escape probability 79 */ 27 80 public void setProbEscape(double probEscape) { 28 81 this.probEscape = probEscape; 29 82 } 30 83 84 /** 85 * <p> 86 * Returns the escape probability of the model. 87 * </p> 88 * 89 * @return escape probability of the model 90 */ 31 91 public double getProbEscape() { 32 92 return probEscape; 33 93 } 34 94 95 /** 96 * <p> 97 * Calculates the probability of the next event based on the formula:<br> 98 * P_{PPM}(X_n|X_{n-1},...,X_{n-k}) = \sum_{i=k}^1 escape^{i-1} 99 * P_{MM^i}(X_n|X_{n-1},...,X_{n-i})(1-escape)<br> 100 * P_{MM^i} denotes the probability in an i-th order Markov model. 101 * </p> 102 */ 35 103 @Override 36 public double getProbability(List<? extends Event<?>> context, Event<?> symbol) { 104 public double getProbability(List<? extends Event<?>> context, 105 Event<?> symbol) { 37 106 double result = 0.0d; 38 107 double resultCurrentContex = 0.0d; 39 108 double resultShorterContex = 0.0d; 40 109 41 110 List<Event<?>> contextCopy; 42 if( context.size()>=trieOrder ) { 43 contextCopy = new LinkedList<Event<?>>(context.subList(context.size()-trieOrder+1, context.size())); 111 if (context.size() >= trieOrder) { 112 contextCopy = new LinkedList<Event<?>>(context.subList( 113 context.size() - trieOrder + 1, context.size())); 44 114 } else { 45 115 contextCopy = new LinkedList<Event<?>>(context); 46 116 } 47 117 48 49 118 List<Event<?>> followers = trie.getFollowingSymbols(contextCopy); // \Sigma' 50 119 int sumCountFollowers = 0; // N(s\sigma') 51 for ( Event<?> follower : followers) {120 for (Event<?> follower : followers) { 52 121 sumCountFollowers += trie.getCount(contextCopy, follower); 53 122 } 54 123 55 124 int countSymbol = trie.getCount(contextCopy, symbol); // N(s\sigma) 56 if ( contextCopy.size()==0) {125 if (contextCopy.size() == 0) { 57 126 resultCurrentContex = ((double) countSymbol) / sumCountFollowers; 58 127 } else { 59 if ( sumCountFollowers==0) {128 if (sumCountFollowers == 0) { 60 129 resultCurrentContex = 0.0; 130 } else { 131 resultCurrentContex = ((double) countSymbol / sumCountFollowers) 132 * (1 - probEscape); 61 133 } 62 else { 63 resultCurrentContex = ((double) countSymbol / sumCountFollowers)*(1-probEscape); 64 } 65 contextCopy.remove(0); 134 contextCopy.remove(0); 66 135 double probSuffix = getProbability(contextCopy, symbol); 67 if ( followers.size()==0) {136 if (followers.size() == 0) { 68 137 resultShorterContex = probSuffix; 69 138 } else { 70 resultShorterContex = probEscape *probSuffix;139 resultShorterContex = probEscape * probSuffix; 71 140 } 72 141 } 73 result = resultCurrentContex +resultShorterContex;74 142 result = resultCurrentContex + resultShorterContex; 143 75 144 return result; 76 145 }
Note: See TracChangeset
for help on using the changeset viewer.