Below is the C++ implementation of the likelihood_ratio_test function that uses the Likelihood Ratio Test (LRT) to determine if two sets of multivariate normal samples originate from the same distribution or different distributions. The function leverages the Eigen library for linear algebra operations and the Boost library for statistical computations.
#include <vector>
#include <Eigen/Dense>
#include <boost/math/distributions/chi_squared.hpp>
float likelihood_ratio_test(const std::vector<std::vector<float>>& samples1, const std::vector<std::vector<float>>& samples2) {
using Eigen::MatrixXd;
using Eigen::VectorXd;
// Convert input data to Eigen matrices
size_t n1 = samples1.size();
size_t n2 = samples2.size();
size_t dim = samples1.empty() ? 0 : samples1[0].size();
if (dim == 0 || n1 == 0 || n2 == 0 || samples2.empty() || samples2[0].size() != dim) {
throw std::invalid_argument("Invalid input: Samples must be non-empty and have consistent dimensions.");
}
MatrixXd X1(n1, dim);
MatrixXd X2(n2, dim);
for (size_t i = 0; i < n1; ++i) {
for (size_t j = 0; j < dim; ++j) {
X1(i, j) = samples1[i][j];
}
}
for (size_t i = 0; i < n2; ++i) {
for (size_t j = 0; j < dim; ++j) {
X2(i, j) = samples2[i][j];
}
}
// Compute sample means
VectorXd mean1 = X1.colwise().mean();
VectorXd mean2 = X2.colwise().mean();
// Compute sample covariance matrices
MatrixXd centered1 = X1.rowwise() - mean1.transpose();
MatrixXd centered2 = X2.rowwise() - mean2.transpose();
MatrixXd cov1 = (centered1.adjoint() * centered1) / static_cast<double>(n1 - 1);
MatrixXd cov2 = (centered2.adjoint() * centered2) / static_cast<double>(n2 - 1);
// Compute pooled covariance matrix
MatrixXd pooled_cov = ((n1 - 1) * cov1 + (n2 - 1) * cov2) / static_cast<double>(n1 + n2 - 2);
// Compute likelihood ratio test statistic
double log_det_cov1 = cov1.determinant() == 0 ? 0 : std::log(cov1.determinant());
double log_det_cov2 = cov2.determinant() == 0 ? 0 : std::log(cov2.determinant());
double log_det_pooled = pooled_cov.determinant() == 0 ? 0 : std::log(pooled_cov.determinant());
double lrt_statistic = (n1 + n2) * log_det_pooled - n1 * log_det_cov1 - n2 * log_det_cov2;
// Degrees of freedom for the chi-squared distribution
size_t df = dim * (dim + 1) / 2;
// Compute p-value using the chi-squared distribution
boost::math::chi_squared chi_dist(df);
double p_value = 1.0 - boost::math::cdf(chi_dist, lrt_statistic);
return static_cast<float>(p_value);
}
Explanation of the Code
-
Input Validation:
- The function checks that both input vectors are non-empty and have consistent dimensions. If not, it throws an exception.
-
Data Conversion:
- The input
std::vector<std::vector<float>> is converted into Eigen matrices (MatrixXd) for efficient computation.
-
Mean and Covariance Computation:
- The sample means (
mean1 and mean2) are computed using Eigen's colwise().mean() method.
- The centered data matrices (
centered1 and centered2) are obtained by subtracting the mean from each row.
- The sample covariance matrices (
cov1 and cov2) are calculated using the formula for covariance.
-
Pooled Covariance Matrix:
- The pooled covariance matrix is computed as a weighted average of the two sample covariance matrices.
-
Likelihood Ratio Test Statistic:
- The LRT statistic is derived from the determinants of the covariance matrices and the pooled covariance matrix.
-
Chi-Squared Distribution:
- The degrees of freedom (
df) for the chi-squared distribution is determined based on the dimensionality of the data.
- The p-value is computed using the cumulative distribution function (CDF) of the chi-squared distribution provided by Boost.
-
Return Value:
- The function returns the computed p-value as a
float.
Dependencies
- Eigen Library: Used for matrix and vector operations.
- Boost Library: Used for statistical functions (chi-squared distribution).
Notes
- Ensure that the input data is valid and preprocessed (e.g., no missing values).
- If the covariance matrices are singular (determinant is zero), the log-determinant is set to zero to avoid numerical issues.
- The function assumes that the input data follows a multivariate normal distribution.
Example Usage
int main() {
std::vector<std::vector<float>> samples1 = {{1.0, 2.0}, {2.0, 3.0}, {3.0, 4.0}};
std::vector<std::vector<float>> samples2 = {{4.0, 5.0}, {5.0, 6.0}, {6.0, 7.0}};
float p_value = likelihood_ratio_test(samples1, samples2);
std::cout << "P-value: " << p_value << std::endl;
return 0;
}
This code provides a concise and efficient implementation of the Likelihood Ratio Test for comparing two sets of multivariate normal samples.