-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaTaBaSe_New_3.cpp
148 lines (112 loc) · 4.83 KB
/
DaTaBaSe_New_3.cpp
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
/*
Bayesian Approximate Query Processing
@author: Rohan Mukherjee, rm38@rice.edu, Rice University
@advisor: Dr. Christopher M. Jermaine
*/
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cassert>
#include <cstdlib> // std::rand, std::srand
#include <cstring>
#include <omp.h>
#include <chrono>
using namespace std;
#include "utils.h"
#include "table.h"
#include "Generat_Table.h"
#include "Random_Table.h"
#include "Correlated_Table.h"
vector<double> log_factorial_vals;
int main(){
log_factorial_vals = eval_logfactorial(NUM_ID);
std::srand ( unsigned ( std::time(0) ) );
int No_of_sample_sizes = 7;
int sample_size_arr[7] = {5000,50000,500000,1000000,2000000,5000000,10000000};
clock_t begin = clock();
std::chrono::time_point<std::chrono::system_clock> start,end;
start=std::chrono::system_clock::now();
int n_max = N_MAX, n_min = N_MIN;
int INP_DIM = NUM_ID;
Generat_TableFixedSN _Table1( TOTAL_SIZE_1, n_max, n_min, INP_DIM , s_mu );
CorrelatedGenTableFixedSN _Table2( TOTAL_SIZE_2, n_max, n_min, INP_DIM, s_mu, &_Table1 );
vector<long double> Orig_Join(NUM_RUNS);
for(int iter=0;iter<NUM_RUNS;iter++){
Orig_Join.at(iter) = _Table1.Join(_Table2);
}
printf("No of keys : %d, Correlation :: %lf, s_mu :: %lf , Prior for correlation %lf / %lf ", INP_DIM, KenT, s_mu, corr_alpha,corr_beta );
printf("Original Join :: %Le \n\n\n", Orig_Join.at(0));
for(int ij=0;ij<No_of_sample_sizes;ij++){
int sample_size = sample_size_arr[ij];
vector<long double> Freq_val(NUM_RUNS), Bay_val_cmp(NUM_RUNS);
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for
for(int iter =0;iter<NUM_RUNS;iter++){
vector<int> Sample_1 = _Table1.get_samples(sample_size,ij);
vector<int> Sample_2 = _Table2.get_samples(sample_size,ij);
long double Freq_Join = get_Freq_Join(Sample_1,Sample_2, sample_size);
Freq_val.at(iter) = Freq_Join;
//************************//
int NUM_MAX = NUM_ID;
int N_MIN=0;
for(int i=0;i<Sample_1.size();i++){
if(Sample_1.at(i)>0 || Sample_2.at(i) > 0)
N_MIN++;
//cout << Sample_1.at(i) << " " << Sample_2.at(i) << endl ;
}
N_MIN = std::max(N_MIN , n_min);
long double Bay_Join_runng_avg_cmp = 0;
Random_Table Pred_Table_1_cmp(TOTAL_SIZE_1,NUM_MAX, N_MIN, log_factorial_vals);
Pred_Table_1_cmp.H = get_ranks(Sample_1,Sample_2);
Correlated_Table Pred_Table_2_cmp(TOTAL_SIZE_2,NUM_MAX, N_MIN, log_factorial_vals) ;//, &Pred_Table_1_cmp);
Pred_Table_2_cmp.H = get_ranks(Sample_2,Sample_1);
int Gibbs_iter = 0;
Pred_Table_1_cmp._N = INP_DIM ;//std::max(_Table1._N , N_MIN);
Pred_Table_2_cmp._N = INP_DIM ;//std::max(_Table2._N , N_MIN);
double avg_temp_rank=0;
int sampling_iter_num = 0;
while(Gibbs_iter<NUM_GIBBS_ITER){
Pred_Table_1_cmp.sample_s(Sample_1);
Pred_Table_2_cmp.sample_s(Sample_2);
//Pred_Table_1_cmp.s = _Table1.s;
//Pred_Table_2_cmp.s = _Table2.s;
// Pred_Table_1_cmp._N = INP_DIM ;//std::max(_Table1._N , N_MIN);
// Pred_Table_2_cmp._N = INP_DIM ;//std::max(_Table2._N , N_MIN);
// Pred_Table_1_cmp.sample_N(Sample_1);
// Pred_Table_2_cmp.sample_N(Sample_2);
//Pred_Table_1_cmp.H = _Table1.H;
// Pred_Table_2_cmp.H = _Table2.H;
Pred_Table_1_cmp.sample_H(Sample_1,Pred_Table_2_cmp.H, Sample_2);
Pred_Table_2_cmp.sample_corr(Sample_2, Pred_Table_1_cmp.H, Sample_1);
// int curr_N = Pred_Table_2_cmp._N;
// Pred_Table_2_cmp.compute_rank2(Pred_Table_1_cmp.H);
// double corr = Pred_Table_2_cmp.get_inv_count(Pred_Table_2_cmp.rank2, curr_N )/(double)((curr_N)*(curr_N-1)/2);
// cout << 1 - corr << endl;
if(Gibbs_iter > BURN_IN){
sampling_iter_num++;
long double frac1 = (sampling_iter_num - 1) / (long double)(sampling_iter_num);
long double temp2 = Pred_Table_1_cmp.Bay_Join(Pred_Table_2_cmp ,Sample_1, Sample_2)/(long double)(sampling_iter_num);
Bay_Join_runng_avg_cmp = Bay_Join_runng_avg_cmp * frac1 + temp2;
}
Gibbs_iter++;
}
Bay_val_cmp.at(iter) = Bay_Join_runng_avg_cmp;
//cout << Bay_val_cmp.at(iter) << endl;
}
cout << " Sample Size:: " << sample_size << " ";
int num_runs = NUM_RUNS; long double sigma;
sigma = get_SE(Bay_val_cmp,Orig_Join,num_runs);
printf("Bayesian Comp SE :: %Le ", sigma);
sigma = get_SE(Freq_val, Orig_Join, num_runs);
printf("Frequentist SE :: %Le \n", sigma);
}
clock_t endp = clock();
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
double elapsed_secs = double(endp - begin) / CLOCKS_PER_SEC;
double elapsed_mins = elapsed_secs/60;
//printf("The elapsed time is :: %lf mins \n", elapsed_mins);
cout << "elapsed time: " << elapsed_seconds.count()/(double) 60 << "m\n";
return 0;
};