#ADMMs p.215
library(dplyr)
data=data.frame(num=1:20,y=c(0.20, 0.10, 0.49, 0.26, 0.92, 0.95, 0.18, 0.19, 0.44, 0.79, 0.61, 0.41, 0.49, 0.34, 0.62, 0.99, 0.38, 0.22,0.71,0.40),x1=c(84,87,86,85,82,87,92,94,88,84.9,78,90,88,87,82,84,86,83,85.2,82),x2=c(61,55.5,57,57,50,50,66.5,65,60.5,49.5,49.5,61,59.5,58.4,53.5,54,60,58.8,54,56),x3=c(24.5,24,23,22.5,22,24,25,26,25,24,23,21,26,25.5,24,23,24,24,24,22))
p=10;lambda=50
ite=10000
Y=data$y;X=as.matrix(data[,colnames(data) %in% c("x1","x2")])
X=t((t(X)-apply(X,2,mean))/apply(X,2,sd))
A=cbind(rep(1,nrow(X)),X)
x=rep(1,ncol(A))
z=x;guzai=rep(0,ncol(A))
eta=0.0001;
for(j in 1:ite){
vec=x
for(i in 1:length(x)){
x_sub=vec;x_sub[i]=x_sub[i]+0.01
dx1=sum((Y-A%*%x_sub)^2)+p*sum((x_sub-z+u)^2)
dx2=sum((Y-A%*%x)^2)+p*sum((x-z+u)^2)
x[i]=x[i]-eta*(dx1-dx2)/0.01
}
vec=z
for(i in 1:length(z)){
z_sub=vec;z_sub[i]=z_sub[i]+0.01
dz1=lambda*sum(abs(z_sub))+p*sum((x-z_sub+u)^2)
dz2=lambda*sum(abs(z))+p*sum((x-z+u)^2)
z[i]=z[i]-eta*(dz1-dz2)/0.01
}
guzai=guzai+p*(x-z);u=guzai/p
cost=sum((Y-A%*%x)^2)+lambda*sum(abs(z))+p*sum((x-z+u)^2)/2
print(cost)
}
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
00