-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawDataNormalize.js
166 lines (130 loc) · 4.38 KB
/
rawDataNormalize.js
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
/*******************************************************************************************/
PURCHASEABLE_nationalMetrics.processData = function() {
var data = PURCHASEABLE_nationalMetrics.data;
var min = {};
var max = {};
for (var nationCode in data) {
var nationData = data[nationCode];
for (var column in nationData.metrics) {
min[column] = Infinity;
max[column] = -Infinity;
}
break; //first only
} //establish bounds: min & max base
for (var nationCode in data) {
var nationData = data[nationCode];
for (var metricsName in nationData.metrics) {
var value = nationData.metrics[metricsName];
if (value === null) {
continue;
}
if (min[metricsName] > value) {
min[metricsName] = value;
}
if (max[metricsName] < value) {
max[metricsName] = value;
}
}
} //establish bounds: process
PURCHASEABLE_nationalMetrics.min = min;
PURCHASEABLE_nationalMetrics.max = max;
var bottom = {
'numbeo_localPurchasingPower': 0.1
, 'nationmaster_localPurchasingPower': 0.15
, 'wikipedia_economicFreedom': 0.5
, 'wikipedia_humanDevelopmentIndex': 0.5
};
//some countries received uncommon substitutions - Lichtenstein, Monaco and Vatican
for (var nationCode in data) {
var nationData = data[nationCode];
for (var metricsName in nationData.metrics) {
var value = nationData.metrics[metricsName];
if (value === null) {
nationData.metrics['_normalized_'+ metricsName] = bottom[metricsName];
} else {
var eachMin = min[metricsName];
var eachMax = max[metricsName];
nationData.metrics['_normalized_'+ metricsName] = (value- eachMin)/ (eachMax- eachMin);
}
}
} //normalize to <0,1>
var weightening = {
'_normalized_numbeo_localPurchasingPower': 1
, '_normalized_nationmaster_localPurchasingPower': 1
, '_normalized_wikipedia_economicFreedom': 1
, '_normalized_wikipedia_humanDevelopmentIndex': 1
};
var minAvg = Infinity;
var maxAvg = -Infinity;
for (var nationCode in data) {
var nationData = data[nationCode];
var sum = 0;
var count = 0;
for (var metricsName in nationData.metrics) {
if (metricsName.indexOf('_normalized_') !== 0) {
continue;
}
var value = nationData.metrics[metricsName];
if (value === null) {
continue;
}
count += weightening[metricsName];
sum += value* weightening[metricsName];
}
//var average = count!=0? sum/count: 0.2
var average = sum/count;
minAvg = Math.min(minAvg, average);
maxAvg = Math.max(maxAvg, average);
// var sin = (-Math.cos(average*Math.PI)+1)/2;
var exponential = 0.2* Math.exp(3.4* average);
nationData.metrics["_aggregated_average"] = average;
// nationData.metrics["_aggregated_sin"] = sin;
nationData.metrics["_aggregated_exponential"] = exponential;
} //aggregated metrics
PURCHASEABLE_nationalMetrics.min.avg = minAvg;
PURCHASEABLE_nationalMetrics.max.avg = maxAvg;
var minExp = 0.2* Math.exp(3.4* minAvg);
var maxExp = 0.2* Math.exp(3.4* maxAvg);
//console.log(minAvg, maxAvg);
//console.log(minExp, maxExp);
//console.log(maxExp/minExp);
}; //routine
/*******************************************************************************************/
PURCHASEABLE_nationalMetrics.exportToCsv = function() {
var data = PURCHASEABLE_nationalMetrics.data;
var columns = [];
var exportedLines = [];
for (var nationCode in data) {
var nationData = data[nationCode];
for (var column in nationData) {
if (column === 'metrics') {
continue;
}
columns.push('"'+column+'"');
}
for (var column in nationData.metrics) {
columns.push('"'+column+'"');
}
break; //first only
}
exportedLines.push(columns);
for (var nationCode in data) {
var nationData = data[nationCode];
var line = [];
for (var column in nationData) {
if (column === 'metrics') {
continue;
}
line.push('"'+nationData[column]+'"');
}
for (var column in nationData.metrics) {
var value = nationData.metrics[column];
line.push(value===null?"":value);
}
exportedLines.push(line);
}
exportedLines.map(function(v) {return v.join(',');});
exportedLines = exportedLines.join('\r\n');
return exportedLines;
}; //routine
/*******************************************************************************************/