[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.usageprofiles;
|
---|
[518] | 16 |
|
---|
| 17 | import java.util.Collection;
|
---|
| 18 | import java.util.LinkedList;
|
---|
| 19 | import java.util.List;
|
---|
| 20 | import java.util.Random;
|
---|
| 21 |
|
---|
[922] | 22 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
[518] | 23 |
|
---|
| 24 | /**
|
---|
| 25 | * <p>
|
---|
| 26 | * Implements high-order Markov models.
|
---|
| 27 | * </p>
|
---|
| 28 | *
|
---|
| 29 | * @author Steffen Herbold
|
---|
| 30 | * @version 1.0
|
---|
| 31 | */
|
---|
| 32 | public class HighOrderMarkovModel extends TrieBasedModel {
|
---|
| 33 |
|
---|
[559] | 34 | /**
|
---|
| 35 | * <p>
|
---|
| 36 | * Id for object serialization.
|
---|
| 37 | * </p>
|
---|
| 38 | */
|
---|
| 39 | private static final long serialVersionUID = 1L;
|
---|
[518] | 40 |
|
---|
[559] | 41 | /**
|
---|
| 42 | * <p>
|
---|
| 43 | * Constructor. Creates a new HighOrderMarkovModel with a defined Markov order.
|
---|
| 44 | * </p>
|
---|
| 45 | *
|
---|
| 46 | * @param maxOrder
|
---|
| 47 | * Markov order of the model
|
---|
| 48 | * @param r
|
---|
| 49 | * random number generator used by probabilistic methods of the class
|
---|
| 50 | */
|
---|
| 51 | public HighOrderMarkovModel(int maxOrder, Random r) {
|
---|
| 52 | super(maxOrder, r);
|
---|
| 53 | }
|
---|
[518] | 54 |
|
---|
[559] | 55 | /**
|
---|
| 56 | * <p>
|
---|
| 57 | * Calculates the probability of the next Event being symbol based on the order of the Markov
|
---|
| 58 | * model. The order is defined in the constructor {@link #HighOrderMarkovModel(int, Random)}.
|
---|
| 59 | * </p>
|
---|
| 60 | *
|
---|
[922] | 61 | * @see de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
|
---|
| 62 | * de.ugoe.cs.autoquest.eventcore.Event)
|
---|
[559] | 63 | */
|
---|
| 64 | @Override
|
---|
| 65 | public double getProbability(List<Event> context, Event symbol) {
|
---|
| 66 | if (context == null) {
|
---|
[766] | 67 | throw new IllegalArgumentException("context must not be null");
|
---|
[559] | 68 | }
|
---|
| 69 | if (symbol == null) {
|
---|
[766] | 70 | throw new IllegalArgumentException("symbol must not be null");
|
---|
[559] | 71 | }
|
---|
| 72 | double result = 0.0d;
|
---|
[518] | 73 |
|
---|
[559] | 74 | List<Event> contextCopy;
|
---|
| 75 | if (context.size() >= trieOrder) {
|
---|
| 76 | contextCopy =
|
---|
| 77 | new LinkedList<Event>(context.subList(context.size() - trieOrder + 1,
|
---|
| 78 | context.size()));
|
---|
| 79 | }
|
---|
| 80 | else {
|
---|
| 81 | contextCopy = new LinkedList<Event>(context);
|
---|
| 82 | }
|
---|
[518] | 83 |
|
---|
[559] | 84 | Collection<Event> followers = trie.getFollowingSymbols(contextCopy);
|
---|
| 85 | int sumCountFollowers = 0; // N(s\sigma')
|
---|
| 86 | for (Event follower : followers) {
|
---|
| 87 | sumCountFollowers += trie.getCount(contextCopy, follower);
|
---|
| 88 | }
|
---|
[518] | 89 |
|
---|
[559] | 90 | int countSymbol = trie.getCount(contextCopy, symbol);
|
---|
| 91 | if (sumCountFollowers != 0) {
|
---|
| 92 | result = ((double) countSymbol / sumCountFollowers);
|
---|
| 93 | }
|
---|
[518] | 94 |
|
---|
[559] | 95 | return result;
|
---|
| 96 | }
|
---|
[518] | 97 |
|
---|
| 98 | }
|
---|