round_stochastic function

Round a stochastic vector or a row-stochastic matrix

Round a stochastic vector or a row-stochastic matrix

Rounds a vector such that the sum of 1 is preserved. Rounds a matrix such that each row sum up to 1. One entry is adjusted after rounding such that the rounding error is the smallest.

round_stochastic(x, digits = 7)

Arguments

  • x: a stochastic vector or a row-stochastic matrix.
  • digits: number of digits for rounding.

Returns

The rounded vector or matrix.

Examples

# regular rounding would not sum up to 1 x <- c(0.333, 0.334, 0.333) round_stochastic(x) round_stochastic(x, digits = 2) round_stochastic(x, digits = 1) round_stochastic(x, digits = 0) # round a stochastic matrix m <- matrix(runif(15), ncol = 3) m <- sweep(m, 1, rowSums(m), "/") m round_stochastic(m, digits = 2) round_stochastic(m, digits = 1) round_stochastic(m, digits = 0)

See Also

round

  • Maintainer: Michael Hahsler
  • License: GPL (>= 3)
  • Last published: 2024-12-05