fn bu_finite(x: f64) -> bool { x - x == 0.0 } pub fn bu_valid(edges: vector) -> bool { let mut i = 0; while i < len(edges) { if !bu_finite(at(edges, i)) { return false; } if i > 0 && at(edges, i) <= at(edges, i - 1) { return false; } i += 1; } true } fn bu_find(edges: vector, x: f64) -> i64 { let mut low = 0; let mut high = len(edges); while low < high { let mid = low + (high - low) / 2; if x < at(edges, mid) { high = mid; } else { low = mid + 1; } } low } pub fn bu_bucket(edges: vector, x: f64) -> i64 { if !bu_valid(edges) || !bu_finite(x) { return -1; } bu_find(edges, x) } // The missing bucket is -1. All other buckets include the lower boundary. pub fn bu_count(data: vector, edges: vector, bucket: i64) -> i64 { if !bu_valid(edges) || bucket < -1 || bucket > len(edges) { return -1; } let mut count = 0; let mut i = 0; while i < len(data) { let x = at(data, i); if bu_finite(x) { if bucket >= 0 && bu_find(edges, x) == bucket { count += 1; } } else { if bucket == -1 { count += 1; } } i += 1; } count } fn bu_label(edges: vector, i: i64) { if i == -1 { print!("Missing or non-finite"); return; } if len(edges) == 0 { print!("All finite values"); return; } if i == 0 { print!("Below "); print!(at(edges, 0)); return; } print!(at(edges, i - 1)); if i == len(edges) { print!(" and above"); return; } print!(" to below "); print!(at(edges, i)); } // Emit a semantic HTML fragment. Labels come from numeric edges. pub fn bu_table(data: vector, edges: vector) -> bool { if !bu_valid(edges) { return false; } println!(""); println!(""); let mut bucket = 0; while bucket <= len(edges) + 1 { let mut id = bucket; if bucket == len(edges) + 1 { id = -1; } let n = bu_count(data, edges, id); let mut total = len(data); if total == 0 { total = 1; } print!(""); bucket += 1; } println!("
Distribution of observations
IntervalCountShare
"); bu_label(edges, id); print!(""); print!(n); print!(" "); print!(n); print!(" / "); print!(len(data)); println!("
"); true }