[518] | 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 |
|
---|
[559] | 20 | /**
|
---|
| 21 | * <p>
|
---|
| 22 | * Id for object serialization.
|
---|
| 23 | * </p>
|
---|
| 24 | */
|
---|
| 25 | private static final long serialVersionUID = 1L;
|
---|
[518] | 26 |
|
---|
[559] | 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 | }
|
---|
[518] | 40 |
|
---|
[559] | 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) {
|
---|
[766] | 53 | throw new IllegalArgumentException("context must not be null");
|
---|
[559] | 54 | }
|
---|
| 55 | if (symbol == null) {
|
---|
[766] | 56 | throw new IllegalArgumentException("symbol must not be null");
|
---|
[559] | 57 | }
|
---|
| 58 | double result = 0.0d;
|
---|
[518] | 59 |
|
---|
[559] | 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 | }
|
---|
[518] | 69 |
|
---|
[559] | 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 | }
|
---|
[518] | 75 |
|
---|
[559] | 76 | int countSymbol = trie.getCount(contextCopy, symbol);
|
---|
| 77 | if (sumCountFollowers != 0) {
|
---|
| 78 | result = ((double) countSymbol / sumCountFollowers);
|
---|
| 79 | }
|
---|
[518] | 80 |
|
---|
[559] | 81 | return result;
|
---|
| 82 | }
|
---|
[518] | 83 |
|
---|
| 84 | }
|
---|