# オンラインアルゴリズムとストリームアルゴリズム
# p.107
k=10
z=sample(seq(0.01,1,0.01),k,replace=T)
w=rep(1,k)
y=sample(seq(0.01,1,0.01),1,replace=T)
beta=10^(-20)
n=1000
for(l in 1:n){
p=sum(w*z)/sum(w)
for(i in 1:k){
w[i]=w[i]*beta^abs(y-z[i])
}
print(abs(y-p))
}
# p.127 Adaboost 練習
h_dt=function(x,a,b){
return((1/(1+exp(-(a*x+b)))))
}
N=100
y=sample(seq(0.01,1,0.01),N,replace=T)
x=sample(seq(0.01,1,0.01),N,replace=T)
# 弱学習のパラメータ作成
cost=function(a,b){
return(sum((y-h_dt(x,a,b))^2))
}
A=0.01;B=0.01
eta=0.001
ite=10000
h=0.01
L=1;
while(L>0.01)({
cost_pre=cost(A,B)
A=A-eta*(cost(A+h,B)-cost(A,B))/h
B=B-eta*(cost(A,B+h)-cost(A,B))/h
L=abs(cost(A,B)-cost_pre)
})
# Adaboost
t=30
w=rep(1,N)
pi=sample(seq(0.01,1,0.01),N,replace=T)
for(l in 1:t){
h_vec=h_dt(x,A,B)
ep=mean(pi*abs(h_vec-y))
beta=1/(ep/(1-ep))
for(i in 1:N){
w[i]=w[i]*beta^(1-abs(h_dt(x[i],A,B)-y[i]))
}
pi=w/sum(w)
print(sum(pi*abs(h_vec-y)))
}
# p.127 Adaboost 練習
h_dt=function(x,a,b){
return((1/(1+exp(-(a*x+b)))))
}
N=10
y=sample(seq(0.01,1,0.01),N,replace=T)/100+0.5
# y=c(rep(1,5),rep(0,5))
x=sample(seq(0.01,1,0.01),N,replace=T)
# x=c(0.7,0.8,0.75,0.65,0.9,0.1,0.2,0.3,0.2,0.1)
# 弱学習のパラメータ作成
eta=0.001
h=0.01
# Adaboost
t=30000
w=rep(1,N)
h_mat=array(0,dim=c(t,N))
pi=sample(seq(0.01,1,0.01),N,replace=T)
params=array(0,dim=c(t,2))
beta_vec=c()
val_pre=10;val=9
for(l in 1:t){
if((val_pre-val)>0){
val_pre=val
cost=function(a,b){
return(sum(w*abs(y-h_dt(x,a,b))))
}
A=0.01;B=0.01
L=1;
while(L>0.01)({
cost_pre=cost(A,B)
A=A-eta*(cost(A+h,B)-cost(A,B))/h
B=B-eta*(cost(A,B+h)-cost(A,B))/h
L=abs(cost(A,B)-cost_pre)
})
params[l,]=c(A,B)
h_vec=h_dt(x,A,B)
h_mat[l,]=h_vec
ep=mean(w*abs(h_vec-y))
beta=(ep/(1-ep))
beta_vec=c(beta_vec,beta)
h_mat_sub=h_mat
for(i in 1:N){
w[i]=w[i]*beta^(abs(y[i]-h_vec[i]))
}
for(i in 1:l){
h_mat_sub[i,]=h_mat_sub[i,]*beta_vec[i]
}
w=w/sum(w)
if(l==1){
pre=h_mat_sub[1,]
}else{
pre=apply(h_mat_sub[1:l,],2,sum)
}
val=sum(abs(pre-y))
print(sum(abs(pre-y)))
}
}