61 const std::vector<arma::mat>& conversations,
65 if (max_window < 0) max_window = 0;
66 const int n_lags = max_window + 1;
69 out.
lag = arma::regspace<arma::vec>(0, max_window);
70 out.
frob = arma::vec(n_lags).fill(arma::datum::nan);
79 for (
const arma::mat& X : conversations) {
80 if (X.n_cols > 0) { C = X.n_cols;
break; }
82 if (C == 0)
return out;
85 for (
int i = 0; i < n_lags; ++i) {
87 arma::mat Cov_sum = arma::zeros<arma::mat>(C, C);
88 double total_weight = 0.0;
89 double noise_floor_sq_weighted_sum = 0.0;
91 for (
const arma::mat& X_k : conversations) {
92 const int N_k =
static_cast<int>(X_k.n_rows);
93 const int weight = N_k - l;
94 if (weight < min_overlap)
continue;
97 const arma::mat A = X_k.rows(l, N_k - 1);
98 const arma::mat B = X_k.rows(0, N_k - 1 - l);
100 const arma::rowvec p_A = arma::mean(A, 0);
101 const arma::rowvec p_B = arma::mean(B, 0);
104 const arma::mat Cov_k = (B.t() * A) / weight - p_B.t() * p_A;
107 const double tr_var_A =
108 arma::accu(arma::square(A.each_row() - p_A)) / (weight - 1);
109 const double tr_var_B =
110 arma::accu(arma::square(B.each_row() - p_B)) / (weight - 1);
111 const double var_k_sq = tr_var_A * tr_var_B;
113 Cov_sum += weight * Cov_k;
114 total_weight += weight;
115 noise_floor_sq_weighted_sum += weight * var_k_sq;
118 if (total_weight > 0.0) {
119 const arma::mat Cov_pooled = Cov_sum / total_weight;
120 const double frob_sq_pooled = arma::accu(arma::square(Cov_pooled));
121 const double noise_floor_sq_pooled =
122 noise_floor_sq_weighted_sum / (total_weight * total_weight);
123 const double frob_sq_unbiased_pooled =
124 frob_sq_pooled - noise_floor_sq_pooled;
126 const double sgn = (frob_sq_unbiased_pooled > 0.0) ? 1.0
127 : (frob_sq_unbiased_pooled < 0.0) ? -1.0 : 0.0;
129 out.
frob[i] = std::sqrt(frob_sq_pooled);
141 double max_f = -arma::datum::inf;
142 for (
int i = 1; i < n_lags; ++i) {
143 if (std::isfinite(f[i]) && f[i] > 0.0 && f[i] > max_f) {
160 for (
int i = peak_lag; i < n_lags; ++i) {
161 if (f[i] / max_f <= 0.5) { decay_lag = i;
break; }
164 if (decay_lag >= 0) {
170 for (
int i = peak_lag; i < n_lags; ++i) {
171 if (std::isfinite(f[i])) last_valid = i;
173 out.
window_size = (last_valid >= 0) ? last_valid : 1;
CCDResult ccd_window(const std::vector< arma::mat > &conversations, int max_window=20, int min_overlap=10)
Estimate the ENA moving-window size via cross-covariance decay.
Definition ccd.hpp:60