source: trunk/autoquest-core-usageprofiles/src/main/java/de/ugoe/cs/autoquest/usageprofiles/HighOrderMarkovModel.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 3.1 KB
Line 
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
15package de.ugoe.cs.autoquest.usageprofiles;
16
17import java.util.Collection;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.Random;
21
22import de.ugoe.cs.autoquest.eventcore.Event;
23
24/**
25 * <p>
26 * Implements high-order Markov models.
27 * </p>
28 *
29 * @author Steffen Herbold
30 * @version 1.0
31 */
32public class HighOrderMarkovModel extends TrieBasedModel {
33
34    /**
35     * <p>
36     * Id for object serialization.
37     * </p>
38     */
39    private static final long serialVersionUID = 1L;
40
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    }
54
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     *
61     * @see de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
62     *      de.ugoe.cs.autoquest.eventcore.Event)
63     */
64    @Override
65    public double getProbability(List<Event> context, Event symbol) {
66        if (context == null) {
67            throw new IllegalArgumentException("context must not be null");
68        }
69        if (symbol == null) {
70            throw new IllegalArgumentException("symbol must not be null");
71        }
72        double result = 0.0d;
73
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        }
83
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        }
89
90        int countSymbol = trie.getCount(contextCopy, symbol);
91        if (sumCountFollowers != 0) {
92            result = ((double) countSymbol / sumCountFollowers);
93        }
94
95        return result;
96    }
97
98}
Note: See TracBrowser for help on using the repository browser.