This function computes convergence criteria for iterative algorithms using different methods that balance speed and accuracy.

check_convergence(
  new_values,
  old_values,
  method = "root_mean_square",
  sample_fraction = 0.1,
  min_sample = 10
)

Arguments

new_values

A numeric vector or array containing the new iteration values.

old_values

A numeric vector or array containing the previous iteration values.

method

Character string specifying the convergence method. Options are: - `"root_mean_square"` (default): RMS of relative changes, good balance of speed and accuracy. - `"aggregate"`: Fast sum-based relative change, fastest but least accurate. - `"element_wise"`: Maximum relative change across all elements, most accurate but slowest. - `"sample"`: Maximum relative change on a random sample, faster than element-wise.

sample_fraction

Numeric value between 0 and 1 specifying the fraction of elements to sample when `method = "sample"`. Default is 0.1 (ten percent).

min_sample

Integer specifying minimum number of elements to sample when `method = "sample"`. Default is 10.

Value

A numeric value representing the convergence criterion.

Examples

old_vals <- c(1.0, 2.0, 3.0, 4.0)
new_vals <- c(1.01, 2.02, 2.99, 4.01)

# Different methods
check_convergence(new_vals, old_vals, method = "root_mean_square")
#> [1] 0.007371586
check_convergence(new_vals, old_vals, method = "aggregate")
#> [1] 0.003
check_convergence(new_vals, old_vals, method = "element_wise")
#> [1] 0.01
check_convergence(new_vals, old_vals, method = "sample", sample_fraction = 0.2)
#> [1] 0.01