-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkov.java
More file actions
386 lines (355 loc) · 14.4 KB
/
Markov.java
File metadata and controls
386 lines (355 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import java.util.*;
/**
* Implements methods to solve problems involving Markov Chain.
*
* @author Daniel Bielech
*/
class Markov {
/**
* Possible direction of movement on the 2D grid.
*/
public static final List<Direction> DIRECTIONS = List.of(Direction.NORTH, Direction.EAST, Direction.WEST, Direction.SOUTH);
/**
* Numbers of iterations for the algorithm loop.
*/
public static final double ITERATIONS = 10000000.0;
/**
* Starting from given state, simulate a random walk of n steps.
* Save the result to a 2D array where each cell consists of the number of times it has been visited.
*
* @param grid The 2D space where the movement of the random walker happen.
* @param nOfSteps Given number of steps.
* @param startingState The state from which the movement will begin.
* @return Array representing the movement distribution after x trials.
*/
public static int[][] metropolisAlgorithm(int[][] grid, int nOfSteps, State startingState) {
int[][] gridCopy = grid.clone();
int i = 0;
int j = 0;
State currentState = startingState;
while (i < ITERATIONS) {
while (j < nOfSteps) {
State newState = getAdjacentRandomState(currentState);
double acceptanceProbability =
getAcceptanceProbability(
calculateSSP(newState, gridCopy.length),
calculateSSP(currentState, gridCopy.length));
double r = Math.random();
if (r < acceptanceProbability) {
currentState = newState;
}
j++;
}
gridCopy[currentState.getX()][currentState.getY()] += 1;
currentState = startingState;
j = 0;
i++;
}
return gridCopy;
}
/**
* Get the sum of all transitions.
*
* @param steps Number of steps in each state for CTMC.
* @return The sum.
*/
private static double getSumOfTransitions(int[] steps) {
int sum = 0;
for (int r : steps) {
sum += r;
}
return sum;
}
/**
* Calculate propensity (sum of rates for state transitions from given state).
*
* @param state Given state.
* @param rates List of rates for CTMC.
* @return Propensity.
*/
private static double getPropensity(int state, double[] rates) {
return rates[state * 2 - 1] + rates[state * 2 - 2];
}
/**
* Calculate the steady state probability for given state, assuming it is equal for all states.
*
* @param state Given state.
* @param gridSize The size of the grid (length of one row, assuming the grid is a square).
* @return Steady state probability.
*/
private static double calculateSSP(State state, int gridSize) {
if ((state.getY()) >= gridSize
|| (state.getY()) < 0
|| (state.getX()) >= gridSize
|| (state.getX()) < 0) {
return 0.0;
} else {
return 1.0 / gridSize * gridSize;
}
}
/**
* Get the steady state probability for given state from the list of steady state probabilities.
*
* @param state Given state.
* @param gridSize The size of the grid (length of one row, assuming the grid is a square).
* @param ssProbs Steady state probabilities.
* @return Steady state probability.
*/
private static double getBiasSSP(State state, int gridSize, double[] ssProbs) {
if ((state.getY()) >= gridSize
|| (state.getY()) < 0
|| (state.getX()) >= gridSize
|| (state.getX()) < 0) {
return 0.0;
} else {
return ssProbs[state.getIndex() - 1];
}
}
/**
* Get a random state within a given grid.
*
* @param gridSize The size of the grid (length of one row, assuming the grid is a square).
* @return Random state.
*/
private static State getRandomState(int gridSize) {
return new State(new Random().nextInt(gridSize), new Random().nextInt(gridSize));
}
/**
* Get a random state adjacent to the given state.
*
* @param currentState Given state.
* @return Random adjacent state.
*/
private static State getAdjacentRandomState(State currentState) {
Direction movementDirection = DIRECTIONS.get(new Random().nextInt(DIRECTIONS.size()));
return getAdjacentState(movementDirection, currentState);
}
/**
* Calculate acceptance probability of going from state A to state B.
*
* @param sspB State B.
* @param sspA State A.
* @return Acceptance probability.
*/
private static double getAcceptanceProbability(double sspB, double sspA) {
return Math.min(1, sspB / sspA);
}
/**
* Get proposal probability to transition to a random state.
*
* @return Transition probability.
*/
private static double getProposeProbability() {
return 1.0 / DIRECTIONS.size();
}
/**
* Get state, adjacent to the current state, in a given direction.
*
* @param direction Given direction.
* @param currentState Current state.
* @return Adjacent state.
*/
private static State getAdjacentState(Direction direction, State currentState) {
switch (direction) {
case NORTH:
return new State(currentState.getX() - 1, currentState.getY());
case EAST:
return new State(currentState.getX(), currentState.getY() + 1);
case WEST:
return new State(currentState.getX(), currentState.getY() - 1);
case SOUTH:
return new State(currentState.getX() + 1, currentState.getY());
default:
throw new RuntimeException("Cannot go in any direction. Terminating!");
}
}
/**
* Get given state direction in relation to the current state.
*
* @param currentState Current state.
* @param newState Given state.
* @return Direction.
*/
private static Direction getDirectionOfMovement(State currentState, State newState) {
if (Math.abs(newState.getX() - currentState.getX()) == 1) {
if (newState.getX() - currentState.getX() == 1) {
return Direction.SOUTH;
} else {
return Direction.NORTH;
}
}
if (Math.abs(newState.getY() - currentState.getY()) == 1) {
if (newState.getY() - currentState.getY() == 1) {
return Direction.EAST;
} else {
return Direction.WEST;
}
}
throw new RuntimeException("Could not detect the direction of the movement");
}
/**
* Tells whether given states are adjacent.
*
* @param s1 State one.
* @param s2 State two.
* @return True if states are adjacent, false otherwise.
*/
private static boolean statesAreAdjacent(State s1, State s2) {
for (Direction d : DIRECTIONS) {
if (s2.equals(getAdjacentState(d, s1))) return true;
}
return false;
}
/**
* Calculate the transition probability of the random walker getting from state s1 to state s2.
* Assume the steady state probabilities are equal.
*
* @param s1 State one.
* @param s2 State two.
* @param n Number of states.
* @return Transition probability.
*/
public static double getTransitionProbability(int s1, int s2, int n) {
int gridSize = (int) Math.sqrt(n); // gridSize * gridSize = nOfStates
State currentState = new State(String.valueOf(s1), gridSize);
State newtState = new State(String.valueOf(s2), gridSize);
if (!statesAreAdjacent(currentState, newtState) && s1 != s2) return 0.0;
var largestTransitionProbability = 0.0;
var totalTransitionProbability = 0.0;
for (Direction d : DIRECTIONS) {
var transitionProbability = getAcceptanceProbability(
calculateSSP(getAdjacentState(d, currentState), gridSize),
calculateSSP(currentState, gridSize)) * getProposeProbability();
if (transitionProbability > largestTransitionProbability)
largestTransitionProbability = transitionProbability;
totalTransitionProbability += transitionProbability;
}
var selfTransitionProbability = 1.0 - totalTransitionProbability;
if (s1 == s2) {
return selfTransitionProbability;
} else {
return largestTransitionProbability;
}
}
/**
* Calculate estimated probability that the walker is in state s2 at time step TS when it started in state s1
*
* @param s1 Starting state.
* @param s2 Ending state.
* @param numStates Number of states.
* @param TS Number of transitions.
* @return Probability of being in state two after given number of transitions.
*/
public static double getEstimatedProbability(int s1, int s2, int numStates, double TS) {
int gridSize = (int) Math.sqrt(numStates);
State startingState = new State(String.valueOf(s1), gridSize);
State desiredState = new State(String.valueOf(s2), gridSize);
int[][] markovChain = new int[gridSize][gridSize];
int[][] result = metropolisAlgorithm(markovChain, (int) TS, startingState);
return result[desiredState.getX()][desiredState.getY()] / ITERATIONS;
}
/**
* Calculate the transition probability of the random walker getting from state s1 to state s2.
* Take into consideration given steady state probabilities.
*
* @param s1 State one.
* @param s2 State two.
* @param ssprob Steady state probabilities.
* @return Transition probability.
*/
public static double getBiasTransitionProbability(int s1, int s2, double[] ssprob) {
int gridSize = 3; // gridSize * gridSize = nOfStates
State currentState = new State(String.valueOf(s1), gridSize);
State newtState = new State(String.valueOf(s2), gridSize);
if (!statesAreAdjacent(currentState, newtState) && s1 != s2) return 0.0;
var totalTransitionProbability = 0.0;
Map<Direction, Double> transitionProbabilities = new HashMap<>();
for (Direction d : DIRECTIONS) {
var transitionProbability = getAcceptanceProbability(
getBiasSSP(getAdjacentState(d, currentState), gridSize, ssprob),
getBiasSSP(currentState, gridSize, ssprob)) * getProposeProbability();
totalTransitionProbability += transitionProbability;
transitionProbabilities.put(d, transitionProbability);
}
var selfTransitionProbability = 1.0 - totalTransitionProbability;
if (s1 == s2) {
return selfTransitionProbability;
} else {
return transitionProbabilities.get(getDirectionOfMovement(currentState, newtState));
}
}
/**
* Calculate transition probability from state s1 to state s2 in Continuous Time Markov Chain.
*
* @param s1 State one.
* @param s2 State two.
* @param rates List of transition rates.
* @return Transition probability.
*/
public static double getContinuousTransitionProbability(int s1, int s2, double[] rates) {
if (s1 == 1 && s2 == 2) {
return rates[0] / getPropensity(s1, rates);
} else if (s1 == 1 && s2 == 3) {
return rates[1] / getPropensity(s1, rates);
} else if (s1 == 2 && s2 == 1) {
return rates[2] / getPropensity(s1, rates);
} else if (s1 == 2 && s2 == 3) {
return rates[3] / getPropensity(s1, rates);
} else if (s1 == 3 && s2 == 1) {
return rates[4] / getPropensity(s1, rates);
} else if (s1 == 3 && s2 == 2) {
return rates[5] / getPropensity(s1, rates);
}
// States are the same. Cannot perform self-transition in CTMC.
return 0.0;
}
/**
* Calculate probability that at time TSC the Markov Chain is in state s2 when it was started in state s1.
*
* @param s1 State one.
* @param s2 State two.
* @param rates List of transition rates.
* @param TSC Given timestamp.
* @return Probability of Markov Chain being in s2 at time TSC.
*/
public static double getContinuousEstimatedProbability(int s1, int s2, double[] rates, double TSC) {
double time = 0;
int currentState = s1;
int[] markovChain = new int[4];
int i = 0;
while (i < ITERATIONS) {
while (time < TSC) {
double propensity = getPropensity(currentState, rates);
double r = Math.random();
double waitingTime = -(1 / propensity) * Math.log(r);
time += waitingTime;
int newState = getRandomAdjacentStateUsingTowerSampling(currentState, rates);
// Make the transition
currentState = newState;
}
markovChain[currentState] += 1;
currentState = s1;
time = 0;
i++;
}
return markovChain[s2] / getSumOfTransitions(markovChain);
}
/**
* Get a random state, adjacent to the current state, based on the transition probabilities.
*
* @param currentState Current state.
* @param rates List of transition rates.
* @return Random adjacent state.
*/
private static int getRandomAdjacentStateUsingTowerSampling(int currentState, double[] rates) {
List<Integer> adjacentStates = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
if (i != currentState) {
adjacentStates.add(i);
}
}
double transProb1 = getContinuousTransitionProbability(currentState, adjacentStates.get(0), rates);
double random = Math.random();
return random < transProb1 ? adjacentStates.get(0) : adjacentStates.get(1);
}
}