source: trunk/autoquest-core-usageprofiles/src/main/java/de/ugoe/cs/autoquest/usageprofiles/PredictionByPartialMatch.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: 7.0 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 Prediction by Partial Match (PPM) based on the following formula (LaTeX-style
27 * notation):<br>
28 * P_{PPM}(X_n|X_{n-1},...,X_{n-k}) = \sum_{i=k}^min escape^{k-i} P_{MM^i}(X_n
29 * |X_{n-1},...,X_{n-i})(1-escape)+escape^(k-min)P(X_n|X_{n-i},... ,X_{n-min})<br>
30 * P_{MM^i} denotes the probability in an i-th order Markov model.
31 * </p>
32 *
33 * @author Steffen Herbold
34 *
35 */
36public class PredictionByPartialMatch extends TrieBasedModel {
37
38    /**
39     * <p>
40     * Id for object serialization.
41     * </p>
42     */
43    private static final long serialVersionUID = 1L;
44
45    /**
46     * <p>
47     * Minimum order of the Markov model.
48     * </p>
49     */
50    protected int minOrder;
51
52    /**
53     * <p>
54     * Probability to use a lower-order Markov model
55     * </p>
56     */
57    protected double probEscape;
58
59    /**
60     * <p>
61     * Constructor. Creates a new PredictionByPartialMatch model with a given Markov order and a
62     * default escape probability of 0.1.
63     * </p>
64     *
65     * @param markovOrder
66     *            Markov order of the model
67     * @param r
68     *            random number generator used by probabilistic methods of the class
69     */
70    public PredictionByPartialMatch(int markovOrder, Random r) {
71        this(markovOrder, r, 0.1);
72    }
73
74    /**
75     * <p>
76     * Creates a new PredictionByPartialMatch model with a given Markov order and escape
77     * probability.
78     * </p>
79     *
80     * @param markovOrder
81     *            Markov order of the model
82     * @param r
83     *            random number generator used by probabilistic methods of the class
84     * @param probEscape
85     *            escape probability used by the model
86     */
87    public PredictionByPartialMatch(int markovOrder, Random r, double probEscape) {
88        this(markovOrder, 0, r, probEscape);
89    }
90
91    /**
92     * <p>
93     * Creates a new PredictionByPartialMatch model with a given Markov order and escape
94     * probability.
95     * </p>
96     *
97     * @param markovOrder
98     *            Markov order of the model
99     * @param minOrder
100     *            minimum order of the model; if this order is reached, there is no escape
101     * @param r
102     *            random number generator used by probabilistic methods of the class
103     * @param probEscape
104     *            escape probability used by the model
105     * @throws IllegalArgumentException
106     *             thrown if minOrder is less than 0 or greater than markovOrder or probEscape is
107     *             not in the interval (0,1)
108     */
109    public PredictionByPartialMatch(int markovOrder, int minOrder, Random r, double probEscape) {
110        super(markovOrder, r);
111        if (minOrder < 0) {
112            throw new IllegalArgumentException("minOrder must be greather than or equal to 0");
113        }
114        if (minOrder > markovOrder) {
115            throw new IllegalArgumentException(
116                                                "minOrder must be less than or equal to markovOrder");
117        }
118        if (probEscape <= 0.0 || probEscape >= 1.0) {
119            throw new IllegalArgumentException("probEscape must be in the interval (0,1)");
120        }
121        this.probEscape = probEscape;
122        this.minOrder = minOrder;
123    }
124
125    /**
126     * <p>
127     * Sets the escape probability of the model.
128     * </p>
129     *
130     * @param probEscape
131     *            new escape probability
132     */
133    public void setProbEscape(double probEscape) {
134        this.probEscape = probEscape;
135    }
136
137    /**
138     * <p>
139     * Returns the escape probability of the model.
140     * </p>
141     *
142     * @return escape probability of the model
143     */
144    public double getProbEscape() {
145        return probEscape;
146    }
147
148    /**
149     * <p>
150     * Calculates the probability of the next event based on the formula:<br>
151     * P_{PPM}(X_n|X_{n-1},...,X_{n-k}) = \sum_{i=k}^min escape^{k-i} P_{MM^i}(X_n
152     * |X_{n-1},...,X_{n-i})(1-escape)+escape^(k-min)P(X_n|X_{n-i},... ,X_{n-min})<br>
153     * P_{MM^i} denotes the probability in an i-th order Markov model.
154     * </p>
155     *
156     * @see de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess#getProbability(java.util.List,
157     *      de.ugoe.cs.autoquest.eventcore.Event)
158     */
159    @Override
160    public double getProbability(List<Event> context, Event symbol) {
161        if (context == null) {
162            throw new IllegalArgumentException("context must not be null");
163        }
164        if (symbol == null) {
165            throw new IllegalArgumentException("symbol must not be null");
166        }
167        double result = 0.0d;
168        double resultCurrentContex = 0.0d;
169        double resultShorterContex = 0.0d;
170
171        List<Event> contextCopy;
172        if (context.size() >= trieOrder) {
173            contextCopy =
174                new LinkedList<Event>(context.subList(context.size() - trieOrder + 1,
175                                                      context.size()));
176        }
177        else {
178            contextCopy = new LinkedList<Event>(context);
179        }
180
181        Collection<Event> followers = trie.getFollowingSymbols(contextCopy); // \Sigma'
182        int sumCountFollowers = 0; // N(s\sigma')
183        for (Event follower : followers) {
184            sumCountFollowers += trie.getCount(contextCopy, follower);
185        }
186
187        int countSymbol = trie.getCount(contextCopy, symbol); // N(s\sigma)
188        if (sumCountFollowers == 0) {
189            resultCurrentContex = 0.0;
190        }
191        else {
192            resultCurrentContex = (double) countSymbol / sumCountFollowers;
193        }
194        if (contextCopy.size() > minOrder) {
195            resultCurrentContex *= (1 - probEscape);
196            contextCopy.remove(0);
197            if (contextCopy.size() >= minOrder) {
198                double probSuffix = getProbability(contextCopy, symbol);
199
200                if (followers.size() == 0) {
201                    resultShorterContex = probSuffix;
202                }
203                else {
204                    resultShorterContex = probEscape * probSuffix;
205                }
206            }
207        }
208        result = resultCurrentContex + resultShorterContex;
209
210        return result;
211    }
212}
Note: See TracBrowser for help on using the repository browser.