File indexing completed on 2024-04-28 15:14:12
0001 /*************************************************************************** 0002 File : NSLStatsTest.cpp 0003 Project : LabPlot 0004 Description : NSL Tests for statistical functions 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * * 0011 * This program is free software; you can redistribute it and/or modify * 0012 * it under the terms of the GNU General Public License as published by * 0013 * the Free Software Foundation; either version 2 of the License, or * 0014 * (at your option) any later version. * 0015 * * 0016 * This program is distributed in the hope that it will be useful, * 0017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0019 * GNU General Public License for more details. * 0020 * * 0021 * You should have received a copy of the GNU General Public License * 0022 * along with this program; if not, write to the Free Software * 0023 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0024 * Boston, MA 02110-1301 USA * 0025 * * 0026 ***************************************************************************/ 0027 0028 #include "NSLStatsTest.h" 0029 0030 extern "C" { 0031 #include "backend/nsl/nsl_stats.h" 0032 } 0033 0034 //############################################################################## 0035 //################# Quantile test 0036 //############################################################################## 0037 0038 const int N = 10; 0039 const int NQ = 13; 0040 0041 void NSLStatsTest::testQuantile() { 0042 const double data_sorted[] = {1, 1, 1, 3, 4, 7, 9, 11, 13, 13}; 0043 double data_unsorted[] = {3, 7, 11, 1, 13, 1, 9, 1, 13, 4}; 0044 const double quantile[] = {.0, .1, .2, .25, .3, .4, .5, .6, .7, .75, .8, .9, 1.}; 0045 const double result[NSL_STATS_QUANTILE_TYPE_COUNT][NQ+1] = { 0046 {1., 1., 1., 1., 1., 3., 4., 7., 9., 11., 11., 13., 13., 4.}, 0047 {1., 1., 1., 2., 2., 3.5, 5.5, 8., 10., 12., 12., 13., 13., 5.5}, 0048 {1., 1., 1., 1., 1., 3., 4., 7., 9., 11., 11., 13., 13., 4.}, 0049 {1., 1., 1., 1., 1., 3., 4., 7., 9., 10., 11., 13., 13., 4.}, 0050 {1., 1., 1., 1., 2., 3.5, 5.5, 8., 10., 11., 12., 13., 13., 5.5}, 0051 {1., 1., 1., 1., 1.6, 3.4, 5.5, 8.2, 10.4, 11.5, 12.6, 13., 13., 5.5}, 0052 {1., 1., 1., 1.5, 2.4, 3.6, 5.5, 7.8, 9.6, 10.5, 11.4, 13., 13., 5.5}, 0053 {1., 1., 1., 1., 28./15., 52./15., 5.5, 121./15., 152./15., 335./30., 12.2, 13., 13., 5.5}, 0054 {1., 1., 1., 1., 1.9, 3.475, 5.5, 8.05, 10.1, 11.125, 12.15, 13., 13., 5.5}}; 0055 0056 int type, i; 0057 for (type = 1; type <= NSL_STATS_QUANTILE_TYPE_COUNT; ++type) { 0058 printf("quantile type %d\n", type); 0059 for (i = 0; i < NQ; ++i) { 0060 double value = nsl_stats_quantile_sorted(data_sorted, 1, N, quantile[i], (nsl_stats_quantile_type)type); 0061 //printf("%d %d: %g %g\n", i, j, value, result[i-1][j]); 0062 QCOMPARE(value, result[type-1][i]); 0063 } 0064 QCOMPARE(nsl_stats_median_sorted(data_sorted, 1, N, (nsl_stats_quantile_type)type), result[type-1][NQ]); 0065 } 0066 for (type = 1; type <= NSL_STATS_QUANTILE_TYPE_COUNT; ++type) { 0067 printf("quantile type %d\n", type); 0068 for (i = 0; i < NQ; ++i) { 0069 double value = nsl_stats_quantile(data_unsorted, 1, N, quantile[i], (nsl_stats_quantile_type)type); 0070 //printf("%d %d: %g %g\n", i, j, value, result[i-1][j]); 0071 QCOMPARE(value, result[type-1][i]); 0072 } 0073 QCOMPARE(nsl_stats_median_sorted(data_sorted, 1, N, (nsl_stats_quantile_type)type), result[type-1][NQ]); 0074 } 0075 } 0076 0077 //############################################################################## 0078 //################# performance 0079 //############################################################################## 0080 0081 QTEST_MAIN(NSLStatsTest)