xref: /aosp_15_r20/external/rappor/analysis/R/alternative.R (revision 2abb31345f6c95944768b5222a9a5ed3fc68cc00)
1*2abb3134SXin Li# Copyright 2014 Google Inc. All rights reserved.
2*2abb3134SXin Li#
3*2abb3134SXin Li# Licensed under the Apache License, Version 2.0 (the "License");
4*2abb3134SXin Li# you may not use this file except in compliance with the License.
5*2abb3134SXin Li# You may obtain a copy of the License at
6*2abb3134SXin Li#
7*2abb3134SXin Li#     http://www.apache.org/licenses/LICENSE-2.0
8*2abb3134SXin Li#
9*2abb3134SXin Li# Unless required by applicable law or agreed to in writing, software
10*2abb3134SXin Li# distributed under the License is distributed on an "AS IS" BASIS,
11*2abb3134SXin Li# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*2abb3134SXin Li# See the License for the specific language governing permissions and
13*2abb3134SXin Li# limitations under the License.
14*2abb3134SXin Li
15*2abb3134SXin Lilibrary(limSolve)
16*2abb3134SXin Lilibrary(Matrix)
17*2abb3134SXin Li
18*2abb3134SXin Li# The next two functions create a matrix (G) and a vector (H) encoding
19*2abb3134SXin Li# linear inequality constraints that a solution vector (x) must satisfy:
20*2abb3134SXin Li#                       G * x >= H
21*2abb3134SXin Li
22*2abb3134SXin Li# Currently represent three sets of constraints on the solution vector:
23*2abb3134SXin Li#  - all solution coefficients are nonnegative
24*2abb3134SXin Li#  - the sum total of all solution coefficients is no more than 1
25*2abb3134SXin Li#  - in each of the coordinates of the target vector (estimated Bloom filter)
26*2abb3134SXin Li#    we don't overshoot by more than three standard deviations.
27*2abb3134SXin LiMakeG <- function(n, X) {
28*2abb3134SXin Li  d <- Diagonal(n)
29*2abb3134SXin Li  last <- rep(-1, n)
30*2abb3134SXin Li  rbind2(rbind2(d, last), -X)
31*2abb3134SXin Li}
32*2abb3134SXin Li
33*2abb3134SXin LiMakeH <- function(n, Y, stds) {
34*2abb3134SXin Li  # set the floor at 0.01 to avoid degenerate cases
35*2abb3134SXin Li  YY <- apply(Y + 3 * stds,  # in each bin don't overshoot by more than 3 stds
36*2abb3134SXin Li              1:2,
37*2abb3134SXin Li              function(x) min(1, max(0.01, x)))  # clamp the bound to [0.01,1]
38*2abb3134SXin Li
39*2abb3134SXin Li  c(rep(0, n),  # non-negativity condition
40*2abb3134SXin Li    -1,         # coefficients sum up to no more than 1
41*2abb3134SXin Li    -as.vector(t(YY))   # t is important!
42*2abb3134SXin Li    )
43*2abb3134SXin Li}
44*2abb3134SXin Li
45*2abb3134SXin LiMakeLseiModel <- function(X, Y, stds) {
46*2abb3134SXin Li  m <- dim(X)[1]
47*2abb3134SXin Li  n <- dim(X)[2]
48*2abb3134SXin Li
49*2abb3134SXin Li# no slack variables for now
50*2abb3134SXin Li#   slack <- Matrix(FALSE, nrow = m, ncol = m, sparse = TRUE)
51*2abb3134SXin Li#   colnames(slack) <- 1:m
52*2abb3134SXin Li#   diag(slack) <- TRUE
53*2abb3134SXin Li#
54*2abb3134SXin Li#   G <- MakeG(n + m)
55*2abb3134SXin Li#   H <- MakeH(n + m)
56*2abb3134SXin Li#
57*2abb3134SXin Li#   G[n+m+1,n:(n+m)] <- -0.1
58*2abb3134SXin Li#  A = cbind2(X, slack)
59*2abb3134SXin Li
60*2abb3134SXin Li  w <- as.vector(t(1 / stds))
61*2abb3134SXin Li  w_median <- median(w[!is.infinite(w)])
62*2abb3134SXin Li  if(is.na(w_median))  # all w are infinite
63*2abb3134SXin Li    w_median <- 1
64*2abb3134SXin Li  w[w > w_median * 2] <- w_median * 2
65*2abb3134SXin Li  w <- w / mean(w)
66*2abb3134SXin Li
67*2abb3134SXin Li  list(# coerce sparse Boolean matrix X to sparse numeric matrix
68*2abb3134SXin Li       A = Diagonal(x = w) %*% (X + 0),
69*2abb3134SXin Li       B = as.vector(t(Y)) * w,  # transform to vector in the row-first order
70*2abb3134SXin Li       G = MakeG(n, X),
71*2abb3134SXin Li       H = MakeH(n, Y, stds),
72*2abb3134SXin Li       type = 2)  # Since there are no equality constraints, lsei defaults to
73*2abb3134SXin Li                  # solve.QP anyway, but outputs a warning unless type == 2.
74*2abb3134SXin Li}
75*2abb3134SXin Li
76*2abb3134SXin Li# CustomLM(X, Y)
77*2abb3134SXin LiConstrainedLinModel <- function(X,Y) {
78*2abb3134SXin Li  model <- MakeLseiModel(X, Y$estimates, Y$stds)
79*2abb3134SXin Li  coefs <- do.call(lsei, model)$X
80*2abb3134SXin Li  names(coefs) <- colnames(X)
81*2abb3134SXin Li
82*2abb3134SXin Li  coefs
83*2abb3134SXin Li}