Skip to content

Commit 9727f9a

Browse files
committed
update examples
1 parent fcafe97 commit 9727f9a

File tree

5 files changed

+188
-6
lines changed

5 files changed

+188
-6
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
src/*.o
6+
src/*.so
7+
src/*.dll

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Description: More about what it does (maybe more than one line)
99
License: GPL
1010
Encoding: UTF-8
1111
LazyData: true
12-
Imports: Rcpp,RcppParallel
12+
Imports: Rcpp,RcppParallel,bench
1313
LinkingTo: Rcpp,RcppParallel
1414
SystemRequirements: GNU make

man/multi_kernelmatrix.Rd

Lines changed: 178 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,187 @@ remove.packages("kernelCpp",.libPaths())
8888

8989
}
9090
\examples{
91-
##---- Should be DIRECTLY executable !! ----
92-
##-- ==> Define data, use random,
93-
##-- or do help(data=index) for the standard data sets.
91+
##compare with functions implemented in R
9492

95-
## The function is currently defined as
96-
function (x)
93+
Epanechnikov <- function (x)
9794
{
95+
96+
xPh<- abs(x)
97+
xPh[xPh <=1] <-1
98+
xPh[xPh>1] <- 0
99+
kernalX <- 0.75*(1-x^2)*xPh
100+
return(kernalX)
101+
}
102+
103+
k_kernel<-function(h,x){
104+
#h smoothing parameters
105+
#X regressors
106+
p=dim(x)[2]
107+
temp=1
108+
for(j in 1:p){
109+
xx=outer(x[,j],x[,j],'-')
110+
k_value=Epanechnikov(xx/h)
111+
temp=temp*k_value/h
112+
}
113+
temp=temp#-diag(diag(temp))
114+
return(temp)
115+
}
116+
epan_kernel<-function(h,x){
117+
#h smoothing parameters
118+
#X regressors
119+
p=dim(x)[2]
120+
temp=1
121+
for(j in 1:p){
122+
xx=outer(x[,j],x[,j],'-')
123+
k_value=0.75*(1-(xx/h)^2)*(abs(xx/h) < 1)
124+
temp=temp*k_value/h
125+
}
126+
temp=temp#-diag(diag(temp))
127+
return(temp)
128+
}
129+
quartic_kernel<-function(h,x){
130+
#h smoothing parameters
131+
#X regressors
132+
p=dim(x)[2]
133+
temp=1
134+
for(j in 1:p){
135+
xx=outer(x[,j],x[,j],'-')
136+
k_value=0.9375*(1-(xx/h)^2)^2*(abs(xx/h) < 1)
137+
temp=temp*k_value/h
138+
}
139+
temp=temp#-diag(diag(temp))
140+
return(temp)
141+
}
142+
triweight_kernel<-function(h,x){
143+
#h smoothing parameters
144+
#X regressors
145+
p=dim(x)[2]
146+
temp=1
147+
for(j in 1:p){
148+
xx=outer(x[,j],x[,j],'-')
149+
k_value=1.09375*(1-(xx/h)^2)^3*(abs(xx/h) < 1)
150+
temp=temp*k_value/h
151+
}
152+
temp=temp#-diag(diag(temp))
153+
return(temp)
154+
}
155+
triangle_kernel<-function(h,x){
156+
#h smoothing parameters
157+
#X regressors
158+
p=dim(x)[2]
159+
temp=1
160+
for(j in 1:p){
161+
xx=outer(x[,j],x[,j],'-')
162+
k_value=(1-abs(xx/h))*(abs(xx/h) < 1)
163+
temp=temp*k_value/h
164+
}
165+
temp=temp#-diag(diag(temp))
166+
return(temp)
167+
}
168+
169+
cosine_kernel<-function(h,x){
170+
#h smoothing parameters
171+
#X regressors
172+
p=dim(x)[2]
173+
temp=1
174+
for(j in 1:p){
175+
xx=outer(x[,j],x[,j],'-')
176+
k_value=pi/4*cos(pi*(xx/h)/2)*(abs(xx/h) < 1)
177+
temp=temp*k_value/h
178+
}
179+
temp=temp#-diag(diag(temp))
180+
return(temp)
181+
}
182+
183+
normal_kernel<-function(h,x){
184+
#h smoothing parameters
185+
#X regressors
186+
p=dim(x)[2]
187+
temp=1
188+
for(j in 1:p){
189+
xx=outer(x[,j],x[,j],'-')
190+
k_value=dnorm(xx/h)
191+
temp=temp*k_value/h
98192
}
193+
temp=temp#-diag(diag(temp))
194+
return(temp)
195+
}
196+
n=300
197+
p=5
198+
h =0.1
199+
X <- matrix(runif(n*p),n,p)
200+
e <- rnorm(n)
201+
#epan
202+
k_kernel(h,X) -> r1
203+
multi_kernelmatrix(X,h,"e") -> r2
204+
epan_kernel(h,X) -> r3
205+
bench::mark(
206+
k_kernel(h,X),
207+
epan_kernel(h,X),
208+
multi_kernelmatrix(X,h,"e"),
209+
check = TRUE,
210+
relative = TRUE
211+
)
212+
sum(abs(r1-r2))
213+
214+
#norm
215+
normal_kernel(h,X) -> t1
216+
multi_kernelmatrix(X,h,"g") -> t2
217+
bench::mark(
218+
normal_kernel(h,X),
219+
multi_kernelmatrix(X,h,"g"),
220+
check = TRUE,
221+
relative = TRUE
222+
)
223+
sum(abs(t1-t2))
224+
225+
#quartifc
226+
227+
quartic_kernel(h,X) ->q1
228+
multi_kernelmatrix(X,h,"q") -> q2
229+
bench::mark(
230+
quartic_kernel(h,X),
231+
multi_kernelmatrix(X,h,"q"),
232+
check = TRUE,
233+
relative = TRUE
234+
)
235+
sum(abs(q1-q2))
236+
237+
#triweight
238+
triweight_kernel(h,X) -> tw1
239+
multi_kernelmatrix(X,h,"triw") -> tw2
240+
bench::mark(
241+
triweight_kernel(h,X),
242+
multi_kernelmatrix(X,h,"triw") ,
243+
check = TRUE,
244+
relative = TRUE
245+
)
246+
sum(abs(tw1-tw2))
247+
mean(abs(tw1-tw2))
248+
249+
#triangle
250+
triangle_kernel(h,X) -> ta1
251+
multi_kernelmatrix(X,h,"tria") -> ta2
252+
bench::mark(
253+
triangle_kernel(h,X),
254+
multi_kernelmatrix(X,h,"tria") ,
255+
check = TRUE,
256+
relative = TRUE
257+
)
258+
sum(abs(ta1-ta2))
259+
260+
#cosine_kernel
261+
262+
cosine_kernel(h,X) -> c1
263+
multi_kernelmatrix(X,h,"c") -> c2
264+
bench::mark(
265+
cosine_kernel(h,X),
266+
multi_kernelmatrix(X,h,"c"),
267+
check = TRUE,
268+
relative = TRUE
269+
)
270+
sum(abs(c1-c2))
271+
99272
}
100273

101274
% Add one or more standard keywords, see file 'KEYWORDS' in the

0 commit comments

Comments
 (0)