EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F...

24
Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 1 / 24

Transcript of EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F...

Page 1: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

Nonparametric density estimation

Christopher F Baum

EC 823: Applied Econometrics

Boston College, Spring 2013

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 1 / 24

Page 2: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

Kernel density plot

To describe a categorical variable or a continuous variable taking ondiscrete values, such as age measured in years, a histogram is oftenemployed. For a continuous variable taking on many values, the kerneldensity plot is a better alternative to the histogram. This smoothedrendition connects the midpoints of the histogram, rather than formingthe histogram as a step function, and it gives more weight to data thatare closer to the point of evaluation.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 2 / 24

Page 3: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

Let f (x) denote the density function of a continuous RV. The kerneldensity estimate of f (x) at x = x0 is then

f̂ (x0) =1

Nh

N∑i=1

K(

xi − x0

h

)where K (·) is a kernel function that places greater weight on points xithat are closer to x0.

The kernel function is symmetric around zero and integrates to one.Either K (z) = 0 if |z| ≥ z0, for some z0, or K (z)→ 0 as z →∞.

A histogram with bin width 2h evaluated at x0 is the special caseK (z) = 0.5 if |z| < 1, K (z) = 0 otherwise.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 3 / 24

Page 4: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

A kernel density plot requires the choice of a kernel function, K (·) anda bandwidth h. You then evaluate the kernel density function at anumber of values x0, and plot those estimates against x0.

In Stata, the kdensity command produces the kernel densityestimate. The default kernel function is the Epanechnikov kernel,which sets K (z) = (3/4)(1− z2/5)/

√5 for |z| <

√5 and zero

otherwise. This kernel function is said to be the most efficient inminimizing the mean integrated squared error.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 4 / 24

Page 5: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

Other kernel functions available include an alternative Epanechnikovkernel, as well as biweight, cosine, Gaussian, Parzen, rectangular, andtriangle kernels. All but the Gaussian have a cutoff point, beyond whichthe kernel function is zero.

The choice of kernel bandwidth (the bwidth() option) determineshow quickly the cutoff is reached. A small bandwidth will cause thekernel density estimate to depend only on values close to the point ofevaluation, while a larger bandwidth will include more of the values inthe vicinity of the point, yielding a smoother estimate. Mostresearchers agree that the choice of kernel is not as important as thechoice of bandwidth.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 5 / 24

Page 6: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

If no bandwidth is specified, it is chosen according to

m = min(

sd(x),IQR(x)1.349

)h =

0.9mn1/5

where sd(x) and IQR(x) refer to the standard deviation andinter-quartile range of the series x , respectively.

The default number of x0 points is 50, which may be set with the n()option, or a variable containing values at which the kernel densityestimate is to be produced may be specified with the at() option. Youmay also use the generate() option to produce new variablescontaining the plotted coordinates.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 6 / 24

Page 7: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

. use mus02psid92m.dta

. g earningsk = earnings/1000(209 missing values generated)

. lab var earningsk "Total labor income, $000"

. kdensity earningsk

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 7 / 24

Page 8: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

Kernel density of labor earnings0

.005

.01

.015

.02

Den

sity

0 200 400 600 800 1000Total labor income, $000

kernel = epanechnikov, bandwidth = 3.2458

Kernel density estimate

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 8 / 24

Page 9: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

. g lek = log(earningsk)(498 missing values generated)

. lab var lek "Log total labor income, $000"

. kdensity lek

. gr export 82303b.pdf, replace(file /Users/cfbaum/Dropbox/baum/EC823 S2013/82303b.pdf written in PDF format)

. kdensity lek, bw(0.20) normal n(4000) leg(rows(1))

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 9 / 24

Page 10: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

Kernel density of log earnings, default bandwidth0

.2.4

.6D

ensi

ty

-2 0 2 4 6 8Log total labor income, $000

kernel = epanechnikov, bandwidth = 0.1227

Kernel density estimate

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 10 / 24

Page 11: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

kdensity

Kernel density with wider bandwidth, n ' N of sample0

.2.4

.6D

ensi

ty

-2 0 2 4 6 8Log total labor income, $000

Kernel density estimate Normal densitykernel = epanechnikov, bandwidth = 0.2000

Kernel density estimate

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 11 / 24

Page 12: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

bidensity

Bivariate kernel density estimates

We may also want to consider bivariate relationships, and analyze anempirical bivariate density using nonparametric means. The univariatekernel density estimator can be generalized to a bivariate context.Gallup and Baum’s bidensity command, available from SSC,produces bivariate kernel density estimates and illustrates them with acontourline, or topographic map, plot.

Available kernels include Epanechnikov and alternative, Gaussian,rectangle and triangle, each the product of the univariate kernelfunctions defined in kdensity. The bandwidth defaults are thoseemployed in kdensity. The saving() option allows you to create anew dataset containing the x , y , and f (x , y) variables.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 12 / 24

Page 13: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

bidensity

. webuse grunfeld, clear

. gen linv = log(invest)

. lab var linv "Log[Investment]"

. gen lmkt = log(mvalue)

. lab var lmkt "Log[Mkt value]"

. bidensity linv lmkt

