-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathExamplefDKDE.m
93 lines (63 loc) · 2 KB
/
ExamplefDKDE.m
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
%Put here the path where you have put the Matlab codes, for example:
addpath('C:\Matlab')
%Author: Aurore Delaigle
%This code illustrates how to use the functions for computing the deconvolution kernel density estimator and its bandwidths
%Noise to signal ratio=varU/varX
NSR=0.2
%Sample size
n=500
%Generate data from a normal mixture
X=normrnd(5,.4,1,n);
X2=normrnd(2,1,n);
pmix=0.75;
tmp=unifrnd(0,1,1,n);
X(tmp<pmix)=X2(tmp<pmix);
%true density of X
truedens=@(xx) (1-pmix)*normpdf(xx,5,.4)+pmix*normpdf(xx,2,1);
%Specify error distribution (normal or Laplace in this case) and generate data from this error distribution
errortype='Lap';
if strcmp(errortype,'norm')==1
%normal case
sigU=sqrt(NSR*var(X));
U=normrnd(0,sigU,1,n);
varU=sigU^2;
elseif strcmp(errortype,'Lap')==1
%Laplace case
sigU=sqrt(NSR*var(X)/2);
varU=2*sigU^2;
U=rlap(sigU,1,n);
end
%Contaminated data
W=X+U;
varW=var(W);
%Grid where to estimate the true density
xx=-2:0.1:8;
dx=xx(2)-xx(1);
%Plot the true density
plot(xx,truedens(xx),'r')
%PI bandwidth of Delaigle and Gijbels
hPI=PI_deconvUknownth4(W,errortype,varU,sigU);
%DKDE estimator without rescaling (density does not integrate exactly to 1)
y=fdecUknown(xx,W,hPI,errortype,sigU);
%With rescaling: here xx must be equispaced and must cover the range where the estimated density is significantly non zero
y2=fdecUknown(xx,W,hPI,errortype,sigU,dx);
hold
plot(xx,y,'k')
hold
hold
plot(xx,y2,'g')
hold
%CV bandwidth of Stefanski and Carroll
hCV=CVdeconv(W,errortype,sigU)
y3=fdecUknown(xx,W,hCV,errortype,sigU,dx);
hold
plot(xx,y3,'m')
hold
%Compare with the naive KDE estimator that ignores the error (using normal reference bandwidth and standard normal kernel)
h=1.06*sqrt(var(W))*n^(-1/5);
xout=outerop(xx,W,'-');
fnaive=normpdf(xout,0,h)*ones(n,1)/n;
hold
plot(xx,fnaive,'c')
hold
legend('true f', 'fdec, hPI', 'fdec rescaled, hPI', 'fdec rescaled, hCV', 'naive estimator, hNR')