Commit b0c08636 by Fredrik Bagge Carlson

### Extracted many methods from train_RBF.. Moving towrads several different...

`Extracted many methods from train_RBF.. Moving towrads several different RBF-network implementations`
parent 55740f37
 ... @@ -13,7 +13,7 @@ FitResult, ... @@ -13,7 +13,7 @@ FitResult, IdData, IdData, # Functions # Functions ar,arx,getARregressor,getARXregressor,find_na, ar,arx,getARregressor,getARXregressor,find_na, toeplitz, kalman, kalman_smoother toeplitz, kalman, kalman_smoother, forward_kalman ## Fit Methods ================= ## Fit Methods ================= :LS :LS ... ...
 ... @@ -105,3 +105,15 @@ function find_na(y::Vector{Float64},n::Int) ... @@ -105,3 +105,15 @@ function find_na(y::Vector{Float64},n::Int) plotsub(error,"-o") plotsub(error,"-o") show() show() end end import PyPlot function PyPlot.plot(y,m::AR) na = length(m.a) y,A = getARregressor(y,na) yh = A*m.a error = y-yh newplot(y,"k") plot(yh,"b") plot(error,"r") title("Fitresult, AR, n_a: \$na, RMSE = \$(rms(error)) Fit = \$(fit(y,yh))") end
 ... @@ -15,7 +15,7 @@ Based on implementation by ... @@ -15,7 +15,7 @@ Based on implementation by } } http://www.mathworks.com/matlabcentral/fileexchange/29809-cuckoo-search--cs--algorithm http://www.mathworks.com/matlabcentral/fileexchange/29809-cuckoo-search--cs--algorithm """ """ function cuckoo_search(f,X0, Lb,Ub;n=25,pa=0.25, Tol=1.0e-5, max_iter = 1e5, timeout = Inf) function cuckoo_search(f,X0, Lb,Ub;n=25,pa=0.25, Tol=1.0e-5, max_iter = 1e3, timeout = Inf) X00 = deepcopy(X0) X00 = deepcopy(X0) nd=size(X0,1); nd=size(X0,1); X0t = X0' X0t = X0' ... ...
 ... @@ -24,35 +24,37 @@ function forward_kalman(y,A,R1,R2, P0) ... @@ -24,35 +24,37 @@ function forward_kalman(y,A,R1,R2, P0) xk = xkk xk = xkk Pk = Pkk Pk = Pkk i = 1 i = 1 Hk = A[i,:] e = y[i]-Hk*xk[:,i] e = y[i]-Hk*xk[:,i] S = Hk*Pk[:,:,i]*Hk' + R2 S = Hk*Pk[:,:,i]*Hk' + R2 K = (Pk[:,:,i]*H')/S K = (Pk[:,:,i]*Hk')/S xkk[:,i] = xk[:,i] + K*e xkk[:,i] = xk[:,i] + K*e Pkk[:,:,i] = (I - K*Hk)*Pk[:,:,i] Pkk[:,:,i] = (I - K*Hk)*Pk[:,:,i] for i = 2:N for i = 2:N Fk = 1 Fk = 1 Hk = A[i,:]' Hk = A[i,:] xk[:,i] = Fk*xkk[:,i-1] xk[:,i] = Fk*xkk[:,i-1] Pk[:,:,i] = Fk*Pkk[:,:,i-1]*Fk' + R1 Pk[:,:,i] = Fk*Pkk[:,:,i-1]*Fk' + R1 e = y[i]-Hk*xk[:,i] e = y[i]-Hk*xk[:,i] S = Hk*Pk[:,:,i]*Hk' + R2 S = Hk*Pk[:,:,i]*Hk' + R2 K = (Pk[:,:,i]*H')/S K = (Pk[:,:,i]*Hk')/S xkk[:,i] = xk[:,i] + K*e xkk[:,i] = xk[:,i] + K*e Pkk[:,:,i] = (I - K*Hk)*Pk[:,:,i] Pkk[:,:,i] = (I - K*Hk)*Pk[:,:,i] end end return xkk,xk,Pkk,Pk return xkk,xk,Pkk,Pk end end """A kalman parameter smoother""" using Debug # """A kalman parameter smoother""" function kalman_smoother(y, A, R1, R2, P0) function kalman_smoother(y, A, R1, R2, P0) na = size(R1,1) na = size(R1,1) N = length(y) N = length(y) xkk,xk,Pkk,Pk = forward_kalman(y,A,R1,R2, P0) xkk,xk,Pkk,Pk = forward_kalman(y,A,R1,R2, P0) xkn = zeros(xkk) xkn = zeros(xkk) Pkn = zeros(P) Pkn = zeros(Pkk) for i = N-1:-1:1 for i = N-1:-1:1 Ai = A[i,:] Fk = 1 Fk = 1 Hk = A[i,:]' Hk = A[i,:]' C = Pkk[:,:,i]/Pk[:,:,i+1] C = Pkk[:,:,i]/Pk[:,:,i+1] ... @@ -60,6 +62,5 @@ function kalman_smoother(y, A, R1, R2, P0) ... @@ -60,6 +62,5 @@ function kalman_smoother(y, A, R1, R2, P0) Pkn[:,:,i] = Pkk[:,:,i] + C*(Pkn[:,:,i+1] - Pk[:,:,i+1])*C' Pkn[:,:,i] = Pkk[:,:,i] + C*(Pkn[:,:,i+1] - Pk[:,:,i+1])*C' end end return xkn, Pkn return xkn end end