File indexing completed on 2024-05-05 03:48:45

0001 /*
0002     File                 : NSLStatsTest.cpp
0003     Project              : LabPlot
0004     Description          : NSL Tests for statistical functions
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2019 Stefan Gerlach <stefan.gerlach@uni.kn>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "NSLStatsTest.h"
0012 #include "backend/nsl/nsl_stats.h"
0013 
0014 // ##############################################################################
0015 // #################  Quantile test
0016 // ##############################################################################
0017 
0018 const int N = 10;
0019 const int NQ = 13;
0020 
0021 void NSLStatsTest::testQuantile() {
0022     const double data_sorted[] = {1, 1, 1, 3, 4, 7, 9, 11, 13, 13};
0023     double data_unsorted[] = {3, 7, 11, 1, 13, 1, 9, 1, 13, 4};
0024     const double quantile[] = {.0, .1, .2, .25, .3, .4, .5, .6, .7, .75, .8, .9, 1.};
0025     const double result[NSL_STATS_QUANTILE_TYPE_COUNT][NQ + 1] = {
0026         {1., 1., 1., 1., 1., 3., 4., 7., 9., 11., 11., 13., 13., 4.},
0027         {1., 1., 1., 2., 2., 3.5, 5.5, 8., 10., 12., 12., 13., 13., 5.5},
0028         {1., 1., 1., 1., 1., 3., 4., 7., 9., 11., 11., 13., 13., 4.},
0029         {1., 1., 1., 1., 1., 3., 4., 7., 9., 10., 11., 13., 13., 4.},
0030         {1., 1., 1., 1., 2., 3.5, 5.5, 8., 10., 11., 12., 13., 13., 5.5},
0031         {1., 1., 1., 1., 1.6, 3.4, 5.5, 8.2, 10.4, 11.5, 12.6, 13., 13., 5.5},
0032         {1., 1., 1., 1.5, 2.4, 3.6, 5.5, 7.8, 9.6, 10.5, 11.4, 13., 13., 5.5},
0033         {1., 1., 1., 1., 28. / 15., 52. / 15., 5.5, 121. / 15., 152. / 15., 335. / 30., 12.2, 13., 13., 5.5},
0034         {1., 1., 1., 1., 1.9, 3.475, 5.5, 8.05, 10.1, 11.125, 12.15, 13., 13., 5.5}};
0035 
0036     int type, i;
0037     for (type = 1; type <= NSL_STATS_QUANTILE_TYPE_COUNT; ++type) {
0038         printf("quantile type %d\n", type);
0039         for (i = 0; i < NQ; ++i) {
0040             double value = nsl_stats_quantile_sorted(data_sorted, 1, N, quantile[i], (nsl_stats_quantile_type)type);
0041             // printf("%d %d: %g %g\n", i, j, value, result[i-1][j]);
0042             QCOMPARE(value, result[type - 1][i]);
0043         }
0044         QCOMPARE(nsl_stats_median_sorted(data_sorted, 1, N, (nsl_stats_quantile_type)type), result[type - 1][NQ]);
0045     }
0046     for (type = 1; type <= NSL_STATS_QUANTILE_TYPE_COUNT; ++type) {
0047         printf("quantile type %d\n", type);
0048         for (i = 0; i < NQ; ++i) {
0049             double value = nsl_stats_quantile(data_unsorted, 1, N, quantile[i], (nsl_stats_quantile_type)type);
0050             // printf("%d %d: %g %g\n", i, j, value, result[i-1][j]);
0051             QCOMPARE(value, result[type - 1][i]);
0052         }
0053         QCOMPARE(nsl_stats_median_sorted(data_sorted, 1, N, (nsl_stats_quantile_type)type), result[type - 1][NQ]);
0054     }
0055 }
0056 
0057 // ##############################################################################
0058 // #################  performance
0059 // ##############################################################################
0060 
0061 QTEST_MAIN(NSLStatsTest)