. gr export 82303d.pdf, replace(file /Users/cfbaum/Dropbox/baum/EC823 S2013/82303d.pdf written in PDF format)

. bidensity linv lmkt, scatter(msize(vsmall) mcolor(black)) ///> colorlines levels(8) format(%3.2f)

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 13 / 24

Page 14: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

bidensity

Bivariate kernel density

0

2

4

6

8

Log[

Inve

stm

ent]

4 5 6 7 8 9Log[Mkt value]

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 14 / 24

Page 15: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

bidensity

Bivariate kernel density with scatterplot overlay

0

2

4

6

8

Log[

Inve

stm

ent]

4 5 6 7 8 9Log[Mkt value]

0.090.080.060.050.040.030.01

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 15 / 24

Page 16: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

Local polynomial regression

While the bivariate density provides a nonparametric estimate of thejoint density of x and y , it does not presume any causal relationshipamong those variables. A variety of local linear regression techniquesmay be employed to flexibly model the relationship betweenexplanatory variable x and outcome variable y .

The local linear aspect of these techniques refers to the concept thatthe relationship is modeled as linear in the neighborhood, but may varyacross values of x .

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 16 / 24

Page 17: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

Local linear regression techniques model y = m(x) + u, where theconditional mean function m(·) is not specified. The estimate of m(x)at x = x0 is a local weighted average of yi where high weight is placedon observations for which xi is close to x0 and little or no weight isplaced on observations with xi far from x0. Formally,

m̂(x0) =N∑

i=1

w(xi , x0,h)yi

where the weights w(·) sum to one and decrease as the distance|xi − x0| increases.

As in the kernel density estimator, the bandwidth parameter h controlsthe process. A narrower bandwidth (smaller h) causes more weight tobe placed on nearby observations.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 17 / 24

Page 18: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

After defining a kernel function K (·), a local linear regression estimateat x = x0 can be obtained by minimizing

N∑i=1

K(

xi − x0

h

)(yi − α− β(xi − x0))

2

This may be generalized to the local polynomial regression estimateproduced by Stata’s lpoly, where the term β(xi − x0) becomesβ(xi − x0)

d , where d is an integer power. If d = 0, this becomes localmean smoothing. For d = 1, we have a locally weighted least squaresmodel. An estimate fit with higher powers of d has better biasproperties than the zero-degree local polynomial. Odd-order degreesare preferable.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 18 / 24

Page 19: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

The bandwidth, if not specified, is chosen by the rule-of-thumbmethod, which provides the asymptotically optimal constantbandwidth: that which minimizes the conditional weighted meanintegrated squared error. As with kdensity, a bandwidth may also bespecified. The same set of kernel functions is available, as are optionsto alter the number of evaluation points (n()) and save the results asnew variables with generate().

Confidence bands for the local polynomial regression estimate mayalso be produced with option ci, and the sequence of standard errorssaved as a new variable with se().

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 19 / 24

Page 20: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

An example of local polynomial regression:

. webuse motorcycle, clear(Motorcycle data from Fan & Gijbels (1996))

. lpoly accel time, msize(vsmall)

. gr export 82303f.pdf, replace(file /Users/cfbaum/Dropbox/baum/EC823 S2013/82303f.pdf written in PDF format)

. lpoly accel time, degree(3) kernel(epan2) msize(vsmall)

. gr export 82303g.pdf, replace(file /Users/cfbaum/Dropbox/baum/EC823 S2013/82303g.pdf written in PDF format)

. lpoly accel time, degree(3) kernel(gaussian) msize(vsmall) ci

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 20 / 24

Page 21: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

The default kernel and bandwidth do not work very well:-1

50-1

00-5

00

5010

0ac

cele

ratio

n (g

)

0 20 40 60time (msec)

kernel = epanechnikov, degree = 0, bandwidth = 2.75

Local polynomial smooth

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 21 / 24

Page 22: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

Choosing a different kernel and higher degree improve the fit:-1

50-1

00-5

00

5010

0ac

cele

ratio

n (g

)

0 20 40 60time (msec)

kernel = epan2, degree = 3, bandwidth = 6.88

Local polynomial smooth

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 22 / 24

Page 23: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

We may also examine the confidence bands around the estimate, nowcomputed with a Gaussian kernel:

-150

-100

-50

050

100

acce

lera

tion

(g)

0 20 40 60time (msec)

95% CI acceleration (g) lpoly smoothkernel = gaussian, degree = 3, bandwidth = 2.46, pwidth = 3.69

Local polynomial smooth

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 23 / 24

Page 24: EC 823: Applied Econometrics - Boston Collegefm · Nonparametric density estimation Christopher F Baum EC 823: Applied Econometrics Boston College, Spring 2013 Christopher F Baum

lpoly

A similar methodology is provided by lowess (Cleveland, JASA 1979),which makes use of a variable bandwidth, with evaluation points nearthe extrema are smoothed using a narrower bandwidth. Observationswith large residuals in the local linear regression are alsodownweighted, making this method more computationally demandingthan local polynomial regression.

Christopher F Baum (BC / DIW) Nonparametric density estimation Boston College, Spring 2013 24 / 24