-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathMarkovRewardDensityCalculator.java
More file actions
162 lines (122 loc) · 5.72 KB
/
Copy pathMarkovRewardDensityCalculator.java
File metadata and controls
162 lines (122 loc) · 5.72 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
package dr.app.tools;
import java.io.IOException;
import dr.app.util.Arguments;
import dr.inference.markovjumps.SericolaSeriesMarkovReward;
import dr.inference.markovjumps.TwoStateOccupancyMarkovReward;
import dr.inference.markovjumps.TwoStateSericolaSeriesMarkovReward;
/**
* The goal is to calculate the pdf and cdf of time spent in a given state
* for a continuous-time Markov chain with rewards.
*
* There are several implementations that can achieve this.
* We will compare them here and output the results in a json
* file format.
* @author JT McCrone
*/
public class MarkovRewardDensityCalculator {
private double[] Q ;
private final int dim = 2;
private final double[] r = new double[]{0.0,1.0}; // rewards
private TwoStateOccupancyMarkovReward twoStateOccupancyMarkovReward;
private TwoStateSericolaSeriesMarkovReward twoStateSericolaSeriesMarkovReward;
private SericolaSeriesMarkovReward sericolaSeriesMarkovReward;
private MarkovRewardDensityCalculator(double rate,double bias){
this.Q = new double[]{
-rate * bias, rate * bias,
rate * (1.0 - bias), -rate * (1.0 - bias)
};
this.twoStateOccupancyMarkovReward = new TwoStateOccupancyMarkovReward(Q);
this.twoStateSericolaSeriesMarkovReward = new TwoStateSericolaSeriesMarkovReward(Q, r, dim);
this.sericolaSeriesMarkovReward = new SericolaSeriesMarkovReward(Q, r, dim);
}
void run(double time){
double s=0.0;
double[] times = new double[100];
double pdf;
double cdf;
double conditional;
System.out.print("{\"data\":[");
for(int i=0; i<times.length; i++){
times[i] = time * i / 100.0;
System.out.print("{");
//TODO remove duplication
pdf = twoStateOccupancyMarkovReward.computePdf(times[i], time, 0, 0);
// cdf = twoStateOccupancyMarkovReward.computeCdf(times[i], time, 0, 0);
conditional = twoStateOccupancyMarkovReward.computeConditionalProbability( time, 0, 0);
System.out.print("\"time\": " + times[i] + ",");
System.out.print("\"totalTime\": " + time + ",");
// System.out.print("\"twoStateOccupancyMarkovReward\": {");
System.out.print("\"pdf\": " + pdf + ",");
// System.out.print("\"cdf\": " + cdf + ",");
System.out.print("\"conditional\": " + conditional + ",");
System.out.print("\"implementation\": \"twoStateOccupancyMarkovReward\"");
System.out.print("},");
System.out.print("{");
pdf = twoStateSericolaSeriesMarkovReward.computePdf(times[i], time, 0, 0);
cdf = twoStateSericolaSeriesMarkovReward.computeCdf(times[i], time, 0, 0);
conditional = twoStateSericolaSeriesMarkovReward.computeConditionalProbability(time, 0, 0);
System.out.print("\"time\": " + times[i] + ",");
System.out.print("\"totalTime\": " + time + ",");
System.out.print("\"pdf\": " + pdf + ",");
System.out.print("\"cdf\": " + cdf + ",");
System.out.print("\"conditional\": " + conditional + ",");
System.out.print("\"implementation\": \"twoStateSericolaSeriesMarkovReward\"");
System.out.print("},");
System.out.print("{");
pdf = sericolaSeriesMarkovReward.computePdf(times[i], time, 0, 0);
cdf = sericolaSeriesMarkovReward.computeCdf(times[i], time, 0, 0);
conditional = sericolaSeriesMarkovReward.computeConditionalProbability(time, 0, 0);
System.out.print("\"time\": " + times[i] + ",");
System.out.print("\"totalTime\": " + time + ",");
System.out.print("\"pdf\": " + pdf + ",");
System.out.print("\"cdf\": " + cdf + ",");
System.out.print("\"conditional\": " + conditional + ",");
System.out.print("\"implementation\": \"sericolaSeriesMarkovReward\"");
if(i < times.length - 1){
System.out.print("},");
}else{
System.out.print("}");
}
}
System.out.println("]}");
}
public static void main(String[] args) throws IOException, Arguments.ArgumentException {
Arguments arguments = new Arguments(
new Arguments.Option[]{
new Arguments.RealOption("rate","r", "Rate parameter"),
new Arguments.RealOption("bias","b", "Bias parameter"),
new Arguments.RealOption("length","l", "branchlength to evaluate"),
new Arguments.Option("help", "h","option to print this message"),
}
);
try {
arguments.parseArguments(args);
} catch (Arguments.ArgumentException ae) {
System.err.println(ae);
System.exit(1);
}
double rate = arguments.getRealOption("rate");
double bias = arguments.getRealOption("bias");
double length = arguments.getRealOption("length");
MarkovRewardDensityCalculator calculator = new MarkovRewardDensityCalculator(rate, bias);
calculator.run(length);
}
private class Result{
double pdf;
double cdf;
double length;
double s;
double rate;
double bias;
String calculator;
public Result(double pdf, double cdf, double length, double s, double rate, double bias, String calculator) {
this.pdf = pdf;
this.cdf = cdf;
this.length = length;
this.s = s;
this.rate = rate;
this.bias = bias;
this.calculator = calculator;
}
}
}