The sum of the largest \(k\) values of a vector or matrix.

sum_largest(x, k)

Arguments

x

An Expression, vector, or matrix.

k

The number of largest values to sum over.

Value

An Expression representing the sum of the largest k values of the input.

Examples

set.seed(122)
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)
y <- X %*% b + stats::rnorm(m)

beta <- Variable(n+1)
obj <- sum_largest((y - X %*% beta)^2, 100)
prob <- Problem(Minimize(obj))
result <- solve(prob)
result$getValue(beta)
#>                [,1]
#>  [1,] -0.0343461443
#>  [2,]  0.8606400152
#>  [3,] -0.0007471474
#>  [4,]  1.0303675537
#>  [5,]  0.3066423315
#>  [6,] -0.0749035312
#>  [7,]  0.4531070455
#>  [8,]  1.0492884686
#>  [9,]  0.0295552578
#> [10,]  0.7136916224