00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00039 #include "fastlib/fastlib.h"
00040 #include "dataset_scaler.h"
00041 #include "dualtree_kde.h"
00042 #include "dualtree_vkde.h"
00043 #include "naive_kde.h"
00044
00045 void VariableBandwidthKde(Matrix &queries, Matrix &references,
00046 Matrix &reference_weights,
00047 bool queries_equal_references,
00048 struct datanode *kde_module) {
00049
00050
00051 bool do_naive = fx_param_exists(kde_module, "do_naive");
00052
00053 if(!strcmp(fx_param_str(kde_module, "kernel", "gaussian"), "gaussian")) {
00054
00055 Vector fast_kde_results;
00056
00057
00058 if(fx_param_exists(kde_module, "multiplicative_expansion")) {
00059
00060 printf("O(p^D) expansion KDE\n");
00061 DualtreeVKde<GaussianKernel> fast_kde;
00062 fast_kde.Init(queries, references, reference_weights,
00063 queries_equal_references, kde_module);
00064 fast_kde.Compute(&fast_kde_results);
00065
00066 if(fx_param_exists(kde_module, "fast_kde_output")) {
00067 fast_kde.PrintDebug();
00068 }
00069 }
00070
00071
00072 else {
00073
00074 printf("O(D^p) expansion KDE\n");
00075 DualtreeVKde<GaussianKernel> fast_kde;
00076 fast_kde.Init(queries, references, reference_weights,
00077 queries_equal_references, kde_module);
00078 fast_kde.Compute(&fast_kde_results);
00079
00080 if(true || fx_param_exists(kde_module, "fast_kde_output")) {
00081 fast_kde.PrintDebug();
00082 }
00083 }
00084
00085 if(do_naive) {
00086 NaiveKde<GaussianKernel> naive_kde;
00087 naive_kde.Init(queries, references, reference_weights, kde_module);
00088 naive_kde.Compute();
00089
00090 if(true || fx_param_exists(kde_module, "naive_kde_output")) {
00091 naive_kde.PrintDebug();
00092 }
00093 naive_kde.ComputeMaximumRelativeError(fast_kde_results);
00094 }
00095 }
00096 else if(!strcmp(fx_param_str(kde_module, "kernel", "epan"), "epan")) {
00097 DualtreeVKde<EpanKernel> fast_kde;
00098 Vector fast_kde_results;
00099
00100 fast_kde.Init(queries, references, reference_weights,
00101 queries_equal_references, kde_module);
00102 fast_kde.Compute(&fast_kde_results);
00103
00104 if(fx_param_exists(kde_module, "fast_kde_output")) {
00105 fast_kde.PrintDebug();
00106 }
00107
00108 if(do_naive) {
00109 NaiveKde<EpanKernel> naive_kde;
00110 naive_kde.Init(queries, references, reference_weights, kde_module);
00111 naive_kde.Compute();
00112
00113 if(fx_param_exists(kde_module, "naive_kde_output")) {
00114 naive_kde.PrintDebug();
00115 }
00116 naive_kde.ComputeMaximumRelativeError(fast_kde_results);
00117 }
00118 }
00119 }
00120
00121 void FixedBandwidthKde(Matrix &queries, Matrix &references,
00122 Matrix &reference_weights,
00123 bool queries_equal_references,
00124 struct datanode *kde_module) {
00125
00126
00127 bool do_naive = fx_param_exists(kde_module, "do_naive");
00128
00129 if(!strcmp(fx_param_str(kde_module, "kernel", "gaussian"), "gaussian")) {
00130
00131 Vector fast_kde_results;
00132
00133
00134 if(fx_param_exists(kde_module, "multiplicative_expansion")) {
00135
00136 printf("O(p^D) expansion KDE\n");
00137 DualtreeKde<GaussianKernelMultAux> fast_kde;
00138 fast_kde.Init(queries, references, reference_weights,
00139 queries_equal_references, kde_module);
00140 fast_kde.Compute(&fast_kde_results);
00141
00142 if(fx_param_exists(kde_module, "fast_kde_output")) {
00143 fast_kde.PrintDebug();
00144 }
00145 }
00146
00147
00148 else {
00149
00150 printf("O(D^p) expansion KDE\n");
00151 DualtreeKde<GaussianKernelAux> fast_kde;
00152 fast_kde.Init(queries, references, reference_weights,
00153 queries_equal_references, kde_module);
00154 fast_kde.Compute(&fast_kde_results);
00155
00156 if(true || fx_param_exists(kde_module, "fast_kde_output")) {
00157 fast_kde.PrintDebug();
00158 }
00159 }
00160
00161 if(do_naive) {
00162 NaiveKde<GaussianKernel> naive_kde;
00163 naive_kde.Init(queries, references, reference_weights, kde_module);
00164 naive_kde.Compute();
00165
00166 if(true || fx_param_exists(kde_module, "naive_kde_output")) {
00167 naive_kde.PrintDebug();
00168 }
00169 naive_kde.ComputeMaximumRelativeError(fast_kde_results);
00170 }
00171
00172 }
00173 else if(!strcmp(fx_param_str(kde_module, "kernel", "epan"), "epan")) {
00174 DualtreeKde<EpanKernelAux> fast_kde;
00175 Vector fast_kde_results;
00176
00177 fast_kde.Init(queries, references, reference_weights,
00178 queries_equal_references, kde_module);
00179 fast_kde.Compute(&fast_kde_results);
00180
00181 if(fx_param_exists(kde_module, "fast_kde_output")) {
00182 fast_kde.PrintDebug();
00183 }
00184
00185 if(do_naive) {
00186 NaiveKde<EpanKernel> naive_kde;
00187 naive_kde.Init(queries, references, reference_weights, kde_module);
00188 naive_kde.Compute();
00189
00190 if(fx_param_exists(kde_module, "naive_kde_output")) {
00191 naive_kde.PrintDebug();
00192 }
00193 naive_kde.ComputeMaximumRelativeError(fast_kde_results);
00194 }
00195 }
00196 }
00197
00268 int main(int argc, char *argv[]) {
00269
00270
00271 fx_init(argc, argv, &kde_main_doc);
00272
00274
00275
00276
00277
00278
00279 struct datanode* kde_module = fx_submodule(fx_root, "kde");
00280
00281
00282 const char* references_file_name = fx_param_str_req(fx_root, "data");
00283
00284
00285 const char* queries_file_name =
00286 fx_param_str(fx_root, "query", references_file_name);
00287
00288
00289 Matrix references;
00290 Matrix reference_weights;
00291 Matrix queries;
00292
00293
00294 bool queries_equal_references =
00295 !strcmp(queries_file_name, references_file_name);
00296
00297
00298 data::Load(references_file_name, &references);
00299 if(queries_equal_references) {
00300 queries.Alias(references);
00301 }
00302 else {
00303 data::Load(queries_file_name, &queries);
00304 }
00305
00306
00307
00308 if(fx_param_exists(fx_root, "dwgts")) {
00309 data::Load(fx_param_str(fx_root, "dwgts", NULL), &reference_weights);
00310 }
00311 else {
00312 reference_weights.Init(1, references.n_cols());
00313 reference_weights.SetAll(1);
00314 }
00315
00316
00317 if(!strcmp(fx_param_str(kde_module, "scaling", "none"), "range")) {
00318 DatasetScaler::ScaleDataByMinMax(queries, references,
00319 queries_equal_references);
00320 }
00321 else if(!strcmp(fx_param_str(kde_module, "scaling", "none"),
00322 "standardize")) {
00323 DatasetScaler::StandardizeData(queries, references,
00324 queries_equal_references);
00325 }
00326
00327
00328 if(!strcmp(fx_param_str(kde_module, "mode", "fixedbw"), "variablebw")) {
00329 VariableBandwidthKde(queries, references, reference_weights,
00330 queries_equal_references, kde_module);
00331 }
00332 else {
00333 FixedBandwidthKde(queries, references, reference_weights,
00334 queries_equal_references, kde_module);
00335 }
00336
00337 fx_done(fx_root);
00338 return 0;
00339 }