Changeset 342


Ignore:
Timestamp:
12/21/11 16:54:59 (12 years ago)
Author:
sherbold
Message:
Location:
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models
Files:
5 edited

Legend:

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

    r102 r342  
    11package de.ugoe.cs.eventbench.models; 
    22 
     3import java.security.InvalidParameterException; 
    34import java.util.Collection; 
    45import java.util.LinkedList; 
     
    5253        public double getProbability(List<? extends Event<?>> context, 
    5354                        Event<?> symbol) { 
     55                if( context==null ) { 
     56                        throw new InvalidParameterException("context must not be null"); 
     57                } 
     58                if( symbol==null ) { 
     59                        throw new InvalidParameterException("symbol must not be null"); 
     60                } 
    5461                double result = 0.0d; 
    5562 
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/HighOrderMarkovModel.java

    r102 r342  
    11package de.ugoe.cs.eventbench.models; 
    22 
     3import java.security.InvalidParameterException; 
    34import java.util.Collection; 
    45import java.util.LinkedList; 
     
    910 
    1011/** 
    11  * <p>Implements high-order Markov models.</p> 
     12 * <p> 
     13 * Implements high-order Markov models. 
     14 * </p> 
    1215 *  
    1316 * @author Steffen Herbold 
     
    1518 */ 
    1619public class HighOrderMarkovModel extends TrieBasedModel { 
    17          
     20 
    1821        /** 
    1922         * <p> 
     
    2427 
    2528        /** 
    26          * <p>Constructor. Creates a new HighOrderMarkovModel with a defined Markov order.</p> 
    27          * @param maxOrder Markov order of the model 
    28          * @param r random number generator used by probabilistic methods of the class 
     29         * <p> 
     30         * Constructor. Creates a new HighOrderMarkovModel with a defined Markov 
     31         * order. 
     32         * </p> 
     33         *  
     34         * @param maxOrder 
     35         *            Markov order of the model 
     36         * @param r 
     37         *            random number generator used by probabilistic methods of the 
     38         *            class 
    2939         */ 
    3040        public HighOrderMarkovModel(int maxOrder, Random r) { 
    3141                super(maxOrder, r); 
    3242        } 
    33          
     43 
    3444        /** 
    3545         * <p> 
    36          * Calculates the probability of the next Event being symbol based on the order of the Markov model. The order is defined in the constructor {@link #HighOrderMarkovModel(int, Random)}.  
     46         * Calculates the probability of the next Event being symbol based on the 
     47         * order of the Markov model. The order is defined in the constructor 
     48         * {@link #HighOrderMarkovModel(int, Random)}. 
    3749         * </p> 
    38          * @see de.ugoe.cs.eventbench.models.IStochasticProcess#getProbability(java.util.List, de.ugoe.cs.eventbench.data.Event) 
     50         *  
     51         * @see de.ugoe.cs.eventbench.models.IStochasticProcess#getProbability(java.util.List, 
     52         *      de.ugoe.cs.eventbench.data.Event) 
    3953         */ 
    4054        @Override 
    41         public double getProbability(List<? extends Event<?>> context, Event<?> symbol) { 
     55        public double getProbability(List<? extends Event<?>> context, 
     56                        Event<?> symbol) { 
     57                if (context == null) { 
     58                        throw new InvalidParameterException("context must not be null"); 
     59                } 
     60                if (symbol == null) { 
     61                        throw new InvalidParameterException("symbol must not be null"); 
     62                } 
    4263                double result = 0.0d; 
    43                  
     64 
    4465                List<Event<?>> contextCopy; 
    45                 if( context.size()>=trieOrder ) { 
    46                         contextCopy = new LinkedList<Event<?>>(context.subList(context.size()-trieOrder+1, context.size())); 
     66                if (context.size() >= trieOrder) { 
     67                        contextCopy = new LinkedList<Event<?>>(context.subList( 
     68                                        context.size() - trieOrder + 1, context.size())); 
    4769                } else { 
    4870                        contextCopy = new LinkedList<Event<?>>(context); 
    4971                } 
    5072 
    51          
    5273                Collection<Event<?>> followers = trie.getFollowingSymbols(contextCopy); 
    5374                int sumCountFollowers = 0; // N(s\sigma') 
    54                 for( Event<?> follower : followers ) { 
     75                for (Event<?> follower : followers) { 
    5576                        sumCountFollowers += trie.getCount(contextCopy, follower); 
    5677                } 
    57                  
     78 
    5879                int countSymbol = trie.getCount(contextCopy, symbol); 
    59                 if( sumCountFollowers!=0 ) { 
     80                if (sumCountFollowers != 0) { 
    6081                        result = ((double) countSymbol / sumCountFollowers); 
    6182                } 
    62                  
     83 
    6384                return result; 
    6485        } 
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/IStochasticProcess.java

    r248 r342  
    22 
    33import java.io.Serializable; 
     4import java.security.InvalidParameterException; 
    45import java.util.Collection; 
    56import java.util.List; 
     
    3031         * @return probabilty the {@code symbol} is the next event, given the last 
    3132         *         events {@code context} 
     33         * @throws InvalidParameterException 
     34         *             thrown if context or symbol is null 
    3235         */ 
    3336        double getProbability(List<? extends Event<?>> context, Event<?> symbol); 
     
    4245         *            sequences of which the probability is calculated 
    4346         * @return probability of the sequences; 1.0 if sequence is empty or null 
     47         * @throws InvalidParameterException 
     48         *             thrown if sequence is null 
    4449         */ 
    4550        double getProbability(List<? extends Event<?>> sequence); 
     
    6671         * @return generated sequences 
    6772         * @see #generateSequences(int, boolean) 
     73         * @throws InvalidParameterException 
     74         *             thrown if length is less than or equal to 0 
    6875         */ 
    6976        public Collection<List<? extends Event<?>>> generateSequences(int length); 
     
    8491         *            {@link Event#STARTEVENT} 
    8592         * @return generated sequences 
     93         * @throws InvalidParameterException 
     94         *             thrown if length is less than or equal to 0 
    8695         */ 
    8796        public Collection<List<? extends Event<?>>> generateSequences(int length, 
     
    99108         * @param length 
    100109         * @return generated sequences 
     110         * @throws InvalidParameterException 
     111         *             thrown if length is less than or equal to 0 
    101112         */ 
    102113        public Collection<List<? extends Event<?>>> generateValidSequences( 
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/PredictionByPartialMatch.java

    r258 r342  
    11package de.ugoe.cs.eventbench.models; 
    22 
     3import java.security.InvalidParameterException; 
    34import java.util.Collection; 
    45import java.util.LinkedList; 
     
    9394         * @param probEscape 
    9495         *            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) 
    9597         */ 
    9698        public PredictionByPartialMatch(int markovOrder, int minOrder, Random r, 
    9799                        double probEscape) { 
    98100                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                } 
    99110                this.probEscape = probEscape; 
    100111                this.minOrder = minOrder; 
     
    133144         * P_{MM^i} denotes the probability in an i-th order Markov model. 
    134145         * </p> 
     146         *  
     147         * @see de.ugoe.cs.eventbench.models.IStochasticProcess#getProbability(java.util.List, 
     148         *      de.ugoe.cs.eventbench.data.Event) 
    135149         */ 
    136150        @Override 
    137151        public double getProbability(List<? extends Event<?>> context, 
    138152                        Event<?> symbol) { 
     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                } 
    139159                double result = 0.0d; 
    140160                double resultCurrentContex = 0.0d; 
     
    161181                        resultCurrentContex = (double) countSymbol / sumCountFollowers; 
    162182                } 
    163                 if (contextCopy.size() >= minOrder) { 
     183                if (contextCopy.size() > minOrder) { 
    164184                        resultCurrentContex *= (1 - probEscape); 
    165185                        contextCopy.remove(0); 
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/TrieBasedModel.java

    r325 r342  
    7070         *            random number generator used by probabilistic methods of the 
    7171         *            class 
     72         * @throws InvalidParameterException 
     73         *             thrown if markovOrder is less than 0 or the random number 
     74         *             generator r is null 
    7275         */ 
    7376        public TrieBasedModel(int markovOrder, Random r) { 
    7477                super(); 
     78                if (markovOrder < 0) { 
     79                        throw new InvalidParameterException( 
     80                                        "markov order must not be less than 0"); 
     81                } 
     82                if (r == null) { 
     83                        throw new InvalidParameterException( 
     84                                        "random number generator r must not be null"); 
     85                } 
    7586                this.trieOrder = markovOrder + 1; 
    7687                this.r = r; 
     
    8798         * @param sequences 
    8899         *            training data 
     100         * @throws InvalidParameterException 
     101         *             thrown is sequences is null 
    89102         */ 
    90103        public void train(Collection<List<? extends Event<?>>> sequences) { 
     
    103116         * @param sequences 
    104117         *            training data 
     118         * @throws InvalidParameterException 
     119         *             thrown is sequences is null 
    105120         */ 
    106121        public void update(Collection<List<? extends Event<?>>> sequences) { 
    107122                if (sequences == null) { 
    108                         return; 
     123                        throw new InvalidParameterException("sequences must not be null"); 
    109124                } 
    110125                if (trie == null) { 
     
    129144        public List<? extends Event<?>> randomSequence() { 
    130145                List<Event<?>> sequence = new LinkedList<Event<?>>(); 
    131                 if( trie!=null ) { 
     146                if (trie != null) { 
    132147                        IncompleteMemory<Event<?>> context = new IncompleteMemory<Event<?>>( 
    133148                                        trieOrder - 1); 
    134149                        context.add(Event.STARTEVENT); 
    135          
     150 
    136151                        Event<?> currentState = Event.STARTEVENT; 
    137          
     152 
    138153                        boolean endFound = false; 
    139          
     154 
    140155                        while (!endFound) { 
    141156                                double randVal = r.nextDouble(); 
     
    147162                                                endFound = (symbol == Event.ENDEVENT); 
    148163                                                if (!(symbol == Event.STARTEVENT || symbol == Event.ENDEVENT)) { 
    149                                                         // only add the symbol the sequence if it is not START 
     164                                                        // only add the symbol the sequence if it is not 
     165                                                        // START 
    150166                                                        // or END 
    151167                                                        context.add(symbol); 
     
    344360        @Override 
    345361        public double getProbability(List<? extends Event<?>> sequence) { 
     362                if (sequence == null) { 
     363                        throw new InvalidParameterException("sequence must not be null"); 
     364                } 
    346365                double prob = 1.0; 
    347                 if (sequence != null) { 
    348                         List<Event<?>> context = new LinkedList<Event<?>>(); 
    349                         for (Event<?> event : sequence) { 
    350                                 prob *= getProbability(context, event); 
    351                                 context.add(event); 
    352                         } 
     366                List<Event<?>> context = new LinkedList<Event<?>>(); 
     367                for (Event<?> event : sequence) { 
     368                        prob *= getProbability(context, event); 
     369                        context.add(event); 
    353370                } 
    354371                return prob; 
Note: See TracChangeset for help on using the changeset viewer.