[518] | 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
|
---|
| 31 | * 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
|
---|
| 38 | * class
|
---|
| 39 | */
|
---|
| 40 | public HighOrderMarkovModel(int maxOrder, Random r) {
|
---|
| 41 | super(maxOrder, r);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * <p>
|
---|
| 46 | * Calculates the probability of the next Event being symbol based on the
|
---|
| 47 | * order of the Markov model. The order is defined in the constructor
|
---|
| 48 | * {@link #HighOrderMarkovModel(int, Random)}.
|
---|
| 49 | * </p>
|
---|
| 50 | *
|
---|
| 51 | * @see de.ugoe.cs.quest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
|
---|
| 52 | * de.ugoe.cs.quest.eventcore.Event)
|
---|
| 53 | */
|
---|
| 54 | @Override
|
---|
[547] | 55 | public double getProbability(List<Event> context,
|
---|
| 56 | Event symbol) {
|
---|
[518] | 57 | if (context == null) {
|
---|
| 58 | throw new InvalidParameterException("context must not be null");
|
---|
| 59 | }
|
---|
| 60 | if (symbol == null) {
|
---|
| 61 | throw new InvalidParameterException("symbol must not be null");
|
---|
| 62 | }
|
---|
| 63 | double result = 0.0d;
|
---|
| 64 |
|
---|
[547] | 65 | List<Event> contextCopy;
|
---|
[518] | 66 | if (context.size() >= trieOrder) {
|
---|
[547] | 67 | contextCopy = new LinkedList<Event>(context.subList(
|
---|
[518] | 68 | context.size() - trieOrder + 1, context.size()));
|
---|
| 69 | } else {
|
---|
[547] | 70 | contextCopy = new LinkedList<Event>(context);
|
---|
[518] | 71 | }
|
---|
| 72 |
|
---|
[547] | 73 | Collection<Event> followers = trie.getFollowingSymbols(contextCopy);
|
---|
[518] | 74 | int sumCountFollowers = 0; // N(s\sigma')
|
---|
[547] | 75 | for (Event follower : followers) {
|
---|
[518] | 76 | sumCountFollowers += trie.getCount(contextCopy, follower);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | int countSymbol = trie.getCount(contextCopy, symbol);
|
---|
| 80 | if (sumCountFollowers != 0) {
|
---|
| 81 | result = ((double) countSymbol / sumCountFollowers);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return result;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | }
|
---|