source: trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles/PredictionByPartialMatch.java @ 655

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