-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_experiment.Rmd
558 lines (450 loc) · 16.2 KB
/
simulation_experiment.Rmd
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
---
title: "797 project formulation"
author: "Tobias Kuhlmann, Rui Zhang"
date: "12/11/2018"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(logspline)
library(KernSmooth)
library(plyr)
```
## Data
For our project we simulate univariate data $$[x_i, y_i] \quad i \epsilon\{1,...,n\},$$ where $y_i \sim iid$ and an unknown and known (different cases) smooth density f(x). This may not be just one dataset, but several.
## Models
##### Univariate kernel density estimator
We use a univariate kernel density function following Wand and Jones (1995). A density function can be estimated by
$$\hat f(x;h)=(nh)^{-1}\sum_{i=1}^{n}K\{(x-X_{i})/h\},$$
where K is a kernel function satisfying $\int K(x) dx=1$ and h is the bandwidth.
####### R resources
- Wand and Jones (1995)
- https://stat.ethz.ch/R-manual/R-devel/library/stats/html/density.html
##### Univariate density estimation with logspline
Let $B$ be a collection of feasible column vectors following Stone, Hansen, Kooperberg, and Truong (1997). If $\beta \epsilon B$, then
$$f(x;\beta)=exp(\beta_1B_1(x)+\cdots+\beta_JB_J(x)-C(\beta)), L<x<U$$
where $$C(\beta)=log(\int_{L}^{U} exp(\beta_1B_1(x)+\cdots+\beta_JB_J(x))dy).$$ Then $f(x;\beta)$ is a positive density function on (L,U).
####### R resources
- https://www.rdocumentation.org/packages/logspline/versions/2.1.11
- https://www.rdocumentation.org/packages/logspline/versions/2.1.11/topics/logspline
- https://www.rdocumentation.org/packages/logspline/versions/2.1.11/topics/dlogspline
- Stone, Hansen, Kooperberg, and Truong (1997)
## Goal
After estimating both models on several sets of simulated data with different sample sizes, our goal is to study and compare the rates of convergence of the MISE as $n -> \infty$.
# Simulation experiment
## Simulation
## Stats: Univariate Kernel density estimator test
```{r kde}
y <- rnorm(5000, 0, 1)
# Univariate kernel density estimator
# use bandwidth estimation as recommended in Venables and Ripley (2002)
fit <- density(y, bw = 'sj')
keep.fits <- fit$y
mise <- mean((fit$y-dnorm(fit$x, 0, 1))^2)
mise
keep.log.mise <- log10(mise)
# histogram overlay
hist(y, freq = FALSE)
lines(fit)
# plot fit over x
plot(x=fit$x, y=fit$y)
```
## KernSmooth (Wand): Univariate Kernel density estimator test
```{r kde wand}
# Univariate kernel density estimator following Wand (1995)
y <- rnorm(1000, 0, 1)
# select optimal bandwidth
h <- dpik(y)
# kde following Wand (1995)
fit <- bkde(x=y, bandwidth=h)
keep.fits <- fit$y
mise <- mean((fit$y-dnorm(fit$x, 0, 1))^2)
mise
keep.log.mise <- log10(mise)
# histogram overlay
hist(y, freq = FALSE)
lines(fit)
# plot fit over x
plot(x=fit$x, y=fit$y)
```
## Logspline density estimator test
```{r log}
y <- rweibull(n=1000, shape=1.5, scale=0.5)
# logspline density estimator
fit <- logspline(y)
# summary(fit)
# density object
x = seq(from=0, to=10, length.out=1000)
dens <- dlogspline(q=x, fit=fit)
#summary(dens)
# MISE: mean(dlogspline quantiles - true_quantiles)^2
keep.fits <- dens
mise <- mean((dens-dlnorm(x, 0, 1))^2)
mise
keep.log.mise <- log10(mise)
# histogram overlay
hist(y, freq = FALSE)
# plot density of logsplinefit
#plot(fit, n = 101, what = "d")
# density overlay
lines(x, dens, type = "l")
# plot dlogspline fit over x
plot(x, dens, type = "l")
```
## Monte Carlo experiment
## Normal distribution
```{r mc}
reps.per.n <- 10
log10.ns <- seq(from=2,to=5,length=20) # equally space n's on log scale
ns <- round(10^log10.ns)
log10.ns <- log10(ns)
# make storage for what we want to keep
keep.kernel.wand <- data.frame(log.n = rep(log10.ns,each=reps.per.n),log.mise=NA)
keep.kernel <- data.frame(log.n = rep(log10.ns,each=reps.per.n),log.mise=NA)
keep.logspline <- data.frame(log.n = rep(log10.ns,each=reps.per.n),log.mise=NA)
# let's keep the fits too (it's always useful to look at estimates)
keep.kernel.wand.fits <- matrix(NA,length(keep.kernel$log.n),401)
keep.kernel.fits <- matrix(NA,length(keep.kernel$log.n),401)
keep.logspline.fits <- matrix(NA,length(keep.logspline$log.n),401)
counter <- 1
for (n.i in ns)
{
for (mc.i in 1:reps.per.n)
{
# generate data
# TODO1: add some noise to distributions?
y <- rnorm(n.i, 0, 1)
# Univariate kernel density estimator from KernSmooth package (Wand (1995))
# select optimal bandwidth
h <- dpik(y)
# kde following Wand (1995)
fit <- bkde(x=y, bandwidth=h, gridsize = 401)
# keep fits
keep.kernel.wand.fits[counter,] <- fit$y
# calc mse
mise <- mean((fit$y-dnorm(fit$x, 0, 1))^2)
# log mise
keep.kernel.wand$log.mise[counter] <- log10(mise)
# store kde x values for log spline evaluation
x = fit$x
# Univariate kernel density estimator from stats package
# use smoothing parameter (standard deviation of kernel) 'sj', as recommended in Venables and Ripley (2002)
fit <- density(y, bw = 'sj', n=401)
# keep fits
keep.kernel.fits[counter,] <- fit$y
# calc mse
mise <- mean((fit$y-dnorm(fit$x, 0, 1))^2)
# log mise
keep.kernel$log.mise[counter] <- log10(mise)
# Logspline density estimator
fit <- logspline(y)
# density values on 401 equally spaced points
dens <- dlogspline(q=x, fit=fit)
# keep fits
keep.logspline.fits[counter,] <- dens
# calc mise
mise <- mean((dens-dnorm(x, 0, 1))^2)
# log mise
keep.logspline$log.mise[counter] <- log10(mise)
# runtime
counter <- counter + 1
if (counter %% 20 == 0){
print(paste0("Iteration: ", counter))
}
}
}
```
##### Visualization
```{r visualization}
# plot results
par(mfrow=c(1,3))
# kde Wand (1995) plot
plot(keep.kernel.wand$log.n,keep.kernel.wand$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n", ylim=c(-7,-2))
points(keep.kernel.wand$log.n,keep.kernel.wand$log.mise,pch=16,cex=.75)
lmfit_kernel.wand <- lm(log.mise~log.n,data=keep.kernel.wand)
abline(lmfit_kernel.wand,col="red")
title("KernSmooth MISE")
# kde plot
plot(keep.kernel$log.n,keep.kernel$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n", ylim=c(-7,-2))
points(keep.kernel$log.n,keep.kernel$log.mise,pch=16,cex=.75)
lmfit_kernel <- lm(log.mise~log.n,data=keep.kernel)
abline(lmfit_kernel,col="red")
title("Stats KDE MISE")
# logspline plot
plot(keep.logspline$log.n,keep.logspline$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n", ylim=c(-7,-2))
points(keep.logspline$log.n,keep.logspline$log.mise,pch=16,cex=.75)
lmfit_logspline <- lm(log.mise~log.n,data=keep.logspline)
abline(lmfit_logspline,col="red")
title("Logspline DE MISE")
# look at kernel coefficient of interest
print(summary(lmfit_kernel.wand)$coefficients)
print(confint(lmfit_kernel.wand))
# look at kernel coefficient of interest
print(summary(lmfit_kernel)$coefficients)
print(confint(lmfit_kernel))
# look at coefficient of interest
print(summary(lmfit_logspline)$coefficients)
print(confint(lmfit_logspline))
```
```{r visualization combined normal}
# plot results
par(mfrow=c(1,1))
# kernel
plot(keep.kernel$log.n,keep.kernel$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n")
points(keep.kernel$log.n,keep.kernel$log.mise,pch=16,cex=.75, col='blue')
lmfit <- lm(log.mise~log.n,data=keep.kernel)
abline(lmfit,col="red")
# logspline
points(keep.logspline$log.n,keep.logspline$log.mise,pch=16,cex=.75, col='green')
lmfit <- lm(log.mise~log.n,data=keep.logspline)
abline(lmfit,col="red")
title("Density estimation asymptotic MISE")
# legend
# mean plots
kernel_mean_mise = aggregate(keep.kernel, by=list(keep.kernel$log.n), mean)
logspline_mean_mise = aggregate(keep.logspline, by=list(keep.logspline$log.n), mean)
plot(kernel_mean_mise$log.n,kernel_mean_mise$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n")
points(kernel_mean_mise$log.n,kernel_mean_mise$log.mise,pch=16,cex=.75, col='blue')
points(logspline_mean_mise$log.n,logspline_mean_mise$log.mise,pch=16,cex=.75, col='green')
title("Density estimation asymptotic MISE")
```
##Weibull distribution
```{r mc}
reps.per.n <- 10
log10.ns <- seq(from=2,to=5,length=20) # equally space n's on log scale
ns <- round(10^log10.ns)
log10.ns <- log10(ns)
# make storage for what we want to keep
keep.kernel.wand <- data.frame(log.n = rep(log10.ns,each=reps.per.n),log.mise=NA)
keep.logspline <- data.frame(log.n = rep(log10.ns,each=reps.per.n),log.mise=NA)
# let's keep the fits too (it's always useful to look at estimates)
keep.kernel.wand.fits <- matrix(NA,length(keep.kernel.wand$log.n),401)
keep.logspline.fits <- matrix(NA,length(keep.logspline$log.n),401)
counter <- 1
for (n.i in ns)
{
for (mc.i in 1:reps.per.n)
{
# generate data
# TODO1: add some noise to distributions?
y <- rweibull(n=n.i, shape=1.5, scale=0.5)
# Univariate kernel density estimator from KernSmooth package (Wand (1995))
# select optimal bandwidth
h <- dpik(y)
# kde following Wand (1995)
fit <- bkde(x=y, bandwidth=h, gridsize = 401L)
# keep fits
keep.kernel.wand.fits[counter,] <- fit$y
# calc mse
mise <- mean((fit$y-rweibull(fit$x, shape=1.5, scale=0.5))^2)
# log mise
keep.kernel.wand$log.mise[counter] <- log10(mise)
# store kde x values for log spline evaluation
x = fit$x
# Logspline density estimator
fit <- logspline(y)
# density values on 401 equally spaced points
dens <- dlogspline(q=x, fit=fit)
# keep fits
keep.logspline.fits[counter,] <- dens
# calc mise
mise <- mean((dens-rweibull(x, shape=1.5, scale=0.5))^2)
# log mise
keep.logspline$log.mise[counter] <- log10(mise)
# runtime
counter <- counter + 1
if (counter %% 20 == 0){
print(paste0("Iteration: ", counter))
}
}
}
```
##### Visualization
```{r visualization}
# plot results
par(mfrow=c(1,2))
# kde Wand (1995) plot
plot(keep.kernel.wand$log.n,keep.kernel.wand$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n", ylim=c(-7,0))
points(keep.kernel.wand$log.n,keep.kernel.wand$log.mise,pch=16,cex=.75)
lmfit_kernel.wand <- lm(log.mise~log.n,data=keep.kernel.wand)
abline(lmfit_kernel.wand,col="red")
title("KernSmooth MISE")
# logspline plot
plot(keep.logspline$log.n,keep.logspline$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n", ylim=c(-7,0))
points(keep.logspline$log.n,keep.logspline$log.mise,pch=16,cex=.75)
lmfit_logspline <- lm(log.mise~log.n,data=keep.logspline)
abline(lmfit_logspline,col="red")
title("Logspline DE MISE")
# look at kernel coefficient of interest
print(summary(lmfit_kernel.wand)$coefficients)
print(confint(lmfit_kernel.wand))
# look at coefficient of interest
print(summary(lmfit_logspline)$coefficients)
print(confint(lmfit_logspline))
```
```{r visualization combined normal}
# plot results
par(mfrow=c(1,1))
# kernel
plot(keep.kernel.wand$log.n,keep.kernel.wand$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n")
points(keep.kernel.wand$log.n,keep.kernel.wand$log.mise,pch=16,cex=.75, col='blue')
lmfit <- lm(log.mise~log.n,data=keep.kernel.wand)
abline(lmfit,col="red")
# logspline
points(keep.logspline$log.n,keep.logspline$log.mise,pch=16,cex=.75, col='green')
lmfit <- lm(log.mise~log.n,data=keep.logspline)
abline(lmfit,col="red")
title("Density estimation asymptotic MISE")
# legend
# mean plots
kernel_mean_mise = aggregate(keep.kernel.wand, by=list(keep.kernel.wand$log.n), mean)
logspline_mean_mise = aggregate(keep.logspline, by=list(keep.logspline$log.n), mean)
plot(kernel_mean_mise$log.n,kernel_mean_mise$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n")
points(kernel_mean_mise$log.n,kernel_mean_mise$log.mise,pch=16,cex=.75, col='blue')
points(logspline_mean_mise$log.n,logspline_mean_mise$log.mise,pch=16,cex=.75, col='green')
title("Density estimation asymptotic MISE")
```
## Chi squared distribution
```{r x2 vis}
y <- rchisq(seq(from=0,to=25,length=1000), df=3)
# Univariate kernel density estimator
# use bandwidth estimation as recommended in Venables and Ripley (2002)
fit <- density(y, bw = 'sj')
# histogram overlay
hist(y, freq = FALSE)
lines(fit)
x <- fit$x
# logspline density estimator
fit <- logspline(y)
# summary(fit)
# density object
dens <- dlogspline(q=x, fit=fit)
#summary(dens)
# histogram overlay
hist(y, freq = FALSE)
# plot density of logsplinefit
#plot(fit, n = 101, what = "d")
# density overlay
lines(x, dens, type = "l")
```
```{r mc x2}
reps.per.n <- 10
log10.ns <- seq(from=2,to=5,length=20) # equally space n's on log scale
ns <- round(10^log10.ns)
log10.ns <- log10(ns)
# make storage for what we want to keep
keep.kernel.wand <- data.frame(log.n = rep(log10.ns,each=reps.per.n),log.mise=NA)
keep.logspline <- data.frame(log.n = rep(log10.ns,each=reps.per.n),log.mise=NA)
# let's keep the fits too (it's always useful to look at estimates)
keep.kernel.wand.fits <- matrix(NA,length(keep.kernel.wand$log.n),401)
keep.logspline.fits <- matrix(NA,length(keep.logspline$log.n),401)
counter <- 1
for (n.i in ns)
{
for (mc.i in 1:reps.per.n)
{
# generate data
# TODO1: add some noise to distributions!
y <- rchisq(n.i, df=3)
# Univariate kernel density estimator from KernSmooth package (Wand (1995))
# select optimal bandwidth
h <- dpik(y)
# kde following Wand (1995)
fit <- bkde(x=y, bandwidth=h, kernel='normal', gridsize = 401L)
# keep fits
keep.kernel.wand.fits[counter,] <- fit$y
# calc mse
mise <- mean((fit$y-dnorm(fit$x, 0, 1))^2)
# log mise
keep.kernel.wand$log.mise[counter] <- log10(mise)
# store kde x values for log spline evaluation
x = fit$x
# Logspline density estimator
fit <- logspline(y)
# density values on 401 equally spaced points
dens <- dlogspline(q=x, fit=fit)
# keep fits
keep.logspline.fits[counter,] <- dens
# calc mise
mise <- mean((dens-dchisq(x, df=3))^2)
# log mise
keep.logspline$log.mise[counter] <- log10(mise)
# runtime
counter <- counter + 1
if (counter %% 20 == 0){
print(paste0("Iteration: ", counter))
}
}
}
```
##### Visualization
```{r visualization x2}
# plot results
# plot results
par(mfrow=c(1,2))
# kde Wand (1995) plot
plot(keep.kernel.wand$log.n,keep.kernel.wand$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n", ylim=c(-7,-2))
points(keep.kernel.wand$log.n,keep.kernel.wand$log.mise,pch=16,cex=.75)
lmfit_kernel.wand <- lm(log.mise~log.n,data=keep.kernel.wand)
abline(lmfit_kernel.wand,col="red")
title("KernSmooth MISE")
# logspline de
plot(keep.logspline$log.n,keep.logspline$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n", ylim=c(-7,-2))
points(keep.logspline$log.n,keep.logspline$log.mise,pch=16,cex=.75)
lmfit_logspline <- lm(log.mise~log.n,data=keep.logspline)
abline(lmfit_logspline,col="red")
title("Logspline DE MISE")
# look at kernel coefficient of interest
print(summary(lmfit_kernel.wand)$coefficients)
print(confint(lmfit_kernel.wand))
# look at coefficient of interest
print(summary(lmfit_logspline)$coefficients)
print(confint(lmfit_logspline))
```
```{r visualization combined chi2}
# plot results
par(mfrow=c(1,1)) # Assumes 20 different sample sizes
# kernel
plot(keep.kernel$log.n,keep.kernel$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n")
points(keep.kernel$log.n,keep.kernel$log.mise,pch=16,cex=.75, col='blue')
lmfit <- lm(log.mise~log.n,data=keep.kernel)
abline(lmfit,col="red")
# logspline
points(keep.logspline$log.n,keep.logspline$log.mise,pch=16,cex=.75, col='green')
lmfit <- lm(log.mise~log.n,data=keep.logspline)
abline(lmfit,col="red")
title("Density estimation asymptotic MISE")
# legend
# mean plots
kernel_mean_mise = aggregate(keep.kernel, by=list(keep.kernel$log.n), mean)
logspline_mean_mise = aggregate(keep.logspline, by=list(keep.logspline$log.n), mean)
plot(kernel_mean_mise$log.n,kernel_mean_mise$log.mise,xlab="Log10 n",ylab="Log10 MISE",type="n")
points(kernel_mean_mise$log.n,kernel_mean_mise$log.mise,pch=16,cex=.75, col='blue')
points(logspline_mean_mise$log.n,logspline_mean_mise$log.mise,pch=16,cex=.75, col='green')
title("Density estimation asymptotic MISE")
```
## Questions
- Why are convergance rates different (normal vs. chi2)? Maybe dependend on Kernel?
- Answer: -4/5 is the minimum (optimal) MISE convergance, which means the convergence can be slower if the density gets harder to estimate!
## Conclusions
- Is logspline log mise asymptotic behaviour linear?
- Logspline log mise goes faster to zero than kernel density estimation!
```{r plotfits}
#par(mfrow=c(4,5),mar=c(1,1,1,1)) # Assumes 20 different sample sizes
#for (log.n.i in unique(keep$log.n))
#{
# junk <- keep.fits[keep$log.n==log.n.i,]
# plot(fit$x,f(fit$x),type="n",ylim=range(junk),main=paste("n=",10^log.n.i),
# xlab="n",ylab="n",axes=F)
# for (i in 1:dim(junk)[1])
# lines(fit$x,junk[i,])
# lines(fit$x,f(fit$x),col="red")
#
#}
```
```{}
```
```{}
```
```{}
```