Ignore:
Timestamp:
07/04/11 15:21:50 (13 years ago)
Author:
sherbold
Message:
  • code documentation
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/PredictionByPartialMatch.java

    r93 r101  
    77import de.ugoe.cs.eventbench.data.Event; 
    88 
     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 */ 
    921public class PredictionByPartialMatch extends TrieBasedModel { 
    10          
     22 
    1123        /** 
     24         * <p> 
    1225         * Id for object serialization. 
     26         * </p> 
    1327         */ 
    1428        private static final long serialVersionUID = 1L; 
    15          
     29 
     30        /** 
     31         * <p> 
     32         * Probability to use a lower-order Markov model 
     33         * </p> 
     34         */ 
    1635        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         */ 
    1849        public PredictionByPartialMatch(int markovOrder, Random r) { 
    1950                this(markovOrder, r, 0.1); 
    2051        } 
    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         */ 
    2267        public PredictionByPartialMatch(int markovOrder, Random r, double probEscape) { 
    2368                super(markovOrder, r); 
    2469                this.probEscape = probEscape; 
    2570        } 
    26          
     71 
     72        /** 
     73         * <p> 
     74         * Sets the escape probability of the model. 
     75         * </p> 
     76         *  
     77         * @param probEscape 
     78         *            new escape probability 
     79         */ 
    2780        public void setProbEscape(double probEscape) { 
    2881                this.probEscape = probEscape; 
    2982        } 
    30          
     83 
     84        /** 
     85         * <p> 
     86         * Returns the escape probability of the model. 
     87         * </p> 
     88         *  
     89         * @return escape probability of the model 
     90         */ 
    3191        public double getProbEscape() { 
    3292                return probEscape; 
    3393        } 
    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         */ 
    35103        @Override 
    36         public double getProbability(List<? extends Event<?>> context, Event<?> symbol) { 
     104        public double getProbability(List<? extends Event<?>> context, 
     105                        Event<?> symbol) { 
    37106                double result = 0.0d; 
    38107                double resultCurrentContex = 0.0d; 
    39108                double resultShorterContex = 0.0d; 
    40                  
     109 
    41110                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())); 
    44114                } else { 
    45115                        contextCopy = new LinkedList<Event<?>>(context); 
    46116                } 
    47117 
    48          
    49118                List<Event<?>> followers = trie.getFollowingSymbols(contextCopy); // \Sigma' 
    50119                int sumCountFollowers = 0; // N(s\sigma') 
    51                 for( Event<?> follower : followers ) { 
     120                for (Event<?> follower : followers) { 
    52121                        sumCountFollowers += trie.getCount(contextCopy, follower); 
    53122                } 
    54                  
     123 
    55124                int countSymbol = trie.getCount(contextCopy, symbol); // N(s\sigma) 
    56                 if( contextCopy.size()==0 ) { 
     125                if (contextCopy.size() == 0) { 
    57126                        resultCurrentContex = ((double) countSymbol) / sumCountFollowers; 
    58127                } else { 
    59                         if( sumCountFollowers==0 ) { 
     128                        if (sumCountFollowers == 0) { 
    60129                                resultCurrentContex = 0.0; 
     130                        } else { 
     131                                resultCurrentContex = ((double) countSymbol / sumCountFollowers) 
     132                                                * (1 - probEscape); 
    61133                        } 
    62                         else { 
    63                                 resultCurrentContex = ((double) countSymbol / sumCountFollowers)*(1-probEscape); 
    64                         } 
    65                         contextCopy.remove(0);  
     134                        contextCopy.remove(0); 
    66135                        double probSuffix = getProbability(contextCopy, symbol); 
    67                         if( followers.size()==0 ) { 
     136                        if (followers.size() == 0) { 
    68137                                resultShorterContex = probSuffix; 
    69138                        } else { 
    70                                 resultShorterContex = probEscape*probSuffix; 
     139                                resultShorterContex = probEscape * probSuffix; 
    71140                        } 
    72141                } 
    73                 result = resultCurrentContex+resultShorterContex; 
    74                  
     142                result = resultCurrentContex + resultShorterContex; 
     143 
    75144                return result; 
    76145        } 
Note: See TracChangeset for help on using the changeset viewer.