|
| 1 | +##' Solving Regularized Support Vector Machine |
| 2 | +##' |
| 3 | +##' @description |
| 4 | +##' This function solves the regularized support vector machine |
| 5 | +##' of the following form: |
| 6 | +##' \deqn{ |
| 7 | +##' \min_{\beta}\ \frac{C}{n}\sum_{i=1}^n \max(1-y_i x_i^T\beta,0) + |
| 8 | +##' \frac{1}{2}\Vert\beta\Vert_2^2 |
| 9 | +##' } |
| 10 | +##' where \eqn{\beta\in\mathbb{R}^d} is a length-\eqn{d} vector, |
| 11 | +##' \eqn{x_i\in\mathbb{R}^d} is the feature vector for the \eqn{i}-th observation, |
| 12 | +##' \eqn{y_i\in\{-1,1\}} is a binary label, |
| 13 | +##' and \eqn{C} is the cost parameter. |
| 14 | +##' |
| 15 | +##' @param x The data matrix \eqn{X=(x_1,\ldots,x_n)^T} of size |
| 16 | +##' \eqn{n\times d}, representing \eqn{n} observations |
| 17 | +##' and \eqn{d} features. |
| 18 | +##' @param y The length-\eqn{n} response vector. |
| 19 | +##' @param C The cost parameter. |
| 20 | +##' @param max_iter Maximum number of iterations. |
| 21 | +##' @param tol Tolerance parameter for convergence test. |
| 22 | +##' @param shrink Whether to use the shrinkage algorithm. |
| 23 | +##' @param verbose Level of verbosity. |
| 24 | +##' |
| 25 | +##' @return A list of the following components: |
| 26 | +##' \item{beta}{Optimized value of the \eqn{\beta} vector.} |
| 27 | +##' \item{xi,Lambda,Gamma}{Values of dual variables.} |
| 28 | +##' \item{niter}{Number of iterations used.} |
| 29 | +##' \item{dual_objfns}{Dual objective function values during the optimization process.} |
| 30 | +##' |
| 31 | +##' @author Yixuan Qiu \url{https://statr.me} |
| 32 | +##' |
| 33 | +##' Ben Dai \url{https://bendai.org} |
| 34 | +##' |
| 35 | +svm = function(x, y, C = 1, max_iter = 1000, tol = 1e-5, shrink = TRUE, verbose = 0) |
| 36 | +{ |
| 37 | + n = nrow(x) |
| 38 | + |
| 39 | + Umat = -C / n * matrix(y, nrow = 1) |
| 40 | + Vmat = matrix(C / n, 1, n) |
| 41 | + |
| 42 | + res = rehline(x, Umat, Vmat, max_iter = max_iter, |
| 43 | + tol = tol, shrink = shrink, verbose = verbose) |
| 44 | + res$beta |
| 45 | +} |
0 commit comments