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

Last change on this file since 2049 was 2049, checked in by sherbold, 8 years ago
  • Property svn:mime-type set to text/plain
File size: 3.5 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 an inverted high-order Markov models. The idea behind this is that the most likely events, become the most unlikely events and the most unlikely events become the most likely events.
27 * </p>
28 * <p>
29 * The empirical probability of an observation is N-n/(k-1)N with N the total number of observations, n the number a specific observation was seen and k the number of different observations.
30 *
31 * @author Steffen Herbold
32 * @version 1.0
33 */
34public class InvertedHighOrderMarkovModel extends TrieBasedModel {
35
36    /**
37     * <p>
38     * Id for object serialization.
39     * </p>
40     */
41    private static final long serialVersionUID = 1L;
42
43    /**
44     * <p>
45     * Constructor. Creates a new HighOrderMarkovModel with a defined Markov order.
46     * </p>
47     *
48     * @param maxOrder
49     *            Markov order of the model
50     * @param r
51     *            random number generator used by probabilistic methods of the class
52     */
53    public InvertedHighOrderMarkovModel(int maxOrder, Random r) {
54        super(maxOrder, r);
55    }
56
57    /**
58     * <p>
59     * Calculates the probability of the next Event being symbol based on the order of the Markov
60     * model. The order is defined in the constructor {@link #HighOrderMarkovModel(int, Random)}.
61     * </p>
62     *
63     * @see de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
64     *      de.ugoe.cs.autoquest.eventcore.Event)
65     */
66    @Override
67    public double getProbability(List<Event> context, Event symbol) {
68        if (context == null) {
69            throw new IllegalArgumentException("context must not be null");
70        }
71        if (symbol == null) {
72            throw new IllegalArgumentException("symbol must not be null");
73        }
74        double result = 0.0d;
75
76        List<Event> contextCopy;
77        if (context.size() >= trieOrder) {
78            contextCopy =
79                new LinkedList<Event>(context.subList(context.size() - trieOrder + 1,
80                                                      context.size()));
81        }
82        else {
83            contextCopy = new LinkedList<Event>(context);
84        }
85
86        Collection<Event> followers = trie.getFollowingSymbols(contextCopy);
87        int sumCountFollowers = 0; // N(s\sigma')
88        for (Event follower : followers) {
89            sumCountFollowers += trie.getCount(contextCopy, follower);
90        }
91
92        int countSymbol = trie.getCount(contextCopy, symbol);
93        if (sumCountFollowers != 0) {
94            result = ((double) (sumCountFollowers-countSymbol)) / ((followers.size()-1)*sumCountFollowers);
95        }
96
97        return result;
98    }
99
100}
Note: See TracBrowser for help on using the repository browser.