design a regress function whose coeficients satisfy certain conditions

来源:互联网 发布:电影cms系统哪个好 编辑:程序博客网 时间:2024/04/28 00:23

Problem:

  Given n points (x_1, x_2, ...x_n) as independent variables, where x_i can be a value or a vector,  and the dependent variables (y_1, y_2, ... y_i), design a regression that satifies:

  1. f(x) = t(w) * x  (assume the the attribute of x corresponding w_0 is 1)

  2. sigma(w) = 1

  3. w_i >= 0

  3. minimize sigma(y - f(x))^2


Solution:

  The solution is based on KKT condition which is a generized from langrange muptiply method.

  KKT conditions of this question:

    1. delta(siagma(y - f(x))^2 + a * delta(w) + sigma(b_i * delta(w_i)) = 0

     2. sigma(w) = 1

  solve about equotions, we  get:

     b_i = 0 

     rbind(

        cbind(2 * t(X) *X, c(1, ....), 

         c(w, 1)

     )  * c(w, a) = 2 * t(X) * Y


R code: ( assume x is a vector, whose first element is 1 correponding w_0)

   

regress <- function(x, y) {  A <- 2 * t(x) %*% x  B <- rep(1, nrow(A))  C <- 2 * t(x) %*% y  D <- c(rep(1, nrow(A)), 0)  left <- rbind(cbind(A, B), D)  coef <- solve(left, c(C, 1))  coef[1:length(coef) - 1]}

原创粉丝点击