18 name =
"Descriptives";
29 const type& new_mean,
const type& new_standard_deviation)
31 name =
"Descriptives";
45void Descriptives::set(
const type& new_minimum,
const type& new_maximum,
46 const type& new_mean,
const type& new_standard_deviation)
98 Tensor<type, 1> statistics_vector(4);
100 statistics_vector[1] =
maximum;
101 statistics_vector[2] =
mean;
104 return statistics_vector;
113 if(
abs(
minimum + type(1)) < type(NUMERIC_LIMITS_MIN) &&
abs(
maximum - type(1)) < type(NUMERIC_LIMITS_MIN))
142 cout << title << endl
143 <<
"Minimum: " <<
minimum << endl
144 <<
"Maximum: " <<
maximum << endl
145 <<
"Mean: " <<
mean << endl
149BoxPlot::BoxPlot(
const type& new_minimum,
150 const type& new_first_cuartile,
151 const type& new_median,
152 const type& new_third_quartile,
153 const type& new_maximum)
155 minimum = new_minimum;
156 first_quartile = new_first_cuartile;
158 third_quartile = new_third_quartile;
159 maximum = new_maximum;
163void BoxPlot::set(
const type& new_minimum,
164 const type& new_first_cuartile,
165 const type& new_median,
166 const type& new_third_quartile,
167 const type& new_maximum)
169 minimum = new_minimum;
170 first_quartile = new_first_cuartile;
172 third_quartile = new_third_quartile;
173 maximum = new_maximum;
183 ofstream file(file_name.c_str());
187 ostringstream buffer;
189 buffer <<
"OpenNN Exception: Statistics Class.\n"
190 <<
"void save(const string&) const method.\n"
191 <<
"Cannot open descriptives data file.\n";
193 throw logic_error(buffer.str());
198 file <<
"Minimum: " <<
minimum << endl
199 <<
"Maximum: " <<
maximum << endl
200 <<
"Mean: " <<
mean << endl
232 const Tensor<Index, 1>&new_frequencies)
244 const Index& number_of_bins)
246 const type data_maximum = maximum(data);
247 const type data_minimum = minimum(data);
248 const type step = (data_maximum - data_minimum) / type(number_of_bins);
250 Tensor<type, 1> new_centers(number_of_bins);
252 for(Index i = 0; i < number_of_bins; i++)
254 new_centers(i) = data_minimum + (type(0.5) * step) + (step * type(i));
257 Tensor<Index, 1> new_frequencies(number_of_bins);
258 new_frequencies.setZero();
261 Index corresponding_bin;
263 for(Index i = 0; i < data.dimension(0); i++)
266 corresponding_bin = int((value - data_minimum) / step);
268 new_frequencies(corresponding_bin)++;
281 const size_t number_of_bins = 10;
282 type data_maximum = maximum(probability_data);
283 const type data_minimum = type(0);
285 if(data_maximum > type(1))
287 data_maximum = type(100.0);
291 data_maximum = type(1);
294 const type step = (data_maximum - data_minimum) / type(number_of_bins);
296 Tensor<type, 1> new_centers(number_of_bins);
298 for(
size_t i = 0; i < number_of_bins; i++)
300 new_centers(i) = data_minimum + (type(0.5) * step) + (step * type(i));
303 Tensor<Index, 1> new_frequencies(number_of_bins);
304 new_frequencies.setZero();
307 Index corresponding_bin;
309 for(Index i = 0; i < probability_data.dimension(0); i++)
311 value = probability_data(i);
312 corresponding_bin = int((value - data_minimum) / step);
314 new_frequencies(corresponding_bin)++;
338 for(Index i = 0; i < size; i++)
367 const Tensor<Index, 0> max_element =
frequencies.maximum();
384 Index minimal_indices_size = 0;
388 Tensor<type, 1> nan(1);
389 nan.setValues({
static_cast<type
>(NAN)});
397 minimal_indices_size++;
403 Tensor<type, 1> minimal_centers(minimal_indices_size);
409 minimal_centers(index) =
static_cast<type
>(
centers(i));
415 return minimal_centers;
425 Index maximal_indices_size = 0;
429 Tensor<type, 1> nan(1);
430 nan.setValues({
static_cast<type
>(NAN)});
438 maximal_indices_size++;
444 Tensor<type, 1> maximal_centers(maximal_indices_size);
450 maximal_centers(index) =
static_cast<type
>(
centers(i));
456 return maximal_centers;
467 if(bins_number == 0)
return 0;
469 const type minimum_center =
centers[0];
470 const type maximum_center =
centers[bins_number - 1];
472 const type length =
static_cast<type
>(maximum_center - minimum_center)/
static_cast<type
>(bins_number - 1.0);
474 type minimum_value =
centers[0] - length / type(2);
475 type maximum_value = minimum_value + length;
477 if(value < maximum_value)
return 0;
479 for(Index j = 1; j < bins_number - 1; j++)
481 minimum_value = minimum_value + length;
482 maximum_value = maximum_value + length;
484 if(value >= minimum_value && value < maximum_value)
return j;
487 if(value >= maximum_value)
489 return bins_number - 1;
493 ostringstream buffer;
495 buffer <<
"OpenNN Exception: Statistics Class.\n"
496 <<
"Index Histogram::calculate_bin(const type&) const.\n"
497 <<
"Unknown return value.\n";
499 throw logic_error(buffer.str());
511 if(bins_number == 0)
return 0;
521void Histogram::save(
const string& histogram_file_name)
const
523 const Index number_of_bins =
centers.dimension(0);
524 ofstream histogram_file(histogram_file_name);
526 histogram_file <<
"centers,frequencies" << endl;
527 for(Index i = 0; i < number_of_bins; i++)
529 histogram_file <<
centers(i) <<
",";
533 histogram_file.close();
540type minimum(
const Tensor<type, 1>& vector)
542 const Index size = vector.dimension(0);
544 if(size == 0)
return type(NAN);
546 type minimum = numeric_limits<type>::max();
548 for(Index i = 0; i < size; i++)
550 if(vector(i) < minimum && !
isnan(vector(i)))
563Index minimum(
const Tensor<Index, 1>& vector)
565 const Index size = vector.size();
567 if(size == 0)
return NAN;
569 Index minimum = numeric_limits<Index>::max();
571 for(Index i = 0; i < size; i++)
573 if(vector(i) < minimum)
587type minimum(
const Tensor<type, 1>& vector,
const Tensor<Index, 1>& indices)
589 const Index size = indices.dimension(0);
591 if(size == 0)
return type(NAN);
593 type minimum = numeric_limits<type>::max();
597 for(Index i = 0; i < size; i++)
601 if(vector(index) < minimum && !
isnan(vector(index)))
603 minimum = vector(index);
614type maximum(
const Tensor<type, 1>& vector)
616 const Index size = vector.dimension(0);
618 if(size == 0)
return type(NAN);
620 type maximum = -numeric_limits<type>::max();
622 for(Index i = 0; i < size; i++)
624 if(!
isnan(vector(i)) && vector(i) > maximum)
638type maximum(
const Tensor<type, 1>& vector,
const Tensor<Index, 1>& indices)
640 const Index size = indices.dimension(0);
642 if(size == 0)
return type(NAN);
644 type maximum = -numeric_limits<type>::max();
648 for(Index i = 0; i < size; i++)
652 if(!
isnan(vector(index)) && vector(index) > maximum)
654 maximum = vector(index);
664Index maximum(
const Tensor<Index, 1>& vector)
666 const Index size = vector.size();
668 if(size == 0)
return NAN;
670 Index maximum = -numeric_limits<Index>::max();
672 for(Index i = 0; i < size; i++)
674 if(vector(i) > maximum)
691Tensor<type, 1> columns_maximums(
const Tensor<type, 2>& matrix,
692 const Tensor<Index, 1>& rows_indices,
693 const Tensor<Index, 1>& columns_indices)
695 const Index rows_number = matrix.dimension(0);
696 const Index columns_number = matrix.dimension(1);
698 Tensor<Index, 1> used_columns_indices;
700 if(columns_indices.dimension(0) == 0)
702 used_columns_indices.resize(columns_number);
704 for(Index i = 0; i < columns_number; i++)
706 used_columns_indices(i) = i;
711 used_columns_indices = columns_indices;
714 Tensor<Index, 1> used_rows_indices;
716 if(rows_indices.dimension(0) == 0)
718 used_rows_indices.resize(rows_number);
720 for(Index i = 0; i < rows_number; i++)
722 used_rows_indices(i) = i;
727 used_rows_indices = rows_indices;
730 const Index rows_indices_size = used_rows_indices.size();
731 const Index columns_indices_size = used_columns_indices.size();
733 Tensor<type, 1> maximums(columns_indices_size);
738 Tensor<type, 1> column(rows_indices_size);
740 for(Index j = 0; j < columns_indices_size; j++)
742 column_index = used_columns_indices(j);
744 for(Index i = 0; i < rows_indices_size; i++)
746 row_index = used_rows_indices(i);
748 column(i) = matrix(row_index,column_index);
751 maximums(j) = maximum(column);
763type mean(
const Tensor<type, 1>& vector,
const Index& begin,
const Index& end)
769 ostringstream buffer;
771 buffer <<
"OpenNN Exception: Statistics class.\n"
772 <<
"type mean(const Tensor<type, 1>& vector, const Index& begin, const Index& end) \n"
773 <<
"Begin must be less or equal than end.\n";
775 throw logic_error(buffer.str());
780 if(end == begin)
return vector[begin];
784 for(Index i = begin; i <= end; i++)
789 return sum /
static_cast<type
>(end-begin+1);
796type mean(
const Tensor<type, 1>& vector)
798 const Index size = vector.dimension(0);
800 if(size == 0)
return type(0);
806 ostringstream buffer;
808 buffer <<
"OpenNN Exception: Statistics Class.\n"
809 <<
"type mean(const Tensor<type, 1>& vector, const Index& begin, const Index& end) "
811 <<
"Size must be greater than zero.\n";
813 throw logic_error(buffer.str());
822 for(Index i = 0; i < size; i++)
824 if(!
isnan(vector(i)))
831 const type mean = sum /
static_cast<type
>(count);
840type variance(
const Tensor<type, 1>& vector)
842 const Index size = vector.dimension(0);
848 ostringstream buffer;
850 buffer <<
"OpenNN Exception: Statistics Class.\n"
851 <<
"type variance(const Tensor<type, 1>& vector) "
853 <<
"Size must be greater than zero.\n";
855 throw logic_error(buffer.str());
861 type squared_sum = type(0);
865 for(Index i = 0; i < size; i++)
867 if(!
isnan(vector(i)))
870 squared_sum += vector(i) * vector(i);
876 if(count <= 1)
return type(0);
878 const type variance = squared_sum/
static_cast<type
>(count - 1)
879 - (sum/
static_cast<type
>(count))*(sum/
static_cast<type
>(count))*
static_cast<type
>(count)/
static_cast<type
>(count-1);
888type variance(
const Tensor<type, 1>& vector,
const Tensor<Index, 1>& indices)
890 const Index size = indices.dimension(0);
896 ostringstream buffer;
898 buffer <<
"OpenNN Exception: Statistics Class.\n"
899 <<
"type variance(const Tensor<type, 1>&, const Tensor<Index, 1>&) "
901 <<
"Indeces size must be greater than zero.\n";
903 throw logic_error(buffer.str());
909 type squared_sum = type(0);
915 for(Index i = 0; i < size; i++)
919 if(!
isnan(vector(index)))
921 sum += vector(index);
922 squared_sum += vector(index) * vector(index);
928 if(count <= 1)
return type(0);
930 const type variance = squared_sum/
static_cast<type
>(count - 1) -(sum/
static_cast<type
>(count))*(sum/
static_cast<type
>(count))*
static_cast<type
>(count)/
static_cast<type
>(count-1);
939type standard_deviation(
const Tensor<type, 1>& vector)
943 const Index size = vector.dimension(0);
947 ostringstream buffer;
949 buffer <<
"OpenNN Exception: Statistics Class.\n"
950 <<
"type standard_deviation(const Tensor<type, 1>&) const method.\n"
951 <<
"Size must be greater than zero.\n";
953 throw logic_error(buffer.str());
958 if(vector.size() == 0)
return type(0);
960 if(variance(vector) <
static_cast<type
>(1e-9)){
961 return static_cast<type
>(0);
963 return sqrt(variance(vector));
971type standard_deviation(
const Tensor<type, 1>& vector,
const Tensor<Index, 1>& indices)
975 const Index size = vector.dimension(0);
979 ostringstream buffer;
981 buffer <<
"OpenNN Exception: Statistics Class.\n"
982 <<
"type standard_deviation(const Tensor<type, 1>&, const Tensor<Index, 1>&) const method.\n"
983 <<
"Size must be greater than zero.\n";
985 throw logic_error(buffer.str());
990 if(variance(vector, indices) <
static_cast<type
>(1e-9))
992 return static_cast<type
>(0);
995 return sqrt(variance(vector, indices));
1000Tensor<type, 1> standard_deviation(
const Tensor<type, 1>& vector,
const Index& period)
1002 const Index size = vector.dimension(0);
1004 Tensor<type, 1>
std(size);
1006 type mean_value = type(0);
1009 for(Index i = 0; i < size; i++)
1011 const Index begin = i < period ? 0 : i - period + 1;
1012 const Index end = i;
1014 mean_value = mean(vector, begin,end);
1016 for(Index j = begin; j < end+1; j++)
1018 sum += (vector(j) - mean_value) *(vector(j) - mean_value);
1021 std(i) =
sqrt(sum / type(period));
1023 mean_value = type(0);
1034type asymmetry(
const Tensor<type, 1>& vector)
1036 const Index size = vector.dimension(0);
1042 ostringstream buffer;
1044 buffer <<
"OpenNN Exception: Statistics Class.\n"
1045 <<
"type asymmetry(const Tensor<type, 1>& vector) const method.\n"
1046 <<
"Size must be greater than zero.\n";
1048 throw logic_error(buffer.str());
1058 const type standard_deviation_value = standard_deviation(vector);
1060 const type mean_value = mean(vector);
1066 for(Index i = 0; i < size; i++)
1068 if(!
isnan(vector(i)))
1070 sum += (vector(i) - mean_value) *(vector(i) - mean_value) *(vector(i) - mean_value);
1076 const type numerator = sum / type(count);
1077 const type denominator = standard_deviation_value * standard_deviation_value * standard_deviation_value;
1079 return numerator/denominator;
1086type kurtosis(
const Tensor<type, 1>& vector)
1088 const Index size = vector.dimension(0);
1093 ostringstream buffer;
1095 buffer <<
"OpenNN Exception: Statistics Class.\n"
1096 <<
"type kurtosis(const Tensor<type, 1>& vector) const method.\n"
1097 <<
"Size must be greater than zero.\n";
1099 throw logic_error(buffer.str());
1109 const type standard_deviation_value = standard_deviation(vector);
1111 const type mean_value = mean(vector);
1117 for(Index i = 0; i < size; i++)
1119 if(!
isnan(vector(i)))
1121 sum += (vector(i) - mean_value)*(vector(i) - mean_value)*(vector(i) - mean_value)*(vector(i) - mean_value);
1127 const type numerator = sum / type(count);
1128 const type denominator = standard_deviation_value*standard_deviation_value*standard_deviation_value*standard_deviation_value;
1130 return numerator/denominator - type(3);
1137type median(
const Tensor<type, 1>& vector)
1139 const Index size = vector.dimension(0);
1145 for(Index i = 0; i < size; i++)
1147 if(!
isnan(vector(i))) new_size++;
1150 Tensor<type, 1> sorted_vector;
1151 sorted_vector.resize(new_size);
1153 Index sorted_index = 0;
1155 for(Index i = 0; i < size; i++)
1157 if(!
isnan(vector(i)))
1159 sorted_vector(sorted_index) = vector(i);
1167 sort(sorted_vector.data(), sorted_vector.data() + sorted_vector.size(), less<type>());
1171 if(new_size % 2 == 0)
1173 median_index =
static_cast<Index
>(new_size / 2);
1175 return (sorted_vector(median_index-1) + sorted_vector(median_index)) /
static_cast<type
>(2.0);
1179 median_index =
static_cast<Index
>(new_size / 2);
1181 return sorted_vector(median_index);
1189Tensor<type, 1> quartiles(
const Tensor<type, 1>& vector)
1191 const Index size = vector.dimension(0);
1197 for(Index i = 0; i < size; i++)
1199 if(!
isnan(vector(i))) new_size++;
1202 Tensor<type, 1> sorted_vector;
1203 sorted_vector.resize(new_size);
1205 Index sorted_index = 0;
1207 for(Index i = 0; i < size; i++)
1209 if(!
isnan(vector(i)))
1211 sorted_vector(sorted_index) = vector(i);
1217 sort(sorted_vector.data(), sorted_vector.data() + new_size, less<type>());
1221 Tensor<type, 1> first_sorted_vector(new_size/2);
1222 Tensor<type, 1> last_sorted_vector(new_size/2);
1224 if(new_size % 2 == 0)
1226 for(Index i = 0; i < new_size/2 ; i++)
1228 first_sorted_vector(i) = sorted_vector(i);
1229 last_sorted_vector(i) = sorted_vector[i + new_size/2];
1234 for(Index i = 0; i < new_size/2 ; i++)
1236 first_sorted_vector(i) = sorted_vector(i);
1237 last_sorted_vector(i) = sorted_vector[i + new_size/2 + 1];
1242 Tensor<type, 1> quartiles(3);
1246 quartiles(0) = sorted_vector(0);
1247 quartiles(1) = sorted_vector(0);
1248 quartiles(2) = sorted_vector(0);
1250 else if(new_size == 2)
1252 quartiles(0) = (sorted_vector(0)+sorted_vector(1))/ type(4);
1253 quartiles(1) = (sorted_vector(0)+sorted_vector(1))/ type(2);
1254 quartiles(2) = (sorted_vector(0)+sorted_vector(1))* type(3/4);
1256 else if(new_size == 3)
1258 quartiles(0) = (sorted_vector(0)+sorted_vector(1))/ type(2);
1259 quartiles(1) = sorted_vector(1);
1260 quartiles(2) = (sorted_vector(2)+sorted_vector(1))/ type(2);
1264 quartiles(0) = median(first_sorted_vector);
1265 quartiles(1) = median(sorted_vector);
1266 quartiles(2) = median(last_sorted_vector);
1276Tensor<type, 1> quartiles(
const Tensor<type, 1>& vector,
const Tensor<Index, 1>& indices)
1278 const Index indices_size = indices.dimension(0);
1285 for(Index i = 0; i < indices_size; i++)
1289 if(!
isnan(vector(index))) new_size++;
1292 Tensor<type, 1> sorted_vector;
1293 sorted_vector.resize(new_size);
1295 Index sorted_index = 0;
1297 for(Index i = 0; i < indices_size; i++)
1301 if(!
isnan(vector(index)))
1303 sorted_vector(sorted_index) = vector(index);
1309 sort(sorted_vector.data(), sorted_vector.data() + sorted_vector.size(), less<type>());
1313 Tensor<type, 1> first_sorted_vector(new_size/2);
1314 Tensor<type, 1> last_sorted_vector(new_size/2);
1316 for(Index i = 0; i < new_size/2 ; i++)
1318 first_sorted_vector(i) = sorted_vector(i);
1321 for(Index i = 0; i < new_size/2; i++)
1323 last_sorted_vector(i) = sorted_vector(i + new_size - new_size/2);
1326 Tensor<type, 1> quartiles(3);
1330 quartiles(0) = sorted_vector(0);
1331 quartiles(1) = sorted_vector(0);
1332 quartiles(2) = sorted_vector(0);
1334 else if(new_size == 2)
1336 quartiles(0) = (sorted_vector(0)+sorted_vector(1))/ type(4);
1337 quartiles(1) = (sorted_vector(0)+sorted_vector(1))/ type(2);
1338 quartiles(2) = (sorted_vector(0)+sorted_vector(1))* type(3/4);
1340 else if(new_size == 3)
1342 quartiles(0) = (sorted_vector(0)+sorted_vector(1))/ type(2);
1343 quartiles(1) = sorted_vector(1);
1344 quartiles(2) = (sorted_vector(2)+sorted_vector(1))/ type(2);
1346 else if(new_size % 2 == 0)
1348 Index median_index =
static_cast<Index
>(first_sorted_vector.size() / 2);
1349 quartiles(0) = (first_sorted_vector(median_index-1) + first_sorted_vector(median_index)) /
static_cast<type
>(2.0);
1351 median_index =
static_cast<Index
>(new_size / 2);
1352 quartiles(1) = (sorted_vector(median_index-1) + sorted_vector(median_index)) /
static_cast<type
>(2.0);
1354 median_index =
static_cast<Index
>(last_sorted_vector.size() / 2);
1355 quartiles(2) = (last_sorted_vector(median_index-1) + last_sorted_vector(median_index)) /
static_cast<type
>(2.0);
1359 quartiles(0) = sorted_vector(new_size/4);
1360 quartiles(1) = sorted_vector(new_size/2);
1361 quartiles(2) = sorted_vector(new_size*3/4);
1372BoxPlot box_plot(
const Tensor<type, 1>& vector)
1376 if(vector.dimension(0) == 0) {
1377 box_plot.minimum = type(NAN);
1378 box_plot.first_quartile = type(NAN);
1379 box_plot.median = type(NAN);
1380 box_plot.third_quartile = type(NAN);
1381 box_plot.maximum = type(NAN);
1386 const Tensor<type, 1> quartiles = OpenNN::quartiles(vector);
1388 box_plot.minimum = minimum(vector);
1389 box_plot.first_quartile = quartiles(0);
1390 box_plot.median = quartiles(1);
1391 box_plot.third_quartile = quartiles(2);
1392 box_plot.maximum = maximum(vector);
1402BoxPlot box_plot(
const Tensor<type, 1>& vector,
const Tensor<Index, 1>& indices)
1406 if(vector.dimension(0) == 0 || indices.dimension(0) == 0)
return box_plot;
1408 const Tensor<type, 1> quartiles = OpenNN::quartiles(vector, indices);
1410 box_plot.minimum = minimum(vector, indices);
1411 box_plot.first_quartile = quartiles(0);
1412 box_plot.median = quartiles(1);
1413 box_plot.third_quartile = quartiles(2);
1414 box_plot.maximum = maximum(vector, indices);
1429Histogram histogram(
const Tensor<type, 1>& vector,
const Index& bins_number)
1435 ostringstream buffer;
1437 buffer <<
"OpenNN Exception: Statistics Class.\n"
1438 <<
"Histogram histogram(const Tensor<type, 1>&, "
1439 "const Index&) const method.\n"
1440 <<
"Number of bins is less than one.\n";
1442 throw logic_error(buffer.str());
1447 const Index size = vector.dimension(0);
1449 Tensor<type, 1> minimums(bins_number);
1450 Tensor<type, 1> maximums(bins_number);
1452 Tensor<type, 1> centers(bins_number);
1453 Tensor<Index, 1> frequencies(bins_number);
1454 frequencies.setZero();
1456 Index unique_values_number = 1;
1457 Tensor<type, 1> old_unique_values(1);
1458 Tensor<type, 1> unique_values(1);
1459 unique_values(0) = vector(0);
1460 old_unique_values = unique_values;
1462 for(Index i = 1; i < size; i++)
1464 if(find(unique_values.data(), unique_values.data()+unique_values.size(), vector(i))
1465 == unique_values.data()+unique_values.size())
1467 unique_values_number++;
1469 unique_values.resize(unique_values_number);
1471 for(Index j = 0; j < unique_values_number-1; j++) unique_values(j) = old_unique_values(j);
1473 unique_values(unique_values_number-1) = vector(i);
1475 old_unique_values = unique_values;
1478 if(unique_values_number > bins_number)
break;
1481 if(unique_values_number <= bins_number)
1483 sort(unique_values.data(), unique_values.data() + unique_values.size(), less<type>());
1485 centers = unique_values;
1486 minimums = unique_values;
1487 maximums = unique_values;
1489 frequencies.resize(unique_values_number);
1490 frequencies.setZero();
1492 for(Index i = 0; i < size; i++)
1494 if(
isnan(vector(i)))
continue;
1496 for(Index j = 0; j < unique_values_number; j++)
1498 if(vector(i) - centers(j) <
static_cast<type
>(1e-6))
1508 const type min = minimum(vector);
1509 const type max = maximum(vector);
1511 const type length = (max - min) /
static_cast<type
>(bins_number);
1514 maximums(0) = min + length;
1515 centers(0) = (maximums(0) + minimums(0)) /
static_cast<type
>(2.0);
1519 for(Index i = 1; i < bins_number; i++)
1521 minimums(i) = minimums(i - 1) + length;
1522 maximums(i) = maximums(i - 1) + length;
1524 centers(i) = (maximums(i) + minimums(i)) /
static_cast<type
>(2.0);
1529 const Index size = vector.dimension(0);
1531 for(Index i = 0; i < size; i++)
1533 if(
isnan(vector(i)))
1538 for(Index j = 0; j < bins_number - 1; j++)
1540 if(vector(i) >= minimums(j) && vector(i) < maximums(j))
1547 if(vector(i) >= minimums(bins_number - 1))
1549 frequencies(bins_number - 1)++;
1554 Histogram histogram;
1574Histogram histogram_centered(
const Tensor<type, 1>& vector,
const type& center,
const Index& bins_number)
1580 ostringstream buffer;
1582 buffer <<
"OpenNN Exception: Statistics Class.\n"
1583 <<
"Histogram histogram_centered(const Tensor<type, 1>&, "
1584 "const type&, const Index&) const method.\n"
1585 <<
"Number of bins is less than one.\n";
1587 throw logic_error(buffer.str());
1594 if(bins_number%2 == 0)
1596 bin_center =
static_cast<Index
>(
static_cast<type
>(bins_number)/
static_cast<type
>(2.0));
1600 bin_center =
static_cast<Index
>(
static_cast<type
>(bins_number)/
static_cast<type
>(2.0) +
static_cast<type
>(0.5));
1603 Tensor<type, 1> minimums(bins_number);
1604 Tensor<type, 1> maximums(bins_number);
1606 Tensor<type, 1> centers(bins_number);
1607 Tensor<Index, 1> frequencies(bins_number);
1608 frequencies.setZero();
1610 const type min = minimum(vector);
1611 const type max = maximum(vector);
1613 const type length = (max - min)/
static_cast<type
>(bins_number);
1615 minimums(bin_center-1) = center - length;
1616 maximums(bin_center-1) = center + length;
1617 centers(bin_center-1) = center;
1621 for(Index i = bin_center; i < bins_number; i++)
1623 minimums(i) = minimums(i - 1) + length;
1624 maximums(i) = maximums(i - 1) + length;
1626 centers(i) = (maximums(i) + minimums(i)) /
static_cast<type
>(2.0);
1629 for(Index i =
static_cast<Index
>(bin_center)-2; i >= 0; i--)
1631 minimums(i) = minimums(i + 1) - length;
1632 maximums(i) = maximums(i + 1) - length;
1634 centers(i) = (maximums(i) + minimums(i)) /
static_cast<type
>(2.0);
1639 const Index size = vector.dimension(0);
1641 for(Index i = 0; i < size; i++)
1643 for(Index j = 0; j < bins_number - 1; j++)
1645 if(vector(i) >= minimums(j) && vector(i) < maximums(j))
1651 if(vector(i) >= minimums(bins_number - 1))
1653 frequencies(bins_number - 1)++;
1657 Histogram histogram(bins_number);
1675Histogram histogram(
const Tensor<bool, 1>& v)
1677 Tensor<type, 1> minimums(2);
1679 Tensor<type, 1> maximums(2);
1680 maximums.setConstant(type(1));
1682 Tensor<type, 1> centers(2);
1683 centers.setValues({type(0), type(1)});
1684 Tensor<Index, 1> frequencies(2);
1685 frequencies.setZero();
1689 const Index size = v.dimension(0);
1691 for(Index i = 0; i < size; i++)
1693 for(Index j = 0; j < 2; j++)
1695 if(
static_cast<Index
>(v(i)) ==
static_cast<Index
>(minimums(j)))
1702 Histogram histogram(2);
1716Tensor<Index, 1> total_frequencies(
const Tensor<Histogram, 1>& histograms)
1718 const Index histograms_number = histograms.size();
1720 Tensor<Index, 1> total_frequencies(histograms_number);
1722 for(Index i = 0; i < histograms_number; i++)
1724 total_frequencies(i) = histograms(i).frequencies(i);
1727 return total_frequencies;
1739Tensor<Histogram, 1> histograms(
const Tensor<type, 2>& matrix,
const Index& bins_number)
1741 const Index rows_number = matrix.dimension(0);
1742 const Index columns_number = matrix.dimension(1);
1744 Tensor<Histogram, 1> histograms(columns_number);
1746 Tensor<type, 1> column(rows_number);
1748 for(Index i = 0; i < columns_number; i++)
1750 column = matrix.chip(i,1);
1752 histograms(i) = histogram(column, bins_number);
1764Tensor<Descriptives, 1> descriptives(
const Tensor<type, 2>& matrix)
1766 const Index rows_number = matrix.dimension(0);
1767 const Index columns_number = matrix.dimension(1);
1771 if(rows_number == 0)
1773 ostringstream buffer;
1775 buffer <<
"OpenNN Exception: Statistics Class.\n"
1776 <<
"Tensor<Descriptives, 1> descriptives(const Tensor<type, 2>&) "
1778 <<
"Number of rows must be greater than one.\n";
1780 throw logic_error(buffer.str());
1785 Tensor<Descriptives, 1> descriptives(columns_number);
1787 Tensor<type, 1> column(rows_number);
1789 #pragma omp parallel for private(column)
1791 for(Index i = 0; i < columns_number; i++)
1793 column = matrix.chip(i,1);
1795 descriptives(i) = OpenNN::descriptives(column);
1798 return descriptives;
1808Tensor<Descriptives, 1> descriptives(
const Tensor<type, 2>& matrix,
1809 const Tensor<Index, 1>& row_indices,
1810 const Tensor<Index, 1>& columns_indices)
1812 const Index row_indices_size = row_indices.size();
1813 const Index columns_indices_size = columns_indices.size();
1815 Tensor<Descriptives, 1> descriptives(columns_indices_size);
1817 Index row_index, column_index;
1819 Tensor<type, 1> minimums(columns_indices_size);
1820 minimums.setConstant(numeric_limits<type>::max());
1822 Tensor<type, 1> maximums(columns_indices_size);
1823 maximums.setConstant(type(NUMERIC_LIMITS_MIN));
1825 Tensor<double, 1> sums(columns_indices_size);
1826 Tensor<double, 1> squared_sums(columns_indices_size);
1827 Tensor<Index, 1> count(columns_indices_size);
1830 squared_sums.setZero();
1833 for(Index i = 0; i < row_indices_size; i++)
1835 row_index = row_indices(i);
1837 #pragma omp parallel for private(column_index)
1839 for(Index j = 0; j < columns_indices_size; j++)
1841 column_index = columns_indices(j);
1843 const type value = matrix(row_index,column_index);
1845 if(
isnan(value))
continue;
1847 if(value < minimums(j)) minimums(j) = value;
1849 if(value > maximums(j)) maximums(j) = value;
1851 sums(j) += double(value);
1852 squared_sums(j) += double(value*value);
1857 const Tensor<double, 1> mean = sums/count.cast<
double>();
1859 Tensor<double, 1> standard_deviation(columns_indices_size);
1861 if(row_indices_size > 1)
1863 #pragma omp parallel for
1865 for(Index i = 0; i < columns_indices_size; i++)
1867 const double variance = squared_sums(i)/
static_cast<double>(count(i)-1)
1868 - (sums(i)/
static_cast<double>(count(i)))*(sums(i)/
static_cast<double>(count(i)))*
static_cast<double>(count(i))/
static_cast<double>(count(i)-1);
1870 standard_deviation(i) =
sqrt(variance);
1874 for(Index i = 0; i < columns_indices_size; i++)
1876 descriptives(i).minimum = minimums(i);
1877 descriptives(i).maximum = maximums(i);
1878 descriptives(i).mean = type(mean(i));
1879 descriptives(i).standard_deviation = type(standard_deviation(i));
1882 return descriptives;
1893Tensor<type, 1> columns_minimums(
const Tensor<type, 2>& matrix,
const Tensor<Index, 1>& rows_indices,
const Tensor<Index, 1>& columns_indices)
1895 const Index rows_number = matrix.dimension(0);
1896 const Index columns_number = matrix.dimension(1);
1898 Tensor<Index, 1> used_columns_indices;
1900 if(columns_indices.dimension(0) == 0)
1902 used_columns_indices.resize(columns_number);
1904 for(Index i = 0; i < columns_number; i++)
1906 used_columns_indices(i) = i;
1911 used_columns_indices = columns_indices;
1914 Tensor<Index, 1> used_rows_indices;
1916 if(rows_indices.dimension(0) == 0)
1918 used_rows_indices.resize(rows_number);
1920 for(Index i = 0; i < rows_number; i++)
1922 used_rows_indices(i) = i;
1927 used_rows_indices = rows_indices;
1930 const Index rows_indices_size = used_rows_indices.size();
1931 const Index columns_indices_size = used_columns_indices.size();
1933 Tensor<type, 1> minimums(columns_indices_size);
1938 for(Index j = 0; j < columns_indices_size; j++)
1940 column_index = used_columns_indices(j);
1942 Tensor<type, 1> column(rows_indices_size);
1944 for(Index i = 0; i < rows_indices_size; i++)
1946 row_index = used_rows_indices(i);
1948 column(i) = matrix(row_index,column_index);
1951 minimums(j) = minimum(column);
1964Tensor<type, 1> columns_maximums(
const Tensor<type, 2>& matrix,
const Tensor<Index, 1>& columns_indices)
1966 const Index rows_number = matrix.dimension(0);
1967 const Index columns_number = matrix.dimension(1);
1969 Tensor<Index, 1> used_columns_indices;
1971 if(columns_indices.dimension(0) == 0 && columns_indices.dimension(1) == 0)
1973 used_columns_indices.resize(columns_number);
1977 used_columns_indices = columns_indices;
1980 const Index columns_indices_size = used_columns_indices.size();
1982 Tensor<type, 1> maximums(columns_indices_size);
1985 Tensor<type, 1> column(rows_number);
1987 for(Index i = 0; i < columns_indices_size; i++)
1989 column_index = used_columns_indices(i);
1991 column = matrix.chip(column_index,1);
1993 maximums(i) = maximum(column);
2000type range(
const Tensor<type, 1>& vector)
2002 const type min = minimum(vector);
2003 const type max = maximum(vector);
2005 return abs(max - min);
2012Descriptives descriptives(
const Tensor<type, 1>& vector)
2014 const Index size = vector.dimension(0);
2020 ostringstream buffer;
2022 buffer <<
"OpenNN Exception: Statistics Class.\n"
2023 <<
"type descriptives(const Tensor<type, 1>&, "
2024 "const Tensor<Index, 1>&).\n"
2025 <<
"Size must be greater than zero.\n";
2027 throw logic_error(buffer.str());
2032 Descriptives descriptives;
2034 type minimum = numeric_limits<type>::max();
2035 type maximum = -numeric_limits<type>::max();
2038 type squared_sum = type(0);
2041 for(Index i = 0; i < size; i++)
2043 if(!
isnan(vector(i)))
2045 if(vector(i) < minimum) minimum = vector(i);
2047 if(vector(i) > maximum) maximum = vector(i);
2050 squared_sum += vector(i) *vector(i);
2056 const type mean = sum/
static_cast<type
>(count);
2058 type standard_deviation;
2062 standard_deviation = type(0);
2066 const type numerator = type(squared_sum) - (sum * sum) / type(count);
2067 const type denominator = type(size) -
static_cast<type
>(1.0);
2069 standard_deviation = numerator / denominator;
2072 standard_deviation =
sqrt(standard_deviation);
2074 descriptives.minimum = minimum;
2075 descriptives.maximum = maximum;
2076 descriptives.mean = mean;
2077 descriptives.standard_deviation = standard_deviation;
2079 return descriptives;
2089Index perform_distribution_distance_analysis(
const Tensor<type, 1>& vector)
2091 Tensor<type, 1> distances(2);
2092 distances.setZero();
2094 const Index nans = count_nan(vector);
2096 const Index new_size = vector.size() - nans;
2098 Tensor<type, 1> new_vector(new_size);
2102 for(Index i = 0; i < vector.size(); i++)
2104 if(!
isnan(vector(i)))
2106 new_vector(index) = vector(i);
2111 Tensor<type, 1> sorted_vector(new_vector);
2113 sort(sorted_vector.data(), sorted_vector.data() + sorted_vector.size(), less<type>());
2115 const Descriptives descriptives = OpenNN::descriptives(vector);
2117 const type mean = descriptives.mean;
2118 const type standard_deviation = descriptives.standard_deviation;
2119 const type minimum = sorted_vector(0);
2120 const type maximum = sorted_vector(new_size-1);
2122 #pragma omp parallel for schedule(dynamic)
2124 for(Index i = 0; i < new_size; i++)
2126 const type normal_distribution =
static_cast<type
>(0.5)
2127 * type(
erfc(
double(mean - sorted_vector(i))))/
static_cast<type
>((standard_deviation*
static_cast<type
>(
sqrt(2))));
2129 const type uniform_distribution = (sorted_vector(i)-minimum)/(maximum - minimum);
2131 type empirical_distribution;
2135 if(new_vector(i) < sorted_vector(0))
2137 empirical_distribution = type(0);
2139 else if(new_vector(i) >= sorted_vector(new_size-1))
2141 empirical_distribution = type(1);
2145 counter =
static_cast<Index
>(i + 1);
2147 for(Index j = i+1; j < new_size; j++)
2149 if(sorted_vector(j) <= sorted_vector(i))
2159 empirical_distribution =
static_cast<type
>(counter)/
static_cast<type
>(new_size);
2162 #pragma omp critical
2164 distances(0) +=
abs(normal_distribution - empirical_distribution);
2165 distances(1) +=
abs(uniform_distribution - empirical_distribution);
2169 return minimal_index(distances);
2177Tensor<type, 1> mean(
const Tensor<type, 2>& matrix)
2179 const Index rows_number = matrix.dimension(0);
2180 const Index columns_number = matrix.dimension(1);
2184 if(rows_number == 0)
2186 ostringstream buffer;
2188 buffer <<
"OpenNN Exception: Statistics class.\n"
2189 <<
"Tensor<type, 1> mean(const Tensor<type, 2>&) const method.\n"
2190 <<
"Number of rows must be greater than one.\n";
2192 throw logic_error(buffer.str());
2199 Tensor<type, 1> mean(columns_number);
2202 for(Index j = 0; j < columns_number; j++)
2204 for(Index i = 0; i < rows_number; i++)
2206 if(!
isnan(matrix(i,j)))
2208 mean(j) += matrix(i,j);
2212 mean(j) /=
static_cast<type
>(rows_number);
2223Tensor<type, 1> mean(
const Tensor<type, 2>& matrix,
const Tensor<Index, 1>& columns_indices)
2225 const Index rows_number = matrix.dimension(0);
2227 const Index columns_indices_size = columns_indices.size();
2233 Tensor<type, 1> mean(columns_indices_size);
2236 for(Index j = 0; j < columns_indices_size; j++)
2238 column_index = columns_indices(j);
2240 for(Index i = 0; i < rows_number; i++)
2242 mean(j) += matrix(i, column_index);
2245 mean(j) /=
static_cast<type
>(rows_number);
2258Tensor<type, 1> mean(
const Tensor<type, 2>& matrix,
const Tensor<Index, 1>& row_indices,
const Tensor<Index, 1>& columns_indices)
2260 const Index row_indices_size = row_indices.size();
2261 const Index columns_indices_size = columns_indices.size();
2263 if(row_indices_size == 0 && columns_indices_size == 0)
return Tensor<type, 1>();
2267 const Index rows_number = matrix.dimension(0);
2268 const Index columns_number = matrix.dimension(1);
2272 if(row_indices_size > rows_number)
2274 ostringstream buffer;
2276 buffer <<
"OpenNN Exception: Statistics class.\n"
2277 <<
"Tensor<type, 1> mean(const Tensor<type, 2>& matrix, "
2278 "const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2279 <<
"Size of row indices(" << row_indices_size <<
") is greater than number of rows(" << rows_number <<
").\n";
2281 throw logic_error(buffer.str());
2284 for(Index i = 0; i < row_indices_size; i++)
2286 if(row_indices(i) >= rows_number)
2288 ostringstream buffer;
2290 buffer <<
"OpenNN Exception: Statistics class.\n"
2291 <<
"Tensor<type, 1> mean(const Tensor<type, 2>& matrix, "
2292 "const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2293 <<
"Row index " << i <<
" must be less than rows number.\n";
2295 throw logic_error(buffer.str());
2299 if(row_indices_size == 0)
2301 ostringstream buffer;
2303 buffer <<
"OpenNN Exception: Statistics class.\n"
2304 <<
"Tensor<type, 1> mean(const Tensor<type, 2>& matrix, "
2305 "const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2306 <<
"Size of row indices must be greater than zero.\n";
2308 throw logic_error(buffer.str());
2313 if(columns_indices_size > columns_number)
2315 ostringstream buffer;
2317 buffer <<
"OpenNN Exception: Statistics class.\n"
2318 <<
"Tensor<type, 1> mean(const Tensor<type, 2>& matrix, "
2319 "const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2320 <<
"Column indices size must be equal or less than columns number.\n";
2322 throw logic_error(buffer.str());
2325 for(Index i = 0; i < columns_indices_size; i++)
2327 if(columns_indices(i) >= columns_number)
2329 ostringstream buffer;
2331 buffer <<
"OpenNN Exception: Statistics class.\n"
2332 <<
"Tensor<type, 1> mean(const Tensor<type, 2>& matrix, "
2333 "const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2334 <<
"Column index " << i <<
" must be less than columns number.\n";
2336 throw logic_error(buffer.str());
2349 Tensor<type, 1> mean(columns_indices_size);
2352 for(Index j = 0; j < columns_indices_size; j++)
2354 column_index = columns_indices(j);
2358 for(Index i = 0; i < row_indices_size; i++)
2360 row_index = row_indices(i);
2362 if(!
isnan(matrix(row_index,column_index)))
2364 mean(j) += matrix(row_index,column_index);
2369 mean(j) /=
static_cast<type
>(count);
2379type mean(
const Tensor<type, 2>& matrix,
const Index& column_index)
2381 const Index rows_number = matrix.dimension(0);
2382 const Index columns_number = matrix.dimension(1);
2384 if(rows_number == 0 && columns_number == 0)
return type(NAN);
2388 if(rows_number == 0)
2390 ostringstream buffer;
2392 buffer <<
"OpenNN Exception: Statistics class.\n"
2393 <<
"type mean(const Tensor<type, 2>&, const Index&) const method.\n"
2394 <<
"Number of rows must be greater than one.\n";
2396 throw logic_error(buffer.str());
2399 if(column_index >= columns_number)
2401 ostringstream buffer;
2403 buffer <<
"OpenNN Exception: Statistics class.\n"
2404 <<
"type mean(const Tensor<type, 2>&, const Index&) const method.\n"
2405 <<
"Index of column must be less than number of columns.\n";
2407 throw logic_error(buffer.str());
2412 if(rows_number == 0 && columns_number == 0)
return type(NAN);
2416 type mean = type(0);
2420 for(Index i = 0; i < rows_number; i++)
2422 if(!
isnan(matrix(i,column_index)))
2424 mean += matrix(i,column_index);
2429 mean /=
static_cast<type
>(count);
2438Tensor<type, 1> median(
const Tensor<type, 2>& matrix)
2440 const Index rows_number = matrix.dimension(0);
2441 const Index columns_number = matrix.dimension(1);
2445 if(rows_number == 0)
2447 ostringstream buffer;
2449 buffer <<
"OpenNN Exception: Matrix template.\n"
2450 <<
"Tensor<type, 1> median() const method.\n"
2451 <<
"Number of rows must be greater than one.\n";
2453 throw logic_error(buffer.str());
2460 Tensor<type, 1> median(columns_number);
2462 for(Index j = 0; j < columns_number; j++)
2464 Tensor<type, 1> sorted_column(matrix.chip(j,1));
2466 sort(sorted_column.data(), sorted_column.data() + sorted_column.size(), less<type>());
2468 if(rows_number % 2 == 0)
2470 median(j) = (sorted_column[rows_number*2/4] + sorted_column[rows_number*2/4+1])/ type(2);
2474 median(j) = sorted_column[rows_number*2/4];
2485type median(
const Tensor<type, 2>& matrix,
const Index& column_index)
2487 const Index rows_number = matrix.dimension(0);
2491 const Index columns_number = matrix.dimension(1);
2493 if(rows_number == 0)
2495 ostringstream buffer;
2497 buffer <<
"OpenNN Exception: Matrix template.\n"
2498 <<
"type median(const Index&) const method.\n"
2499 <<
"Number of rows must be greater than one.\n";
2501 throw logic_error(buffer.str());
2504 if(column_index >= columns_number)
2506 ostringstream buffer;
2508 buffer <<
"OpenNN Exception: Matrix template.\n"
2509 <<
"type median(const Index&) const method.\n"
2510 <<
"Index of column must be less than number of columns.\n";
2512 throw logic_error(buffer.str());
2519 type median = type(0);
2521 Tensor<type, 1> sorted_column(matrix.chip(column_index,1));
2523 sort(sorted_column.data(), sorted_column.data() + sorted_column.size(), less<type>());
2525 if(rows_number % 2 == 0)
2527 median = (sorted_column[rows_number*2/4] + sorted_column[rows_number*2/4+1])/ type(2);
2531 median = sorted_column[rows_number*2/4];
2543Tensor<type, 1> median(
const Tensor<type, 2>& matrix,
const Tensor<Index, 1>& columns_indices)
2545 const Index rows_number = matrix.dimension(0);
2547 const Index columns_indices_size = columns_indices.size();
2553 Tensor<type, 1> median(columns_indices_size);
2555 for(Index j = 0; j < columns_indices_size; j++)
2557 column_index = columns_indices(j);
2559 Tensor<type, 1> sorted_column(matrix.chip(column_index, 1));
2561 sort(sorted_column.data(), sorted_column.data() + sorted_column.size(), less<type>());
2563 if(rows_number % 2 == 0)
2565 median(j) = (sorted_column[rows_number*2/4] + sorted_column[rows_number*2/4+1])/type(2);
2569 median(j) = sorted_column[rows_number*2/4];
2582Tensor<type, 1> median(
const Tensor<type, 2>& matrix,
const Tensor<Index, 1>& row_indices,
const Tensor<Index, 1>& columns_indices)
2585 const Index row_indices_size = row_indices.size();
2586 const Index columns_indices_size = columns_indices.size();
2590 const Index rows_number = matrix.dimension(0);
2591 const Index columns_number = matrix.dimension(1);
2595 if(row_indices_size > rows_number)
2597 ostringstream buffer;
2599 buffer <<
"OpenNN Exception: Matrix template.\n"
2600 <<
"Tensor<type, 1> median(const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2601 <<
"Size of row indices(" << row_indices_size <<
") is greater than number of rows(" << rows_number <<
").\n";
2603 throw logic_error(buffer.str());
2606 for(Index i = 0; i < row_indices_size; i++)
2608 if(row_indices(i) >= rows_number)
2610 ostringstream buffer;
2612 buffer <<
"OpenNN Exception: Matrix template.\n"
2613 <<
"Tensor<type, 1> median(const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2614 <<
"Row index " << i <<
" must be less than rows number.\n";
2616 throw logic_error(buffer.str());
2620 if(row_indices_size == 0)
2622 ostringstream buffer;
2624 buffer <<
"OpenNN Exception: Matrix template.\n"
2625 <<
"Tensor<type, 1> median(const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2626 <<
"Size of row indices must be greater than zero.\n";
2628 throw logic_error(buffer.str());
2633 if(columns_indices_size > columns_number)
2635 ostringstream buffer;
2637 buffer <<
"OpenNN Exception: Matrix template.\n"
2638 <<
"Tensor<type, 1> median(const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2639 <<
"Column indices size must be equal or less than columns number.\n";
2641 throw logic_error(buffer.str());
2644 for(Index i = 0; i < columns_indices_size; i++)
2646 if(columns_indices(i) >= columns_number)
2648 ostringstream buffer;
2650 buffer <<
"OpenNN Exception: Matrix template.\n"
2651 <<
"Tensor<type, 1> median(const Tensor<Index, 1>&, const Tensor<Index, 1>&) const method.\n"
2652 <<
"Column index " << i <<
" must be less than columns number.\n";
2654 throw logic_error(buffer.str());
2664 Tensor<type, 1> median(columns_indices_size);
2666 for(Index j = 0; j < columns_indices_size; j++)
2668 column_index = columns_indices(j);
2670 Tensor<type, 1> sorted_column(row_indices_size);
2672 for(Index k = 0; k < row_indices_size; k++)
2674 const Index row_index = row_indices(k);
2676 sorted_column(k) = matrix(row_index, column_index);
2679 sort(sorted_column.data(), sorted_column.data() + sorted_column.size(), less<type>());
2681 if(row_indices_size % 2 == 0)
2683 median(j) = (sorted_column[row_indices_size*2/4] + sorted_column[row_indices_size*2/4 + 1])/ type(2);
2687 median(j) = sorted_column[row_indices_size * 2 / 4];
2699type normal_distribution_distance(
const Tensor<type, 1>& vector)
2701 type normal_distribution_distance = type(0);
2703 const Index n = vector.dimension(0);
2705 const type mean_value = mean(vector);
2706 const type standard_deviation = OpenNN::standard_deviation(vector);
2708 type normal_distribution;
2709 type empirical_distribution;
2711 Tensor<type, 1> sorted_vector(vector);
2713 sort(sorted_vector.data(), sorted_vector.data() + sorted_vector.size(), less<type>());
2717 for(Index i = 0; i < n; i++)
2719 normal_distribution =
static_cast<type
>(0.5) *
static_cast<type
>(
erfc(
double(mean_value - sorted_vector(i))))/(standard_deviation*
static_cast<type
>(
sqrt(2.0)));
2722 for(Index j = 0; j < n; j++)
2724 if(sorted_vector(j) <= sorted_vector(i))
2734 empirical_distribution =
static_cast<type
>(counter)/
static_cast<type
>(n);
2736 normal_distribution_distance +=
abs(normal_distribution - empirical_distribution);
2739 return normal_distribution_distance;
2747type half_normal_distribution_distance(
const Tensor<type, 1>& vector)
2749 type half_normal_distribution_distance = type(0);
2751 const Index n = vector.dimension(0);
2753 const type standard_deviation = OpenNN::standard_deviation(vector);
2755 type half_normal_distribution;
2756 type empirical_distribution;
2758 Tensor<type, 1> sorted_vector(vector);
2760 sort(sorted_vector.data(), sorted_vector.data() + sorted_vector.size(), less<type>());
2764 for(Index i = 0; i < n; i++)
2766 half_normal_distribution = type(
erf(
double(sorted_vector(i))))/(standard_deviation *
static_cast<type
>(
sqrt(2)));
2770 for(Index j = 0; j < n; j++)
2772 if(sorted_vector(j) <= sorted_vector(i))
2782 empirical_distribution =
static_cast<type
>(counter)/
static_cast<type
>(n);
2784 half_normal_distribution_distance +=
abs(half_normal_distribution - empirical_distribution);
2787 return half_normal_distribution_distance;
2795type uniform_distribution_distance(
const Tensor<type, 1>& vector)
2797 type uniform_distribution_distance = type(0);
2799 const Index n = vector.dimension(0);
2801 type uniform_distribution;
2802 type empirical_distribution;
2804 Tensor<type, 1> sorted_vector(vector);
2806 sort(sorted_vector.data(), sorted_vector.data() + sorted_vector.size(), less<type>());
2808 const type minimum = sorted_vector[0];
2809 const type maximum = sorted_vector[n-1];
2813 for(Index i = 0; i < n; i++)
2815 uniform_distribution = (sorted_vector(i)-minimum)/(maximum - minimum);
2818 for(Index j = 0; j < n; j++)
2820 if(sorted_vector(j) <= sorted_vector(i))
2830 empirical_distribution =
static_cast<type
>(counter)/
static_cast<type
>(n);
2832 uniform_distribution_distance +=
abs(uniform_distribution - empirical_distribution);
2835 return uniform_distribution_distance;
2841type normality_parameter(
const Tensor<type, 1>& vector)
2843 const type max = maximum(vector);
2844 const type min = minimum(vector);
2846 const Index n = vector.dimension(0);
2848 const type mean_value = mean(vector);
2849 const type standard_deviation = OpenNN::standard_deviation(vector);
2851 type normal_distribution;
2852 type empirical_distribution;
2853 type previous_normal_distribution = type(0);
2854 type previous_empirical_distribution = type(0);
2856 Tensor<type, 1> sorted_vector(vector);
2858 sort(sorted_vector.data(), sorted_vector.data() + sorted_vector.size(), less<type>());
2860 type empirical_area = type(0);
2861 type normal_area = type(0);
2865 for(Index i = 0; i < n; i++)
2867 normal_distribution =
static_cast<type
>(0.5) *
static_cast<type
>(
erfc(
double(mean_value - sorted_vector(i))))/(standard_deviation*
static_cast<type
>(
sqrt(2.0)));
2870 for(Index j = 0; j < n; j++)
2872 if(sorted_vector(j) <= sorted_vector(i))
2882 empirical_distribution =
static_cast<type
>(counter)/
static_cast<type
>(n);
2886 previous_normal_distribution = normal_distribution;
2887 previous_empirical_distribution = empirical_distribution;
2891 normal_area +=
static_cast<type
>(0.5)*(sorted_vector(i)-sorted_vector[i-1])*(normal_distribution+previous_normal_distribution);
2892 empirical_area +=
static_cast<type
>(0.5)*(sorted_vector(i)-sorted_vector[i-1])*(empirical_distribution+previous_empirical_distribution);
2894 previous_normal_distribution = normal_distribution;
2895 previous_empirical_distribution = empirical_distribution;
2899 const type uniform_area = (max - min)/
static_cast<type
>(2.0);
2901 return uniform_area;
2905Tensor<type, 1> variation_percentage(
const Tensor<type, 1>& vector)
2907 const Index size = vector.dimension(0);
2909 Tensor<type, 1> new_vector(size);
2911 for(Index i = 1; i < size; i++)
2913 if(
abs(vector[i-1]) < type(NUMERIC_LIMITS_MIN))
2915 new_vector(i) = (vector(i) - vector[i-1])*
static_cast<type
>(100.0)/vector[i-1];
2925Index minimal_index(
const Tensor<type, 1>& vector)
2927 const Index size = vector.dimension(0);
2929 if(size == 0)
return Index();
2931 Index minimal_index = 0;
2932 type minimum = vector[0];
2934 for(Index i = 1; i < size; i++)
2936 if(vector(i) < minimum)
2939 minimum = vector(i);
2943 return minimal_index;
2949Index maximal_index(
const Tensor<type, 1>& vector)
2951 const Index size = vector.dimension(0);
2953 if(size == 0)
return Index();
2955 Index maximal_index = 0;
2956 type maximum = vector[0];
2958 for(Index i = 1; i < size; i++)
2960 if(vector(i) > maximum)
2963 maximum = vector(i);
2967 return maximal_index;
2974Tensor<Index, 1> minimal_indices(
const Tensor<type, 1>& vector,
const Index& number)
2976 Tensor<type, 1> vector_ = vector;
2978 const Index size = vector.dimension(0);
2979 Tensor<Index, 1> minimal_indices(number);
2980 Eigen::Tensor<type, 0> maxim = vector.maximum();
2986 ostringstream buffer;
2988 buffer <<
"OpenNN Exception: Statistics class.\n"
2989 <<
"Tensor<Index, 1> minimal_indices(Tensor<type, 1>& , const Index& ) \n"
2990 <<
"Number of minimal indices to be computed must be lower (or equal) than the size of the imput vector.\n";
2992 throw logic_error(buffer.str());
2996 for(Index j = 0; j < number; j++)
2998 Index minimal_index = 0;
2999 type minimum = vector_(0);
3001 for(Index i = 0; i < size; i++)
3003 if(vector_(i) < minimum)
3006 minimum = vector_(i);
3010 vector_(minimal_index) = maxim(0) + type(1);
3011 minimal_indices(j) = minimal_index;
3013 return minimal_indices;
3021Tensor<Index, 1> maximal_indices(
const Tensor<type, 1>& vector,
const Index& number)
3023 Tensor<type, 1> vector_ = vector;
3025 const Index size = vector.dimension(0);
3026 Tensor<Index, 1> maximal_indices(number);
3027 const Eigen::Tensor<type, 0> minim = vector.minimum();
3033 ostringstream buffer;
3035 buffer <<
"OpenNN Exception: Statistics class.\n"
3036 <<
"Tensor<Index, 1> maximal_indices(Tensor<type, 1>& , const Index& ) \n"
3037 <<
"Number of maximal indices to be computed must be lower (or equal) than the size of the imput vector.\n";
3039 throw logic_error(buffer.str());
3043 for(Index j = 0; j < number; j++)
3045 Index maximal_index = 0;
3046 type maximal = vector_(0);
3048 for(Index i = 0; i < size; i++)
3050 if(vector_(i) > maximal)
3053 maximal = vector_(i);
3057 vector_(maximal_index) = minim(0) - type(1);
3058 maximal_indices(j) = maximal_index;
3061 return maximal_indices;
3067Tensor<Index, 1> minimal_indices(
const Tensor<type, 2>& matrix)
3069 const Index rows_number = matrix.dimension(0);
3070 const Index columns_number = matrix.dimension(1);
3072 type minimum = matrix(0,0);
3074 Tensor<Index, 1> minimal_indices(2);
3075 minimal_indices.setZero();
3077 for(Index i = 0; i < rows_number; i++)
3079 for(Index j = 0; j < columns_number; j++)
3081 if(!
isnan(matrix(i,j)) && matrix(i,j) < minimum)
3083 minimum = matrix(i,j);
3084 minimal_indices(0) = i;
3085 minimal_indices(1) = j;
3090 return minimal_indices;
3096Tensor<Index, 1> maximal_indices(
const Tensor<type, 2>& matrix)
3098 const Index rows_number = matrix.dimension(0);
3099 const Index columns_number = matrix.dimension(1);
3101 type maximum = matrix(0,0);
3103 Tensor<Index, 1> maximal_indices(2);
3104 maximal_indices.setZero();
3106 for(Index i = 0; i < rows_number; i++)
3108 for(Index j = 0; j < columns_number; j++)
3110 if(!
isnan(matrix(i,j)) && matrix(i,j) > maximum)
3112 maximum = matrix(i,j);
3113 maximal_indices(0) = i;
3114 maximal_indices(1) = j;
3119 return maximal_indices;
3126Tensor<Index, 2> maximal_columns_indices(
const Tensor<type, 2>& matrix,
const Index& maximum_number)
3128 const Index rows_number = matrix.dimension(0);
3129 const Index columns_number = matrix.dimension(1);
3131 Tensor<Index, 2> maximal_columns_indices(maximum_number, columns_number);
3133 Tensor<type, 1> columns_minimums = OpenNN::columns_minimums(matrix);
3135 for(Index j = 0; j < columns_number; j++)
3137 Tensor<type, 1> column = matrix.chip(j,1);
3139 for(Index i = 0; i < maximum_number; i++)
3141 Index maximal_index = 0;
3142 type maximal = column(0);
3144 for(Index k = 0; k < rows_number; k++)
3146 if(column(k) > maximal && !
isnan(column(k)))
3149 maximal = column(k);
3153 column(maximal_index) = columns_minimums(j)-
static_cast<type
>(1);
3154 maximal_columns_indices(i,j) = maximal_index;
3158 return maximal_columns_indices;
3164Tensor<type, 1> percentiles(
const Tensor<type, 1>& vector)
3166 const Index size = vector.dimension(0);
3172 ostringstream buffer;
3174 buffer <<
"OpenNN Exception: Statistics.\n"
3175 <<
"Tensor<type, 1> percentiles(const Tensor<type, 1>& vector) method.\n"
3176 <<
"Size must be greater than 10.\n";
3178 throw logic_error(buffer.str());
3185 for(Index i = 0; i < size; i++)
3187 if(!
isnan(vector(i)))
3195 Tensor<type, 1> nan(1);
3196 nan.setValues({
static_cast<type
>(NAN)});
3202 Tensor<type, 1> new_vector(new_size);
3204 for(Index i = 0; i < size; i++)
3206 if(!
isnan(vector(i)))
3208 new_vector(index) = vector(i);
3213 Tensor<type, 1> sorted_vector(new_vector);
3215 sort(sorted_vector.data(), sorted_vector.data() + new_size, less<type>());
3220 Tensor<type, 1> percentiles(10);
3222 for(Index i = 0; i < 9; i++)
3224 if(new_size * (i + 1) % 10 == 0)
3225 percentiles[i] = (sorted_vector[new_size * (i + 1) / 10 - 1] + sorted_vector[new_size * (i + 1) / 10]) /
static_cast<type
>(2.0);
3228 percentiles[i] =
static_cast<type
>(sorted_vector[new_size * (i + 1) / 10]);
3230 percentiles[9] = maximum(new_vector);
3239Index count_nan(
const Tensor<type, 1>& vector)
3241 Index nan_number = 0;
3243 for(Index i = 0; i < vector.dimension(0); i++)
3245 if(
isnan(vector(i))) nan_number++;
uint32 sqrt(uint32 &r, int &exp)
unsigned int erf(unsigned int arg)
HALF_CONSTEXPR half abs(half arg)
HALF_CONSTEXPR bool isnan(half arg)
Extensions to the C++ standard library.
bool has_minimum_minus_one_maximum_one()
Tensor< type, 1 > to_vector() const
Descriptives()
Default constructor.
bool has_mean_zero_standard_deviation_one()
virtual ~Descriptives()
Destructor.
void print(const string &="Descriptives:") const
Print the tittle of descriptives structure.
type minimum
Smallest value of a set, function, etc.
string name
Name of variable.
void set_maximum(const type &)
void set_standard_deviation(const type &)
type standard_deviation
Standard deviation value of a set, function, etc.
void set_mean(const type &)
type mean
Mean value of a set, function, etc.
void set_minimum(const type &)
type maximum
Biggest value of a set, function, etc.
void save(const string &file_name) const
virtual ~Histogram()
Destructor.
Index calculate_most_populated_bin() const
Retuns the index of the most populated bin.
Index count_empty_bins() const
Returns the number of bins with zero variates.
Tensor< type, 1 > minimums
Minimum positions of the bins in the histogram.
Index calculate_minimum_frequency() const
Returns the number of variates in the less populated bin.
Index calculate_frequency(const type &) const
Tensor< type, 1 > calculate_minimal_centers() const
Returns a vector with the centers of the less populated bins.
Tensor< type, 1 > calculate_maximal_centers() const
Returns a vector with the centers of the most populated bins.
Index calculate_bin(const type &) const
Index calculate_maximum_frequency() const
Returns the number of variates in the most populated bin.
Tensor< type, 1 > maximums
Maximum positions of the bins in the histogram.
Tensor< Index, 1 > frequencies
Population of the bins in the histogram.
Tensor< type, 1 > centers
Positions of the bins in the histogram.
Histogram()
Default constructor.
Index get_bins_number() const
Returns the number of bins in the histogram.