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