Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/cortex-path-m1/src/metrics.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / cortex-path-m1 / src / metrics.rs` 104 lines ยท 3565 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 /// Binary nDCG@k. Discount at rank `i` (1-based) is `1 / log2(i + 1)`. 17 pub fn ndcg_at_k(ranked_ids: &[String], relevant: &[String], k: usize) -> f64 { 18 if relevant.is_empty() || k == 0 { 19 return 0.0; 20 } 21 let gain = |rank: usize, hit: bool| -> f64 { 22 if !hit { 23 return 0.0; 24 } 25 1.0 / ((rank + 1) as f64).log2() 26 }; 27 let dcg: f64 = ranked_ids 28 .iter() 29 .take(k) 30 .enumerate() 31 .fold(0.0, |sum, (idx, id)| { 32 sum + gain(idx + 1, relevant.iter().any(|item| item == id)) 33 }); 34 let ideal_hits = relevant.len().min(k); 35 let idcg: f64 = (0..ideal_hits).map(|offset| gain(offset + 1, true)).sum(); 36 if idcg == 0.0 { 37 0.0 38 } else { 39 dcg / idcg 40 } 41 } 42 43 pub fn recall_at_k(ranked_ids: &[String], relevant: &[String], k: usize) -> f64 { 44 if relevant.is_empty() { 45 return 0.0; 46 } 47 let hits = ranked_ids 48 .iter() 49 .take(k) 50 .filter(|id| relevant.iter().any(|item| item == *id)) 51 .count(); 52 hits as f64 / relevant.len() as f64 53 } 54 55 pub fn mean(values: &[f64]) -> f64 { 56 if values.is_empty() { 57 0.0 58 } else { 59 values.iter().sum::() / values.len() as f64 60 } 61 } 62 63 /// Paired bootstrap of the mean. Returns `(low, high)` as the 2.5 and 97.5 percentiles. 64 pub fn bootstrap_mean_ci(values: &[f64], samples: u32, seed: u64) -> Option<(f64, f64)> { 65 if values.is_empty() || samples == 0 { 66 return None; 67 } 68 let mut state = seed; 69 let mut means = Vec::with_capacity(samples as usize); 70 for _ in 0..samples { 71 let mut total = 0.0; 72 for _ in 0..values.len() { 73 let index = splitmix(&mut state) as usize % values.len(); 74 total += values[index]; 75 } 76 means.push(total / values.len() as f64); 77 } 78 means.sort_by(|left, right| left.partial_cmp(right).unwrap_or(std::cmp::Ordering::Equal)); 79 let low_index = ((samples as f64) * 0.025).floor() as usize; 80 let high_index = (((samples as f64) * 0.975).ceil() as usize).saturating_sub(1); 81 let high_index = high_index.min(means.len() - 1); 82 Some((means[low_index.min(means.len() - 1)], means[high_index])) 83 } 84 85 pub fn percentile_95(mut values: Vec) -> f64 { 86 if values.is_empty() { 87 return 0.0; 88 } 89 values.sort_by(|left, right| left.partial_cmp(right).unwrap_or(std::cmp::Ordering::Equal)); 90 let index = (0.95 * values.len() as f64).ceil() as usize - 1; 91 values[index.min(values.len() - 1)] 92 } 93 94 fn splitmix(state: &mut u64) -> u64 { 95 *state = state.wrapping_add(0x9E37_79B9_7F4A_7C15); 96 let mut mixed = *state; 97 mixed = (mixed ^ (mixed >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); 98 mixed = (mixed ^ (mixed >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); 99 mixed ^ (mixed >> 31) 100 } 101 102 pub fn ci_excludes_zero(interval: (f64, f64)) -> bool { 103 interval.0 > 0.0 || interval.1 < 0.0 104 }