1 | package de.ugoe.cs.eventbench.models;
|
---|
2 |
|
---|
3 | import java.util.LinkedList;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Random;
|
---|
6 |
|
---|
7 | import de.ugoe.cs.eventbench.data.Event;
|
---|
8 |
|
---|
9 | public 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 | }
|
---|