forked from errorcodexero/standing_predictor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiff.cpp
More file actions
225 lines (194 loc) · 4.56 KB
/
diff.cpp
File metadata and controls
225 lines (194 loc) · 4.56 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
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
#include<fstream>
#include<cmath>
#include<boost/tokenizer.hpp>
#include "io.h"
#include "vector.h"
#include "vector_void.h"
#include "interval.h"
#include "optional.h"
#include "map.h"
#include "set_flat.h"
#include "probability.h"
using namespace std;
struct Line{
tba::Team_key team;
tba::Event_key event;
std::string event_name;
double p_dcmp;
std::optional<double> p_cmp;
};
std::optional<Line> parse_line(std::string const& a){
if(a.empty()){
return std::nullopt;
}
auto sp=split(a,',');
assert(sp.size()>=4);
std::string_view s;
return Line{
//tba::Team_ketba::decode2(std::string_view(sp[0]),(tba::Team_key*)0),
//tba::decode2(s,(tba::Team_key*)0),
tba::Team_key(stoi(sp[0])),
tba::Event_key(string_view(sp[1])),
sp[2],
stof(sp[3]),
[=]()->std::optional<double>{
if(sp.size()>4 && sp[4].size()){
PRINT(sp[4]);
return stof(sp[4]);
}
return std::nullopt;
}()
};
}
std::vector<Line> parse_file(std::string path){
ifstream f(path);
std::vector<Line> r;
{
std::string header;
getline(f,header);
}
while(f.good()){
std::string s;
getline(f,s);
try{
auto p=parse_line(s);
if(p){
r|=p;
}
}catch(...){}
}
return r;
}
void histogram(std::vector<double> a){
static const int BOXES=100;
auto lim1=limits(a);
if(!lim1){
return;
}
auto lim=*lim1;
auto box=[&](auto x){
auto v=(x-lim.min)/lim.width();
return int(v*BOXES);
};
auto c=count(mapf(box,a));
auto labels=range(lim.min,lim.max,lim.width()/BOXES);
auto z=zip(labels,values(c));
//print_lines(z);
for(auto [a,b]:z){
cout<<a<<","<<b<<"\n";
}
}
void histogram(std::vector<std::optional<double>>){}
using Team=tba::Team_key;
void result_table(std::vector<std::pair<Team,double>> v){
static const int BOXES=20;
const int box_size=v.size()/BOXES+1;
//std::sort(v.begin(),v.end(),[](auto a,auto b){ return a.second<b.second; },v);
v=sort_by(v,[](auto x){ return x.second; });
while(!v.empty()){
auto here=take(box_size,v);
v=skip(box_size,v);
cout<<limits(seconds(here))<<"\t"<<here.size()<<"\t"<<take(5,firsts(here))<<"\n";
}
}
void result_table(std::vector<std::pair<Team,std::optional<double>>>){
}
void examine_file(){
auto file=parse_file("../standing_predictor_output/2/results.csv");
//would be interesting to know what the distribution of probabilities looks like
//and how much they change each week
//are the set of teams seen different? (and how?)
auto prs=mapf([](auto x){ return x.p_dcmp; },file);
PRINT(sum(prs));
PRINT(limits(prs));
PRINT(quartiles(prs));
PRINT(deciles(prs));
histogram(prs);
}
auto as_map(std::string path){
return dict(mapf([](auto x){ return make_pair(x.team,x.p_dcmp); },parse_file(path)));
//return dict(mapf([](auto x){ return make_pair(x.team,x.p_cmp); },parse_file(path)));
}
#define LINE2(X)\
X(tba::Team_key,team)\
X(tba::Event_key,event)\
X(std::string,event_name)\
X(Pr,dcmp)\
X(Pr,cmp)\
STRUCT_DECLARE(Line2,LINE2)
PRINT_STRUCT(Line2,LINE2)
auto parse_line2(std::string const& path){
ifstream file(path);
string line;
std::vector<Line2> r;
{
string header;
getline(file,header);
}
while(getline(file,line)){
boost::tokenizer<boost::escaped_list_separator<char>> tk(line);
std::vector<std::string> fields;
for (auto i = tk.begin(); i != tk.end(); ++i) {
fields.push_back(*i);
}
r|=Line2{
tba::Team_key(stoi(fields[0])),
tba::Event_key(fields[1]),
fields[2],
stod(fields[3]),
stod(fields[4])
};
}
return r;
}
double fabs(std::optional<double> a){
if(a){
return fabs(*a);
}
return 0;
}
int main1(std::string f1,std::string f2){
//auto f1="../standing_predictor_output/0/results.csv";
auto x1=as_map(f1);
//auto x2=as_map("../standing_predictor_output/4/results.csv");
auto x2=as_map(f2);
auto k1=keys(x1);
auto k2=keys(x2);
diff(k1,k2);
//assert(k1==k2);
auto m=sorted(mapf(
[=](auto t){
auto v1=x1.at(t);
auto v2=x2.at(t);
return make_tuple(v2-v1,t,v1,v2);
},
k1&k2
));
m=sort_by(m,[](auto x){ return fabs(std::get<0>(x)); });
print_lines(m);
auto diffs=mapf([](auto x){ return std::get<0>(x); },m);
histogram(diffs);
result_table(mapf([](auto x){ return make_pair(get<1>(x),get<0>(x)); },m));
{
ofstream f("diff.csv");
for(auto [diff,team,v1,v2]:m){
f<<team<<","<<v1<<","<<v2<<"\n";
}
}
return 0;
}
int main(int argc,char **argv){
if(argc==3){
std::string f1=argv[1];
std::string f2=argv[2];
return main1(f1,f2);
}else{
return main1(
"../standing_predictor_output/0/results.csv",
"../standing_predictor_output/4/results.csv"
);
}
/*auto p=parse_line2("results.csv");
print_lines(p);*/
//return main1();
}