1ST EDITION

Statistics for Data Scientists 
50 Essential Concepts

Peter C. Bruce and Andrew G. Bruce

BostonPractical Statistics for Data Scientists
by Peter Bruce and Andrew Bruce

Copyright © 2016 Peter Bruce and Andrew Bruce. All rights reserved.

Printed in the United States of America.

Published by O’Reilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles ( http://safaribooksonline.com ). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com .

Editor: Shannon Cutt
Production  Editor:  FILL  IN  PRODUCTION  EDI‐
TOR
Copyeditor: FILL IN COPYEDITOR
Proofreader: FILL IN PROOFREADER

Indexer: FILL IN INDEXER
Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest

August 2016:

 First Edition

Revision History for the First Edition
2016-08-01:  First Early Release

See http://oreilly.com/catalog/errata.csp?isbn=9781491952894 for release details.

The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Practical Statistics for Data Scientists,
the cover image, and related trade dress are trademarks of O’Reilly Media, Inc.

While  the  publisher  and  the  author(s)  have  used  good  faith  efforts  to  ensure  that  the  information  and
instructions contained in this work are accurate, the publisher and the author(s) disclaim all responsibil‐
ity for errors or omissions, including without limitation responsibility for damages resulting from the use
of or reliance on this work. Use of the information and instructions contained in this work is at your own
risk.  If  any  code  samples  or  other  technology  this  work  contains  or  describes  is  subject  to  open  source
licenses  or  the  intellectual  property  rights  of  others,  it  is  your  responsibility  to  ensure  that  your  use
thereof complies with such licenses and/or rights.

978-1-491-95289-4

[FILL IN]

Table of Contents

Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  vii

1. Exploratory Data Analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  11
Elements of Structured Data                                                                                           13
Further Reading                                                                                                            15
Rectangular Data                                                                                                              15
Data Frames and Indexes                                                                                            16
Graph Data                                                                                                                     17
Further Reading                                                                                                            18
Estimates of Location                                                                                                       18
Mean                                                                                                                               20
Median and Robust Estimates                                                                                     21
Example: Location Estimates of Population and Murder Rates                             22
Further Reading                                                                                                            23
Estimates of Variability                                                                                                    24
Standard Deviation and Related Estimates                                                               25
Estimates Based on Percentiles                                                                                   27
Example: Variability Estimates of State Population                                                 28
Further Reading                                                                                                            29
Exploring the Data Distribution                                                                                    29
Percentiles and Boxplots                                                                                              30
Frequency Table and Histograms                                                                               29
Density Estimates                                                                                                          33
Further reading                                                                                                             35
Exploring Binary and Categorical Data                                                                        35
Mode                                                                                                                               37
Expected Value                                                                                                              37
Further Reading                                                                                                            38

iii

Correlation                                                                                                                        38
Scatterplots                                                                                                                     41
Exploring Two or More Variables                                                                                  42
Hexagonal Binning and Contours (plotting numeric vs. numeric)                      43
Two Categorical Variables                                                                                           45
Categorical and Numeric Data                                                                                    46
Visualizing Multiple Variables                                                                                    48
Further Reading                                                                                                            50
Conclusion                                                                                                                        50

2. Data and Sampling Distributions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 

 51
Random sampling and sample bias                                                                               53
Bias                                                                                                                                  54
Random Selection                                                                                                         55
Size Versus Quality                                                                                                       57
Sample Mean Versus Population Mean                                                                     57
Further Reading                                                                                                            58
Selection bias                                                                                                                     58
Regression to the mean                                                                                                60
Further Reading                                                                                                            62
Sampling Distribution of a Statistic                                                                               62
Central Limit Theorem                                                                                                65
Standard error                                                                                                               65
Further Reading                                                                                                            66
The bootstrap                                                                                                                    67
Resampling versus bootstrapping                                                                               70
Further Reading                                                                                                            70
Confidence intervals                                                                                                        71
Further reading                                                                                                             73
Normal distribution                                                                                                         74
Standard Normal and QQ-Plots                                                                                 76
Long-Tailed Distributions                                                                                               77
Further Reading                                                                                                            79
Student’s t distribution                                                                                                     79
Further Reading                                                                                                            82
Binomial distribution                                                                                                       82
Further Reading                                                                                                            84
Poisson and Related Distributions                                                                                 85
Poisson Distributions                                                                                                   85
Exponential distribution                                                                                              86
Inference                                                                                                                        86
Weibull distribution                                                                                                     87

iv 

| 

Table of Contents

Further reading                                                                                                             87
Summary                                                                                                                           88

Table of Contents 

| 

v

Preface

What to Expect

Key Terms
Data science is a fusion of multiple disciplines, including statistics, computer science,
information  technology  and  domain  specific  fields.  As  a  result,  a  several  different
terms could be used to reference a given concept. Key terms and their synonyms will
be highlighted throughout the book in a sidebar such as this.

Conventions Used in This Book
The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program ele‐
ments  such  as  variable  or  function  names,  databases,  data  types,  environment
variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.

vii

This element signifies a tip or suggestion.

This element signifies a general note.

This element indicates a warning or caution.

Using Code Examples
Supplemental  material  (code  examples,  exercises,  etc.)  is  available  for  download  at
https://github.com/oreillymedia/title_title.

This book is here to help you get your job done. In general, if example code is offered
with  this  book,  you  may  use  it  in  your  programs  and  documentation.  You  do  not
need to contact us for permission unless you’re reproducing a significant portion of
the code. For example, writing a program that uses several chunks of code from this
book  does  not  require  permission.  Selling  or  distributing  a  CD-ROM  of  examples
from  O’Reilly  books  does  require  permission.  Answering  a  question  by  citing  this
book and quoting example code does not require permission. Incorporating a signifi‐
cant amount of example code from this book into your product’s documentation does
require permission.

We  appreciate,  but  do  not  require,  attribution.  An  attribution  usually  includes  the
title,  author,  publisher,  and  ISBN.  For  example:  “Book  Title  by  Some  Author
(O’Reilly). Copyright 2012 Some Copyright Holder, 978-0-596-xxxx-x.”

If  you  feel  your  use  of  code  examples  falls  outside  fair  use  or  the  permission  given
above, feel free to contact us at permissions@oreilly.com.

Safari® Books Online

Safari Books Online is an on-demand digital library that deliv‐
ers  expert  content  in  both  book  and  video  form  from  the
world’s leading authors in technology and business.

viii 

|  Preface

Technology professionals, software developers, web designers, and business and crea‐
tive  professionals  use  Safari  Books  Online  as  their  primary  resource  for  research,
problem solving, learning, and certification training.

Safari  Books  Online  offers  a  range  of  plans  and  pricing  for  enterprise,  government,
education, and individuals.

Members  have  access  to  thousands  of  books,  training  videos,  and  prepublication
manuscripts  in  one  fully  searchable  database  from  publishers  like  O’Reilly  Media,
Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que,
Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kauf‐
mann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders,
McGraw-Hill,  Jones  &  Bartlett,  Course  Technology,  and  hundreds  more.  For  more
information about Safari Books Online, please visit us online.

How to Contact Us
Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional
information.  You  can  access  this  page  at  http://www.oreilly.com/catalog/<catalog
page>.

To  comment  or  ask  technical  questions  about  this  book,  send  email  to  bookques‐
tions@oreilly.com.

For more information about our books, courses, conferences, and news, see our web‐
site at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Preface 

| 

ix

CHAPTER 1
Exploratory Data Analysis

As a discipline, statistics has mostly developed in the past century. Probability theory
— the  mathematical  foundation  for  statistics — was  developed  in  the  17th  to  19th
centuries based on work by Thomas Bayes, Pierre-Simon Laplace and Carl Gauss. In
contrast to the purely theoretical nature of probability, statistics is an applied science
concerned with analysis and modeling of data. Modern statistics as a rigorous scien‐
tific  discipline  traces  its  roots  back  to  the  late  1800’s  and  Francis  Galton  and  Karl
Pearson. R. A. Fischer, in the early 20th century, was a leading pioneer of modern sta‐
tistics, introducing key ideas of experimental design and maximum likelihood estima‐
tion.  These  and  many  other  statistical  concepts  live  largely  in  the  recesses  of  data
science.  The  main  goal  of  this  book  is  to  help  illuminate  these  concepts  and  clarify
their importance — or lack thereof — in the context of data science and big data.

11

Figure 1-1. John Tukey, a preeminent statistician, whose ideas developed over fifty years
ago form the foundation of data science.

This chapter focuses on the first step in any data science project: exploring the data.
Exploratory data analysis, or EDA, is a comparatively new area of statistics. Classical
statistics focused almost exclusively on inference, a sometimes complex set of proce‐
dures  for  drawing  conclusons  about  large  populations  based  on  small  samples.  In
1962, John W. Tukey called for a reformation of statistics in his seminal paper “The
Future  of  Data  Analysis”  ???.  He  proposed  a  new  scientific  discipline  called  “Data
Analysis” that included statistical inference as just one component. Tukey forged links
to  the  engineering  and  computer  science  communities  (he  coined  the  terms  “bit,”
short for binary digit, and “software”), and his original tenets are suprisingly durable
and form part of the foundation for data science. The field of exploratory data analy‐
sis  was  established  with  Tukey’s  1977  now  classic  book  “Exploratory  Data  Analy‐
sis” ???.

With  the  ready  availablility  of  computing  power  and  expressive  data  analysis  soft‐
ware, exploratory data analysis has evolved well beyond its original scope. Key drivers
of this discipline have been the rapid development of new technology, access to more
and bigger data, and the greater use of quantitative analysis in a variety of disciplines.

12 

| 

Chapter 1: Exploratory Data Analysis

David Donoho, professor of Statistics at Stanford University and undergraduate stu‐
dent  of  Tukey,  authored  an  excellent  article  based  on  his  presentation  at  the  Tukey
Centennial workshop in Princeton, NJ ???. Donoho traces the genesis of data science
back to the Tukey’s pioneering work in data analysis.

Elements of Structured Data
Data comes from many sources: sensor measurements, events, text, images, and vid‐
eos. The Internet of Things is spewing out streams of information. Much of this data is
unstructured: images are a collection of pixels with each pixel containing RGB color
information. Texts are sequences of words and non-word characters, often organized
by sections, sub-sections, etc.. Click streams are sequences of actions by a user inter‐
acting with an app or web page. In fact, a major challenge of data science is to harness
this torrent of raw data into actionable information. To apply the statistical concepts
covered in this book, unstructured raw data must be processed and manipulated into
a structured form, as it might emerge from a relational database, or be collected for a
study.

Key Terms for Data Types

Continuous

Data that can take on any value in an interval.

Synonyms

interval, float, numeric

Discrete

Data that can only take on integer values, such as counts.

Synonyms

integer, count

Categorical

Data that can only take on a specific set of values.

Synonyms

enums, enumerated, factors, nominal, polychotomous

Binary

A special case of categorical with just two categories (0/1, True, False).

Synonyms

dichotomous, logical, indicator

Ordinal

Categorical data that has an explicit ordering.

Elements of Structured Data 

| 

13

Synonyms

ordered factor

There are two basic types of structured data: numeric and categorical. Numeric data
comes  in  two  forms:  continuous,  such  as  wind  speed  or  time  duration,  and  discrete,
such as the count of the occurence of an event. Categorical data takes only a fixed set
of values, such as a type of TV screen (plasma, LCD, LED, …) or a state name (Ala‐
bama,  Alaska,  …).  Binary  data  is  an  important  special  case  of  categorical  data  that
takes on only one of two values, such as 0/1, yes/no or true/false. Another useful type
of categorical data is ordinal data in which the categories are ordered; an example of
this is a numerical rating (1, 2, 3, 4, or 5).

Why do we bother with a taxonomy of data types? It turns out that for the purposes
of data analysis and predictive modeling, the data type is important to help determine
the type of visual display, data analysis or statistical model. In fact, data science soft‐
ware, such as R and Python, use these data types to improve computational perfor‐
mance.  More  important,  the  data  type  for  a  variable  determines  how  software  will
handle computations for that variable.

Software  engineers  and  database  programmers  may  puzzle  why  we  even  need  the
notion of categorical and ordinal data for analytics. After all, categories are merely a
collection of text (or numeric) values, and the underlying database automatically han‐
dles the internal representation. However, explicit identification of data as categorical,
as distinct from text, does offer some advantages.

1. Knowing that data is categorical can act as a signal to tell software how statistical
procedures, such as producing a chart or fitting a model, should behave. In par‐
ticular, ordinal data can be represented as an ordered.factor in R and python,
preserving a user-specified ordering in charts, tables and models.

2. Storage and indexing can be optimized (as in relational database).

3. The possible values a given categorical variable can take are enforced in the soft‐

ware (like an enum).

The  third  “benefit”  can  lead  to  unintended  or  unexpected  behavior:  the  default
behavior of data import functions in R (e.g., read.csv) is to automatically convert a
text  column  into  a  factor.  Subsequent  operations  on  that  column  will  assume  that
only  the  allowable  values  for  that  column  are  the  ones  originally  imported,  and

14 

| 

Chapter 1: Exploratory Data Analysis

assigning  a  new  text  value  will  introduce  a  warning  and  produce  an  NA  (missing
value).

Key Ideas

1. Data are typically classified in software by their type

2. Data types include continuous, discrete, categorical (which includes binary), and

ordinal

3. Data-typing  in  software  acts  as  a  signal  to  the  software  on  how  to  process  the

data

Further Reading

1. Data types can be confusing, since types may overlap, and the taxonomy in one
software may differ from that in another. Here is a tutorial for the taxonomy for
R: http://www.r-tutor.com/r-introduction/basic-data-types

2. Databases  are  more  detailed  in  their  classification  of  data  types,  incorporating
considerations  of  precision  levels,  fixed  or  variable  length  fields,  etc.;  see  this
guide for SQL: http://www.w3schools.com/sql/sql_datatypes_general.asp

Rectangular Data
The  typical  frame  of  reference  for  an  analysis  in  data  science  is  a  rectangular  data
object, like a spreadsheet or database table.

Key Terms for Rectangular Data

Data frame

Rectangular data (like a spreadsheet) is the basic data structure for statistical and
machine learning models

Feature

A column in the table is commonly refered to as a feature.

Synonyms

attribute, input, predictor, variable

Outcome

Many  data  science  projects  involve  predicting  an  outcome  -  often  a  yes/no  out‐
come (in Table 1-1, it is “auction was competitive or not”). The features are some‐
times used to predict the outcome in an experiment or study.

Rectangular Data 

| 

15

Synonyms

dependent variable, response, target, output

Records

A row in the table is commonly referred to as a record.

Synonyms

case, example, instance, observation, pattern, sample

Retangular  data  is  essentially  a  2-dimensional  matrix  with  rows  indicating  records
(cases) and columns indicating features (variables). The data don’t always start in this
form: unstructured data (e.g. text) must be processed and manipulated so that it can
be represented as a set of features in the rectangular data (see “Elements of Structured
Data”  on  page  13).  Data  that  are  in  relational  databases  must  be  extracted  and  put
into a single table for most data analysis and modeling tasks.

In Table 1-1 there is a mix of measured or counted data (e.g. duration and price), and
categorical data (e.g. category and currency). A special form of categorical variable is
a  binary  (yes/no  or  0/1)  variable,  seen  in  the  right-most  column  in  Table  1-1 — an
indicator variable showing whether an auction was competitive or not.

Table 1-1. A Typical Data Format

Category

currency

sellerRating Duration endDay

ClosePrice OpenPrice

Competitive?

Music/Movie/Game US

Music/Movie/Game US

Automotive

Automotive

Automotive

Automotive

Automotive

Automotive

US

US

US

US

US

US

3249

3249

3115

3115

3115

3115

3115

3115

5

5

7

7

7

7

7

7

Mon

0.01

Mon

0.01

Tue

Tue

Tue

Tue

Tue

Tue

0.01

0.01

0.01

0.01

0.01

0.01

0.01

0.01

0.01

0.01

0.01

0.01

0.01

0.01

0

0

0

0

0

0

1

1

Data Frames and Indexes
Traditional  database  tables  will  have  one  or  more  columns  designated  as  an  index.
This  can  vastly  improve  the  efficiency  of  certain  SQL  queries.  In  Python,  with  the
pandas library, the basic rectangular data structure is a DataFrame object. By default,

16 

| 

Chapter 1: Exploratory Data Analysis

an automatic integer index is created for a DataFrame based on the order of the rows.
In  pandas,  it  is  also  possible  to  set  multi-level/hierarchical  indexes  to  improve  the
efficiency of certain operations.

In R, the basic rectangular data structure is a data.frame object. A data.frame also
has an implicit integer index based on the row order. While a custom key can be cre‐
ated through the row.names attribute, the native R data.frame does not support user-
specified  or  multi-level  indexes.  To  overcome  this  deficiency,  two  new  packages  are
gaining widespread use: data.table and dplyr. Both support multi-level indexes and
offer significant speed-ups in working with a data.frame.

Terminology Differences

Terminology  for  rectangular  data  can  be  confusing.  Statisticians
and data scientists use different terms for the same thing. For a sta‐
tistician,  predictor  variables  are  used  in  a  model  to  predict  a
response or dependent variable. For a data scientist, features are used
to  predict  a  target.  One  synonym  is  particularly  confusing:  com‐
puter scientists will use the term sample for a single row; a sample
to a statistian means a collection of rows

Graph Data
In  additional  to  rectangular  data,  another  important  data  structure  is  graph  or  net‐
work.  These  are  used  to  represent  physical,  social  and  abstract  relationships.  For
example, a graph of a social network, such as Facebook or LinkedIn, may represent
connections  between  people  on  the  network.  Distribution  hubs  connected  by  roads
are an example of a physical network. Graph structures are useful for certain types of
problems, such network optimization and recommender systems (see ???). Nonethe‐
less, the vast majority of applications in data science are based on the rectangular data
structure.

Rectangular Data 

| 

17

Graphs in Statistics

In  computer  science  and  information  technology,  the  term  graph
typically  refers  to  a  depiction  of  the  connections  among  entities,
and to the underlying data structure. In statistics, graph is used to
refer to a variety of plots and visualizations, not just of connections
among entities, and the term applies just to the visualization, not to
the data structure.

Key Ideas

1. The basic data structure in data science is a rectangular matrix in which rows are

records and columns are variables (features).

2. Terminology can be confusing; there are a variety of synonyms arising from the
different disciplines that contribute to data science (statistics, computer science,
information technology).

Further Reading

1. Documentation  on  data  frames  in  R:  https://stat.ethz.ch/R-manual/R-devel/

library/base/html/data.frame.html

2. Documentation on data frames in Python: http://pandas.pydata.org/pandas-docs/

stable/dsintro.html#dataframe

Estimates of Location
Variables  with  measured  or  count  data  might  have  thousands  of  distinct  values.  A
basic step in exploring your data is getting a “typical value” for each feature (variable):
an estimate of where most of the data are located (i.e. their central tendency).

Key Terms for Estimates of Location

Mean

The sum of all values divided by the number of values.

Synonyms

average

Weighted Mean

The sum of all values times a weight divided by the sum of the weights.

18 

| 

Chapter 1: Exploratory Data Analysis

Synonyms

weighted average

Median

The value such that one-half of the data lies above and below.

Synonyms

50th percentile

Weighted Median

The value such that one-half of the sum of the weights lies above and below the
sorted data.

Trimmed Mean

The average of all values after dropping a fixed number of extreme values.

Synonyms

truncated mean

Robust

Not sensitive to extreme values.

Synonyms

resistant

Outlier

A data value that is very different from most of the data.

Synonyms

extreme value

At first glance, summarizing data might seem fairly trivial: just take the mean of the
data (see “Mean” on page 20). In fact, while the mean is easy to compute and expedi‐
ent to use, it may not always be the best measure for a central value. For this reason,
statisticians have developed and promoted several alternative estimates to the mean.

Metrics and Estimates

Statisticians often use the term estimates for values calculated from
the data at hand, to draw a distinction between what we see from
the data, and the theoretical true or exact state of affairs. Data sci‐
entists and business analysts are more likely to refer to such values
as a metric. The difference reflects the approach of statistics versus
data science: Accounting for uncertainty lies at the heart of the dis‐
cipline  of  statistics,  whereas  concrete  business  or  organizational
objectives  are  the  focus  of  data  science.  Hence,  statisticians  esti‐
mate, and data scientists measure.

Estimates of Location 

| 

19

Mean
The  most  basic  estimate  of  location  is  the  mean,  or  average  value.  The  mean  is  the
sum of all the values divided by the number of values. Consider the following set of
numbers: {3 5 1 2}. The mean is (3+5+1+2)/4= 11/4 = 2.75. You will encounter the
symbol x to represent the mean of a sample from a population (pronounced x-bar).
The formula to compute the mean for a set of N values x1, x2, ..., xN is

Mean = x =

N xi
∑i
N

A variation of the mean is a trimmed mean, calculated by dropping a fixed number of
sorted  values  at  each  end  and  then  take  an  average  of  the  remaining  values.  Repre‐
senting the sorted by x 1 , x 2 , ..., x N  where x 1  is the smallest value and x N , the for‐
mula to compute the trimmed mean with p smallest and largest values omitted is

Trimmed Mean = x =

N − p
∑i = p + 1
N − 2p

x i

A trimmed mean eliminates the influence of extreme values. For example, scoring for
international diving is obtained dropping the top and bottom score from five judges
and taking the average of the three remaining judges ???> This makes it difficult for a
single  judge  to  manipulate  the  score,  perhaps  to  favor  their  country’s  contestant.
Trimmed means are widely used, and in many cases, are preferable to use instead of
the ordinary mean: see “Median and Robust Estimates” on page 21 for further discus‐
sion.

Another type of mean is a weighted mean, calculated by multiplying each data value xi
by a weight wi and dividing their sum by the sum of the weights. The formula for a
weighted mean is

Weighted Mean = xw =

N wixi
∑i = 1
N wi
∑i

There are two main motivations for using a weighted mean:

1. Some  values  are  intrinsically  more  variable  than  others,  and  highly  variable
observations are given a lower weight. For example, if we are taking the average
from  multiple  sensors  and  one  of  the  sensors  is  less  accurate,  then  we  might
downweight the data from that sensor.

20 

| 

Chapter 1: Exploratory Data Analysis

2. The  data  collected  does  not  equally  represent  the  different  groups  that  we  are
interested in measure. For example, because of the way an online experiment was
conducted, we may not have a set of data that accurately reflects all groups in the
user  base.  To  correct  that,  we  can  give  a  higher  weight  to  the  values  from  the
groups that were underrepresented.

Median and Robust Estimates
The median is the middle number on a sorted list of the data. If there is an even num‐
ber  of  data  values,  the  middle  value  is  one  that  is  not  actually  in  the  data  set,  but
rather is the average of the two values that divide the sorted data into upper and lower
halves. Compared to the mean, which uses all observations, the median only depends
on the values in the center of the sorted data. While this might seem to be a disadvan‐
tage, since the mean is much more sensitive to the data, there are many instances in
which the median is a better metric for location. Let’s say we want to look at typical
household  incomes  in  neighborhoods  around  Lake  Washington  in  Seattle.  In  com‐
paring the Medina neighborhood to the Windermere neighborhood, using the mean
would produce very different results because Bill Gates lives in Medina. If we use the
median, it won’t matter how rich Bill Gates is — the position of the middle observa‐
tion will remain the same.

For the same reasons that one uses a weighted mean, it is also possible to compute a
weighted median. As with the median, we first sort the data, although each data value
has an associated weight. Instead of taking the middle number, the weighted median
is the value such that sum of weights is equal for the lower and upper halves of the
sorted list. Like the median, the weighted median is robust to outliers.

Outliers

The median is referred to as a robust estimate of location since it is not influenced by
outliers (extreme cases) that could skew the results. An outlier is any value that is very
distant from the other values in a dataset. The exact definition of an outlier is some‐
what  subjective,  although  certain  conventions  are  used  in  various  data  summaries
and plots (see “Percentiles and Boxplots” on page 30). Being an outlier in itself does
not make a data value invalid or erroneous (as in the example above with Bill Gates).
Still, outliers are often the result of data errors such as mixing data of different units
(kilometers  versus  meters)  or  bad  readings  from  a  sensor.  When  outliers  are  the
result  of  bad  data,  the  mean  will  result  in  a  poor  estimate  of  location  while  the
median will be still be valid. In any case, outliers should be identified and are often
worthy of further investigation.

Estimates of Location 

| 

21

Anomaly Detection

In  contrast  to  typical  data  analysis,  where  outliers  are  sometimes
informative  and  sometimes  a  nuisance,  in  anomaly  detection  the
points  of  interest  are  the  outliers,  and  the  greater  mass  of  data
serves  primarily  to  define  the  “normal”  against  which  anomalies
are measured.

The  median  is  not  the  only  robust  estimate  of  location.  In  fact,  a  trimmed  mean  is
widely used to avoid the influence of outliers. For example, trimming the bottom and
top 10% (a common choice) of the data will provide protection against outliers in all
but  the  smallest  data  sets.  The  trimmed  mean  can  be  thought  of  as  a  compromise
between the median and the mean: it is robust to extreme values in the data, but uses
more data to calculate the estimate for location.

Other Robust Metrics for Location

Statisticians have developed a plethora of other estimators for loca‐
tion,  primarily  with  the  goal  of  developing  an  estimator  more
robust than the mean but more efficient (i.e. better able to discern
small location differences between datasets). While these methods
are potentially useful for small data sets, they are not likely to pro‐
vide added benefit for large or even moderately sized data sets.

Example: Location Estimates of Population and Murder Rates
Table 1-2 shows the first few rows in the data set containing population and murder
rates (in units of murders per 100,000 people per year) for each state. Compute the
mean, trimmed mean and median for the population using R:

> state <- read.csv(file="/Users/andrewbruce1/book/state.csv")
> mean(state[["Population"]])
[1] 6162876
> mean(state[["Population"]], trim=0.1)
[1] 4783697
> median(state[["Population"]])
[1] 4436370

Table 1-2. A few rows of the data.frame state of population and murder rate by state.

State

Population Murder Rate

1 Alabama

4,779,736

5.7

2 Alaska

710,231

5.6

3 Arizona

6,392,017

4.7

22 

| 

Chapter 1: Exploratory Data Analysis

State

Population Murder Rate

4 Arkansas

2,915,918

5.6

5

6

7

California

37,253,956

4.4

Colorado

5,029,196

2.8

Connecticut

3,574,097

2.4

8 Delaware

897,934

5.8

The mean is bigger than the trimmed mean which is bigger than the median. This is
because the trimmed mean excludes the largest and smallest 5 states (trim=0.1 drops
10% from each end). If we want to compute the average murder rate for the country,
we need to use a weighted mean or median to account for different populations in the
states.  Since  base  R  doesn’t  have  a  function  for  weighted  median,  it  is  necessary  to
install a package such as matrixStats

> weighted.mean(state[["Murder.Rate"]], w=state[["Population"]])
[1] 4.445834
> library("matrixStats")
> weightedMedian(state[["Murder.Rate"]], w=state[["Population"]])
[1] 4.4

In this case, the weighted mean and median are about the same.

Key Ideas

1. The basic metric for location is the mean, but it can be sensitive to extreme val‐

ues (outlier)

2. Other metrics (median, trimmed mean) are more robust

Further Reading

1. Michael Levine (Purdue) has posted some useful slides on basic calculations for
location:  http://www.stat.purdue.edu/~mlevins/STAT511_2012/

measures  of 
Lecture2standard.pdf

2. John Tukey’s 1977 classic Exploratory Data Analysis (Pearson) is still widely read.

Estimates of Location 

| 

23

Estimates of Variability
Location is just one dimension in summarizing a feature. A second dimension, varia‐
bility, also referred to as dispersion, measures whether the data values are tightly clus‐
tered or spread out. At the heart of statistics lies variability: measuring it, reducing it,
distinguishing  random  from  real  variability,  identifying  the  various  sources  of  real
variability and making decisions in the presence of it.

Key Terms for Variability Metrics

Deviations

The difference between the observed values and the estimate of location.

Synonyms

errors, residuals.

Variance

The  sum  of  squared  deviations  from  the  mean  divided  by  N-1  where  N  is  the
number of data values.

Synonyms

mean-squared-error.

Standard Deviation

The square root of the variance.

Synonyms

l2-norm, Euclidean norm

Mean Absolute Deviation

The mean of the absolute value of the deviations from the mean.

Synonyms

l1-norm, Manhattan norm

Median Absolute Deviation from the Median

The median of the absolute value of the deviations from the median.

Range

The difference between the largest and the smallest value in a data set.

Order Statistics

Metrics based on the data values sorted from smallest to biggest.

Synonyms
ranks

24 

| 

Chapter 1: Exploratory Data Analysis

Percentile

The value such that P percent of the values take on this value or less and (100-P)
percent take on this value or more.

Synonyms

quantile

Interquartile Range

The difference between the 75th percentile and the 25th percentile

Synonyms
IQR

Just as there are different ways to measure location (mean, median, …) there are also
different ways to measure variability.

Standard Deviation and Related Estimates
The most widely used estimates of variation are based on the the differences, or devi‐
ations, between the estimate of location and the observed data. For a set of data {1, 4,
4}, the mean is 3 and the median is 4. The deviations from the mean are the differ‐
ences: 1 - 3 = -2, 4 - 3 = 1 , 4 - 3 = 1. These deviations tell us how dispersed the data is
around the central value.

One  way  to  measure  variability  is  to  estimate  a  typical  value  for  these  deviations.
Averaging the deviations themselves would not tell us much - the negative deviations
offset the positive ones. In fact, the sum of the deviations from the mean is precisely
zero.  Instead,  a  simple  approach  is  to  take  the  average  of  the  absolute  values  of  the
deviations from the mean. In the above example, the absolute value of the deviations
is {2 1 1} and their average is (2+1+1)/3 = 1.33. This is known as the mean absolute
deviation and is computed using the formula

Mean Absolution Deviation =

N
∑i = 1

xi − x
N

where x is the sample mean.

The best known estimates for variability are the variance and the standard deviation
which  are  based  on  squared  deviations.  The  variance  is  an  average  of  the  squared
deviations and the standard deviation is the square root of the variance.

Variance = s2 =

∑ x − x 2
N − 1

Standard Deviation = s = Variance

Estimates of Variability 

| 

25

The standard deviation is much easier to interpret than the variance since it is on the
same scale as the original data. Still, with its more complicated and less intuitive for‐
mula, it might seem peculiar that the standard deviation is preferred in statistics over
the mean absolute deviation. Its owes its preeminence to statistical theory: mathemat‐
ically,  it  turns  out  that  working  with  squared  values  is  much  more  convenient  than
absolute values, especially for statistical models (see ???).

Degrees of Freedom, and N or N-1?

In statistics books, there is always some discussion of why we have
N-1 in the denominator in the above formula, instead of N, leading
into  the  concept  of  degrees  of  freedom.  This  distinction  is  not
important since N is generally large enough so it won’t make much
difference  whether  you  divide  by  N  or  N-1.  But,  in  case  you  are
interested, here is the story.

If you use the intuitive denominator of N in the above formula, you
will  underestimate  the  true  value  of  the  standard  deviation  in  the
population. This is refered to as a biased estimate. However, if you
divide  by  N-1  instead  of  N,  the  standard  deviation  becomes  an
unbiased estimate.

To fully explain why using N leads to a biased estimate involves the
notion of degrees of freedom, which takes into account the number
of constraints in computing an estimate. In this case, there are N-1
degrees of freedom since there is one constraint: the standard devi‐
ation depends on calculating the sample mean. For many problems,
data scientists do not need to worry about degrees of freedom, but
there are cases where the concept is important (see ???).

Neither  the  variance,  the  standard  deviation  nor  the  mean  absolute  deviation  are
robust to outliers and extreme values (see “Median and Robust Estimates” on page 21
for  a  discussion  of  robust  estimates  for  location).  The  variance  and  standard  devia‐
tion are especially sensitive to outliers since they are based on the squared deviations.

An  robust  estimate  of  variability  is  the  median  absolute  deviation  from  the  median,
sometimes denoted by MAD:

Median Absolution Deviation = Median x1 − m , x2 − m , ..., xN − m

where m is the median. Like the median, the MAD is not influenced by extreme val‐
ues.  It  is  also  possible  to  compute  a  trimmed  standard  deviation  analogous  to  the
trimmed mean (see ???).

26 

| 

Chapter 1: Exploratory Data Analysis

The variance, the standard deviation, mean absolute deviation and
median absolute deviation from the median are not equivalent esti‐
mates, even in the case where the data comes from a normal distri‐
bution.  In  fact,  the  standard  deviation  is  always  greater  than  the
mean  absolute  deviation  which  itself  is  greater  than  the  median
absolute  deviation.  Sometimes,  the  median  absolute  deviation  is
multiplied by a factor of 1.4826: this puts MAD on the same scale
as the standard deviation in the case of a normal distribution.

Estimates Based on Percentiles
A different approach to estimating dispersion is based on looking at the spread of the
sorted data. Statistics based on sorted (ranked) data are refered to as order statistics.
The most basic measure is the range: the difference between the largest and smallest
number.  The  minimum  and  maximum  values  themselves  are  useful  to  know,  and
helpful in identifying outliers, but the range is extremely sensitive to outliers and not
very useful as a general measure of dispersion in the data.

To avoid the sensitivity to outliers, we can look at the range of the data after dropping
values  from  each  end.  Formally,  these  types  of  estimates  are  based  on  differences
between percentiles. In a dataset, the P-th percentile is a value such that at least P per‐
cent of the values take on this value or less and at least (100-P) percent of the values
take  on  this  value  or  more.  For  example,  to  find  the  80th  percentile,  sort  the  data.
Then,  starting  with  the  smallest  value,  proceed  80  percent  of  the  way  to  the  largest
value. Note that the median is the same thing as the 50th percentile. The percentile is
essentially the same as a quantile, with quantiles indexed by fractions (so the .8 quan‐
tile is the same as the 80th percentile).

A common measurement of variability is the difference between the 25th percentile
and the 75th percentile, called the interquartile range (or IQR). Here is a simple exam‐
ple:  3,1,5,3,6,7,2,9.  We  sort  these  to  get  1,2,3,3,5,6,7,9.  The  25th  percentile  is  at  2.5,
and the 75th percentile is at 6.5, so the interquartile range is 6.5 - 2.5 = 4. Software
can  have  slightly  differing  approaches  that  yield  different  answers  (see  the  note
below.) Typically these differences are smaller.

For  very  large  datasets,  calculating  exact  percentiles  can  be  computationally  very
expensive since it requires sorting all the data values. Machine learning and statistical
software  use  special  algorithms,  such  as  ???  to  calculate  an  approximate  percentile
that can be calculated very quickly and is guaranteed to have a certain accuracy.

Estimates of Variability 

| 

27

Percentile: Precise Definition

If we have an even number of data (N is even), then the percentile
is ambiguous under the above definition. In fact, we could take on
any value between the order statistics x j  and x j + 1  where j satis‐
fies

100 *

j
N

≤ P < 100 *

j + 1
N

Formally, the percentile is the weighted average

Percentile P = 1 − w x j + wx j + 1

for some weight w between 0 and 1. Statistical software has slightly
differing approaches to choose w In fact, the R function quantile
offers nine different alternatives to compute the quantile: see ??? for
a full discussion. Except for small datasets, you don’t usually need
to worry about the precise way a percentile is calculated.

Example: Variability Estimates of State Population
Table 1-2 shows the first few rows in the data set containing population and murder
rates for each state. Using R’s built-in functions for the standard deviation, interquar‐
tile  range  (IQR)  and  the  median  absolution  deviation  from  the  median  (MAD),  we
can compute estimates of variability for the state population data:

> sd(state[["Population"]])
[1] 6848235
> IQR(state[["Population"]])
[1] 4847308
> mad(state[["Population"]])
[1] 3849870

The standard deviation is almost twice as large as the MAD (in R, by default, the scale
of the MAD is adjusted to be on the same scale as the mean) This is not surprising
since the standard deviation is sensitive to outliers.

Key Ideas

1. The  variance  and  standard  deviation  are  the  most  widespread  and  routinely

reported statistics of variabilit

2. Both are sensitive to outliers

28 

| 

Chapter 1: Exploratory Data Analysis

3. More  robust  metrics  include  mean  and  median  absolute  deviations  from  the

mean, and percentiles (quantiles)

Further Reading

1. David  Lane’s  online  statistics  resource  has  a  section  on  percentiles  here:  http://

onlinestatbook.com/2/introduction/percentiles.html

2. Kevin  Davenport  has  a  useful  post  on  deviations  from  the  median,  and  their
robust  properties  in  R-Bloggers:  http://www.r-bloggers.com/absolute-deviation-
around-the-median/

Exploring the Data Distribution
Each  of  the  estimates  described  above  sums  up  the  data  in  a  single  number  to
describe the location or variability of the data. It is also useful to explore how the data
are distributed overall.

Key Terms for Exploring the Distribution

Boxplot

A plot introduced by Tukey as a quick way to visualize the distribution of data.

Synonyms

Box and whiskers plot

Frequency Table

A tally of the count of numeric data values that fall into a set of intervals (bins).

Histogram

A plot of the frequency table with the bins on the x-axis and the count (or pro‐
portion) on the y-axis.

Density Plot

A smoothed version of the histogram, often based on a kernal density estimate.

Exploring the Data Distribution 

| 

29

Statistical Moments

In  statistical  theory,  location  and  variability  are  referred  to  as  the
first  and  second  moments  of  a  distribution.  The  third  and  fourth
moments  are  called  skewness  and  kurtosis.  Skewness  refers  to
whether  the  data  is  skewed  to  larger  or  small  values  and  kurtosis
indicates the propensity of the data to have extreme values. Gener‐
ally,  metrics  are  not  used  to  measure  skewness  and  kurtosis;
instead,  these  are  discovered  using  the  visual  displays  such  as
Figure  1-2  and  Figure  1-3.  See  ???  for  formulas  and  more  details
about skewness and kurtosis.

Percentiles and Boxplots
In “Estimates Based on Percentiles” on page 27, we explored how percentiles can be
used to measure the spread of the data. Percentiles are also valuable to summarize the
entire distribution. It is common to report the quartiles (25th, 50th and 75th percen‐
tiles) and the deciles (the 10th, 20th, …, 90th percentiles). Percentiles are especially
valuable to summarize the tails (the outer range) of the distribution. Popular culture
has coined the term one-percenters to refer to the people in the top 99th percentile of
wealth.

Table 1-3. Percentiles of murder rate by state.

5% 25% 50% 75% 95%

1.60

2.42

4.00

5.55

6.51

Table  1-3  displays  some  percentiles  of  the  murder  rate  by  state.  In  R,  this  would  be
produced using the quantile function:

quantile(state[["Murder.Rate"]], p=c(.05, .25, .5, .75, .95))
   5%   25%   50%   75%   95%
1.600 2.425 4.000 5.550 6.510

The median is 4 murders per 100,000 people although there is quite a bit of variabil‐
ity: the 5th percentile is only 1.6 and the 95th percentile is 6.51

30 

| 

Chapter 1: Exploratory Data Analysis

Figure 1-2. Boxplot of State Populations

Boxplots, introduced by Tukey ???, are based on percentiles and give a quick way to
visualize  the  distribution  of  data.  Figure  1-2  shows  a  boxplot  of  the  population  by
state produced by R:

boxplot(state[["Population"]]/1000000, ylab="Population (millions)")

The  top  and  bottom  of  the  box  are  the  75th  and  25th  percentiles,  respectively.  The
median  is  shown  by  the  horizontal  line  in  the  box.  The  dashed  lines,  referred  to  as
whiskers,  extend  from  the  top  and  bottom  to  indicate  the  range  for  the  bulk  of  the
data. There are many variations of a boxplot: see, for example, the documentation for
the R function boxplot ???. By default, the R function extends the whiskers to the fur‐
thest point beyond the box, except that it will not go beyond 1.5 times the IQR. Other
software may use a different rule. Any data outside of the whiskers are plotted as sin‐
gle points.

Frequency Table and Histograms
A frequency table of a variable divides up the variable range into equally spaced seg‐
ments,  and  tells  us  how  many  values  fall  in  each  segment.  Table  1-4  shows  a  fre‐
quency table of the population by state computed using R:

breaks <- seq(from=min(state[["Population"]]),
                to=max(state[["Population"]]), length=11)
pop_freq <- cut(state[["Population"]], breaks=breaks,

Exploring the Data Distribution 

| 

31

                right=TRUE, include.lowest = TRUE)
table(pop_freq)

Table 1-4. A frequency table of population by state.

BinNumber BinRange

Count

States

1

2

3

4

5

6

7

8

9

563,626-4,232,658

4,232,659-7,901,691

7,901,692-11,570,724

11,570,725-15,239,757

15,239,758-18,908,790

18,908,791-22,577,823

22,577,824-26,246,856

26,246,857-29,915,889

29,915,890-33,584,922

10

33,584,923-37,253,956

24

14

6

2

1

1

1

0

0

1

WY,VT,ND,AK,SD,DE,MT,RI,NH,ME,HI,ID,NE,WV,NM,NV,UT,KS,AR,MS,IA,CT,OK,OR

KY,LA,SC,AL,CO,MN,WI,MD,MO,TN,AZ,IN,MA,WA

VA,NJ,NC,GA,MI,OH

PA,IL

FL

NY

TX

CA

The  least  populous  is  Wyoming,  with  563,626  people  (2010  Census)  and  the  most
populous is California, with 37,253,956 people. This gives us a range of 37,253,956 -
563,626 = 36,690,330 which we must divide up into equal size bins - let’s say 10 bins.
With 10 equal size bins, each bin will have a width of 3,669,033, so the first bin will
span  from  563,626  to  4,232,658.  By  contrast,  the  top  bin,  33,584,923  to  37,253,956,
has only one state - California. The two bins immediately below California are empty,
until we reach Texas. It is important to include the empty bins - the fact that there are
no values in those bins is useful information.

Both frequency tables and percentiles summarize the data by creat‐
ing bins. In general, quartiles and deciles will have the same count
in  each  bin  (equal-count  bins)  but  the  bin  sizes  will  be  different.
The  frequency  table,  by  contrast,  will  have  different  counts  in  the
bins (equal-size bins).

A histogram is a way to visualize a frequency table, with bins on the x-axis, and data
count  on  the  y-axis.  To  create  a  histogram  corresponding  to  the  frequency  table
Table 1-4 in R, use the hist function with the breaks argument:

32 

| 

Chapter 1: Exploratory Data Analysis

hist(state[["Population"]], breaks=breaks)

The histogram is shown in Figure 1-3 In general, histograms are plotted such that -
Empty  bins  are  included  in  the  graph  -  Bins  are  equal  width  -  Number  of  bins  (or,
equivalently, bin size) is up to the user - Bars are contiguous — no empty space shows
between bars, unless there is an empty bin.

Figure 1-3. Histogram of State Populations

Density Estimates
Related to the histogram is a density plot which shows the distribution of data values
as a continuous line. It can be thought of as a smoothed histogram, although it is typi‐
cally  computed  directly  from  the  data  using  a  kernal  density  estimate  (see  ???  for  a
short tutorial). Figure 1-4 displays a density estimate superposed on a histogram. In
R, a density estimate can be computed using the density function:

hist(state[["Murder.Rate"]], freq=FALSE)
lines(density(state[["Murder.Rate"]]), lwd=3, col="blue")

A key distinction from the histogram plotted in Figure 1-3 is the scale of the y-axis: a
density plot corresponds to plotting the histogram as a proportion rather than counts
(this is done in R using the argument freq=FALSE).

Exploring the Data Distribution 

| 

33

Figure 1-4. Density of State Murder Rates

Example 1-1. Density Estimation

Density estimate is a rich topic with a long history in statistical literature. In fact, over
twenty R packages have been published that offer functions for density estimation. ???
give a comprehesive review of R packages, with a particular recommendation for ASH
or KernSmooth. For many data science problems, there is no need to worry about the
various types of densities estimates and it suffices to use the base functions.

Key Ideas

1. A frequency histogram plots frequency counts on the y-axis and variable values

on the x-axis; it gives a sense of the distribution of the data at a glance.

2. A frequency table is a tabular version of the frequency counts in a histogram.

3. A boxplot, with the top and bottom of the box at the 75th and 25th percentiles,
respectively,  also  gives  a  quick  sense  of  the  distribution  of  the  data;  it  is  often
used in side-by-side displays to compare distributions.

4. A density plot is a smoothed version of a histogram; it requires a function to esti‐

mate a plot based on the data (multiple estimates are possible, of course).

34 

| 

Chapter 1: Exploratory Data Analysis

Further reading

1. A  step-by-step  guide  to  creating  a  boxplot  can  be  found  here:  http://

www.oswego.edu/~srp/stats/bp_con.htm

2. Density estimation in R is covered in Henry Deng and Hadley Wickham’s paper

http://vita.had.co.nz/papers/density-estimation.pdf

3. R-Bloggers  has  a  useful  post  on  histograms  in  R,  including  customization  ele‐
ments, such as binning (breaks): http://www.r-bloggers.com/basics-of-histograms/

4. A  similar  post  on  box-plots  in  R  can  be  found  here:  http://www.r-bloggers.com/

box-plot-with-r-tutorial/

Exploring Binary and Categorical Data
For categorical data, simple proportions or percentages tell the story of the data.

Key Terms for Exploring Categorical Data

Mode

The most commonly occurring category or value in a dataset.

Expected Value

When the categories can be associated with a numeric value, it gives an average
value based on the probability of occurence of a category.

Bar Charts

The frequency or proportion for each category plotted as bars.

Pie Charts

The frequency or proportion for each category plotted as wedges in a pie.

Getting  a  summary  of  a  binary  variable,  or  a  categorical  variable  with  a  few  cate‐
gories,  is  a  fairly  easy  matter  -  we  just  figure  out  the  proportion  of  1’s,  or  of  the
important categories. For example, Table 1-5 shows the percentage of delayed flights
by the cause of delay at Dallas/Fort Worth airport since 2010. Delays are categorized
as being due to factors under carrier control, air traffic control system delays (ATC),
weather, security or a late inbound aircraft.

Table 1-5. Percentage of delays by cause of delay at Dallas-Ft. Worth airport.

Carrier ATC

Weather

Security

Inbound

23.02

30.40

4.03

0.12

42.43

Exploring Binary and Categorical Data 

| 

35

Figure 1-5. Bar plot airline delays by airport.

Bar charts are a common visual tool for displaying a single categorical variable, often
seen in the popular press. Categories are listed on the x-axis, and frequencies or pro‐
portions on the y-axis. Figure 1-5 shows the airport delays per year by cause of delay
for Dallas/ Fort Worth, and is produced with the R function barplot

barplot(as.matrix(dfw)/6, cex.axis=.5)

Note that a bar chart resembles a histogram; in a bar chart the x-axis represents dif‐
ferent categories of a factor variable, while in a histogram the x axis represents values
of a single variable on a numeric scales. In a histogram, the bars are typically shown
touching each other, with gaps indicating values that did not occur in the data. In a
bar chart, the bars are shown separate from one another.

An alternative to bar charts are pie charts, although statisticians and data visualiza‐
tion experts generally eschew pie charts as less visually informative (see ???).

36 

| 

Chapter 1: Exploratory Data Analysis

Numerical Data as Categorical Data

In “Exploring the Data Distribution” on page 29, we looked at fre‐
quency  tables  based  on  binning  the  data.  This  implicitly  converts
the numeric data to an ordered factor. In this sense, histograms and
barcharts are similar, except that the categories on the x-axis in the
bar chart are not ordered. Converting numeric data to categorical
data is an important and widely used step in data analysis since it
reduces the complexity (and size) of the data. This aids in the dis‐
covery  of  relationships  between  features,  particularly  at  the  initial
stages of an analysis.

Mode
The mode is the value, or values in case of a tie, that appears most often in the data.
For  example,  the  mode  of  the  cause  of  delay  at  Dallas/Fort  Worth  airport  is
“Inbound”. As another example, in most parts of the United States, the mode for reli‐
gious preference would be Christian. The mode is a simple summary statistic for cat‐
egorical data, and it is generally not used for numeric data.

Expected Value
A special type of categorical data is data in which the categories represent, or can be
mapped to, discrete values on the same scale. A marketer for a new cloud technology,
for  example,  offers  two  levels  of  service,  one  priced  at  $300/month  and  another  at
$50/month. The marketer offers free webinars to generate leads, and the firm figures
that 5% of the attendees will sign up for the $300 service, 15% for the $50 service, and
80% will not sign up for anything. These data can be summed up, for financial pur‐
poses,  in  a  single  “expected  value,”  which  is  a  form  of  weighted  mean  in  which  the
weights are probabilities.

The expected value is calculated as follows

1. Multiply each outcome by its probability of occurring.

2. Sum these values.

In the cloud service example, the expected value of a webinar attendee is thus $22.50
per month, calculated as follows:

EV = 0 . 05 300 + 0 . 15 50 + 0 . 80 0 = 22 . 5

The  expected  value  is  really  a  form  of  weighted  mean:  it  adds  the  ideas  of  future
expectations  and  probability  weights,  often  based  on  subjective  judgment.  Expected
value is a fundamental concept in business valuation and capital budgeting, for exam‐

Exploring Binary and Categorical Data 

| 

37

ple, the expected value of 5-years of profits from a new acquisition, or the expected
cost savings from new patient management software at a clinic.

Key Ideas

1. Categorical data is typically summed up in proportions, and can be visualized in

a bar chart.

2. Categories might represent distinct things (apples and oranges, male and female),
they might represent levels of a factor variable (low, medium and high), or they
might represent numeric data that has been binned.

3. Expected  value  is  the  sum  of  values  times  their  probability  of  occurance,  often

used to sum up factor variable levels.

Further Reading

1. No  statistics  course  is  complete  without  a  lesson  on  misleading  graphs,  which
often involve bar charts and pie charts. Here’s one: http://passyworldofmathemat
ics.com/misleading-graphs/

Correlation
Exploratory data analysis in many modeling projects (whether in data science or in
research)  involves  examining  correlation  among  predictors,  and  between  predictors
and a target variable. Variables X and Y (each with measured data) are said to be posi‐
tively correlated if high values of X go with high values of Y, and low values of X go
with low values of Y. If high values of X go with low values of Y, and vice-versa, the
variables are negatively correlated.

Key Terms for Correlation

Correlation coefficient

A metric that measures the extent to which numeric variables are associated with
one another (ranges from -1 to +1).

Correlation matrix

A  table  where  the  variables  are  shown  on  both  rows  and  columns,  and  the  cell
values are the correlations between the variables.

38 

| 

Chapter 1: Exploratory Data Analysis

Scatterplot

A plot in which the x-axis is the value of one variable, and the y-axis the value of
another.

Consider these two variables, perfectly correlated in the sense that each goes from low
to high:

V1: {1, 2, 3} V2: {4, 5, 6}

The  vector  sum  of  products  is  4+10+18  =  32.  Now  try  shuffling  one  of  them  and
recalculating - the vector sum of products will never higher than 32. So this sum of
products could be used as a metric - the observed sum of 32 could be compared to
lots  of  random  shufflings  (in  fact,  this  idea  relates  to  a  resampling  based  estimate:
see ???). Values produced by this metric, though, are not that meaningful, except by
reference to the resampling distribution.

More useful is a standardized variant: the correlation coefficient, which gives an esti‐
mate of the correlation between two variables that always lies on the same scale. Pear‐
son’s correlation coefficient is computed by multiplying deviations from the mean for
variable  1  times  those  for  variable  2,  and  dividing  by  the  product  of  the  standard
deviations:

N
∑i = 1

r =

xi − x yi − y
N − 1 sxsy

Note that we divide by N-1 instead of N: see Degrees of Freedom, and N or N-1? for
more details. The correlation coefficient always lies between +1 (perfect positive cor‐
relation) and -1 (perfect negative correlation); 0 indicates no correlation.

Variables can have an association that is not linear, in which case the correlation coef‐
ficient  may  not  be  a  useful  metric.  The  relationship  between  tax  rates  and  revenue
raised is an example - as tax rates increase from 0, the revenue raised also increases.
However, once tax rates reach a high level, and approach 100%, tax avoidance increa‐
ses and tax revenue actually declines.

Table 1-6. Correlation between telecommunication stock returns.

T

CTL

FTR

VZ

LVLT

T

1.000

0.475

0.328

0.678

0.279

CTL

0.475

1.000

0.420

0.417

0.287

FTR

0.328

0.420

1.000

0.287

0.260

Correlation 

| 

39

T

CTL

FTR

VZ

LVLT

VZ

0.678

0.417

0.287

1.000

0.242

LVLT

0.279

0.287

0.260

0.242

1.000

Table 1-6, called a correlation matrix, shows the correlation between the daily returns
for telecommunication stocks from July, 2012 through June 2015. From the table, you
can  see  that  Verizon  (VZ)  and  ATT  (T)  have  the  highest  correlation.  Level  Three
(LVLT), which is an infrastructure company, has the lowest correlation.

A table of correlations, such as Table 1-6, is commonly plotted to give a visual display
of  the  relationship  between  multiple  variables.  Figure  1-6  shows  the  correlation
between the daily returns for major exchange traded funds (ETF’s). In R, this is easily
created using the package corrplot:

etfs <- sp500_px[row.names(sp500_px)>"2012-07-01",
                 sp500_sym[sp500_sym$sector=="etf", 'symbol']]
library(corrplot, method = "ellipse")
corrplot(cor(etfs))

Figure 1-6. Correlation between ETF Returns

The ETF’s for the S&P 500 (SPY) and the Dow Jones Index (DIA) have a high correla‐
tion.  Similary,  the  QQQ  and  the  XLK,  composed  mostly  of  technology  companies,
are  postively  correlated.  Defensive  ETF’s,  such  as  those  tracking  gold  prices  (GLD),

40 

| 

Chapter 1: Exploratory Data Analysis

oil prices (USO), or market volatility (VXX) tend to be negatively correlated with the
other  ETF’s.  The  orientation  of  the  ellipse  indicates  whether  two  variables  are  posi‐
tively  correlated  (ellipse  is  pointed  right)  or  negatively  correlated  (ellipse  is  pointed
left).  The  shading  and  width  of  the  ellipse  indicate  the  strength  of  the  association:
thinner and darker ellipses correspond to stronger relationships.

Like the mean and standard deviation, the correlation coefficient is sensitive to outli‐
ers in the data. Software packages offer robust alternatives to the classical correlation
coefficient. For example, the R function cor has a trim argument similar to that for
computing a trimmed mean (see ???).

Example 1-2. Other Correlation Estimates

Statisticians  have  long  ago  proposed  other  types  of  correlation  coefficients,  such  as
Spearman’s rho or Kendall’s tau. These are correlation coefficients based on the rank of
the data. Since they work with ranks rather than values, these estimates are robust to
outliers and can handle certain types of non-linearities. However, data scientists can
generally  stick  to  Pearson’s  correlation  coefficient,  and  its  robust  alternatives,  for
exploratory  analysis.  The  appeal  of  rank-based  estimates  is  mostly  for  smaller  data
sets, and specific hyphothesis tests.

Scatterplots
The standard way to visualize the relationship that two measured data variables have
is with a scatterplot. The x-axis represents one variable, the y-axis another, and each
point on the graph is a record. See Figure 1-7 for a plot between the daily returns for
ATT and Verizon. This is produced in R with the command

plot(telecom$T, telecom$VZ, xlab="T", ylab="VZ")

The returns have a strong positive relationship: on most days, when both stocks go up
or go down in tandem. There are very few days where one stock goes down signifi‐
cantly while the other stock goes up (and visa versa).

Correlation 

| 

41

Figure 1-7. Scatter plot between returns for ATT and Verizon

Key Ideas for Correlation

1. The correlation coefficient measures the extent to which two variables are associ‐

ated with one another

2. When high values of v1 go with high values of v2, v1 and v2 are positively associ‐

ated

3. When high values of v1 are associated with low values of v2, v1 and v2 are nega‐

tively associated

4. The correlation coefficient is a standardized metric so that it always ranges from

-1 (perfect negative correlation) to +1 (perfect positive correlation

5. 0  indicates  no  correlation,  but  be  aware  that  random  arrangements  of  data  will
produce  both  positive  and  negative  valies  for  the  correlation  coefficient  just  by
chance

Exploring Two or More Variables
Familiar estimators like mean and variance look at variables one at a time (univariate
analysis). Correlation analysis (see “Correlation” on page 38) is an important method

42 

| 

Chapter 1: Exploratory Data Analysis

that compare two variables (bivariate analysis). In this section we look at additional
estimates and plots, and at more than two variables (multivariate analysis).

Key Terms for Exploring Two or More Variables

Contingency Tables

A tally of counts between two or more categorical variables.

Hexagonal Binning

A plot of two numeric variables with the records binned into hexagons.

Contour Plots

A plot showing the density of two numeric variables like a topographical map.

Violin Plots

Similar to a boxplot but showing the density estimate.

As in a univariate analysis, bivariate analysis involves both computing summary sta‐
tistics and producing visual displays. The appropriate type of bivariate or multivariate
analysis depends on the nature of the data: numeric versus categorical.

Hexagonal Binning and Contours (plotting numeric vs. numeric)
Scatterplots are fine when there are a relatively small number of data values. The plot
of stock returns in Figure 1-7 only involves about 750 points. For data sets with hun‐
dreds of thousands or millions of records, a scatterplot will be too dense and we need
a different way to visualize the relationship.

Figure 1-8 is a hexagon binning plot of the relationship between the finished square
feet versus the tax assessed value for homes in King County, Washington. Rather than
plotting points, which would appear as a monolithic dark cloud, the records are grou‐
ped  into  hexagonal  bins  and  the  hexagons  are  plotted  with  a  color  indicating  the
number of records in that bin. In this chart, the positive relationship between square
feet and tax assessde value is clear. An interesting feature is the presence of a second
cloud above the main cloud where homes have the same square footage as those in
the main cloud, but a higher tax assessed value.

Figure  1-8  was  generated  by  the  powerful  R  package  ggplot2  developed  by  Hadley
Wickham ???. ggplot2 is one of several new software libraries for advanced explora‐
tory visual analysis of data: see “Visualizing Multiple Variables” on page 48.

library(ggplot)
  ggplot(house0, aes(x=FinishedSquareFeet, y=TaxAssessedValue)) +
  stat_binhex(colour="white") +
  theme_bw() +

Exploring Two or More Variables 

| 

43

  scale_fill_gradient(limits=c(0, 9000), low="white", high="blue") +
  labs(x="Finished Square Feet", y="Tax Assessed Value")

Figure 1-8. Hexagonal binning for tax assessed value versus finished square feet

Figure  1-9  uses  contours  overlaid  on  a  scatterplot  to  visualize  the  relationship
between two numeric variables. The contours are essentially a topographical map to
two variables; each contour band represents a specific density of points, increasing as
one nears a “peak.” This plot shows a similar story as Figure 1-8: there is a secondary
peak “north” of the main peak. This chart was also created using ggplot2:

library(ggplot)
house %>%
  filter(TaxAssessedValue < 750000, FinishedSquareFeet>100,
         FinishedSquareFeet<4000) %>%
  ggplot(aes(FinishedSquareFeet, TaxAssessedValue)) +
  theme_bw() +
  geom_point(colour="blue", alpha=0.2) +
  geom_density2d(colour="red") +
  labs(x="Finished Square Feet", y="Tax Assessed Value")

44 

| 

Chapter 1: Exploratory Data Analysis

Figure 1-9. Contour plot for tax assessed value versus finished square feet

Other  types  of  charts  are  used  to  show  the  relationship  between  two  numeric  vari‐
ables, such as heat maps. Heat maps, hexagonal binning and contour plots all give a
visual representation of a two-dimensional density. In this way, they are natural ana‐
logs to histograms and density plots.

Two Categorical Variables
A useful way to summarize two categorical variable is a contingency table - a table of
counts by category. Table 1-7 shows the contingency table between the grade of a per‐
sonal loan and the outcome of that loan. This is taken from data provided by Lending
Club,  a  leader  in  the  peer-to-peer  lending  business  (provide  reference  here).  The
grade goes from A (high) to G (low). The outcome is either paid off, current, late or
charged off (the balance of the loan is not expected to be collected). This table shows
the count and row percentages. High grade loans have a very low late/charge-off per‐
centage  as  compared  with  lower  grade  loans.  Contingency  tables  can  look  at  just
counts,  or  also  include  column  and  total  precentages.  Pivot  tables  in  Excel  are  per‐
haps the most common tool used to create contingency table. Table 1-7 was created in
R using the CrossTable function in the descr package:

library(descr)
CrossTable(loans$grade, loans$status, prop.c=FALSE,
                    prop.chisq=FALSE, prop.t=FALSE)

Exploring Two or More Variables 

| 

45

Table 1-7. Contingency table of loan grade and status

Grade

Fully Paid Current

Late

Charged Off Total

A

B

C

D

E

F

G

20726

52059

494

1588

74867

0.277

0.695

0.007

0.021

0.161

31785

97608

2149

5387

136929

0.232

0.713

0.016

0.039

0.294

23784

92448

2895

6166

125293

0.190

0.738

0.023

0.049

0.269

14040

55293

2421

5135

76889

0.183

0.719

0.031

0.067

0.165

6091

25346

1421

2899

35757

0.170

0.709

0.040

0.081

0.077

2376

8676

622

1556

13230

0.180

0.656

0.047

0.118

655

2042

206

419

0.197

0.615

0.062

0.126

0.028

3322

0.007

Total

99457

333472

10208

23150

466287

Categorical and Numeric Data
Boxplots  (see  “Percentiles  and  Boxplots”  on  page  30)  are  a  simple  way  to  visually
compare  the  distributions  of  a  numeric  variable  grouped  according  to  a  categorical
variable. For example, we might want to compare how the percentage of flight delays
varies across different airlines. Figure 1-10 shows the percentage of flights in a month
that were delayed where the delay was within control of the carrier.

boxplot(pct_delay ~ airline, data=airline_stats, ylim=c(0,30))

46 

| 

Chapter 1: Exploratory Data Analysis

Figure 1-10. Boxplot of percent of airline delays by carrier.

Alaska stands out as having the fewest delays while American has the most delays: the
lower quartile for American is higher than the upper quartile for Alaska.

A violin plot, introduced by ???, is an enhancement to the boxplot and plots the den‐
sity estimate with the density on the y-axis. A mirror image of the density is flipped
over  and  the  resulting  shape  is  filled  in  creating  an  image  resembling  a  violin.  The
advantage of a violin plot is that it can show nuances in the distribution not percepti‐
ble in a boxplot. Often, a violin plot is combined with a boxplot, as in Figure 1-11.
This was created using ggplot2:

ggplot(data=airline_stats, aes(airline, pct_delay)) +
  geom_violin(fill="lightblue") +
  geom_boxplot( alpha=.2) +
  ylim(0, 30)

The violin plot shows a concentration in the distribution near zero for Alaska, and to
a lesser extent, Delta. This phenomenon is not as obvious in the boxplot.

Exploring Two or More Variables 

| 

47

Figure 1-11. Combination of boxplot and violin plot of percent of airline delays by car‐
rier.

Visualizing Multiple Variables
The types of charts used to compare two variables - scatterplots, hexagonal binning,
and boxplots - are readily extended to more variables using the notion of condition‐
ing. As an example, consider the Figure 1-8 that shows the relationship between fin‐
ished  square  feet  and  tax  assessed  value.  We  observed  that  there  appears  to  be  a
cluster of homes that have higher tax assessed value per square foot. Diving deeper,
Figure  1-12  accounts  for  the  effect  of  location  by  plotting  the  data  for  a  set  of  zip
codes. Now the picture is much clearer: tax assessed value is much higher in certain
zip code (98112, 98105) as opposed to other (98108, 98057). This disparity gives rise
to the clusters observed in Figure 1-8.

Figure 1-12 was created using ggplot2 using the idea of facets, or a conditioning vari‐
able (in this case zip code):

  ggplot(house1, aes(x=FinishedSquareFeet, y=TaxAssessedValue)) +
  stat_binhex(colour="white") +
  theme_bw() +
  scale_fill_gradient( low="white", high="blue") +
  labs(x="Finished Square Feet", y="Tax Assessed Value") +
  facet_wrap("zip")

48 

| 

Chapter 1: Exploratory Data Analysis

Figure 1-12. Tax assess value versus finished square feet by zip code.

The concept of conditioning variables in a graphics system was pioneered with Trellis
graphics  developed  by  Rick  Becker,  Bill  Cleveland  and  others  at  Bell  Labs  ???.  This
idea has propogated to various modern graphics systems, such as the lattice (???) and
ggplot2 packages in R and the Seaborn (???) and Bokeh (???) modules in python. Con‐
ditioning variables are also integral to business intelligence platforms such as Tableau
and  Spotfire.  With  the  advent  of  vast  computing  power,  modern  visualization  plat‐
forms have moved well beyond the humble beginnings of exploratory data analysis.
However, key concepts and tools developed over the years still form a foundation for
these systems.

Key Ideas

1. Hexagonal  binning  and  contour  plots  are  useful  tools  that  permit  graphical
examination of two numeric variables at a time, without being overwhelmed by
huge amounts of data.

2. Contingency tables are the standard tool for looking at the counts of two catego‐

rical variables.

3. Box plots and violin plots allow you to plot a numeric variable against a categori‐

cal variable.

Exploring Two or More Variables 

| 

49

Further Reading

1. Ggplot2: Elegant Graphics for Data Analysis, by Hadley Wickham, the creator of

ggplot2 (Springer, 2009)

2. Josef Fruehwald has a web-based tutorial on ggplot2: http://www.ling.upenn.edu/

~joseff/avml2012/

Conclusion
With the development of exploratory data analysis (EDA), pioneered by John Tukey,
statistics  set  a  foundation  that  was  a  precursor  to  the  field  of  data  science.  The  key
idea of EDA: the first and most important step in any project based on data is to look
at the data. By summarizing and visualizing the data, you can gain valuable intuition
and understanding of the project.

This chapter has reviewed several concepts, ranging from simple metrics, such as esti‐
mates  of  location  and  variability,  to  rich  visual  displays  to  explore  the  relationships
between  multiple  variables  as  in  Figure  1-12.  The  rich  set  of  tools  and  techniques
being developed by the open source community, combined with the expressiveness of
the  R  and  Python  languages,  has  created  a  plethora  of  ways  to  explore  and  analyze
data. Exploratory analysis should be a cornerstone of any data science project.

50 

| 

Chapter 1: Exploratory Data Analysis

CHAPTER 2
Data and Sampling Distributions

A popular misconception holds that the era of Big Data means the end of a need for
sampling. In fact, the proliferation of data of varying quality and relevance reinforces
the need for sampling as a tool to work efficiently with a variety of data, and mini‐
mize bias. Even in a Big Data project, predictive models are typically developed and
piloted with samples. Samples are also used in tests of various sorts (e.g. pricing, web
treatments).

51

Figure 2-1. Population versus sample

Figure Figure 2-1 shows a schematic that underpins the concepts in this chapter. The
left-hand  side  represents  a  population  which,  in  statistics,  is  assumed  to  follow  an
underlying  but  unknown  distribution.  The  only  thing  available  is  the  sample  data,
and  its  empirical  distribution,  shown  on  the  right-hand  side.  To  get  from  the  left-
hand  side  to  the  right-hand  side,  a  sampling  procedure  is  used  represented  by  red-
dash  arrows.  Traditional  statistics  focused  very  much  on  the  left-hand  side,  using
theory  based  on  strong  assumptions  about  the  population.  Modern  statistics  has
moved to the right-hand side where such assumptions are not needed.

In general, data scientists need not worry about the theoretical nature of the left-hand
side, and instead focus on the sampling procedures and the data at hand. There are
some  notable  exceptions.  Sometimes  data  is  generated  from  a  physical  process  that
can be modeled. The simplest example is flipping a coin: this follows a binomial dis‐
tribution. In these cases, we can gain additional insight by using our understanding
of the population.

52 

| 

Chapter 2: Data and Sampling Distributions

Random sampling and sample bias
A sample is a subset of data from a larger dataset; statisticians call this larger dataset
the population. A population in statistics is not the same thing as in biology - it is a
large, defined but sometimes theoretical or imaginary, set of data.

Key Terms for Random Sampling

Sample

A subset from a larger dataset

Population

The larger dataset, or idea of a dataset

N (n)

The size of the population (sample)

Random sampling

Drawing elements into a sample at random

Stratified sampling

Dividing the population into strata and randomly sampling from each strata.

Simple random sample

The  sample  that  results  from  random  sampling  without  stratifying  the  popula‐
tion.

Sample bias

A sample that misrepresents the population

Random  sampling  is  a  process  in  which  each  available  member  of  the  population
being sampled has an equal chance of being chosen for the sample at each draw. The
sample  that  results  is  called  a  simple  random  sample.  Sampling  can  be  done  with
replacement, in which observations are put back in the population after each draw for
possible  future  reselection.  Or  it  can  be  done  without  replacement,  in  which  case
observations, once selected, are unavailable for future draws.

Data  quality  often  matters  more  than  data  quantity  when  making  an  estimate  or  a
model based on a sample. Data quality in data science involves completeness, consis‐
tency of format, cleanliness and accuracy of individual data points. Statistics adds the
notion of representativeness.

The classic example is the Literary Digest poll of 1936 that predicted a victory of Al
Landon  against  Franklin  Roosevelt.  The  Literary  Digest,  a  leading  periodical  of  the
day, polled its entire subscriber base, plus additional lists of individuals, a total of over
10 million, and predicted a landslide victory for Landon. George Gallup, founder of

Random sampling and sample bias 

| 

53

the  Gallup  Poll,  conducted  bi-weekly  polls  of  just  2000,  and  accurately  predicted  a
Roosevelt victory. The difference lay in the selection of those polled.

The Literary Digest opted for quantity, paying little attention to the method of selec‐
tion.  They  ended  up  polling  those  with  relatively  high  socio-economic  status  (their
own  subscribers,  plus  those  who,  by  virtue  of  owning  luxuries  like  telephones  and
automobiles, appeared in marketers’ lists). The result was sample bias- the sample was
different in some meaningful non-random way from the larger population it is meant
to  represent.  The  term  “non-random”  is  important  -  hardly  any  sample,  including
random samples, will be exactly representative of the population. Sample bias occurs
when the difference is meaningful, and can be expected to continue for other samples
drawn in the same way as the first.

Bias
Statistical bias refers to measurement or sampling errors that are systematic and pro‐
duced by the measurement or sampling process. An important distinction should be
made  between  errors  due  to  random  chance,  and  errors  due  to  bias.  Consider  the
physical process of a gun shooting at a target. It will not hit the absolute center of the
target every time, or even much at all. An unbiased process will produce error, but it
is random and does not tend strongly in any direction (see Figure 2-2). The results
shown in Figure 2-3 show a biased process - there is still random error in both the x
and y direction, but there is also a bias. Shots tend to fall in the right upper quadrant.

Figure 2-2. Scatter of shots from a gun with true aim

54 

| 

Chapter 2: Data and Sampling Distributions

Figure 2-3. Scatterplot of shots from a gun with biased aim

Bias comes in different forms, and may be observable or invisible. When a result does
suggest bias (e.g. by reference to a benchmark, or actual values), it is often an indica‐
tor that a statistical or machine learning model has been misspecified, or an impor‐
tant variable left out.

Random Selection
To  avoid  the  problem  of  sample  bias  that  led  the  Literary  Digest  to  predict  Landon
over Roosevelt, George Gallup opted for more scientifically-chosen methods to ach‐
ieve  a  sample  that  was  representative  of  the  US  voter.  There  are  now  a  variety  of
methods  to  achieve  representativeness,  but  at  the  heart  of  all  of  them  lies  random
sampling.

Random sampling and sample bias 

| 

55

Figure 2-4. George Gallup, catapaulted to fame by the Literary Digest’s “big data” failure

Random sampling is not always easy. Proper definition of an accessible population is
key. Suppose we want to generate a representative profile of customers and we need
to conduct a pilot customer survey. The survey needs to be representative but is labor
intensive.

First  we  need  to  define  who  is  a  customer.  We  might  select  all  customer  records
where  purchase  amount  >  0.  Do  we  include  all  past  customers?  Do  we  include
refunds? Internal test purchases? Resellers? Both billing agent and customer?

Next we need to specify a sampling procedure. It might be “select 100 customers at
random.” Where a sampling from a flow is involved (e.g. realtime customer transac‐
tions, or web visitors), timing considerations ma be important (e.g. a web visitor at 10
am on a weekday may be different from a web visitor at 10 pm on a weekend).

In stratified sampling, the population is divided up into strata, and random samples
are taken from each stratum. Political pollsters might seek to learn the electoral pref‐
erences of whites, blacks and hispanics. A simple random sample taken from the pop‐
ulation  would  yield  too  few  blacks  and  hispanics,  so  those  strata  could  be
overweighted in stratified sampling to yield equivalent sample sizes.

56 

| 

Chapter 2: Data and Sampling Distributions

Size Versus Quality
Time  and  effort  spent  on  random  sampling  not  only  reduce  bias,  but  also  allow
greater attention to data quality. For example, missing data and outliers may contain
useful information. It might be prohibitively expensive to track down missing values,
or evaluate outliers, in millions of records, but doing so in a sample of several thou‐
sand records may be feasible.

The flip side of this effect is when you have sparse data. Consider the search queries
received by Google, where columns are terms, rows are individual search queries, and
cell values are either 0 or 1, depending on whether a query contains a term. The goal
is to determine the best predicted search destination for a given query. There are over
150,000 words in the English language, and Google processes over 1 trillion queries
per year. This yields a huge matrix, the vast majority of whose entries are “0.”

This  is  a  true  Big  Data  problem  -  only  when  such  enormous  quantities  of  data  are
accumulated can effective search results be returned for most queries. And the more
data  accumulates,  the  better  the  results.  For  popular  search  terms  this  is  not  such  a
problem - effective data can be found fairly quickly for the handful of extremely pop‐
ular topics trending at a particular time. The real value of modern search technology
lies  in  the  ability  to  return  detailed  and  useful  results  for  a  huge  variety  of  search
queries, including those that occur only with a frequency, say, of one in a million.

Consider the search phrase “Ricky Ricardo and Little Red Riding Hood.” In the early
days  of  the  internet,  this  query  would  probably  have  returned  results  on  Ricky
Ricardo the band leader, the television show I Love Lucy in which he starred, and the
children’s story Little Red Riding Hood. Later, now that trillions of search queries have
been accumulated, this search query returns the exact I Love Lucy episode in which
Ricky narrates, in dramatic fashion, the Little Red Riding hood story to his infant son
in a comic mix of English and Spanish.

Keep in mind that the number of actual pertinent records - ones in which this exact
search query, or something very similar, appears (together with information on what
link people ultimately clicked on) might need only be in the thousands to be effective.
However, many trillions of data points are needed in order to obtain these pertinant
records (and random sampling, of course will not help). See also “Long-Tailed Distri‐
butions” on page 77.

Sample Mean Versus Population Mean
The  symbol  x  is  used  to  represent  the  mean  of  a  sample  from  a  population  (pro‐
nounced x-bar), whereas μ is used to represent the mean of a population. Why make
the distinction? Information about samples is observed, and information about large

Random sampling and sample bias 

| 

57

populations is often inferred from smaller samples. Statisticians like to keep the two
things separate in the symbology.

Key Ideas

1. Even in the era of Big Data, random sampling remains an important arrow in the

data scientist’s quiver

2. Bias  occurs  when  measurements  or  observations  are  systematically  in  error

because they are not representative of the full population

3. Data quality is often more important than data quantity, and random sampling
can  reduce  bias  and  facilitate  quality  improvement  that  would  be  prohibitively
expensive

Further Reading

1. A useful review of sampling procedures can be found in Ronald Fricker’s chapter
“Sampling Methods for Web and E-mail Surveys,” found in the Sage Handbook of
Online research Methods. This chapter includes a review of the modifications to
random sampling that are often used for practical reasons of cost or feasibility.

2. The story of the Literary Digest poll failure can be found here: http://www.capital

century.com/1935.html

Selection bias
To  paraphrase  Yogi  Berra,  “If  you  don’t  know  what  you’re  looking  for,  look  hard
enough, and you’ll find it.”

Selection  bias  refers  to  the  practice  of  selectively  choosing  data  -  consciously  or
unconsciously - in a way that that leads to a conclusion that is misleading or ephem‐
eral.

Key Terms

Bias

Systematic error

Data snooping

Extensive hunting through data in search of something interesting

58 

| 

Chapter 2: Data and Sampling Distributions

Massive search effect

Bias or non-reproducibility resulting from repeated data modeling, or modeling
data with large numbers of predictor variables

If you specify a hypothesis and conduct a well-designed experiment to test it, you can
have high confidence in the conclusion. Such is often not the case, however. Often,
one looks at available data and tries to discern patterns. But is the pattern for real, or
just the product of “data snooping,” or extensive hunting through the data until some‐
thing  interesting  emerges?  There  is  a  saying  among  statisticians,  “if  you  torture  the
data long enough, sooner or later it will confess.”

The  difference  between  a  phenomenon  that  you  verify  when  you  test  a  hypothesis
using  an  experiment,  versus  a  phenomenon  that  you  discover  by  perusing  available
data, can be illuminated with this thought experiment:

Imagine that a person tells you she can flip a coin and have it land heads on the next
10 tosses. You challenge her (the equivalent of an experiment), and she proceeds to
toss it 10 times, all landing heads. Clearly you ascribe some special talent to her - the
probabilty that 10 coin tosses will land heads just by chance is 1 in 1000.

Now imagine that the announcer at a sports stadium asks the 20,000 people in attend‐
ance each to toss a coin 10 times, and report to an usher if they get 10 head in a row.
The chance that somebody in the stadium will get 10 heads is extremely high (more
than 99% - it’s 1 minus the probability that nobody gets 10 heads). Clearly, selecting,
after the fact, the person (or persons) who get 10 heads at the stadium does not indi‐
cate they have any special talent - it’s most likely luck.

Since  repeated  review  of  large  data  sets  is  a  key  value  proposition  in  data  science,
selection bias is something to worry about. A form of selection bias of particular con‐
cern to data scientists is what John Elder (founder of Elder Research, a respected data
mining  consultancy)  calls  the  "massive  search  effect.”  If  you  repeatedly  run  different
models and ask different questions with a large data set, you are bound to find some‐
thing  interesting.  Is  the  result  you  found  truly  something  interesting,  or  is  it  the
chance outlier?

We can guard against this by using a holdout set, and sometimes more than one hold‐
out set, against which to validate performance. Elder also advocates the use of what
he calls “target shuffling” (a permutation test, in essence) to test the validity of predic‐
tive associations that a data mining model suggests.

Typical  forms  of  selection  bias  in  statistics,  in  addition  to  the  massive  search  effect,
include  non-random  sampling  (see  sampling  bias),  cherry-picking  data,  selection  of
time intervals that accentuate a partiular statistical effect and stopping an experiment
when the results look “interesting.”

Selection bias 

| 

59

Regression to the mean
Regression  to  the  mean  refers  to  a  phenomenon  involving  successive  measurements
on a given variable: extreme observations tend to be followed be more central ones.
Attaching special focus and meaning to the extreme value can lead to a form of selec‐
tion bias.

Sports  fans  are  familiar  with  the  “Rookie  of  the  year,  sophomore  slump”  phenom‐
enon. Among the athletes who begin their career in a given season (the rookie class),
there is always one who performs better than all the rest. Generally, this “rookie of the
year” does not do as well in his second year. Why not?

In nearly all major sports, at least those played with a ball or puck, there are two ele‐
ments that play a role in overall performance:

• skill

• luck

Regression to the mean is a consequence of a particular form of selection bias. When
we select the rookie with the best performance, skill and good luck are probably con‐
tributing. In his next season, the skill will still be there but, in most cases, the luck will
not, so his performance will decline - it will regress. The phenomenon was first iden‐
tified by Francis Galton in 1886 ???, who wrote of it in connection with genetic ten‐
dencies: e.g., the children of extremely tall men tend not to be as tall as their father.

60 

| 

Chapter 2: Data and Sampling Distributions

Figure 2-5. Galton’s study that identified the phenomena of regression to the mean.

Regression to the mean, meaning to “go back,” is distinct from the
statistical modeling method of linear regression, in which a linear
relationship  is  estimated  between  predictor  variables  and  an  out‐
come variable.

Key Ideas

1. Specifying  a  hypothesis,  then  collecting  data  following  randomization  and  ran‐

dom sampling principles ensures against bias.

2. All other forms of data analysis run the risk of bias resulting from the data collec‐
tion/analysis process (repeated running of models in data mining, data snooping
in research, after-the-fact selection of interesting events).

Selection bias 

| 

61

Further Reading

1. Christopher J. Pannucci and Edwin G. Wilkins’ article “Identifying and Avoiding
Bias  in  Research”  in  (surprisingly)  Plastic  and  Reconstructive  Surgery  (August
2010) has an excellent review of various types of bias that can enter into research,
including selection bias.

2. Michael Harris’s article “Fooled by Randomness Through Selection Bias” (http://
systemtradersuccess.com/fooled-by-randomness-through-selection-bias/)  provides
an  interesting  review  of  selection  bias  considerations  in  stock  market  trading
schemes, from the perspective of traders.

Sampling Distribution of a Statistic
The term sampling distribution of a statistic refers to the distribution of some sample
statistic, over many samples drawn from the same population. Much of classical sta‐
tistics is concerned with making inference from (small) samples to (very large) popu‐
lations.

Key Terms

Sample statistic

A metric calculated for a sample of data drawn from a larger population

Data distribution

The frequency distribution of individual values in a data set

Sampling distribution

The frequency distribution of a sample statistic over many samples or resamples

Central Limit Theorem

The tendency of the sampling distribution to take on a normal shape as sample
size rises

Standard error

The variability (standard deviation) of a sample statistic over many samples (not
to  be  confused  with  standard  deviation,  which,  by  itself,  refers  to  variability  of
individual data values)

Typically a sample is drawn with the goal of measuring something (with a sample sta‐
tistic)  or  modeling  something  (with  a  statistical  or  machine  learning  model).  Since
our estimate or model is based on a sample, it might be in error - it might be different
if we were to draw a different sample. We are therefore interested in how different it
might be - a key concern is sampling variability. If we had lots of data, we could draw

62 

| 

Chapter 2: Data and Sampling Distributions

additional  samples  and  observe  the  distribution  of  a  sample  statistic,  directly.  Typi‐
cally, we will calculate our estimate or model using as much data as are easily avail‐
able, so the option of drawing additional samples from the population is not readily
available.

It is important to distinguish between the distribution of the indi‐
vidual data points - the data distribution, and the distribution of a
sample statistic - the sampling distribution.

The distribution of a sample statistic such as the mean is likely to be more regular and
bell-shaped than the distribution of the data themselves. The larger the sample that
the  statistic  is  based  on,  the  more  this  is  true.  Also,  the  larger  the  sample,  the  nar‐
rower the distribution of the sample statistic.

This is illustrated in the following example using a annual income for loan applicants
to Lending Club (see ??? for a description of the data). Take three samples from this
data:  a  sample  of  1000  values,  a  sample  of  1000  means  of  5  values  and  a  sample  of
1000  means  of  20  values.  Then  plot  a  histogram  of  each  sample  to  produce
Figure 2-6.

Sampling Distribution of a Statistic 

| 

63

Figure 2-6. Histogram of annual incomes of 1000 loan applicants

The  histogram  of  the  individual  data  values  is  broadly  spread  out  and  skewed
towards higher values as is to be expected with income data. The histograms of the
means of 5 and 20 are increasingly compact and more bell-shaped. Here is the R code
to generate these histograms, using the visualization backage ggplot2.

library(ggplot2)
loans_income <- read.csv("/Users/andrewbruce1/book/loans_income.csv")[,1]
# take a simple random sample
samp_data <- data.frame(income=sample(loans_income, 1000),
                        type='data_dist')
# take a sample of means of 5 values
samp_mean_05 <- data.frame(
  income = tapply(sample(loans_income, 1000*5),
                  rep(1:1000, rep(5, 1000)), FUN=mean),
  type = 'mean_of_5')
# take a sample of means of 20 values
samp_mean_20 <- data.frame(
  income = tapply(sample(loans_income, 1000*20),

64 

| 

Chapter 2: Data and Sampling Distributions

                  rep(1:1000, rep(20, 1000)), FUN=mean),
  type = 'mean_of_20')
# bind the data.frames and convert type to a factor
income <- rbind(samp_data, samp_mean_05, samp_mean_20)
income$type = factor(income$type,
                     levels=c('data_dist', 'mean_of_5', 'mean_of_20'),
                     labels=c('Data', 'Mean of 5', 'Mean of 20'))
# plot the histograms
ggplot(income, aes(x=income)) +
  geom_histogram(bins=40) +
  facet_grid(type ~ .)

Central Limit Theorem
This phenomenon is termed the Central Limit Theorem. It says that the means drawn
from multiple samples will be shaped like the familiar bell-shaped normal curve (see
“Normal  distribution”  on  page  74),  even  if  the  source  population  is  not  normally-
distributed,  provided  that  the  sample  size  is  large  enough  and  the  departure  of  the
data  from  normality  is  not  too  great.  The  Central  Limit  Theorem  allows  normal-
approximation formulas like the t-distribution to be used in calculating sampling dis‐
tributions for inference, i.e., confidence intervals and hypothesis tests.

The  Central  Limit  Theorem  receives  much  attention  in  traditional  statistics  texts
because  it  underlies  the  machinery  of  hypothesis  tests  and  confidence  intervals,
which  themselves  consume  half  the  space  in  such  texts.  Data  scientists  should  be
aware of this role, but, since formal hypothesis tests and confidence intervals play a
small role in data science, and the bootstrap is available in any case, the Central Limit
Theorem is not so central in the practice of data science.

Standard error
The standard error is a single metric that sums up the variability in the sampling dis‐
tribution for a statistic. The standard error can be estimated using a statistic based on
the standard deviation s of the sample values, and the sample size n:

Standard Error = SE =

s
n

As  the  sample  size  increases,  the  standard  error  decreases,  corresponding  what  was
observed  in  Figure  2-6.  The  relationship  between  standard  error  and  sample  size  is
sometimes  referred  to  as  the  square-root  of  n  rule:  in  order  to  reduce  the  standard
error by a factor of 2, the sample size must be increased by a factor of 4.

The validity of the standard error formula arises from the central limit theorem (see
“Central Limit Theorem” on page 65). In fact, you don’t need to rely on the central

Sampling Distribution of a Statistic 

| 

65

limit  theorem  to  understand  standard  error.  Consider  the  following  approach  to
measure standard error:

1. Collect a number of brand new samples from the population.

2. For each new sample, calculate the statistic (e.g., mean).

3. Estimate the standard error by the standard deviation of the statistics computed

in step 2.

In  practice,  the  above  approach  of  collecting  new  samples  to  estimate  the  standard
error is typically not feasible (and statistically very wasteful). Fortunately, it turns out
that it is not necessary to draw brand new samples; instead it is possible to use boot‐
strap resamples (see “The bootstrap” on page 67). In modern statistics, the bootstrap
has become the standard way to to estimate standard error. It can be used for virtually
any  statistic  and  does  not  rely  on  the  central  limit  theorem  or  other  distributional
assumptions.

Standard Deviation vs. Standard Error

Do not confuse standard deviation (which measures the variability
of individual data points) with standard error (which measures the
variability of a sample metric).

Key Ideas

1. The  frequency  distribution  of  a  sample  statistic  tells  us  how  that  metric  would

turn out differently from sample to sample

2. This  sampling  distribution  can  be  estimated  via  the  bootstrap,  or  via  formulas

that rely on the central limit theorem

3. A key metric that sums up the variability of a sample statistic is its standard error

Further Reading

1. David Lane’s online multimedia resource in statistics has a useful simulation that
allows you to select a sample statistic, a sample size and number of iterations and
visualize  a  histogram  of  the  resulting  frequency  distribution:  http://onlinestat
book.com/stat_sim/sampling_dist/

66 

| 

Chapter 2: Data and Sampling Distributions

The bootstrap
One easy and effective way to estimate the sampling distribution of a statistic, or of
model parameters, is to draw additional samples, with replacement, from the sample
itself, and recalculate the statistic or model for each resample. This procedure is called
the bootstrap, and it does not necessarily involve any assumptions about the data, or
the sample statistic, being normally-distributed.

Key Terms

Bootstrap sample

A sample taken with replacement from an observed dataset

Resampling

The process of taking repeated samples from observed data; includes both boot‐
strap and permutation (shuffling) procedures

Conceptually, you can imagine the bootstrap as replicating the original sample thou‐
sands or millions of times so that you have a hypothetical population that embodies
all the knowledge from your original sample (it’s just larger). You can then draw sam‐
ples from this hypothetical population for the purpose of estimating a sampling dis‐
tribution.

Figure 2-7. The idea of the bootstrap

In  practice,  it  is  not  necessary  to  actually  replicate  the  sample  a  huge  number  of
times. We simply replace each observation after each draw - we sample with replace‐
ment. In this way we effectively create an infinite population in which the probability
of an element being drawn remains unchanged from draw to draw. The algorithm for
a bootstrap resampling of the mean is as follows, for a sample of size N:

The bootstrap 

| 

67

1. Draw a sample value, record, replace it

2. Repeat N times

3. Record the mean of the N resampled values

4. Repeat steps 1-3 B times

5. Use the B results to:

a. Calculate  their  standard  deviation  (this  estimates  sample  mean  standard

error)

b. Produce a histogram or boxplot

c. Find a confidence interval

B,  the  number  of  iterations  of  the  bootstrap,  is  set  somewhat  arbitrarily.  The  more
iterations you do, the more accurate the estimate of the standard error, or the confi‐
dence interval.

The  bootstrap  can  be  used  with  multivariate  data,  where  the  rows  are  sampled  as
units  (see  Figure  2-8).  A  model  might  then  be  run  on  the  bootstrapped  data,  for
example,  to  estimate  the  stability  (variability)  of  model  parameters,  or  to  improve
predictive power. With classification and regression trees (also called decision trees),
running multiple trees on bootstrap samples and then averaging their predictions (or,
with classification, taking a majority vote) generally performs better than using a sin‐
gle tree. This process is called bagging (short for “bootstrap aggregating”).

68 

| 

Chapter 2: Data and Sampling Distributions

Figure 2-8. Multivariate bootstrap sampling

The  repeated  resampling  of  the  bootstrap  is  conceptually  simple,  and  Julian  Simon,
an  economist  and  demographer,  published  a  compendium  of  resampling  examples,
including  the  bootstrap,  in  his  1969  text  Basic  Research  Methods  in  Social  Science.
However, it is also computationally intensive, and was not a feasible option before the
widespread availability of computing power. The technique gained its name and took
off with the publication of several journal articles and a book by Stanford statistician
Bradley  Efron  in  the  late  1970’s  and  early  1980’s.  It  was  particularly  popular  among
researchers  who  use  statistics  but  are  not  statisticians,  and  for  use  with  metrics  or
models where mathematical approximations are not readily available. The sampling
distribution of the mean has been well established since 1908; the sampling distribu‐
tion of many other metrics has not. The bootstrap can be used for sample size deter‐
mination  -  experiment  with  different  values  for  N  to  see  how  the  sampling
distribution is affected.

The bootstrap met with considerable skepticism when it was first introduced; it had
the aura to many of spinning gold from straw. This skepticism stemmed from a mis‐
understanding of the bootstrap’s purpose.

The bootstrap 

| 

69

The bootstrap does not compensate for a small sample size - it does
not create new data, nor does it fill in holes in an existing dataset. It
merely informs us about lots of additional samples would be have,
when drawn from a population like our original sample.

Resampling versus bootstrapping
Sometimes the term resampling is used synonymously with the term bootstrapping,
as outlined above. More often, the term resampling also includes permutation proce‐
dures (see ???), where multiple samples are combined, and the sampling may be done
without  replacement.  In  any  case,  the  term  bootstrap  always  implies  sampling  with
replacement from an observed dataset.

Key Ideas

1. The bootstrap (sampling with replacement from a dataset), is a powerful tool for

assessing the variability of a sample statistic.

2. The bootstrap can be applied in similar fashion in a wide variety of circumstan‐
ces, without extensive study of mathematical approximations to sampling distri‐
butions.

3. It also allows us to estimate sampling distributions for statistics where no mathe‐

matical approximation has been developed.

4. When applied to predictive models, aggregating multiple bootstrap sample pre‐

dictions (bagging) outperforms the use of a single model

Further Reading

1. An Introduction to the Bootstrap by Efron and Tibshirani (Chapman Hall, 1993);

the first book-length treatment of the bootstrap, and still widely read

2. The section on resampling in Chapter 4 (see ???) also discusses the bootstrap and

permutation procedures.

70 

| 

Chapter 2: Data and Sampling Distributions

Confidence intervals
Frequency  tables,  histograms,  boxplots  and  standard  errors  are  all  ways  to  under‐
stand the potential error in a sample estimate. Confidence intervals are another.

Key Terms

Confidence level

The  percentage  of  confidence  intervals,  constructed  in  the  same  way  from  the
same population, expected to contain the statistic of interest

Interval endpoints

The top and bottom of the confidence interval

There is a natural human aversion to uncertainty - people (especially experts) say “I
don’t know” far too rarely. Analysts and managers, while acknowledging uncertainty,
nonetheless  place  undue  faith  in  an  estimate  when  presented  as  a  single  number  (a
point estimate). Presenting an estimate not as a single number but as a range is one
way to counteract this tendency. Confidence intervals do this in a manner grounded
in statistical sampling principles.

Confidence  intervals  always  come  with  a  coverage  level,  expressed  as  a  (high)  per‐
centage, say 90% or 95%. One way to think of a 90% confidence interval is as follows:
it is the interval that encloses the central 90% of the bootstrap sampling distribution
of a sample statistic (see “The bootstrap” on page 67). More generally, an x% confi‐
dence interval around a sample estimate should, on average, contain similar sample
estimates x% of the time (when a similar sampling procedure is followed).

Given a sample of size n, and a sample statistic of interest, the algorithm for a boot‐
strap confidence interval is as follows:

1. Draw a random sample of size n with replacement from the data (a resample)

2. Record the statistic of interest for the resample

3. Repeat steps 1-2 many times, call it B times

4. For  an  x%  confidence  interval,  trim  [(1-x)/2]%  of  the  B  resample  results  from

either end of the distribution.

5. The trim points are the endpoints of an x% bootstrap confidence interval

Figure  2-9  shows  a  a  90%  confidence  interval  for  the  mean  annual  income  of  loan
applicants, based on a sample of 20 for which the mean was $57,573.

Confidence intervals 

| 

71

Figure 2-9. Bootstrap confidence interval for the annual income of loan applicants,
based on a sample of 20

The bootstrap is a general tool that can be used to generate confidence intervals for
most statistics, or model parameters. Statistical textbooks and software, with roots in
over a half-century of computer-less statistical analysis, will also reference confidence
intervals generated by formulas, especially the t-distribution (see “Student’s t distribu‐
tion” on page 79).

Of course, what we are really interested in when we have a sample
result is “what is the probability that the true value lies within a cer‐
tain  interval?”  This  is  not  really  the  question  that  a  confidence
interval  answers,  but  it  ends  up  being  how  most  people  interpret
the answer.

The  probability  question  associated  with  a  confidence  interval
starts out with the phrase “Given a sampling procedure and a pop‐
ulation, what is the probability that…” To go in the opposite direc‐
tion,  “Given  a  sample  result,  what  is  the  probability  that
(something is true about the population),” involves more complex
calculations and deeper imponderables.

The  percentage  associated  with  the  confidence  interval  is  termed  the  level  of  confi‐
dence. The higher the level of confidence, the wider the interval. Also, the smaller the
sample, the wider the interval (i.e. the more uncertainty). Both make sense: the more
confident you want to be, and the less data you have, the wider you must make the
confidence interval to be sufficiently assured of capturing the true value.

72 

| 

Chapter 2: Data and Sampling Distributions

For a data scientist, a confidence interval is a tool to get an idea of
how  variable  a  sample  result  might  be.  Data  scientists  would  use
this information not to publish a scholarly paper or submit a result
to  a  regulatory  agency  (as  a  researcher  might),  but  most  likely  to
communicate the potential error in an estimate, and, perhaps, learn
whether a larger sample is needed.

Key Ideas

1. Confidence intervals are the typical way to present estimates as an interval range.

2. The more data you have, the less variable it is, and the lower the level of confi‐

dence you can tolerate, the narrower the confidence interval.

3. The bootstrap is an effective way to construct confidence intervals

Further reading

1. For  a  bootstrap  approach  to  confidence  intervals  see  Introductory  Statistics  and
Analytics: A Resampling Perspective by Peter Bruce (Wiley, 2014) or Statistics by
Robin Lock and four other Lock family members (Wiley, 2012).

2. Engineers,  with  a  need  to  understand  the  precision  of  their  measurements,  use
confidence intervals perhaps more than most disciplines, and Modern Engineer‐
ing  Statistics  by  Tom  Ryan  (2007,  Wiley)  discusses  confidence  intervals.  It  also
reviews  a  tool  that  is  just  as  useful  and  gets  less  attention:  prediction  intervals
(intervals around a single value, as opposed to a mean or other summary statis‐
tic)

Confidence intervals 

| 

73

Normal distribution
The  bell-shaped  normal  distribution  is  iconic  in  traditional  statistics.1  The  fact  that
distributions of sample statistics are often normally shaped provided a powerful tool
in the development of mathematical formulas that approximate those distributions.

Key Terms

Error

The difference between a data point and a predicted or average value

Standardize

Subtract the mean and divide by the standard deviation

Z score

The result of standardizing an individual data point

Standard normal

A normal distribution with mean = 0 and standard deviation = 1

QQ-Plot

A plot to visualize how close a sample distribution is to a normal distribution.

In a normal distribution (Figure 2-10) 68% of the data lie within one standard devia‐
tion of the mean, and 95% lie within two standard deviations.

1 Iconic but perhaps overrated. George W. Cobb, the Mount Holyoke statistician noted for his contribution to
the philosophy of teaching introductory statistics, argued in a November 2015 editorial in the American Sta‐
tistician that the “standard introductory course, which puts the normal distribution at its center, had outlived
the usefulness of its centrality.”

74 

| 

Chapter 2: Data and Sampling Distributions

Figure 2-10. Normal curve

It  is  a  common  misconception  that  the  normal  distribution  is
called that because most data follow a normal distribution, i.e. it is
the normal thing. Most of the variables used in a typical data sci‐
ence project, in fact most raw data as a whole, are not normally dis‐
tributed: see “Long-Tailed Distributions” on page 77. The utility of
the  normal  distribution  derives  from  the  fact  that  many  statistics
are  normally  distributed  in  their  sampling  distribution.  Even  so,
assumptions  of  normality  are  generally  a  last  resort,  used  when
empirical  probability  distributions,  or  bootstrap  distributions,  are
not available.

Normal distribution 

| 

75

The normal distribution is also referred to as a Gaussian distribu‐
tion  after  Carl  Friedrich  Gauss,  a  prodigous  German  mathemati‐
cian  from  the  late  18th  and  early  19th  century.  Another  name
previously used for the normal distribution was the “error” distri‐
bution. Statistically speaking, an error is the difference between an
actual  value  and  a  statistical  estimate  like  the  sample  mean.  For
example,  the  standard  deviation  (see  “Estimates  of  Variability”  on
page 24) is based on the errors from the mean of the data. Develop‐
ment of the normal distribution by Gauss came from study of the
errors  of  astronomical  measurements  that  were  found  to  be
normally-distributed.

Standard Normal and QQ-Plots
A standard normal distribution is one in which the units on the x-axis are expressed
in terms of standard deviations away from the mean. To compare data to a standard
normal distribution you subtract the mean then divide by the standard deviation; this
is  also  called  normalization  or  standardization.  Note  that  “standardization”  in  this
sense is unrelated to database record standardization (conversion to a common for‐
mat).  The  transformed  value  is  termed  a  z-score,  and  the  normal  distribution  is
sometimes called the z-distribution.

Figure 2-11. QQ-Plot of a sample of 100 values drawn from a normal distribution.

76 

| 

Chapter 2: Data and Sampling Distributions

A QQ-Plot is used to visually determine how close a sample is to the normal distribu‐
tion. The QQ-Plot orders the z-scores from low to high, and plots each value’s z-score
on  the  y-axis;  the  x-axis  is  the  corresponding  quantile  of  a  normal  distribution  for
that value’s rank. Since the data are normalized, the units correspond to the number
of standard deviations away of the data from the mean. If the points roughly fall on
the  diagonal  line,  then  the  sample  distribution  can  be  considered  close  to  normal.
Figure 2-11 shows a QQ-Plot for a sample of 100 values randomly generated from a
normal  distribution:  as  expected,  the  points  closely  follow  the  line.  This  figure  can
easily be produced in R:

norm_samp <- rnorm(100)
qqnorm(norm_samp)
abline(a=0, b=1, col='grey')

Converting  data  to  z-scores  (i.e.  standardizing  or  normalizing  the
data) does not make the data normally distributed. It just puts the
data  on  the  same  scale  as  the  standard  normal  distribution,  often
for comparison purposes.

Key Ideas

1. The normal distribution was essential to the historical development of statistics

as it permitted mathematical approximation of uncertainty and variability.

2. While raw data are typically not normally-distributed, errors often are.

3. Data  are  often  converted  to  z-scores  by  subtracting  the  mean  of  the  data,  and
dividing by the standard deviation; the data can then be compared to a normal
distribution.

Long-Tailed Distributions
Despite  the  importance  of  the  normal  distribution  historically  in  statistics,  and  in
contrast to what the name would suggest, data are generally not normally distributed.

Key Terms for Long-Tail Distribution

Tail

The  long  narrow  portion  of  a  frequency  distribution,  where  relatively  extreme
values occur at low frequency

Long-Tailed Distributions 

| 

77

Skew

Where one tail of a distribution is longer than the other

While the normal distribution is often appropriate and useful with respect to the dis‐
tribution of errors and sample statistics, it typically does not characterize the distribu‐
tion of raw data. Sometimes, the distribution is highly skewed (asymmetric), such as
with  income  data,  or  the  distribution  can  be  discrete,  as  with  binomial  data.  Both
symmetric and asymmetric distributions may have long tail(s). The tails of a distribu‐
tion  correspond  to  the  extreme  values  (small  and  large).  Long-tails,  and  guarding
against them, are widely recognized in practical work. Nassim Taleb has proposed the
black  swan  theory  which  predicts  that  anamolous  events,  such  as  a  stock  market
crash, are likely to occur in much greater likelihood than would be predicted by the
normal distribution.

Figure 2-12. QQ-Plot of the returns for NFLX.

To illustrate the long-tailed nature of data, a good example is to look at stock returns.
Figure 2-12 shows the QQ-Plot for the daily stock returns for Netflix (NFLX), gener‐
ated in R by

sp500_px <- read.csv("/Users/andrewbruce1/book/sp500_data.csv", row.names = 1)
nflx <- sp500_px[,'NFLX']
nflx <- diff(log(nflx[nflx>0]))
qqnorm(nflx)
abline(a=0, b=1, col='grey')

78 

| 

Chapter 2: Data and Sampling Distributions

In  contrast  to  Figure  2-11,  the  points  are  far  below  the  line  for  low  values  and  far
above the line for high values. This means that we are much more likely to observe
extreme  values  than  would  be  expected  if  the  data  had  a  normal  distribution.
Figure 2-12 shows another common phenomena: the points are close to the line for
the data within one standard deviation of the mean. Tukey calls this phenomena as
data being “normal in the middle”, but having much longer tails (see ???).

There is much statistical literature about the task of fitting statisti‐
cal  distributions  to  observed  data.  Beware  an  excessively  data-
centric approach to this job, which is as much art as science. Data
are variable, and often consistent, on their face, with more than one
shape and type of distribution. It is typically the case that domain
and  statistical  knowledge  must  be  brought  to  bear  to  determine
what type of distribution is appropriate to model a given situation.
For example, we might have data on the level of internet traffic on a
server over many consecutive 5-second periods. It is useful to know
that the best distribution to model “events per time period” is the
Poisson (see “Poisson Distributions” on page 85).

Key Ideas for Long-Tail Distribution

1. Most data are not normally distributed

2. Assuming  a  normal  distribution  can  lead  to  underestimation  of  extreme  events

(“black swans”)

Further Reading

1. The Black Swan by Nassim Taleb (2010, 2nd ed., Random House)

2. Handbook  of  Statistical  Distributions  with  Applications,  by  K.  Krishnamoorthy

(2016, 2nd ed., CRC Press)

Student’s t distribution
The t-distribution is a normally-shaped distribution, but a bit thicker and longer on
the tails. It is used extensively in depicting distributions of sample statistics. Distribu‐
tions of sample means are typically shaped like a t-distribution, and there is a family

Student’s t distribution 

| 

79

of  t-distributions  that  differ  depending  on  how  large  the  sample  is.  The  larger  the
sample, the more normally-shaped the t-distribution becomes.

Key Terms for Student’s t-distribution

n

Sample size

degrees of freedom

A parameter that allows the t-distribution to adjust to different sample sizes, sta‐
tistics, and number of groups. *_

The t-distribution is often called Student’s t because it was published in 1908 in Bio‐
metrika by W. S. Gossett under the name “Student.” Gossett’s employer, the Guiness
brewery, did not want competitors to know that they were using statistical methods,
so insisted that Gossett not use his name on the article.

Gossett wanted to answer the question “what is the sampling distribution of the mean
of  a  sample,  drawn  from  a  larger  population.”  He  started  out  with  a  resampling
experiment - drawing random samples of four from a dataset of 3000 measurements
of  criminals’  height  and  left  middle  finger  lengths.  (This  being  the  era  of  eugenics,
there was much interest in data on criminals, and in discovering correlations between
criminal tendencies and physical or psychological attributes.) He plotted the standar‐
dized results (the z-scores) on the x-axis, and the frequency on the y-axis. Separately,
he had derived a function - now known as Student’s t, and he fit this function over the
sample results, plotting the comparison (see Figure 2-13).

Figure 2-13. Gossett’s resampling experiment results and fitted t-curve (from his 1908
Biometrika paper

80 

| 

Chapter 2: Data and Sampling Distributions

A  number  of  different  statistics  can  be  compared,  after  standardization,  to  the  t-
distribution, to estimate confidence intervals in light of sampling variation. Consider
a sample of size n for which the sample mean, x-bar, has been calculated. We can esti‐
mate a 90% confidence interval around the sample mean by adding and subtracting
the following:

t(n-1).05*(s/sqrt n)

where t(n-1).05 is the value of the t-statistic, with (n-1) degrees of freedom (see ???),
that “chops off ” 5% of the t-distribution at either end.

The t-distribution has been used as a reference for the distribution of a sample mean,
of the difference between two sample means, of regression parameters, and more.

Had computing power been widely available in 1908, statistics would no doubt have
relied much more heavily on computationally intensive resampling methods from the
start. Lacking computers, statisticians turned to mathematics, and functions such as
the  t-distribution,  to  approximate  sampling  distributions.  Computer  power  enabled
practical resampling experiments in the 1980’s but, by then, use of the t-distribution
and similar distributions had become deeply embedded in textbooks and software.

The  t-distribution’s  accuracy  in  depicting  the  behavior  of  a  sample  statistic  requires
that the distribution of that statistic for that sample be shaped like a normal distribu‐
tion. It turns out that sample statistics are often normally distributed, even when the
underlying population data are not (a fact which led to widespread application of the
t-distribution). This phenomenon is termed the Central Limit Theorem (see ???).

What do data scientists need to know about the t-distribution, and
the central limit theorem? Not a whole lot. These distributions are
used  in  classical  statistical  inference,  but  are  not  as  central  to  the
purposes  of  data  science.  Understanding  and  quantifying  uncer‐
tainty and variation are important to data scientists, but empirical
bootstrap  sampling  can  answer  most  questions  about  sampling
error.  However,  data  scientists  will  routinely  encounter  t-statistics
in  output  from  statistical  software  and  statistical  procedures  in  R,
e.g. in A-B tests and regressions, so familiarity with its purpose is
helpful.

Key Ideas for t-distribution

1. The t-distribution is actually a family of distributions resembling the normal dis‐

tribution, but with thicker tails

Student’s t distribution 

| 

81

2. It is widely used as a reference basis for the distribution of sample means, differ‐

erences between two sample means, regression parameters, and more.

Further Reading

1. The  original  Gossett  paper  in  Biometrica  from  1908  can  be  found  here:  http://
seismo.berkeley.edu/~kirchner/eps_120/Odds_n_ends/Students_original_paper.pdf

2. A  standard  treatment  of  the  t-distribution  can  be  found  in  David  Lane’s  online

resource: http://onlinestatbook.com/2/estimation/t_distribution.html

Binomial distribution

Key Terms for Binomial Distribution

Trial

An event with a discrete outcomes, e.g. a coin flip

Success

The outcome of interest for a trial

Synonyms

“1” (as opposed to “0”)

Binomial

Having two outcomes

Synonyms

yes/no, 0/1, binary

Binomial trial

A trial with two outcomes

Synonym

Bernoulli trial

Binomial distribution

Distribution of number of successes in x trials Synonym::: Bernoulli distribution

Yes/no (binomial) outcomes lie at the heart of analytics since they are often the cul‐
mination of a decision or other process - buy/don’t buy, click/don’t click, survive/die,
etc.  Central  to  understanding  the  binomial  distribution  is  the  idea  of  a  set  of  trials,
each trial having two possible outcomes with definite probabilities.

82 

| 

Chapter 2: Data and Sampling Distributions

Image  courtesy  of  CCF  Numismaticsfootnote[Public  domain  or  CC  BY-SA  3.0
(http://creativecommons.org/licenses/by-sa/3.0), via Wikimedia Commons]

For example, flipping a coin ten times is a binomial experiment with ten trials, each
trial having two possible outcomes (heads or tails). Such yes/no or 0/1 outcomes are
termed binary outcomes, and they need not have 50/50 probabilities. Any probabili‐
ties  that  sum  to  1.0  are  possible.  It  is  conventional  in  statistics  to  term  the  “1”  out‐
come the success outcome; it is also common practice to assign “1” to the more rare
outcome. Use of the term “success” does not imply that the outcome is desirable or
beneficial; it does tend to indicate the outcome of interest. For example, a loan default
or a fraudulent transaction are relatively uncommon events that we may be interested
in predicting, so are termed “1’s” or “successes.”

The binomial distribution is the frequency distribution of the number of successes (x)
in a given number of trials (n) with specified probability (p) of success oin each trial.
There  is  a  family  of  binomial  distributions,  depending  on  the  values  of  x,  n  and  p.
The binomial distribution would answer a question like this:

“If  the  probability  of  a  click  converting  to  a  sale  is  0.02,  what  is  the  probability  of
observing 0 sales in 200 clicks?”

The R code to calculate binomial probabilities is

> dbinom(x, n, p)

For example,

> dbinom(2, 5, 0.1)

would return 0.0729, the probability of observing exactly 2 successes in 5 trials, where
the probability of success for each trial is 0.1.

Often  we  are  interested  in  determining  the  probability  of  x  or  fewer  successes  in  n
trials, the code for that is

> pbinom(x, n, p)

For example

> pbinom(2, 5, 0.1)

Binomial distribution 

| 

83

would  return  0.9914,  the  probability  of  observing  2  or  fewer  successes  in  5  trials,
where the probability of success for each trial is 0.1.

The mean of a binomial distribution is np; you can also think of this as the expected
number of successes in n trials, for success probability = p.

The variance is as follows:

np(1-p)

With a large enough number of trials (particularly when p is close to 0.50), the bino‐
mial distribution is virtually indistinguishable from the normal distribution. In fact,
calculating binomial probabilities with large sample sizes is computationally demand‐
ing, and most statistical procedures use the normal distribution, with mean and var‐
iance as above, as an approximation.

Key Ideas

1. Binomial  outcomes  are  important  to  model,  since  they  represent,  among  other
things,  fundamental  decisions  (buy  or  don’t  buy,  click  or  don’t  click,  survive  or
die, etc.)

2. Binomial trial is an experiment with two possible outcomes, one with probability

p and the other with probabiltiy 1-p

3. With large n, and provided p is not too close to 0 or 1, the binomial distribution

can be approximated by the normal distribution

Further Reading

1. An  easy  read  can  be  found  at  https://www.mathsisfun.com/data/binomial-
distribution.html . Check out the section on the “quincunx,” a pinball-like simula‐
tion device for illustrating the binomial distribution.

2. The binomial distribution is a staple of introductory statistics, and all introduc‐

tory statistics texts will have a chapter or two on it.

84 

| 

Chapter 2: Data and Sampling Distributions

Poisson and Related Distributions
Many processes produce events randomly at a given overall rate - visitors arriving at a
web  site,  cars  arriving  at  a  toll  plaza  (events  spread  over  time),  imperfections  in  a
square meter of fabric, or typos per 100 lines of code (events spread over space).

Key Terms for Poisson and Related Distributions

Lambda

The rate (per unit of time or space) at which events occur

Poisson distribution

The frequency distribution of the number of events in sampled units of time or
space

Synonyms

“1” (as opposed to “0”)

Exponential distribution

The  frequency  distribution  of  the  time  or  distance  from  one  event  to  the  next
event

Synonyms

yes/no, 0/1, binary

Weibull distribution

A  generalized  version  of  the  exponential,  in  which  the  event  rate  is  allowed  to
shift over time

Poisson Distributions
From  prior  data  we  can  estimate  the  average  number  of  events  per  unit  of  time  or
space, but we might also want to know how different this might be from one unit of
time/space to another. The Poisson distribution tells us the distribution of events per
unit of time or space, when we sample many such units. It is useful when addressing
queuing questions like “how much capacity do we need to be 95% sure of fully pro‐
cessing the internet traffic that arrives on a server in any 5 second period.”

The key parameter in a Poisson distribution is λ, or lambda. This is the mean number
of events that occurs in a specified interval of time or space. The variance for a Pois‐
son distribution is also λ.

A common technique is to generate random numbers from a Poisson distribution as
part of a queuing simulation. The rpois function in R does this, taking only two argu‐
ments - the quantity of random numbers sought, and lambda:

Poisson and Related Distributions 

| 

85

>rpois(100, lambda = 2)

This code will generate 100 random numbers from a Poisson distribution with λ = 2.
For  example,  if  incoming  customer  service  calls  average  two  per  minute,  this  code
will simulate 100 minutes, returning the number of calls in each of those 100 minutes.

Exponential distribution
Using  the  same  parameter  λ  that  we  used  in  the  Poisson  distribution,  we  can  also
model the distribution of the time between events: time between visits to a web site or
between cars arriving at a toll plaza. It is also used in engineering to model time to
failure, and in process management to model, for example, the time required per ser‐
vice call. The R code to generate random numbers from an exponential distribution
takes two arguments, n (the quantity of numbers to be generated), and rate, the num‐
ber of events per time period. For example:

rexp(n = 100, rate = .2)

This  code  would  generate  100  random  numbers  from  an  exponential  distribution
where the mean number of events per time period is 2. So you could use it to simulate
100  intervals,  in  minutes,  between  service  calls,  where  the  average  rate  of  incoming
calls is 0.2 per minute.

A key assumption in any simulation study for either the Poisson or exponential distri‐
bution is that the rate, λ, remains constant over the period being considered. This is
rarely  reasonable  in  a  global  sense;  for  example,  traffic  on  roads  or  data  networks
varies by time of day, and day of week. However, the time periods, or areas of space,
can usually be divided into segments that are sufficiently homogeneous so that analy‐
sis or simulation within those periods is valid.

Inference
In many applications, the event rate, λ, is known or can be estimated from prior data.
However, for rare events, this is not necessarily so. Aircraft engine failure, for exam‐
ple,  is  sufficiently  rare  (thankfully)  that,  for  a  given  engine  type,  there  may  be  little
data on which to base an estimate of time between failures. With no data at all, there
is little basis on which to estimate an event rate. However, you can make some guess -
if no events have been seen after 20 hours, you can be pretty sure that the rate is not 1
per hour. Via simulation, or direct calculation of probabilities, you can assess differ‐
ent  hypothetical  event  rates  and  estimate  threshold  values  below  which  the  rate  is
very unlikely to fall. If there is some data but not enough to provide a precise, reliable
estimate of the rate, a goodness-of-fit test (see ???) can be applied to various rates to
determine how well they fit the observed data.

86 

| 

Chapter 2: Data and Sampling Distributions

Weibull distribution
In many cases, the event rate does not remain constant over time. If the period over
which it changes is much longer than the typical interval between events, there is no
problem - you just subdivide the analysis into the segments where rates are relatively
constant, as mentioned above. If, however, the event rate changes over the time of the
interval, the exponential (or Poisson) distributions are no longer useful. This is likely
to be the case in mechanical failure - the risk of failure increases as time goes by. The
Weibull  distribution  is  an  extension  of  the  exponential  distribution,  in  which  the
event rate is allowed to change, as specified by a shape paramenter, β. If β is > 1, the
probability of an event increases over time, if β is < 1, it decreases. Because the Wei‐
bull distribution is used with time-to-failure analysis, instead of event rate, the second
parameter is expressed in terms of characteristic life, rather than in terms of the rate
of events per interval. The symbol used is η, the Greek letter eta. It is also called the
scale parameter.

With the Weibull, the estimation task now includes estimation of both parameters, β
and η. Software is used to model the data and yield an estimate of the best fitting Wei‐
bull distribution.

The  R  code  to  generate  random  numbers  from  a  Weibull  distribution  takes  three
arguments, n (the quantity of numbers to be generated), shape and scale. For example,
the following code would generate 100 random numbers (lifetimes) from a Weibull
distribution with shape = 1.5 and characteristic life of 5000:

rweibull(100,1.5,5000)

Key Ideas

1. For events that occur at a constant rate, the number of events per unit of time or

space can be modeled as a Poisson distribution

2. In this scenario, you can also model the time or distance between one event and

the next as an exponential distribution

3. A changing event rate over time (e.g. an increasing probability of device failure)

can be modeled with the Weibull distribution

Further reading

1. Modern Engineering Statistics by Tom Ryan (2007, Wiley) has a chapter devoted

to the probability distributions used in engineering applications

Poisson and Related Distributions 

| 

87

2. Further reading on the use of the Weibull distribution (mainly from an engineer‐
ing  perspective):  http://www.sascommunity.org/sugi/SUGI88/Sugi-13-43%20Kay
%20Price.pdf http://www.ipedr.com/vol75/29_ICQM2014-051.pdf

Summary
In the era of big data, the principles of random sampling remain important in cases
where accurate estimates are needed. Random selection of data can reduce bias, and
yield  a  higher  quality  dataset  than  would  result  from  simply  using  all  the
conveniently-available data. Knowledge of various sampling and data generating dis‐
tributions  can  allow  us  to  quantify  the  potential  error  in  an  estimate  that  might  be
due to random variation. At the same time, the bootstrap (sampling with replacement
from  an  observed  dataset)  is  an  attractive  “one  size  fits  all”  method  of  determining
potential error in sample estimates.

88 

| 

Chapter 2: Data and Sampling Distributions

