Skip to main content

pava

Function pava 

Source
pub fn pava(y: &[f64]) -> Vec<f64>
Expand description

Pool Adjacent Violators Algorithm (PAVA) for isotonic regression.

Finds the monotonically non-decreasing sequence ŷ minimizing Σ(yᵢ - ŷᵢ)².

§Algorithm

1. Initialize blocks: each element is its own block
2. Scan left-to-right:
   - If block[i] > block[i+1], merge them (average values)
   - Backtrack: merged block might violate with previous
3. Repeat until no violations

§Complexity

  • Time: O(n)
  • Space: O(n) for output

§Example

use fynch::pava;

let y = [3.0, 1.0, 2.0, 5.0, 4.0];
let result = pava(&y);
// result ≈ [2.0, 2.0, 2.0, 4.5, 4.5]

// Verify monotonicity
for i in 1..result.len() {
    assert!(result[i] >= result[i-1]);
}