-
Notifications
You must be signed in to change notification settings - Fork 0
/
SliceSampler.java
246 lines (210 loc) · 8.04 KB
/
SliceSampler.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.ArrayList;
import java.util.Random;
/**
*
* @author hoangcuong2011
*/
public class SliceSampler {
class StatisticsPackage {
ArrayList<Integer> cumusCounts = new ArrayList<>();
ArrayList<Double> cumusProbs = new ArrayList<>();
ArrayList<Double> cumusPoints = new ArrayList<>();
}
public StatisticsPackage cumuculativedistribution(ArrayList<Double> sample) {
ArrayList<Integer> cumusCounts = new ArrayList<>();
ArrayList<Double> cumusProbs = new ArrayList<>();
ArrayList<Double> cumusPoints = new ArrayList<>();
StatisticsPackage p = new StatisticsPackage();
double min = 10000;
for (int i = 0; i < sample.size(); i++) {
if (sample.get(i) <= min) {
min = sample.get(i);
}
}
double max = -10000;
for (int i = 0; i < sample.size(); i++) {
if (sample.get(i) >= max) {
max = sample.get(i);
}
}
//pivot = max - min / 10;
int bin_size = 20;
double gaps = (max - min) / (double) bin_size;
for (int b = 0; b < bin_size; b++) {
int count = 0;
double threshold = (min + gaps * (b + 1));
cumusPoints.add(threshold);
for (int i = 0; i < sample.size(); i++) {
//if (sample.get(i) <= (threshold)) - we can lost precision here.
if (threshold - sample.get(i) > -0.0001) {
count++;
}
}
cumusCounts.add(count);
cumusProbs.add((double) count / (double) sample.size());
}
/*for (int b = 0; b < bin_size; b++) {
System.out.println(cumusCounts.get(b));
}*/
p.cumusCounts = cumusCounts;
p.cumusPoints = cumusPoints;
p.cumusProbs = cumusProbs;
return p;
}
public StatisticsPackage cumuculativedistribution(ArrayList<Double> sample, ArrayList<Double> cumusPoints_for_reference) {
ArrayList<Integer> cumusCounts = new ArrayList<>();
ArrayList<Double> cumusProbs = new ArrayList<>();
ArrayList<Double> cumusPoints = new ArrayList<>();
StatisticsPackage p = new StatisticsPackage();
//pivot = max - min / 10;
int bin_size = cumusPoints_for_reference.size();
for (int b = 0; b < bin_size; b++) {
int count = 0;
double threshold = cumusPoints_for_reference.get(b);
cumusPoints.add(threshold);
for (int i = 0; i < sample.size(); i++) {
//if (sample.get(i) <= (threshold)) - we can lost precision here.
if (threshold - sample.get(i) > -0.0001) {
count++;
}
}
cumusCounts.add(count);
cumusProbs.add((double) count / (double) sample.size());
}
/*for (int b = 0; b < bin_size; b++) {
System.out.println(cumusCounts.get(b));
}*/
p.cumusCounts = cumusCounts;
p.cumusPoints = cumusPoints;
p.cumusProbs = cumusProbs;
return p;
}
Random r = new Random();
public double GaussianPDF(double x, double mean, double variance) {
return (Math.exp(-(((x - mean) * (x - mean)) / ((2.0 * variance)))))*(1.0 / (Math.sqrt(variance * 2.0 * Math.PI)));
}
public double sliceSampling_stepout_procedure(double x_0, double y, double mean, double variance) {
double w = 1.0;
double m = 100 ;
double u = r.nextDouble();
double L = x_0 - w*u;
double R = L + w;
double v = r.nextDouble();
int J = (int) Math.floor((m*v));
int K = (int) ((m-1)-J);
//double y = GaussianPDF(x_0, mean, variance);
while(J>0 && y < GaussianPDF(L, mean, variance)) {
L = L - w;
J = J-1;
}
while( K>0 && y<GaussianPDF(R, mean, variance)) {
R = R + w;
K = K - 1;
}
//shrinkage procedure
return shrinkageProcedure(L, R, y, x_0, mean, variance);
}
public double sliceSampling_doubling_procedure(double x_0, double y, double mean, double variance) {
double w = 0.5;
double p = Math.pow(2.0, 10) ;
double u = r.nextDouble();
double L = x_0 - w*u;
double R = L + w;
int K = (int) p;
while( K>0 && (y<GaussianPDF(L, mean, variance) || y<GaussianPDF(R, mean, variance))) {
double v = r.nextDouble();
if(v<0.5) {
L = L - (R - L);
} else {
R = R + (R - L);
}
K--;
}
//shrinkage procedure
return testProcedure(L, R, y, x_0, mean, variance);
}
public double testProcedure(double L, double R, double y, double x_0, double mean, double variance) {
while(true) {
double u = r.nextDouble();
double x_1 = L + u*(R-L);
if(y < GaussianPDF(x_1,mean, variance)) {
return x_1;
}
if(x_1<x_0) {
L = x_1;
}
else {
R = x_1;
}
//System.out.println(x_1);
}
}
public double shrinkageProcedure(double L, double R, double y, double x_0, double mean, double variance) {
while(true) {
double u = r.nextDouble();
double x_1 = L + u*(R-L);
if(y < GaussianPDF(x_1,mean, variance)) {
return x_1;
}
if(x_1<x_0) {
L = x_1;
}
else {
R = x_1;
}
//System.out.println(x_1);
}
}
public double SampleVariance(ArrayList<Double> list, double mean) {
double sum = 0;
for(int i = 0; i < list.size(); i++) {
sum+=Math.pow(list.get(i)-mean, 2.0);
}
sum = sum/((double) list.size());
return sum;
}
public double SampleMean(ArrayList<Double> list) {
double sum = 0;
for(int i = 0; i < list.size(); i++) {
sum+=list.get(i);
}
return sum/((double) list.size());
}
public static void main(String args[]) {
double mean = 0;
double variance = 5;
SliceSampler sampler = new SliceSampler();
double u = 0.0;
ArrayList<Double> list = new ArrayList<>();
double rangeMin = 0.0;
for(int i = 0; i < 10000; i++) {
double rangeMax = sampler.GaussianPDF(u, mean, variance);
double randomValue = rangeMin + (rangeMax - rangeMin) * sampler.r.nextDouble();
u = sampler.sliceSampling_stepout_procedure(u, randomValue, mean, variance);
list.add(u);
}
for(int i = 0; i < 100; i++) {
//System.out.println(list.get(i));
}
System.out.println(sampler.SampleMean(list));
System.out.println(sampler.SampleVariance(list, sampler.SampleMean(list)));
StatisticsPackage p_empirical = sampler.cumuculativedistribution(list);
if (1 == 1) {
Random r = new Random();
ArrayList<Double> another_list_of_means = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
double number = r.nextGaussian() * (Math.sqrt(variance)) + mean;
another_list_of_means.add(number);
}
StatisticsPackage p_reference = sampler.cumuculativedistribution(another_list_of_means);
for (int i = 0; i < p_empirical.cumusPoints.size(); i++) {
System.out.println(p_empirical.cumusPoints.get(i)+"~"+p_reference.cumusPoints.get(i)+"~"+p_empirical.cumusProbs.get(i) + "~" + p_reference.cumusProbs.get(i));
}
}
}
}