CMP++: Uncertainty Quantification & Bayesian Calibration
Loading...
Searching...
No Matches
kde.h
Go to the documentation of this file.
1#ifndef CMP_KDE_H
2#define CMP_KDE_H
3
4#include <cmp_defines.h>
5#include <statistics.h>
6#include <kernel++.h>
7#include <optimization.h>
8#include <distribution.h>
9
10
15namespace cmp::density {
16
17// Here we implement a KDE class that uses the above bandwidth and kernel classes
35class KDE {
36 private:
37 std::shared_ptr<kernel::Bandwidth> bandwidth_{nullptr};
38 std::shared_ptr<kernel::Kernel> kernel_{nullptr};
39
40 // Owning or non-owning data storage
41 Eigen::MatrixXd data_{0, 0};
42 std::optional<Eigen::Ref<const Eigen::MatrixXd>> pData_;
43 bool isOwning_{false};
44
45 // Dimension and number of points
46 size_t dim_{0};
47 size_t nPoints_{0};
48 public:
49
54 KDE();
55
61 KDE(std::shared_ptr<kernel::Bandwidth> bandwidth, std::shared_ptr<kernel::Kernel> kernel);
62
63 // Copy and move constructors
64 KDE(const KDE &other);
65 KDE(KDE &&other) noexcept;
66
67 // Copy and move assignment operators
68 KDE &operator=(const KDE &other);
69 KDE &operator=(KDE &&other) noexcept;
70
76 void set(const std::shared_ptr<kernel::Bandwidth> &bandwidth, const std::shared_ptr<kernel::Kernel> &kernel);
77
83 void condition(const Eigen::Ref<const Eigen::MatrixXd> &data, bool copyData = false);
84
90 double eval(const Eigen::VectorXd &x) const;
91
92 public:
94 SCOTT,
96 };
97
98};
99
107Eigen::MatrixXd bandwidthSelectionRule(const KDE::BandWidthSelectionMethod &method, Eigen::Ref<Eigen::MatrixXd> xObs);
108
121void bandwidthOptimizationCrossValidation(const cmp::statistics::KFold& kf, Eigen::Ref<Eigen::MatrixXd> xObs, std::shared_ptr<cmp::kernel::Kernel> kernel, std::shared_ptr<cmp::kernel::Bandwidth> initialBandwidth, const double &min, const double &max, nlopt::algorithm alg, const double & tol = 1e-4);
122
123
124
125} // namespace cmp::density
126
129#endif // CMP_KDE_H
Multivariate Kernel Density Estimation (KDE) class.
Definition kde.h:35
void set(const std::shared_ptr< kernel::Bandwidth > &bandwidth, const std::shared_ptr< kernel::Kernel > &kernel)
Set the bandwidth and kernel for the KDE.
Definition kde.cpp:37
std::optional< Eigen::Ref< const Eigen::MatrixXd > > pData_
Non-owning reference wrapper to data.
Definition kde.h:42
double eval(const Eigen::VectorXd &x) const
Evaluate the KDE at a given point.
Definition kde.cpp:59
KDE()
Default constructor for KDE. Creates a KDE with identity bandwidth and Gaussian kernel in 1D.
Definition kde.cpp:3
size_t nPoints_
Number of data points.
Definition kde.h:47
size_t dim_
Dimension of the dataset.
Definition kde.h:46
void condition(const Eigen::Ref< const Eigen::MatrixXd > &data, bool copyData=false)
Condition the KDE on the provided data.
Definition kde.cpp:43
BandWidthSelectionMethod
Definition kde.h:93
Eigen::MatrixXd data_
Owning data storage.
Definition kde.h:41
std::shared_ptr< kernel::Bandwidth > bandwidth_
Bandwidth matrix of the kernel.
Definition kde.h:37
std::shared_ptr< kernel::Kernel > kernel_
The density kernel function.
Definition kde.h:38
KDE & operator=(const KDE &other)
Definition kde.cpp:21
bool isOwning_
Flag indicating whether the KDE owns the data.
Definition kde.h:43
K-Fold cross-validation partition generator.
Definition statistics.h:44
Definition kde.h:15
void bandwidthOptimizationCrossValidation(const cmp::statistics::KFold &kf, Eigen::Ref< Eigen::MatrixXd > xObs, std::shared_ptr< cmp::kernel::Kernel > kernel, std::shared_ptr< cmp::kernel::Bandwidth > initialBandwidth, const double &min, const double &max, nlopt::algorithm alg, const double &tol=1e-4)
Optimizes the bandwidth matrix using K-Fold cross-validation over the observations.
Definition kde.cpp:115
Eigen::MatrixXd bandwidthSelectionRule(const KDE::BandWidthSelectionMethod &method, Eigen::Ref< Eigen::MatrixXd > xObs)
Computes the bandwidth matrix using standard rules of thumb.
Definition kde.cpp:72
Functor wrapper for NLopt with automatic, transparent log-scaling.
Header file for statistical functions and classes.