File indexing completed on 2024-05-12 03:47:52

0001 /*
0002     File                 : nsl_sort.h
0003     Project              : LabPlot
0004     Description          : NSL functions for the kernel density estimation
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2021-2023 Alexander Semke <alexander.semke@web.de>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef NSL_KDE_H
0011 #define NSL_KDE_H
0012 
0013 #undef __BEGIN_DECLS
0014 #undef __END_DECLS
0015 #ifdef __cplusplus
0016 #define __BEGIN_DECLS extern "C" {
0017 #define __END_DECLS }
0018 #else
0019 #define __BEGIN_DECLS /* empty */
0020 #define __END_DECLS /* empty */
0021 #endif
0022 __BEGIN_DECLS
0023 
0024 #include "nsl_sf_kernel.h"
0025 #include <stdlib.h>
0026 
0027 #define NSL_KDE_BANDWITH_TYPE_COUNT 2
0028 typedef enum { nsl_kde_bandwidth_silverman, nsl_kde_bandwidth_scott, nsl_kde_bandwidth_custom } nsl_kde_bandwidth_type;
0029 
0030 /* calculates the density at point x for the sample data with the bandwith h */
0031 double nsl_kde(const double data[], double x, nsl_kernel_type kernel, double h, size_t n);
0032 
0033 /*!
0034  * calculates the value of the bandwidth parameter for different methods based on the available statistics (count, sigma, iqr).
0035  * supported bandwidth types:
0036  *  * "Silverman's rule of thumb" - Silverman, B. W. (1986). Density Estimation. London: Chapman and Hall.
0037  *  * "Scott's rule of thumb" - Scott, D. W. (1992) Multivariate Density Estimation: Theory, Practice, and Visualization. New York: Wiley.
0038  */
0039 double nsl_kde_bandwidth(int n, double sigma, double iqr, nsl_kde_bandwidth_type type);
0040 
0041 /* similar to nsl_kde_bandwidth, but calculates the required values for count, sigma and iqr from the data */
0042 double nsl_kde_bandwidth_from_data(double* data, int n, nsl_kde_bandwidth_type type);
0043 
0044 /* calculates the bandwidth according to Silverman's rule of thumb" */
0045 double nsl_kde_silverman_bandwidth(double* data, int n);
0046 
0047 /* calculates the bandwidth according to Scott's rule of thumb" */
0048 double nsl_kde_scott_bandwidth(double* data, int n);
0049 
0050 __END_DECLS
0051 
0052 #endif /* NSL_KDE_H */