-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkld-sampling.cc
138 lines (107 loc) · 3.48 KB
/
kld-sampling.cc
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
/*********************************************************************
KLD-SAMPLING: Adequately Sampling from an Unknown Distribution.
Copyright (C) 2006 - Patrick Beeson ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
*********************************************************************/
#include "kld-sampling.hh"
#include <fstream>
#include <iostream>
#include <math.h>
vector<float> kld_sampling::ztable;
// Constructs Z-table (from ztable.data) to lookup statistics.
kld_sampling::kld_sampling() {
if (kld_sampling::ztable.empty())
build_table();
}
/**
Initialize a round of KLD sampling. Takes in kld-parameters:
quantile, kld-error, bin size, minimum number of samples
**/
void kld_sampling::init(float quantile, float err, const vector<float>& bsz, int sample_min) {
support_samples=0;
num_samples=0;
if (sample_min < absolute_min)
kld_samples=absolute_min;
else kld_samples=sample_min;
bins.clear();
confidence=quantile-0.5; // ztable is from right side of mean
confidence=fmin(0.49998,fmax(0,confidence));
max_error=err;
bin_size=bsz;
zvalue=4.1;
for (unsigned int i=0; i<kld_sampling::ztable.size();i++)
if (kld_sampling::ztable[i] >= confidence) {
zvalue=i/100.0;
break;
}
}
/**
Update kld-sampler with the last sample drawn. Returns a guess at
the number of samples needed before the distribution (which is
unknown) is adequately sampled.
**/
int kld_sampling::update(const vector<float>& sample) {
if (bin_size.empty()) {
cerr << "kld-sampling.cc: Must run init() before update()\n";
exit (-1);
}
if (sample.size() != bin_size.size()){
cerr << "kld-sampling.cc: Sample size not the same number of dimensions as the bins\n";
exit(-1);
}
curr_sample=sample;
num_samples++;
if (in_empty_bin()) {
support_samples++;
if (support_samples >=2) {
int k=support_samples-1;
k=(int)ceil(k/(2*max_error)*pow(1-2/(9.0*k)+sqrt(2/(9.0*k))*zvalue,3));
if (k > kld_samples)
kld_samples=k;
}
}
return kld_samples;
}
/**
Builds a z-table which is necessary for the statiscal kld-sampling.
**/
void kld_sampling::build_table() {
float tmp;
ifstream ifile("ztable.data");
if (ifile.is_open()) {
while (!ifile.eof()) {
ifile >> tmp;
kld_sampling::ztable.push_back(tmp);
}
}
else {
cerr << "kld-sampling.cc: ztable.data does not exist. Error!\n";
exit(-1);
}
}
/**
Determines whether a sample falls into a bin that has already been
sampled.
**/
bool kld_sampling::in_empty_bin() {
vector<float> curr_bin;
for (unsigned int i=0; i<curr_sample.size(); i++)
curr_bin.push_back(floor(curr_sample[i]/bin_size[i]));
for (unsigned int i=0; i < bins.size(); i++)
if (curr_bin==bins[i]) {
return false;
}
bins.push_back(curr_bin);
return true;
}