source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/FirstOrderMarkovModel.java @ 25

Last change on this file since 25 was 25, checked in by sherbold, 13 years ago
  • renamed DotPrinter? to IDotCompatible in package de.ugoe.cs.eventbench.models

+ added getDotRepresentation to de.ugoe.cs.eventbench.models.IDotCompatible

  • removed printDot from de.ugoe.cs.eventbench.models.IDotCompatible
  • Property svn:mime-type set to text/plain
File size: 4.6 KB
Line 
1package de.ugoe.cs.eventbench.models;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import de.ugoe.cs.eventbench.data.Event;
8import de.ugoe.cs.util.console.Console;
9import edu.uci.ics.jung.graph.Graph;
10import edu.uci.ics.jung.graph.SparseMultigraph;
11import edu.uci.ics.jung.graph.util.EdgeType;
12
13import Jama.Matrix;
14
15public class FirstOrderMarkovModel extends HighOrderMarkovModel implements IDotCompatible {
16
17        final static int MAX_STATDIST_ITERATIONS = 1000;
18       
19        public FirstOrderMarkovModel(Random r) {
20                super(1, r);
21        }
22       
23        private Matrix getTransmissionMatrix() {
24                List<Event<?>> knownSymbols = new ArrayList<Event<?>>(trie.getKnownSymbols());
25                int numStates = knownSymbols.size();
26                Matrix transmissionMatrix = new Matrix(numStates, numStates);
27               
28                for( int i=0 ; i<numStates ; i++ ) {
29                        Event<?> currentSymbol = knownSymbols.get(i);
30                        List<Event<?>> context = new ArrayList<Event<?>>();
31                        context.add(currentSymbol);
32                        for( int j=0 ; j<numStates ; j++ ) {
33                                Event<?> follower = knownSymbols.get(j);
34                                double prob = getProbability(context, follower);
35                                transmissionMatrix.set(i, j, prob);
36                        }
37                }
38                return transmissionMatrix;
39        }
40       
41        public String getDotRepresentation() {
42                StringBuilder stringBuilder = new StringBuilder();
43                stringBuilder.append("digraph model {");
44
45                List<Event<?>> knownSymbols = new ArrayList<Event<?>>(trie.getKnownSymbols());
46               
47                for( Event<?> symbol : knownSymbols) {
48                        final String thisSaneId = symbol.getShortId().replace("\"", "\\\"").replaceAll("[\r\n]","");
49                        stringBuilder.append(" " + symbol.hashCode() + " [label=\""+thisSaneId+"\"];");
50                        List<Event<?>> context = new ArrayList<Event<?>>();
51                        context.add(symbol);
52                        List<Event<?>> followers = trie.getFollowingSymbols(context);
53                        for( Event<?> follower : followers ) {
54                                System.out.print(" "+symbol.hashCode()+" -> " + follower.hashCode() + " ");
55                                System.out.println("[label=\"" + getProbability(context, follower) + "\"];");
56                        }
57                }
58                stringBuilder.append('}');
59                return stringBuilder.toString();
60        }
61       
62        public Graph<String, MarkovEdge> getGraph() {
63                Graph<String, MarkovEdge> graph = new SparseMultigraph<String, MarkovEdge>();
64               
65                List<Event<?>> knownSymbols = new ArrayList<Event<?>>(trie.getKnownSymbols());
66               
67                for( Event<?> symbol : knownSymbols) {
68                        String from = symbol.getShortId();
69                        List<Event<?>> context = new ArrayList<Event<?>>();
70                        context.add(symbol);
71                       
72                        List<Event<?>> followers = trie.getFollowingSymbols(context);
73                       
74                        for( Event<?> follower : followers ) {
75                                String to = follower.getShortId();
76                                MarkovEdge prob = new MarkovEdge(getProbability(context, follower));
77                                graph.addEdge(prob, from, to, EdgeType.DIRECTED);
78                        }
79                }
80                return graph;
81        }
82       
83        static public class MarkovEdge {
84                double weight;
85                MarkovEdge(double weight) { this.weight = weight; }
86                public String toString() { return ""+weight; }
87        }
88       
89        public double calcEntropy() {
90                Matrix transmissionMatrix = getTransmissionMatrix();
91                List<Event<?>> knownSymbols = new ArrayList<Event<?>>(trie.getKnownSymbols());
92                int numStates = knownSymbols.size();
93               
94                int startStateIndex = knownSymbols.indexOf(Event.STARTEVENT);
95                int endStateIndex = knownSymbols.indexOf(Event.ENDEVENT);
96                if( startStateIndex==-1 ) {
97                        Console.printerrln("Error calculating entropy. Initial state of markov chain not found.");
98                        return Double.NaN;
99                }
100                if( endStateIndex==-1 ) {
101                        Console.printerrln("Error calculating entropy. End state of markov chain not found.");
102                        return Double.NaN;
103                }
104                transmissionMatrix.set(endStateIndex, startStateIndex, 1);
105               
106                // Calculate stationary distribution by raising the power of the transmission matrix.
107                // The rank of the matrix should fall to 1 and each two should be the vector of the
108                // stationory distribution.
109                int iter = 0;
110                int rank = transmissionMatrix.rank();
111                Matrix stationaryMatrix = (Matrix) transmissionMatrix.clone();
112                while( iter<MAX_STATDIST_ITERATIONS && rank>1 ) {
113                        stationaryMatrix = stationaryMatrix.times(stationaryMatrix);
114                        rank = stationaryMatrix.rank();
115                        iter++;
116                }
117               
118                if( rank!=1 ) {
119                        Console.traceln("rank: " + rank);
120                        Console.printerrln("Unable to calculate stationary distribution.");
121                        return Double.NaN;
122                }
123               
124                double entropy = 0.0;
125                for( int i=0 ; i<numStates ; i++ ) {
126                        for( int j=0 ; j<numStates ; j++ ) {
127                                if( transmissionMatrix.get(i,j)!=0 ) {
128                                        double tmp = stationaryMatrix.get(i, 0);
129                                        tmp *= transmissionMatrix.get(i, j);
130                                        tmp *= Math.log(transmissionMatrix.get(i,j))/Math.log(2);
131                                        entropy -= tmp;
132                                }
133                        }
134                }
135                return entropy;
136        }
137
138}
Note: See TracBrowser for help on using the repository browser.