Here’s a complete C++17 example that uses the Likelihood‐Ratio Test (LRT) to decide
H₀: both samples come from the same Nₚ(μ, Σ)
vs.
H₁: the samples come from two different Nₚ(μ₁, Σ₁) and Nₚ(μ₂, Σ₂)
We use Eigen for linear algebra and Boost.Math for the χ²‐p–value.
#include <iostream>
#include <vector>
#include <Eigen/Dense>
#include <boost/math/distributions/chi_squared.hpp>
// Compute the sample mean (MLE) of a set of vectors
Eigen::VectorXd sampleMean(const std::vector<Eigen::VectorXd>& X) {
int n = X.size(), p = X[0].size();
Eigen::VectorXd mu = Eigen::VectorXd::Zero(p);
for (const auto& x : X) mu += x;
return mu / double(n);
}
// Compute the MLE sample covariance, i.e. (1/n) Σ_i (x_i - mu)(x_i - mu)'
Eigen::MatrixXd sampleCov(const std::vector<Eigen::VectorXd>& X,
const Eigen::VectorXd& mu) {
int n = X.size(), p = X[0].size();
Eigen::MatrixXd S = Eigen::MatrixXd::Zero(p, p);
for (const auto& x : X) {
Eigen::VectorXd d = x - mu;
S += d * d.transpose();
}
return S / double(n);
}
// Perform the LRT and return (statistic, degrees_of_freedom, p_value)
struct LRTResult {
double statistic;
double df;
double p_value;
};
LRTResult multivariateNormalLRT(
const std::vector<Eigen::VectorXd>& X1,
const std::vector<Eigen::VectorXd>& X2)
{
int n1 = X1.size(), n2 = X2.size();
int p = X1[0].size();
if (n1<1 || n2<1 || X2[0].size()!=p) {
throw std::runtime_error("Sample sizes must be >0 and same dimension p.");
}
// 1) MLEs under H1 (separate)
Eigen::VectorXd mu1 = sampleMean(X1);
Eigen::VectorXd mu2 = sampleMean(X2);
Eigen::MatrixXd S1 = sampleCov(X1, mu1);
Eigen::MatrixXd S2 = sampleCov(X2, mu2);
// 2) MLEs under H0 (common)
// pooled covariance: (n1 S1 + n2 S2)/(n1 + n2)
Eigen::VectorXd mu_pooled = (mu1 * n1 + mu2 * n2) / double(n1 + n2);
Eigen::MatrixXd Sp1 = sampleCov(X1, mu_pooled);
Eigen::MatrixXd Sp2 = sampleCov(X2, mu_pooled);
Eigen::MatrixXd S_pool = (Sp1* n1 + Sp2* n2) / double(n1 + n2);
// 3) compute log-determinants
// (Assume all covariances are PD; otherwise det<=0 leads to -inf)
double l1 = std::log(S1.determinant());
double l2 = std::log(S2.determinant());
double lp = std::log(S_pool.determinant());
// 4) -2 log lambda = n1*ln|S1| + n2*ln|S2| - (n1+n2)*ln|S_pool|
double stat = n1 * l1 + n2 * l2 - (n1 + n2) * lp;
// 5) degrees of freedom = #(params H1) - #(params H0)
// = [2p + p(p+1)] - [p + p(p+1)/2] = (p(p+3))/2
double df = double(p) * double(p + 3) / 2.0;
// 6) p-value from chi-squared survival function
boost::math::chi_squared dist(df);
double pval = boost::math::cdf(boost::math::complement(dist, stat));
return { stat, df, pval };
}
int main() {
// Example: p=2, generate two small random samples
std::vector<Eigen::VectorXd> X1, X2;
int n1 = 10, n2 = 12, p = 2;
// Fill X1, X2 with your data. Here we do a trivial toy:
for(int i=0; i<n1; ++i) {
Eigen::VectorXd x(p);
x << std::sin(i*0.1), std::cos(i*0.1);
X1.push_back(x);
}
for(int i=0; i<n2; ++i) {
Eigen::VectorXd x(p);
x << std::sin(i*0.1 + 0.2), std::cos(i*0.1 + 0.2);
X2.push_back(x);
}
auto res = multivariateNormalLRT(X1, X2);
std::cout << "LRT statistic: " << res.statistic << "\n"
<< "df: " << res.df << "\n"
<< "p-value: " << res.p_value << "\n";
if (res.p_value < 0.05)
std::cout << "=> Reject H0: evidence for different distributions.\n";
else
std::cout << "=> Cannot reject H0: no strong evidence they differ.\n";
return 0;
}
How it works:
- We compute MLE‐means μ and covariances Σ under
• H₁: separate (μ₁, Σ₁) & (μ₂, Σ₂)
• H₀: common (μ, Σ) with “pooled” covariance
- The LRT statistic is
−2 ln λ = n₁ ln|Σ₁| + n₂ ln|Σ₂| − (n₁+n₂) ln|Σ|
- Under H₀, −2 ln λ → χ² with df = p(p+3)/2.
- We compute the p‐value via the upper tail of χ²(df).
You can plug in your own data vectors into X1 and X2. Be sure both groups live in the same dimension p.