[13] | 1 | package de.ugoe.cs.eventbench.models;
|
---|
[1] | 2 |
|
---|
[5] | 3 | import java.util.LinkedList;
|
---|
[1] | 4 | import java.util.List;
|
---|
| 5 | import java.util.Random;
|
---|
| 6 |
|
---|
| 7 | import de.ugoe.cs.eventbench.data.Event;
|
---|
| 8 |
|
---|
[12] | 9 | public class PredictionByPartialMatch extends TrieBasedModel {
|
---|
[1] | 10 |
|
---|
[86] | 11 | /**
|
---|
| 12 | * Id for object serialization.
|
---|
| 13 | */
|
---|
| 14 | private static final long serialVersionUID = 1L;
|
---|
| 15 |
|
---|
[12] | 16 | double probEscape;
|
---|
[1] | 17 |
|
---|
[16] | 18 | public PredictionByPartialMatch(int markovOrder, Random r) {
|
---|
| 19 | this(markovOrder, r, 0.1);
|
---|
[7] | 20 | }
|
---|
| 21 |
|
---|
[16] | 22 | public PredictionByPartialMatch(int markovOrder, Random r, double probEscape) {
|
---|
| 23 | super(markovOrder, r);
|
---|
[7] | 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 |
|
---|
[12] | 35 | @Override
|
---|
[93] | 36 | public double getProbability(List<? extends Event<?>> context, Event<?> symbol) {
|
---|
[6] | 37 | double result = 0.0d;
|
---|
| 38 | double resultCurrentContex = 0.0d;
|
---|
| 39 | double resultShorterContex = 0.0d;
|
---|
| 40 |
|
---|
[80] | 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 | }
|
---|
[6] | 47 |
|
---|
| 48 |
|
---|
[7] | 49 | List<Event<?>> followers = trie.getFollowingSymbols(contextCopy); // \Sigma'
|
---|
[6] | 50 | int sumCountFollowers = 0; // N(s\sigma')
|
---|
[7] | 51 | for( Event<?> follower : followers ) {
|
---|
[6] | 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 {
|
---|
[80] | 59 | if( sumCountFollowers==0 ) {
|
---|
| 60 | resultCurrentContex = 0.0;
|
---|
| 61 | }
|
---|
| 62 | else {
|
---|
| 63 | resultCurrentContex = ((double) countSymbol / sumCountFollowers)*(1-probEscape);
|
---|
| 64 | }
|
---|
[6] | 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 | }
|
---|
[1] | 77 | }
|
---|