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