source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/PredictionByPartialMatch.java @ 93

Last change on this file since 93 was 93, checked in by sherbold, 13 years ago
  • moved generation of all possible sequences of a fixed length from de.ugoe.cs.eventbench.coverage.CoverageCalculator? to de.ugoe.cs.eventbench.models.IStochasticProcess
File size: 2.1 KB
Line 
1package de.ugoe.cs.eventbench.models;
2
3import java.util.LinkedList;
4import java.util.List;
5import java.util.Random;
6
7import de.ugoe.cs.eventbench.data.Event;
8
9public class PredictionByPartialMatch extends TrieBasedModel {
10       
11        /**
12         * Id for object serialization.
13         */
14        private static final long serialVersionUID = 1L;
15       
16        double probEscape;
17       
18        public PredictionByPartialMatch(int markovOrder, Random r) {
19                this(markovOrder, r, 0.1);
20        }
21       
22        public PredictionByPartialMatch(int markovOrder, Random r, double probEscape) {
23                super(markovOrder, r);
24                this.probEscape = probEscape;
25        }
26       
27        public void setProbEscape(double probEscape) {
28                this.probEscape = probEscape;
29        }
30       
31        public double getProbEscape() {
32                return probEscape;
33        }
34       
35        @Override
36        public double getProbability(List<? extends Event<?>> context, Event<?> symbol) {
37                double result = 0.0d;
38                double resultCurrentContex = 0.0d;
39                double resultShorterContex = 0.0d;
40               
41                List<Event<?>> contextCopy;
42                if( context.size()>=trieOrder ) {
43                        contextCopy = new LinkedList<Event<?>>(context.subList(context.size()-trieOrder+1, context.size()));
44                } else {
45                        contextCopy = new LinkedList<Event<?>>(context);
46                }
47
48       
49                List<Event<?>> followers = trie.getFollowingSymbols(contextCopy); // \Sigma'
50                int sumCountFollowers = 0; // N(s\sigma')
51                for( Event<?> follower : followers ) {
52                        sumCountFollowers += trie.getCount(contextCopy, follower);
53                }
54               
55                int countSymbol = trie.getCount(contextCopy, symbol); // N(s\sigma)
56                if( contextCopy.size()==0 ) {
57                        resultCurrentContex = ((double) countSymbol) / sumCountFollowers;
58                } else {
59                        if( sumCountFollowers==0 ) {
60                                resultCurrentContex = 0.0;
61                        }
62                        else {
63                                resultCurrentContex = ((double) countSymbol / sumCountFollowers)*(1-probEscape);
64                        }
65                        contextCopy.remove(0);
66                        double probSuffix = getProbability(contextCopy, symbol);
67                        if( followers.size()==0 ) {
68                                resultShorterContex = probSuffix;
69                        } else {
70                                resultShorterContex = probEscape*probSuffix;
71                        }
72                }
73                result = resultCurrentContex+resultShorterContex;
74               
75                return result;
76        }
77}
Note: See TracBrowser for help on using the repository browser.