The sum of the smallest k values of a vector or matrix.
sum_smallest(x, k)
An Expression, vector, or matrix.
The number of smallest values to sum over.
An Expression representing the sum of the smallest k values of the input.
set.seed(1323)
m <- 300
n <- 9
X <- matrix(stats::rnorm(m*n), nrow = m, ncol = n)
X <- cbind(rep(1,m), X)
b <- c(0, 0.8, 0, 1, 0.2, 0, 0.4, 1, 0, 0.7)
factor <- 2*rbinom(m, size = 1, prob = 0.8) - 1
y <- factor * (X %*% b) + stats::rnorm(m)
beta <- Variable(n+1)
obj <- sum_smallest(y - X %*% beta, 200)
prob <- Problem(Maximize(obj), list(0 <= beta, beta <= 1))
result <- solve(prob)
result$getValue(beta)
#> [,1]
#> [1,] 1.353952e-21
#> [2,] 8.584756e-01
#> [3,] 6.910235e-02
#> [4,] 8.841253e-01
#> [5,] 6.710621e-02
#> [6,] -1.213612e-20
#> [7,] 3.143190e-01
#> [8,] 1.000000e+00
#> [9,] 2.171679e-20
#> [10,] 7.565769e-01