1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! Factor selection for rectangular cumulative triangles.
//!
//! Inputs are row-major losses in consistent units; counts identify each row
//! observed prefix at the caller's valuation date. No calendar assumptions are
//! introduced here. Unselectable links produce NaN; callers validate results.
/// Counts-aware simple average. `last_n=Some(k)` restricts to the `k` most
/// recent eligible origins per link (origins are ordered with oldest first).
pub fn simple_average_observed_counts(
cum: &[f64],
n_o: usize,
n_d: usize,
counts: &[usize],
last_n: Option<usize>,
out: &mut [f64],
) {
debug_assert_eq!(cum.len(), n_o * n_d);
debug_assert_eq!(counts.len(), n_o);
let n_links = n_d.saturating_sub(1);
debug_assert_eq!(out.len(), n_links);
for j in 0..n_links {
let limit = last_n.unwrap_or(usize::MAX);
let mut seen_eligible = 0_usize;
let mut sum = 0.0_f64;
let mut count = 0_usize;
for i in (0..n_o).rev() {
if counts[i] <= j + 1 {
continue;
}
if seen_eligible >= limit {
break;
}
seen_eligible += 1;
let x = cum[i * n_d + j];
let y = cum[i * n_d + j + 1];
if x > 0.0 && x.is_finite() && y.is_finite() {
sum += y / x;
count += 1;
}
}
out[j] = if count > 0 {
sum / count as f64
} else {
f64::NAN
};
}
}
/// Counts-aware ex-hi/lo (drop largest and smallest ratio per link;
/// fall back to simple average when fewer than 3 pairs).
///
/// Uses a two-pass index-tracking scan (zero heap allocations and free of
/// catastrophic cancellation on extreme outlier ratios).
pub fn ex_hi_lo_observed_counts(
cum: &[f64],
n_o: usize,
n_d: usize,
counts: &[usize],
out: &mut [f64],
) {
debug_assert_eq!(cum.len(), n_o * n_d);
debug_assert_eq!(counts.len(), n_o);
let n_links = n_d.saturating_sub(1);
debug_assert_eq!(out.len(), n_links);
for j in 0..n_links {
let mut valid_count = 0_usize;
let mut min_val = f64::INFINITY;
let mut max_val = f64::NEG_INFINITY;
let mut min_idx = 0_usize;
let mut max_idx = 0_usize;
for i in 0..n_o {
if counts[i] <= j + 1 {
continue;
}
let x = cum[i * n_d + j];
let y = cum[i * n_d + j + 1];
if x > 0.0 && x.is_finite() && y.is_finite() {
let r = y / x;
if valid_count == 0 || r < min_val {
min_val = r;
min_idx = i;
}
if valid_count == 0 || r > max_val {
max_val = r;
max_idx = i;
}
valid_count += 1;
}
}
if valid_count == 0 {
out[j] = f64::NAN;
continue;
}
// If all valid ratios are equal, min_idx == max_idx; pick any distinct
// second index when trimming >= 3 elements.
let trim = valid_count >= 3;
if trim && min_idx == max_idx {
for i in 0..n_o {
if i == min_idx || counts[i] <= j + 1 {
continue;
}
let x = cum[i * n_d + j];
let y = cum[i * n_d + j + 1];
if x > 0.0 && x.is_finite() && y.is_finite() {
max_idx = i;
break;
}
}
}
let mut sum = 0.0_f64;
let mut kept = 0_usize;
for i in 0..n_o {
if counts[i] <= j + 1 {
continue;
}
if trim && (i == min_idx || i == max_idx) {
continue;
}
let x = cum[i * n_d + j];
let y = cum[i * n_d + j + 1];
if x > 0.0 && x.is_finite() && y.is_finite() {
sum += y / x;
kept += 1;
}
}
out[j] = if kept > 0 {
sum / kept as f64
} else {
f64::NAN
};
}
}
/// Counts-aware volume-weighted, restricted to the `last_n` most recent eligible
/// origins (largest index = most recent in the right-triangle convention).
pub fn volume_weighted_last_n_observed_counts(
cum: &[f64],
n_o: usize,
n_d: usize,
counts: &[usize],
last_n: usize,
out: &mut [f64],
) {
debug_assert_eq!(cum.len(), n_o * n_d);
debug_assert_eq!(counts.len(), n_o);
let n_links = n_d.saturating_sub(1);
debug_assert_eq!(out.len(), n_links);
for j in 0..n_links {
let mut seen = 0_usize;
let mut num = 0.0_f64;
let mut den = 0.0_f64;
for i in (0..n_o).rev() {
if counts[i] <= j + 1 {
continue;
}
if seen >= last_n {
break;
}
seen += 1;
num += cum[i * n_d + j + 1];
den += cum[i * n_d + j];
}
out[j] = if den > 0.0 { num / den } else { f64::NAN };
}
}