# cone projection code: hinge algorithm in R # find theta-hat to minimize || y-theta ||^2 # subject to amat%*%theta >= 0 # # amat must be irreducible # coneproj=function(y,amat){ n=length(y);m=length(amat)/n sm=1e-8;h=1:m<0;obs=1:m;check=0 delta=-amat;b2=delta%*%y if(max(b2)>sm){ i=min(obs[b2==max(b2)]) h[i]=TRUE }else{check=1;theta=1:n*0} while(check==0){ xmat=matrix(delta[h,],ncol=n) a=solve(xmat%*%t(xmat))%*%xmat%*%y if(min(a)<(-sm)){ avec=1:m*0;avec[h]=a i=min(obs[avec==min(avec)]) h[i]=FALSE;check=0 }else{ check=1 theta=t(xmat)%*%a b2=delta%*%(y-theta) if(max(b2)>sm){ i=min(obs[b2==max(b2)]) h[i]=TRUE;check=0 } } } thetahat=y-theta thetahat }