-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 17.8 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
a=ifelse(!is.null(expr),expr,as.expression(substitute(dens(x))))
L=integrate(function(x){(kern(x,...))^2},l2,u2)$value
M1=integrate(function(x){(eval(D(a,"x")))^2},l1,u1)$value
Q=integrate(function(x){(eval(a))^2},l1,u1)$value
g1=integrate(function(x){x*(kern(x))},l2,u2)$value
h= g1^(-2/3)*L^(1/3)*M1^(-1/3)*n^(-1/3)
c=g1^(2/3)*L^(2/3)
mse=2*c*M1^(1/3)*n^(-1/3)
return(list(mise_opt=mse,h_opt=h,L_kern=L,gamma_kern=g1,M_dens=M1,Q_dens=Q))
}
MISE_bias_red=function(expr=NULL,dens=dnorm,kern=new_kern,...,l1=-Inf,u1=Inf,l2=-Inf,u2=Inf,n=1000)
{
a=ifelse(!is.null(expr),expr,as.expression(substitute(dens(x))))
L=integrate(function(x){(kern(x,...))^2},l2,u2)$value
M2=integrate(function(x){(eval(D(D(D(D(a, "x"),"x"),"x"),"x")))^2},l1,u1)$value
g2=integrate(function(x){(x^4)*(kern(x))},l2,u2)$value
h= g2^(-2/9)*L^(1/9)*M2^(-1/9)*n^(-1/9)
c=g2^(2/9)*L^(8/9)
mse=(577/576)*c*M2^(1/9)*n^(-8/9)
return(list(mise_opt=mse,h_opt=h,L_kern=L,gamma_kern=g2,M_dens=M2))
}
######################
#Using Monte-Carlo Simulation
et = function(func1 = rnorm,func2=dnorm,Func = beta_density,l1 = -Inf,u1 = Inf,...)
{
v = func1(1000)
integrate(function(x){(Func(x,v,...)-func2(x))^2},lower = l1,upper = u1,subdivisions = 10000)$value
}
##Functions to calculate the standard error estimate for corresponding methods
a_kd_logis_norm=Vectorize(function(h0){mean(mc_replicate(1000,et(rlogis,dlogis,kern_density,h=h0),detectCores()))},vectorize.args = "h0")
a_hist = Vectorize(function(h0,f1,f2){mean(mc_replicate(1000,et(f1,f2,histogram_density,h=h0),detectCores(),varlist = c('et','histogram_density')))},vectorize.args = "h0")
a_ash.1= Vectorize(function(h0,f1,f2){mean(et(f1,f2,ash_density_,h=h0,l1=-4,l2=4))},vectorize.args = "h0")
a_ash = Vectorize(function(h0,f1,f2){mean(mc_replicate(100,et(f1,f2,ash_density,h=h0),detectCores(),varlist = c('et','ash_density','histogram_density','vec')))},vectorize.args = "h0")
a_ash_i = Vectorize(function(h0,f1,f2){mean(mc_replicate(1000,et(f1,f2,ash_density_int,h=h0),detectCores(),varlist = c('et','ash_density_int','histogram_density','vec')))},vectorize.args = "h0")
#######################
## some useful expression to be used in MISE function
logis_exp=function(mu=0,sd=1){substitute((exp(-(x-mu)/sd))/(sd*(1+exp(-(x-mu)/sd))^2))}
laplace_exp=function(mu=0,sd=1){substitute((.5/sd)*exp(-abs((x-mu)/sd)))}
normal_exp=function(mu=0,sd=1){substitute((1/sqrt(2*pi*sd))*exp((-((x-mu)/sd)^2)/2))}
cauchy_exp=function(mu=0,sd=1){substitute(sd/(sd^2+(x-mu)^2))}
gamma_exp=function(r=1,theta=2){substitute((x^(r-1)*exp(-(x/theta)))/((gamma(r))*(theta^r)))}
beta_exp=function(a=1,b=1){substitute((x^(a-1))*(x^(b-1))/((gamma(a)*gamma(b))/gamma(a+b)))}
weib_exp=function(l=1,k=1){substitute((k/l)*(x/l)^(k-1)*exp(-(x/l)^k))}
pareto_exp=function(r=1,theta=1){substitute((r*(theta)^r)/x^(r+1))}
#######################
## getting pdf of two contaminated distribution
cont_dist_exp=function(dist1,dist2,p=.5)
{
substitute(p*dist1+(1-p)*dist2,list(p=p,dist1=dist1,dist2=dist2))
}
######################
##random sample from unknown distribution
rcont_dist=function(n,dist1,dist2,p=.5)
{
x=rbinom(n,1,p)
r1=dist1(sum(x))
r2=dist2(sum(1-x))
c(r1,r2)
}
##showing asymptotic normality of kernel density estimates
Asymp=function(u=0,n=c(10,30,70,150),expr=NULL,rs=rnorm,ker_f=dnorm,h0=NULL,...)
{
exp_dens=ifelse(!is.null(expr),expr,expression(dnorm(x)))
par(mfrow=c(2,2))
for (i in n)
{
h1=ifelse(is.null(h0),MISE(n=i,expr=exp_dens,kern=ker_f,...)$h_opt,h0)
a=function(){kern_density(u,rs(i),h=h1,func=ker_f)}
z=replicate(1000,a())
y=scale(z,scale = TRUE)
hist(y,main=paste("n = ",i),prob=TRUE,nclass=50)
curve(dnorm,lwd=2,add=TRUE,col="red")
}
}
#####calculation of M_0(h)
M_0=Vectorize(function(v,l1=-Inf,u1=Inf,h=0.1,kern=dnorm,...)
{
cv_likelihood=Vectorize(function(i=1)
{
(sum(kern((v[i]-v)/h,...))-kern(0,...))/((length(v)-1)*h)
})
L=integrate(function(x){(kern_density(x,v,h,kern,...))^2},l1,u1,subdivisions = 5000)$value
L-sum(cv_likelihood(seq_along(v)))*2/length(v)
},vectorize.args = "h")
###likelihood cross validation
l_cv=Vectorize(function(v,h=0.1,kern=dnorm,...)
{
CV=Vectorize(function(i=1)
{
(sum(kern((v[i]-v)/h,...))-kern(0,...))/((length(v)-1)*h)
})
cv_h=mean(log(CV(seq_along(v))))
return(cv_h)
},vectorize.args = "h")
#########################
## L1 loss calculation
et.l1 = function(func1 = rnorm,func2=dnorm,Func = beta_density,l1 = -Inf,u1 = Inf,...)
{
v = func1(1000)
integrate(function(x){abs(Func(x,v,...)-func2(x))},lower = l1,upper = u1,subdivisions = 10000)$value
}
roxygenise()
install.packages("rlang")
library(devtools)
library(roxygen2)
getwd("E:\\Density_Estimation_package")
getwd("E:\\ Density_Estimation_package")
setwd("E:\\Density_Estimation_package")
rm(list = ls())
###SEGMENT 1 : CALLING LIBRARIES
library("parallel")
library("mcreplicate")#needed for parallel computing
library("prettydoc")
library("lattice")
### SEGMENT 2: DEFINITION OF FUNCTIONS
#' Density Estimate using Beta pdf
#'
#' Obtain densiy estimate of a vector using beta pdf
#' @param a Numeric or Vector Value at which the density has to be estimated
#' @param vec Vector of Numeric observations
#' @param isplot Logical; Indicating whether to plot the estimated density; default value FALSE
#' @param addplot Logical; If true add to an existing plot; default value FALSE
#' @param col_plot Colour of the Plot
#'
#' @author Mainak Manna, Samya Praharaj
#'
#' @return If isplot is FALSE then returns the density estimate vector; Otherwise plots the estimated density
#' @example
#' curve(dnorm(x),-3,3,xlab = "N(0,1)",type = "l",col = "blue")
#' curve(beta_density(x,rnorm(1000)),-3,3,xlab = "N(0,1)",ylab="",add = TRUE,col = "red")
#'
#' @export
#'
beta_density=Vectorize(function(a,vec,isplot=FALSE,addplot=FALSE,col_plot=1)
{
n=length(vec)
y=(vec-min(vec))/(max(vec)-min(vec))
mean_y=mean(y)
var_y=var(y)*((n-1)/n)
const=(((mean_y*(1-mean_y))/var_y)-1)
alpha_hat=const*mean_y
beta_hat= const*(1-mean_y)
den_beta_est1=function(u) dbeta((u-min(vec))/(max(vec)-min(vec)),alpha_hat,beta_hat)/(max(vec)-min(vec))
if(isplot==TRUE)curve(den_beta_est1,-3,3,add = addplot,col=col_plot)
else(return(den_beta_est1(a)))
},vectorize.args = "a")
######################
#' Density Estimate using Histogram method
#'
#' Obtain densiy estimate of a vector using histograms
#' @param x Numeric or Vector Value at which the density has to be estimated
#' @param v Vector of Numeric observations
#' @param x0 Numeric; Origin ; default vlue is 0
#' @param h Numeric; Bandwidth; default value is 0.1
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#' @return Returns the density estimate vector
#' @example
#' vec=rnorm(1000)
#' curve(histogram_density(x,vec,x0=0),-3,3,xlab = paste("x0 = ",0.1),ylab="")
#'
#' @export
#'
histogram_density=Vectorize(function(x,v,x0=0,h=.1)
{
i=(x-x0)%/%h
sum(v>=(x0+i*h) & v<=(x0+(i+1)*h))/(h*length(v))
},vectorize.args = "x")
#' Density Estimate using Average Shifted Histogram Method
#'
#' Obtain densiy estimate of a vector using average shifted histograms for removing dependence on choice of origin
#' @param x Numeric or Vector Value at which the density has to be estimated
#' @param v Vector of Numeric observations
#' @param h0 Numeric; Bandwidth; default value is 0.1
#' @param N Integer; Number of averages taken; default value 9
#'
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#' @return Returns the density estimate vector
#' @example
#' vec=rnorm(1000)
#' curve(ash_density_(x,vec),-3,3,xlab = paste("h = ",0.1),ylab="")
#'
#' @export
#'
ash_density = Vectorize(function(x,v,h0=.1,N = 9)
{
a=Vectorize(function(u) histogram_density(x,v,u,h0))
mean(a(seq(-(h0/2),(h0/2),length.out=N)))
},vectorize.args = "x")
#' Density Estimate using Integrated Shifted Histogram Method
#'
#' Obtain density estimate of a vector using integrated shifted histograms for removing dependence on choice of origin
#' @param x Numeric or Vector Value at which the density has to be estimated
#' @param v Vector of Numeric observations
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#' @return Returns the density estimate vector
#' @example
#' vec=rnorm(1000)
#' ash_density_(x,vec)
#'
#' @export
#'
ash_density_int=Vectorize(function(x,v,h=.1)
{
a=Vectorize(function(u) histogram_density(x,v,u,h))
(integrate(a,-h/2,h/2,subdivisions = 1000)$value)/h
},vectorize.args="x")
#Some kernel functions
#Naive Kernel
#' Naive Kernel
#'
#' Obtain value of naive kernel in given vector
#' @param x Numeric or Vector Value at which the density has to be estimated
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#' @return Returns the value of naive kernel in given vector
#'
#' @export
#'
naive_kern=Vectorize(function(x)
{
if (x>=-1 & x<=1) .5
else 0
})
#Epanenchnikov Kernel
#' Epanenchnikov Kernel for L2 loss
#'
#' Obtain value of Epanenchnikov kernel which is Optimal kernel for L2 loss, in given vector
#' @param x Numeric or Vector Value at which the density has to be estimated
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#'
#' @export
#'
opt_kern=Vectorize(function(x)
{
if (x>=-sqrt(5) & x<=sqrt(5)) (3/(4*sqrt(5)))*(1-(x^2)/5)
else 0
})
#Triangular Kernel
#' Triangular Kernel
#'
#' Obtain value of triangular kernel in given vector
#' @param x Numeric or Vector Value at which the density has to be estimated
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#'
#' @export
#'
triangular_kern=Vectorize(function(x)
{
if (x>=-1 & x<=0) x+1
else if (x>=0 & x<=1) 1-x
else 0
})
####shifted exponential kernel
#' Shifted Exponential Kernel
#'
#' Obtain value of shifted exponential kernel in given vector
#' @param x Numeric or Vector Value at which the density has to be estimated
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#'
#' @export
#'
shiftedexpo_kern=Vectorize(function(x)
{
dexp(x+1)
})
##### cosine kernel
#' Cosine Kernel
#'
#' Obtain value of cosine kernel in given vector
#' @param x Numeric or Vector Value at which the density has to be estimated
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#'
#' @export
#'
cosine_kern=Vectorize(function(x)
{
if (x>=-1 & x<=1) ((pi/4)*cos((pi/2)*x))
else 0
})
###### new_kernal
#' Optimal Kernel Which can take Negative Values
#'
#' Obtain value of a kernel which can take negative values in given vector
#' @param x Numeric or Vector Value at which the density has to be estimated
#'
#'
#' @author Mainak Manna, Samya Praharaj
#'
#'
#' @export
#'
new_kern=Vectorize(function(x)
{
if (x>=-1 & x<=1) (1/8)*(5-3*(x^2))
else 0
})
######################
#' To Find density estimates for a given data
#'
#' To Find density estimates for a given data for different kernel functions
#' @param x Numeric or Vector Value at which the density has to be estimated
#' @param v Vector of Observations
#' @param h bandwidth
#' @param func kernel function to be used
#'
#' @author Mainak Manna, Samya Praharaj
#'
#'
#' @export
#'
kern_density=Vectorize(function(x,v,h=.1,func=dnorm,...)
{
sum(func((x-v)/h,...))/(length(v)*h)
},vectorize.args = "x")
#' Density Estimate using Reflection Principle
#'
#' To Find density estimates for a given data under boundary bias using reflection principle for different kernel functions
#' @param x Numeric or Vector Value at which the density has to be estimated
#' @param v Vector of Observations
#' @param h bandwidth
#' @param func kernel function to be used
#' @param bound lnumeric; lower bound of the support of the distribution
#'
#' @author Mainak Manna, Samya Praharaj
#'
#'
#' @export
#'
ref_density=Vectorize(function(x,v,h=.1,func=dnorm,bound=0,...)
{
ifelse(x>bound,2*sum(func((x-v)/h,...))/(length(v)*h),0)
},vectorize.args = "x")
#Finding theoretical Mean Integrated square error
#' Finding theoretical Mean Integrated square error
#'
#' To Finding theoretical Mean Integrated square error for different distributions using different kernel functions
#'
#' @param expr If not NULL, then the distribution for which MISE is to be calculated; Default NULL and uses normal density
#' @param kern kernel
#' @param l1,u1 lower and upper bound of support of the density function
#' @param l2,u2 lower and upper bound of support of the kernel function
#' @param n numeric; sample size of the data
#'
#' @author Mainak Manna, Samya Praharaj
#'
#' @return Returns the value of theoretical MISE in given vector
#'
#' @export
#'
MISE=function(expr=NULL,dens=dnorm,kern=dnorm,...,l1=-Inf,u1=Inf,l2=-Inf,u2=Inf,n=1000)
{
a=ifelse(!is.null(expr),expr,as.expression(substitute(dens(x))))
L=integrate(function(x){(kern(x,...))^2},l2,u2)$value
M=integrate(function(x){(eval(D(D(a, "x"),"x")))^2},l1,u1)$value
Q=integrate(function(x){(eval(a))^2},l1,u1)$value
g=integrate(function(x){x^2*(kern(x))},l2,u2)$value
h= g^(-2/5)*L^(1/5)*M^(-1/5)*n^(-1/5)
c=g^(2/5)*L^(4/5)
mse=(5/4)*c*M^(1/5)*n^(-4/5)
eff=(3/(5*sqrt(5)))*g^(-1/2)*L^(-1)
return(list(mise_opt=mse,efficiency=eff,h_opt=h,L_kern=L,gamma_kern=g,M_dens=M,Q_dens=Q))
}
#' Finding theoretical Mean Integrated square error
#'
#' To Finding theoretical Mean Integrated square error for different distributions using different kernel functions using optimal bandwidth
#'
#' @param expr If not NULL, then the distribution for which MISE is to be calculated; Default NULL and uses normal density
#' @param kern kernel
#' @param l1,u1 lower and upper bound of support of the density function
#' @param l2,u2 lower and upper bound of support of the kernel function
#' @param n numeric; sample size of the data
#'
#' @author Mainak Manna, Samya Praharaj
#'
#' @return Returns the value of naive kernel in given vector
#'
#' @export
#'
MISE_pos=function(expr=NULL,dens=dnorm,kern=dexp,...,l1=0,u1=Inf,l2=0,u2=Inf,n=1000)
{
a=ifelse(!is.null(expr),expr,as.expression(substitute(dens(x))))
L=integrate(function(x){(kern(x,...))^2},l2,u2)$value
M1=integrate(function(x){(eval(D(a,"x")))^2},l1,u1)$value
Q=integrate(function(x){(eval(a))^2},l1,u1)$value
g1=integrate(function(x){x*(kern(x))},l2,u2)$value
h= g1^(-2/3)*L^(1/3)*M1^(-1/3)*n^(-1/3)
c=g1^(2/3)*L^(2/3)
mse=2*c*M1^(1/3)*n^(-1/3)
return(list(mise_opt=mse,h_opt=h,L_kern=L,gamma_kern=g1,M_dens=M1,Q_dens=Q))
}
MISE_bias_red=function(expr=NULL,dens=dnorm,kern=new_kern,...,l1=-Inf,u1=Inf,l2=-Inf,u2=Inf,n=1000)
{
a=ifelse(!is.null(expr),expr,as.expression(substitute(dens(x))))
L=integrate(function(x){(kern(x,...))^2},l2,u2)$value
M2=integrate(function(x){(eval(D(D(D(D(a, "x"),"x"),"x"),"x")))^2},l1,u1)$value
g2=integrate(function(x){(x^4)*(kern(x))},l2,u2)$value
h= g2^(-2/9)*L^(1/9)*M2^(-1/9)*n^(-1/9)
c=g2^(2/9)*L^(8/9)
mse=(577/576)*c*M2^(1/9)*n^(-8/9)
return(list(mise_opt=mse,h_opt=h,L_kern=L,gamma_kern=g2,M_dens=M2))
}
######################
#Using Monte-Carlo Simulation
et = function(func1 = rnorm,func2=dnorm,Func = beta_density,l1 = -Inf,u1 = Inf,...)
{
v = func1(1000)
integrate(function(x){(Func(x,v,...)-func2(x))^2},lower = l1,upper = u1,subdivisions = 10000)$value
}
##Functions to calculate the standard error estimate for corresponding methods
a_kd_logis_norm=Vectorize(function(h0){mean(mc_replicate(1000,et(rlogis,dlogis,kern_density,h=h0),detectCores()))},vectorize.args = "h0")
a_hist = Vectorize(function(h0,f1,f2){mean(mc_replicate(1000,et(f1,f2,histogram_density,h=h0),detectCores(),varlist = c('et','histogram_density')))},vectorize.args = "h0")
a_ash.1= Vectorize(function(h0,f1,f2){mean(et(f1,f2,ash_density_,h=h0,l1=-4,l2=4))},vectorize.args = "h0")
a_ash = Vectorize(function(h0,f1,f2){mean(mc_replicate(100,et(f1,f2,ash_density,h=h0),detectCores(),varlist = c('et','ash_density','histogram_density','vec')))},vectorize.args = "h0")
a_ash_i = Vectorize(function(h0,f1,f2){mean(mc_replicate(1000,et(f1,f2,ash_density_int,h=h0),detectCores(),varlist = c('et','ash_density_int','histogram_density','vec')))},vectorize.args = "h0")
#######################
## some useful expression to be used in MISE function
logis_exp=function(mu=0,sd=1){substitute((exp(-(x-mu)/sd))/(sd*(1+exp(-(x-mu)/sd))^2))}
laplace_exp=function(mu=0,sd=1){substitute((.5/sd)*exp(-abs((x-mu)/sd)))}
normal_exp=function(mu=0,sd=1){substitute((1/sqrt(2*pi*sd))*exp((-((x-mu)/sd)^2)/2))}
cauchy_exp=function(mu=0,sd=1){substitute(sd/(sd^2+(x-mu)^2))}
gamma_exp=function(r=1,theta=2){substitute((x^(r-1)*exp(-(x/theta)))/((gamma(r))*(theta^r)))}
beta_exp=function(a=1,b=1){substitute((x^(a-1))*(x^(b-1))/((gamma(a)*gamma(b))/gamma(a+b)))}
weib_exp=function(l=1,k=1){substitute((k/l)*(x/l)^(k-1)*exp(-(x/l)^k))}
pareto_exp=function(r=1,theta=1){substitute((r*(theta)^r)/x^(r+1))}
#######################
## getting pdf of two contaminated distribution
cont_dist_exp=function(dist1,dist2,p=.5)
{
substitute(p*dist1+(1-p)*dist2,list(p=p,dist1=dist1,dist2=dist2))
}
######################
##random sample from unknown distribution
rcont_dist=function(n,dist1,dist2,p=.5)
{
x=rbinom(n,1,p)
r1=dist1(sum(x))
r2=dist2(sum(1-x))
c(r1,r2)
}
##showing asymptotic normality of kernel density estimates
Asymp=function(u=0,n=c(10,30,70,150),expr=NULL,rs=rnorm,ker_f=dnorm,h0=NULL,...)
{
exp_dens=ifelse(!is.null(expr),expr,expression(dnorm(x)))
par(mfrow=c(2,2))
for (i in n)
{
h1=ifelse(is.null(h0),MISE(n=i,expr=exp_dens,kern=ker_f,...)$h_opt,h0)
a=function(){kern_density(u,rs(i),h=h1,func=ker_f)}
z=replicate(1000,a())
y=scale(z,scale = TRUE)
hist(y,main=paste("n = ",i),prob=TRUE,nclass=50)
curve(dnorm,lwd=2,add=TRUE,col="red")
}
}
#####calculation of M_0(h)
M_0=Vectorize(function(v,l1=-Inf,u1=Inf,h=0.1,kern=dnorm,...)
{
cv_likelihood=Vectorize(function(i=1)
{
(sum(kern((v[i]-v)/h,...))-kern(0,...))/((length(v)-1)*h)
})
L=integrate(function(x){(kern_density(x,v,h,kern,...))^2},l1,u1,subdivisions = 5000)$value
L-sum(cv_likelihood(seq_along(v)))*2/length(v)
},vectorize.args = "h")
###likelihood cross validation
l_cv=Vectorize(function(v,h=0.1,kern=dnorm,...)
{
CV=Vectorize(function(i=1)
{
(sum(kern((v[i]-v)/h,...))-kern(0,...))/((length(v)-1)*h)
})
cv_h=mean(log(CV(seq_along(v))))
return(cv_h)
},vectorize.args = "h")
#########################
## L1 loss calculation
et.l1 = function(func1 = rnorm,func2=dnorm,Func = beta_density,l1 = -Inf,u1 = Inf,...)
{
v = func1(1000)
integrate(function(x){abs(Func(x,v,...)-func2(x))},lower = l1,upper = u1,subdivisions = 10000)$value
}
roxygenise()
rlang::last_trace()
roxygenise()
?beta_density
roxygenise()
roxygenise()
?cosine_kern
?MISE