|
| const Eigen::MatrixXd | DummyMatrix (0, 0) |
| |
| const Eigen::VectorXd | DummyVector (0) |
| |
| Eigen::LDLT< Eigen::MatrixXd > | ldltDecomposition (const Eigen::Ref< const Eigen::MatrixXd > &cov) |
| | Computes the LDLT decomposition of a symmetric matrix.
|
| |
| template<VectorLike V> |
| auto | asEigen (const V &v) -> Eigen::Map< const Eigen::Matrix< typename V::value_type, Eigen::Dynamic, 1 > > |
| | Creates a non-owning, read-only Eigen view (Eigen::Map) of a vector-like container.
|
| |
| template<VectorLike V> |
| auto | asEigen (V &v) -> Eigen::Map< Eigen::Matrix< typename V::value_type, Eigen::Dynamic, 1 > > |
| | Creates a non-owning, read-write Eigen view (Eigen::Map) of a vector-like container.
|
| |
| template<VectorLike V> |
| auto | toEigen (const V &v) -> Eigen::VectorXd |
| | Copies the elements of a vector-like container to a new Eigen::VectorXd.
|
| |
| template<VectorLike V> |
| auto | toStdVector (const V &v) -> std::vector< typename V::value_type > |
| | Copies the elements of a vector-like container to a new std::vector.
|
| |
| template<typename Derived > |
| Derived::PlainObject | slice (const Eigen::MatrixBase< Derived > &mat, const Eigen::VectorXs &indices) |
| | Slices a matrix along its rows based on a set of indices.
|
| |
| template<typename DerivedX , typename DerivedY > |
| auto | trainTestSplit (const Eigen::MatrixBase< DerivedX > &xObs, const Eigen::MatrixBase< DerivedY > &yObs, const std::pair< Eigen::VectorXs, Eigen::VectorXs > &train_test_pair) |
| | Function to split training data into training and test sets.
|
| |
| double | fd_gradient (const Eigen::VectorXd &x_0, const std::function< double(const Eigen::VectorXd &)> fun, const int &i, const accuracy order=SECOND, const double h=1E-5) |
| | Computes the i-th component of the gradient of the function fun evaluated at x_0 using a central difference scheme.
|
| |
| double | fd_hessian (const Eigen::VectorXd &x_0, const std::function< double(const Eigen::VectorXd &)> fun, const int &i, const int &j, const accuracy order=SECOND, const double h=1E-5) |
| | Computes the i-th and j-th component of the Hessian of the function fun evaluated at x_0 using a central difference scheme.
|
| |
| void | write_vector (const std::vector< Eigen::VectorXd > &data, std::ofstream &o_file) |
| | Serializes a list of vector coordinates to a file stream.
|
| |
| void | write_data (const std::vector< Eigen::VectorXd > &x, const Eigen::MatrixXd &y, std::ofstream &o_file) |
| | Write data points and evaluations to a file.
|
| |
| std::vector< Eigen::VectorXd > | read_vector (std::ifstream &i_file, std::string delimiter=",", size_t header=0) |
| | Read the contents of a delimited text file into vector collections.
|
| |
| double | nlopt_max (cmp::ObjectiveFunctor &f, Eigen::Ref< Eigen::VectorXd > x0, const Eigen::Ref< const Eigen::VectorXd > &lb, const Eigen::Ref< const Eigen::VectorXd > &ub, nlopt::algorithm alg=nlopt::LN_SBPLX, double ftol_rel=1e-6) |
| | Global helper function to execute NLopt maximization, managing the log-scaling wrapper. Users pass physical, real-world bounds and initial guesses. The function manages the log translations.
|
| |
| double | wasserstein1D (Eigen::VectorXd &s1, Eigen::VectorXd &s2, double p) |
| | Computes the 1D Wasserstein distance between two empirical distributions.
|
| |
| double | slicedWassersteinDistance (const Eigen::MatrixXd &samples_1, const Eigen::MatrixXd &samples_2, double p, int slices, size_t seed=42) |
| | Computes the sliced-Wasserstein distance of order p between two n-dimensional datasets.
|
| |
| double cmp::fd_gradient |
( |
const Eigen::VectorXd & |
x_0, |
|
|
const std::function< double(const Eigen::VectorXd &)> |
fun, |
|
|
const int & |
i, |
|
|
const accuracy |
order = SECOND, |
|
|
const double |
h = 1E-5 |
|
) |
| |
Computes the i-th component of the gradient of the function fun evaluated at x_0 using a central difference scheme.
Mathematical Formulation Approximates the partial derivative of \(f: \mathbb{R}^D \to \mathbb{R}\) at \(\mathbf{x}_0\) with respect to the \(i\)-th variable using a central difference stencil:
\[
\frac{\partial f(\mathbf{x}_0)}{\partial x_i} \approx \frac{f(\mathbf{x}_0 + h \mathbf{e}_i) - f(\mathbf{x}_0 - h \mathbf{e}_i)}{2h}
\]
where \(\mathbf{e}_i\) is the \(i\)-th standard basis vector, and \(h > 0\) is the step size. For higher-order accuracy (e.g., 4th, 6th, or 8th order), wider stencils are employed.
Implementation Algorithm
- Creates coordinate perturbations \(\mathbf{x}_0 \pm n h \mathbf{e}_i\) for stencil points \(n\).
- Evaluates the function
fun at each perturbed point.
- Takes a linear combination of the function values scaled by the central difference stencil coefficients.
- Parameters
-
| x_0 | The point at which to evaluate the gradient |
| fun | The function of which to evaluate the gradient |
| i | The component of the gradient |
| order | The accuracy order (default = SECOND) |
| h | The step size (default = 1E-5) |
- Returns
- The required component of the gradient
| double cmp::fd_hessian |
( |
const Eigen::VectorXd & |
x_0, |
|
|
const std::function< double(const Eigen::VectorXd &)> |
fun, |
|
|
const int & |
i, |
|
|
const int & |
j, |
|
|
const accuracy |
order = SECOND, |
|
|
const double |
h = 1E-5 |
|
) |
| |
Computes the i-th and j-th component of the Hessian of the function fun evaluated at x_0 using a central difference scheme.
Mathematical Formulation Approximates the second-order partial derivative \(\frac{\partial^2 f(\mathbf{x}_0)}{\partial x_i \partial x_j}\) at \(\mathbf{x}_0\). For diagonal elements ( \(i = j\)):
\[
\frac{\partial^2 f(\mathbf{x}_0)}{\partial x_i^2} \approx \frac{f(\mathbf{x}_0 + h\mathbf{e}_i) - 2f(\mathbf{x}_0) + f(\mathbf{x}_0 - h\mathbf{e}_i)}{h^2}
\]
For off-diagonal elements ( \(i \neq j\)):
\[
\frac{\partial^2 f(\mathbf{x}_0)}{\partial x_i \partial x_j} \approx \frac{f(\mathbf{x}_0 + h\mathbf{e}_i + h\mathbf{e}_j) - f(\mathbf{x}_0 + h\mathbf{e}_i - h\mathbf{e}_j) - f(\mathbf{x}_0 - h\mathbf{e}_i + h\mathbf{e}_j) + f(\mathbf{x}_0 - h\mathbf{e}_i - h\mathbf{e}_j)}{4h^2}
\]
Implementation Algorithm
- Checks if \(i = j\) or \(i \neq j\).
- Prepares perturbations along standard directions \(\mathbf{e}_i\) and \(\mathbf{e}_j\).
- Evaluates the function at the perturbed points and computes the stencil difference quotient.
- Parameters
-
| x_0 | The point at which to evaluate the hessian |
| fun | The function of which to evaluate the hessian |
| i | Row of the hessian |
| j | Column of the hessian |
| order | The accuracy order (default = SECOND) |
| h | The step size (default = 1E-5) |
- Returns
- The required component of the Hessian
| Eigen::LDLT< Eigen::MatrixXd > cmp::ldltDecomposition |
( |
const Eigen::Ref< const Eigen::MatrixXd > & |
cov | ) |
|
|
inline |
Computes the LDLT decomposition of a symmetric matrix.
Mathematical Formulation For a symmetric positive semidefinite matrix \(\mathbf{A} \in \mathbb{R}^{n \times n}\), LDLT decomposes \(\mathbf{A}\) into:
\[
\mathbf{A} = \mathbf{L} \mathbf{D} \mathbf{L}^T
\]
where \(\mathbf{L}\) is a lower unit triangular matrix and \(\mathbf{D}\) is a diagonal matrix. This decomposition is more numerically stable than Cholesky decomposition for semi-definite matrices as it avoids taking square roots of diagonal elements.
Implementation Algorithm Calls Eigen's .ldlt() method on the matrix block.
template<typename Derived >
| Derived::PlainObject cmp::slice |
( |
const Eigen::MatrixBase< Derived > & |
mat, |
|
|
const Eigen::VectorXs & |
indices |
|
) |
| |
Slices a matrix along its rows based on a set of indices.
Mathematical Formulation For a matrix \(\mathbf{X} \in \mathbb{R}^{M \times D}\) and a vector of indices \(\mathcal{I} = [i_1, i_2, \dots, i_K]^T\), computes the sliced matrix \(\mathbf{X}_{\mathcal{I}} \in \mathbb{R}^{K \times D}\):
\[
\left(\mathbf{X}_{\mathcal{I}}\right)_{k, \cdot} = \mathbf{X}_{i_k, \cdot}
\]
- Template Parameters
-
| Derived | The derived Eigen matrix expression type. |
- Parameters
-
| mat | The input matrix. |
| indices | A vector of row indices to extract. |
- Returns
- A new plain matrix containing only the specified rows of mat.
| double cmp::slicedWassersteinDistance |
( |
const Eigen::MatrixXd & |
samples_1, |
|
|
const Eigen::MatrixXd & |
samples_2, |
|
|
double |
p, |
|
|
int |
slices, |
|
|
size_t |
seed = 42 |
|
) |
| |
Computes the sliced-Wasserstein distance of order p between two n-dimensional datasets.
Mathematical Formulation The Sliced Wasserstein distance projects multivariate distributions \(\mu, \nu\) in \(\mathbb{R}^D\) onto 1D lines along random directions \(\boldsymbol{\theta} \in \mathbb{S}^{D-1}\) (the unit sphere):
\[
SW_p^p(\mu, \nu) = \int_{\mathbb{S}^{D-1}} W_p^p(\boldsymbol{\theta}^* \mu, \boldsymbol{\theta}^* \nu) d\boldsymbol{\theta} \approx \frac{1}{L} \sum_{l=1}^L W_p^p(\boldsymbol{\theta}_l^T \mathbf{X}_1, \boldsymbol{\theta}_l^T \mathbf{X}_2)
\]
where \(\boldsymbol{\theta}_l\) are random projection directions drawn uniformly from the unit sphere.
Implementation Algorithm
- Generates \(L\) random directions by sampling from a standard multivariate Gaussian and normalizing to unit norm.
- Projects the multivariate samples \(\mathbf{X}_1\) and \(\mathbf{X}_2\) onto each direction.
- Computes the 1D Wasserstein distance for each projection and returns the arithmetic mean.
- Parameters
-
| samples_1 | The first set of samples (N x D matrix). |
| samples_2 | The second set of samples (M x D matrix). |
| p | The order of the distance. |
| slices | The number of slices to compute. |
| seed | The seed for the random number generator (passed to thread-local PRNGs). |
- Returns
- SW_p^p, the p-th power of the sliced Wasserstein distance.
| double cmp::wasserstein1D |
( |
Eigen::VectorXd & |
s1, |
|
|
Eigen::VectorXd & |
s2, |
|
|
double |
p |
|
) |
| |
Computes the 1D Wasserstein distance between two empirical distributions.
Mathematical Formulation The 1D Wasserstein distance of order \(p \ge 1\) between two empirical measures \(\mu = \frac{1}{N}\sum \delta_{x_i}\) and \(\nu = \frac{1}{M}\sum \delta_{y_j}\) is defined as the \(L^p\) distance between their quantile functions \(F^{-1}(t)\) and \(G^{-1}(t)\):
\[
W_p^p(\mu, \nu) = \int_0^1 |F^{-1}(t) - G^{-1}(t)|^p dt
\]
When \(N = M\), this reduces to:
\[
W_p^p(\mu, \nu) = \frac{1}{N} \sum_{i=1}^N |x_{(i)} - y_{(i)}|^p
\]
where \(x_{(i)}\) and \(y_{(i)}\) are sorted samples. For unequal sample sizes, the integral is solved by integrating the differences of the step functions over partitions of \([0, 1]\).
Implementation Algorithm
- Sorts
s1 and s2 in-place to form the empirical quantiles.
- Iterates through the joint partition of \([0, 1]\) defined by the cumulative weight steps \(i/N\) and \(j/M\).
- Integrates the constant difference raised to power \(p\) over each interval.
- Parameters
-
| s1 | The first set of 1D projected samples. (Modified in-place) |
| s2 | The second set of 1D projected samples. (Modified in-place) |
| p | The order of the distance. |
- Returns
- W_p^p, the p-th power of the Wasserstein distance.
| void cmp::write_data |
( |
const std::vector< Eigen::VectorXd > & |
x, |
|
|
const Eigen::MatrixXd & |
y, |
|
|
std::ofstream & |
o_file |
|
) |
| |
Write data points and evaluations to a file.
Mathematical Formulation Serializes input vectors \(\mathbf{x}_i \in \mathbb{R}^D\) along with corresponding multivariate observations \(\mathbf{y}_i \in \mathbb{R}^Q\):
\[
\text{Row}_i = [x_{i,1}, \dots, x_{i,D}, y_{i,1}, \dots, y_{i,Q}]
\]
Implementation Algorithm Loops over the vector entries, streaming coordinates of \(\mathbf{x}_i\) followed by the corresponding row coordinates of the matrix \(\mathbf{y}_i\) into o_file.
- Parameters
-
| x | The points (as a vector) |
| y | The data (as a matrix) |
| o_file | The output file |
| void cmp::write_vector |
( |
const std::vector< Eigen::VectorXd > & |
data, |
|
|
std::ofstream & |
o_file |
|
) |
| |
Serializes a list of vector coordinates to a file stream.
Mathematical Formulation Serializes a set of vectors \(\{\mathbf{x}_i\}_{i=1}^M\) where \(\mathbf{x}_i \in \mathbb{R}^N\) into a structured grid representation in text format:
\[
\mathbf{x}_i^T = [x_{i,1}, x_{i,2}, \dots, x_{i,N}]
\]
Implementation Algorithm Streams each vector to the output file stream o_file line-by-line, separating coordinates with spaces.
- Parameters
-
| data | A matrix containing the data. |
| o_file | An open and valid file in which to write the contents. |