1 | package de.ugoe.cs.quest.usageprofiles;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Random;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.quest.eventcore.Event;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * <p>
|
---|
12 | * Implements high-order Markov models.
|
---|
13 | * </p>
|
---|
14 | *
|
---|
15 | * @author Steffen Herbold
|
---|
16 | * @version 1.0
|
---|
17 | */
|
---|
18 | public class HighOrderMarkovModel extends TrieBasedModel {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * <p>
|
---|
22 | * Id for object serialization.
|
---|
23 | * </p>
|
---|
24 | */
|
---|
25 | private static final long serialVersionUID = 1L;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * <p>
|
---|
29 | * Constructor. Creates a new HighOrderMarkovModel with a defined Markov order.
|
---|
30 | * </p>
|
---|
31 | *
|
---|
32 | * @param maxOrder
|
---|
33 | * Markov order of the model
|
---|
34 | * @param r
|
---|
35 | * random number generator used by probabilistic methods of the class
|
---|
36 | */
|
---|
37 | public HighOrderMarkovModel(int maxOrder, Random r) {
|
---|
38 | super(maxOrder, r);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * <p>
|
---|
43 | * Calculates the probability of the next Event being symbol based on the order of the Markov
|
---|
44 | * model. The order is defined in the constructor {@link #HighOrderMarkovModel(int, Random)}.
|
---|
45 | * </p>
|
---|
46 | *
|
---|
47 | * @see de.ugoe.cs.quest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
|
---|
48 | * de.ugoe.cs.quest.eventcore.Event)
|
---|
49 | */
|
---|
50 | @Override
|
---|
51 | public double getProbability(List<Event> context, Event symbol) {
|
---|
52 | if (context == null) {
|
---|
53 | throw new IllegalArgumentException("context must not be null");
|
---|
54 | }
|
---|
55 | if (symbol == null) {
|
---|
56 | throw new IllegalArgumentException("symbol must not be null");
|
---|
57 | }
|
---|
58 | double result = 0.0d;
|
---|
59 |
|
---|
60 | List<Event> contextCopy;
|
---|
61 | if (context.size() >= trieOrder) {
|
---|
62 | contextCopy =
|
---|
63 | new LinkedList<Event>(context.subList(context.size() - trieOrder + 1,
|
---|
64 | context.size()));
|
---|
65 | }
|
---|
66 | else {
|
---|
67 | contextCopy = new LinkedList<Event>(context);
|
---|
68 | }
|
---|
69 |
|
---|
70 | Collection<Event> followers = trie.getFollowingSymbols(contextCopy);
|
---|
71 | int sumCountFollowers = 0; // N(s\sigma')
|
---|
72 | for (Event follower : followers) {
|
---|
73 | sumCountFollowers += trie.getCount(contextCopy, follower);
|
---|
74 | }
|
---|
75 |
|
---|
76 | int countSymbol = trie.getCount(contextCopy, symbol);
|
---|
77 | if (sumCountFollowers != 0) {
|
---|
78 | result = ((double) countSymbol / sumCountFollowers);
|
---|
79 | }
|
---|
80 |
|
---|
81 | return result;
|
---|
82 | }
|
---|
83 |
|
---|
84 | }
|
---|