#######################################################
#       AUXILIAR FUNCTIONS                            #
#######################################################

K<-function(mas){
#--Gausiann kernel

return(1/sqrt(2*pi)*exp(-mas^2/2))
}


S<-function(t,mas,bandwidth,exponent){
#-- t vector of points where S will be evaluated.
#-- mas values of the independ variable.
#-- bandwidth window 
#-- r exponent.
    
    return(diag((t(K(outer(mas,t,"-")/bandwidth)))%*%(outer(mas,t,"-")^exponent)))
}

#######################################################
#     Local-linear estimator                          #
#######################################################

EstLL<-function(t,mas,y,bandwidth){
#-- This function returns the estimation which was caluted by no-parametric estimator
#-- of local-linear regresion.
#-- t vector of points where S will be evaluated.
#-- x values of the independ variable.
#-- y values of the depend variable.
#-- bandwidth is the window
        
    return(apply(as.matrix(bandwidth),1,function(x) ( t( K( outer(mas,t,"-") /x) ) * ( S(t,mas,x,2) - t(outer(mas,t,"-")) * S(t,mas,x,1) ) /apply( t( K( outer(mas,t,"-") /x) ) * ( S(t,mas,x,2) - t(outer(mas,t,"-")) * S(t,mas,x,1) ),1,sum ) ))%*%y)
}



########################################################
#    Plug-in window                                    #
########################################################


library(KernSmooth)
dpill(x,y)


#########################################################
# 4.- Nadaraya-Watson estimator                         #
#########################################################


nw<-function(t,mas,y,bandwidth){
#-- This function returns the estimation which was caluted by no-parametric estimator
#-- of Nayadara-Watson regresion.
#-- t vector of points where S will be evaluated.
#-- x values of the independ variable.
#-- y values of the depend variable.
#-- bandwidth is the window

return(apply(as.matrix(bandwidth),1,function(x)(K(outer(t,mas,"-")/x)/apply(K(outer(t,mas,"-")/x),1,sum))%*%y))

}


####################################
##                                ##
##   CROSS-VALIDATION BANDWIDTH   ##
##                                ##
####################################

ll_cv<-function(mas,y,bandwidth){
#--It's the cross-validation function aplicated to no-parametric estimator
#-- of local-linear regresion.
    return(apply(as.matrix(bandwidth),1,function(x) {1/length(mas) * sum(((as.numeric(y - EstLL(mas,mas,y,x)))/ (1 - ( K(0)*S(mas,mas,x,2) / apply( t( K( outer(mas,mas,"-") /x) ) * ( S(mas,mas,x,2) - t(outer(mas,mas,"-")) * S(mas,mas,x,1) ), 1, sum) ))) ^2) } ))
}


cv_nw<-function(mas,y,bandwidth){
#--It's the cross-validation function aplicated to no-parametric estimator
#-- of Nadaraya-Watson regresion.

    return(apply(as.matrix(bandwidth),1,function(x){1/length(mas) * sum( ((y - nw(mas,mas,y,x)) / (1 - (K(0)/ apply( K(outer(mas,mas,"-")/x) ,1,sum) ) ) )^2 )}))

}
