libena 0.4.18-dev.9021
QE C++ library
Loading...
Searching...
No Matches
ccd.hpp
Go to the documentation of this file.
1
18#ifndef LIBQE_CCD_HPP
19#define LIBQE_CCD_HPP
20
21#include <armadillo>
22#include <cmath>
23#include <vector>
24
25namespace qe {
26
32struct CCDResult {
35 arma::vec lag;
36 arma::vec frob;
37 arma::vec frob_sq_unbiased;
39 arma::vec total_weight;
40};
41
61 const std::vector<arma::mat>& conversations,
62 int max_window = 20,
63 int min_overlap = 10
64) {
65 if (max_window < 0) max_window = 0;
66 const int n_lags = max_window + 1;
67
68 CCDResult out;
69 out.lag = arma::regspace<arma::vec>(0, max_window);
70 out.frob = arma::vec(n_lags).fill(arma::datum::nan);
71 out.frob_sq_unbiased = arma::vec(n_lags).fill(arma::datum::nan);
72 out.frob_unbiased_signed = arma::vec(n_lags).fill(arma::datum::nan);
73 out.total_weight = arma::zeros<arma::vec>(n_lags);
74 out.window_size = 1;
75 out.peak_lag = 0;
76
77 // Determine code count from the first non-empty conversation.
78 arma::uword C = 0;
79 for (const arma::mat& X : conversations) {
80 if (X.n_cols > 0) { C = X.n_cols; break; }
81 }
82 if (C == 0) return out; // nothing to work with -> window 1
83
84 // 1. Cross-covariance curves & noise floor across lags.
85 for (int i = 0; i < n_lags; ++i) {
86 const int l = 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;
90
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;
95
96 // A = rows [l, N_k-1], B = rows [0, N_k-1-l]; both have `weight` rows.
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);
99
100 const arma::rowvec p_A = arma::mean(A, 0);
101 const arma::rowvec p_B = arma::mean(B, 0);
102
103 // Window-matched cross-covariance: crossprod(B, A)/w - outer(p_B, p_A).
104 const arma::mat Cov_k = (B.t() * A) / weight - p_B.t() * p_A;
105
106 // Trace-variance noise floor (n-1 denominator, matching R's var()).
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;
112
113 Cov_sum += weight * Cov_k;
114 total_weight += weight;
115 noise_floor_sq_weighted_sum += weight * var_k_sq;
116 }
117
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;
125
126 const double sgn = (frob_sq_unbiased_pooled > 0.0) ? 1.0
127 : (frob_sq_unbiased_pooled < 0.0) ? -1.0 : 0.0;
128
129 out.frob[i] = std::sqrt(frob_sq_pooled);
130 out.frob_sq_unbiased[i] = frob_sq_unbiased_pooled;
131 out.frob_unbiased_signed[i] = sgn * std::sqrt(std::abs(frob_sq_unbiased_pooled));
132 out.total_weight[i] = total_weight;
133 }
134 }
135
136 // 2. Half-life decay lag detection over the signed corrected norm.
137 const arma::vec& f = out.frob_unbiased_signed;
138
139 // Peak among lags > 0 with finite, positive corrected norm.
140 int peak_lag = -1;
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) {
144 max_f = f[i];
145 peak_lag = i; // lag == index
146 }
147 }
148
149 if (peak_lag < 0) {
150 // No positive corrected covariance anywhere -> default window 1.
151 out.window_size = 1;
152 out.peak_lag = 0;
153 return out;
154 }
155 out.peak_lag = peak_lag;
156
157 // First lag at or after the peak whose corrected norm has decayed to <= 50%.
158 // NaN comparisons are false, so NaN lags are skipped (matching R's which()).
159 int decay_lag = -1;
160 for (int i = peak_lag; i < n_lags; ++i) {
161 if (f[i] / max_f <= 0.5) { decay_lag = i; break; }
162 }
163
164 if (decay_lag >= 0) {
165 out.window_size = decay_lag;
166 } else {
167 // No decay below half-life within max_window: fall back to the last lag
168 // (at or after the peak) with a finite corrected norm.
169 int last_valid = -1;
170 for (int i = peak_lag; i < n_lags; ++i) {
171 if (std::isfinite(f[i])) last_valid = i;
172 }
173 out.window_size = (last_valid >= 0) ? last_valid : 1;
174 }
175
176 return out;
177}
178
179} // namespace qe
180
181#endif // LIBQE_CCD_HPP
Definition cxxwrap.hpp:36
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
Result of a CCD window-size estimation.
Definition ccd.hpp:32
arma::vec frob_unbiased_signed
sign * sqrt(|corrected sq|) per lag.
Definition ccd.hpp:38
arma::vec total_weight
Pooled overlap weight per lag.
Definition ccd.hpp:39
int window_size
Estimated half-life lag window size (>= 1).
Definition ccd.hpp:33
arma::vec frob_sq_unbiased
Noise-corrected squared Frobenius norm per lag.
Definition ccd.hpp:37
arma::vec lag
Evaluated lags, 0..max_window.
Definition ccd.hpp:35
arma::vec frob
Uncorrected pooled Frobenius norm per lag.
Definition ccd.hpp:36
int peak_lag
Lag of the peak corrected cross-covariance norm.
Definition ccd.hpp:34