| 1 |
\name{roundfixS} |
| 2 |
\alias{roundfixS} |
| 3 |
\concept{apportionment} |
| 4 |
\title{Round to Integer Keeping the Sum Fixed} |
| 5 |
\description{ |
| 6 |
Given a real numbers \eqn{y_i} with the particular property that |
| 7 |
\eqn{\sum_i y_i} is integer, find \emph{integer} numbers \eqn{x_i} |
| 8 |
which are close to \eqn{y_i} (\eqn{\left|x_i - y_i\right| < 1 \forall i}{% |
| 9 |
|x[i] - y[i]| < 1 for all i}), and have identical \dQuote{marginal} |
| 10 |
sum, \code{sum(x) == sum(y)}. |
| 11 |
|
| 12 |
As I found later, the problem is known as \dQuote{Apportionment Problem} |
| 13 |
and it is quite an old problem with several solution methods proposed |
| 14 |
historically, but only in 1982, Balinski and Young proved that there |
| 15 |
is no method that fulfills three natural desiderata. |
| 16 |
|
| 17 |
Note that the (first) three methods currently available here were all |
| 18 |
(re?)-invented by M.Maechler, without any knowledge of the |
| 19 |
litterature. At the time of writing, I have not even checked to which |
| 20 |
(if any) of the historical methods they match. |
| 21 |
} |
| 22 |
\usage{ |
| 23 |
roundfixS(x, method = c("offset-round", "round+fix", "1greedy")) |
| 24 |
} |
| 25 |
\arguments{ |
| 26 |
\item{x}{a numeric vector which \bold{must} sum to an integer} |
| 27 |
\item{method}{character string specifying the algorithm to be used.} |
| 28 |
% \item{trace}{logical or integer, enabling algorithm tracing.} |
| 29 |
} |
| 30 |
\details{ |
| 31 |
Without hindsight, it may be surprising that all three methods give |
| 32 |
identical results (in all situations and simulations considered), |
| 33 |
notably that the idea of \sQuote{mass shifting} employed in the |
| 34 |
iterative \code{"1greedy"} algorithm seems equivalent to the much simpler |
| 35 |
idea used in \code{"offset-round"}. |
| 36 |
|
| 37 |
I am pretty sure that these algorithms solve the \eqn{L_p} |
| 38 |
optimization problem, \eqn{\min_x \left\|y - x\right\|_p}{min_x ||y - x||_p}, |
| 39 |
typically for all \eqn{p \in [1,\infty]}{p in [1,Inf]} |
| 40 |
\emph{simultaneously}, but have not bothered to find a formal proof. |
| 41 |
} |
| 42 |
\value{ |
| 43 |
a numeric vector, say \code{r}, of the same length as \code{x}, but |
| 44 |
with integer values and fulfulling \code{sum(r) == sum(x)}. |
| 45 |
} |
| 46 |
\author{Martin Maechler, November 2007} |
| 47 |
\references{ |
| 48 |
Michel Balinski and H. Peyton Young (1982) |
| 49 |
\bold{Fair Representation: Meeting the Ideal of One Man, One Vote}; |
| 50 |
|
| 51 |
\url{https://en.wikipedia.org/wiki/Apportionment_paradox} |
| 52 |
|
| 53 |
\url{https://www.ams.org/samplings/feature-column/fcarc-apportionii3} |
| 54 |
} |
| 55 |
\seealso{\code{\link{round}} etc |
| 56 |
} |
| 57 |
\examples{ |
| 58 |
## trivial example |
| 59 |
kk <- c(0,1,7) |
| 60 |
stopifnot(identical(kk, roundfixS(kk))) # failed at some point |
| 61 |
|
| 62 |
x <- c(-1.4, -1, 0.244, 0.493, 1.222, 1.222, 2, 2, 2.2, 2.444, 3.625, 3.95) |
| 63 |
sum(x) # an integer |
| 64 |
r <- roundfixS(x) |
| 65 |
stopifnot(all.equal(sum(r), sum(x))) |
| 66 |
m <- cbind(x=x, `r2i(x)` = r, resid = x - r, `|res|` = abs(x-r)) |
| 67 |
rbind(m, c(colSums(m[,1:2]), 0, sum(abs(m[,"|res|"])))) |
| 68 |
|
| 69 |
chk <- function(y) { |
| 70 |
cat("sum(y) =", format(S <- sum(y)),"\n") |
| 71 |
r2 <- roundfixS(y, method="offset") |
| 72 |
r2. <- roundfixS(y, method="round") |
| 73 |
r2_ <- roundfixS(y, method="1g") |
| 74 |
stopifnot(all.equal(sum(r2 ), S), |
| 75 |
all.equal(sum(r2.), S), |
| 76 |
all.equal(sum(r2_), S)) |
| 77 |
all(r2 == r2. & r2. == r2_) # TRUE if all give the same result |
| 78 |
} |
| 79 |
|
| 80 |
makeIntSum <- function(y) { |
| 81 |
n <- length(y) |
| 82 |
y[n] <- ceiling(y[n]) - (sum(y[-n]) \%\% 1) |
| 83 |
y |
| 84 |
} |
| 85 |
set.seed(11) |
| 86 |
y <- makeIntSum(rnorm(100)) |
| 87 |
chk(y) |
| 88 |
|
| 89 |
## nastier example: |
| 90 |
set.seed(7) |
| 91 |
y <- makeIntSum(rpois(100, 10) + c(runif(75, min= 0, max=.2), |
| 92 |
runif(25, min=.5, max=.9))) |
| 93 |
chk(y) |
| 94 |
|
| 95 |
\dontrun{ |
| 96 |
for(i in 1:1000) |
| 97 |
stopifnot(chk(makeIntSum(rpois(100, 10) + |
| 98 |
c(runif(75, min= 0, max=.2), |
| 99 |
runif(25, min=.5, max=.9))))) |
| 100 |
} |
| 101 |
} |
| 102 |
\keyword{arith} |
| 103 |
\keyword{manip} |