Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pertemuan 4-5. Scalable Algorithm

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

Brian Steele · John Chandler

Swarna Reddy

Algorithms
for Data
Science
Chapter 3
Scalable Algorithms and Associative
Statistics

Abstract It’s not uncommon that a single computer is inadequate to han-


dle a massively large data set. The common problems are that it takes too
long to process the data and the data volume exceeds the storage capacity of
the host. Cleverly designed algorithms sometimes can reduce the processing
time to an acceptable point, but the single host solution will eventually fail if
data volume is sufficiently great. A far-reaching solution to the data volume
problem replaces the single host with a network of computers across which
the data are distributed and processed. However, the hardware solution is in-
complete until the data processing algorithms are adapted to the distributed
computing environment. A complete solution requires algorithms that are
scalable. Scalability depends on the statistics that are being computed by the
algorithm, and the statistics that allow for scalability are associative statis-
tics. Scalability and associative statistics are the subject of this chapter.

3.1 Introduction

Suppose that the data set of concern is so massively large in volume that it
must be divided and processed as subsets by separate host computers. In this
situation, the host computers are connected by a network. A host computer is
often called a node in recognition of its role as member of the network. Since
the computational workload has been distributed across the network, the
scheme is referred to as a distributed computing environment. Our interest
lies in the algorithmic component of the distributed computing solution, and
specifically the situation in which each node executes the same algorithm but
on a subset of the data. When all nodes have completed their computations,
the results from each node are combined. This is a good strategy if it works:

© Springer International Publishing Switzerland 2016 51


B. Steele et al., Algorithms for Data Science,
DOI 10.1007/978-3-319-45797-0_3
52 3 Scalable Algorithms and Associative Statistics

only one algorithm is needed and everything can be automated. If the nodes
are garden-variety computing units, there’s no obvious limit to the amount
of data that can be handled.
The final result should not depend on how the data is divided into subsets.
To be more precise, recall that a partition of a set A is a collection of disjoint
subsets A1 , . . . , An such that A = ∪i Ai . Now, consider divisions of the data
that are partitions (hence no observation or record is included in more than
one data set). An algorithm is scalable if the results are identical for all
possible partitions of the data. If the scalability condition is met, then only
the number of subsets and nodes need be increased if the data volume should
increase. The only limitation is hardware and financial cost. On the other
hand, if the algorithm yields different results depending on the partition,
then we should determine the partition that yields the best solution. The
question of what is the best solution is ambiguous without criteria to judge
what’s best. Intuition argues that the best solution is that obtained from a
single execution of the algorithm using all the data. Under that premise, we
turn to scalable algorithms.
The term scalable is used because the scale, or volume, of the data does not
limit the functionality of the algorithm. Scalable data reduction algorithms
have been encountered earlier in the form of data mappings. The uses of data
mappings were limited to elementary operations such as computing totals and
building lists. To progress to more sophisticated analyses, we need to be able
to compute a variety of statistics using scalable algorithms, for example, the
least squares estimator of a parameter vector β or a correlation matrix.
Not all statistics can be computed using a scalable algorithm. Statistics
that can be computed using scalable algorithms are referred to as associative.
The defining characteristic of an associative statistic is that when a data set
is partitioned into a collection of disjoint subsets and the associative statistic
is computed from each subset, the statistics can be combined or aggregated
to yield the same value as would be obtained from the complete data set. If
the function of the algorithm is to compute an associative statistic, then the
algorithm is scalable.
To make these ideas more concrete, the next section discusses an exam-
ple involving the Centers for Disease Control and Prevention’s BRFSS data
sets. Then, we discuss scalable algorithms for descriptive analytics. A tuto-
rial guides the reader through the mechanics of summarizing the distribution
of a quantitative variable via a scalable algorithm. Scalable algorithms for
computing correlation matrices and the least squares estimator of the lin-
ear regression parameter vector β are the subject of two additional tutorials.
This first look at predictive analytics demonstrates the importance of scalable
algorithms and associative statistics in data science.
3.2 Example: Obesity in the United States 53

3.2 Example: Obesity in the United States

Mokdad et al. [40] describe an apparent rapid increase in the number of


obese adults in the United States between 1990 and 1999. Similar trends have
been observed by other researchers. The phenomenon has been labeled with
the sobriquet the obesity epidemic in light of the apparent rapid increase in
obesity rate and also because of negative effects of obesity. Most prominently,
obesity is associated with type 2 diabetes, a chronic disease responsible for a
number of conditions that negatively impact quality of life.
Mokdad et al. conducted their analysis using data collected by the U.S.
Centers for Disease Control and Prevention.1 The CDC’s Behavioral Risk
Factor Surveillance System (BRFSS) survey is now the largest periodic sam-
ple survey in the world. The CDC asks a sample of U.S. adult residents a
large number of questions regarding health and health-related behaviors. In
recent years, responses from more than 400,000 adults have been collected per
annum. In the following tutorial, the reader will investigate whether the in-
creasing trend in obesity observed in the last decade of the twentieth century
has continued into the first decade of the twenty-first century.
It’s common that the initial steps of data analysis involve computing mea-
sures of the center and spread of a distribution of a quantitative variable.
If a variable of interest is categorical, then a numerical summarization of
the data involves computing the proportions of observational units that pos-
sess a particular attribute or fall into a particular category. For instance,
body mass index (kg/m2 ) is a widely-used quantitative measure of body fat
from which a categorical measure of obesity (obese or not) can be computed.
A first analysis of the BRFSS data might then compare the estimated mean
of body mass index and the proportions of obese residents for the year 2000
to the same statistics for the year 2010 to search for evidence supporting or
refuting a continuation of the purported obesity epidemic. From a statistical
perspective, we aim to estimate the body mass index mean µ of the popula-
tion of U.S. adult residents at two points in time. More information can be
gleaned about body mass index by computing and constructing a histogram
showing the estimated proportion of the population with values falling in a
set of intervals.
The first algorithmic task is to develop a scalable algorithm for comput-
ing estimates of µ and σ 2 using massively large data. Then, we’ll develop a
scalable algorithm for computing histograms and apply it to the BRFSS data
sets and the body mass index variable.

1
We discussed the BRFSS data briefly in Chap. 1, Sect. 1.2.
54 3 Scalable Algorithms and Associative Statistics

3.3 Associative Statistics

We begin with notation and terminology. Let D = {x1 , x2 , . . . , xn } denote


a set of n observations. The ith observation xi = [xi,1 xi,2 · · · xi,p ]T is a
vector consisting of p real numbers. A partition of D is a collection of disjoint
subsets D1 , D2 , . . . , Dr such that D = D1 ∪ D2 · · · ∪ Dr . Let

s(D) = [s1 (D) s2 (D) · · · sd (D)]


T

d×1

denote an associative statistic, a vector, of dimension d ≥ 1. We say that


a statistic is associative if it possesses an associativity property and is low-
dimensional. We use the term low-dimensional informally to describe a statis-
tic that may be stored without taxing computational resources. In a practical
sense, associativity implies that the data set can be partitioned as r subsets
and the statistic can be computed on each subset. At the completion of all
r computations, the r associative statistics can be aggregated to obtain the
value of the statistic computed on the complete data set. Consequently, there
is no information loss or indeterminacy resulting from distributed processing
of the data, and algorithms that compute associative statistics are scalable.
Concisely, a statistic s is associative if, given a partition {D1 , D2 , . . . , Dr }
of D, the statistics s(D1 ), s(D2 ), . . ., s(Dr ) can be combined to produce the
value s(D). An example of an associative statistic computed from a set of n
real numbers, say D = {x1 , x2 , . . . , xn }, is the two-element vector
!"n #
s(D) = i=1 xi .
2×1 n
"n
The elements of s(D) are s1 (D) = i=1 xi , and s2 (D) = n. Associativity
holds because s(D1 ∪ D2 ) = s(D1 ) + s(D2 ). For example, suppose that D is
partitioned as D1 = {x1 , . . . , xm } and D2 = {xm+1 , . . . , xn }. Then,
!"m # !"n #
xi xi
s(D1 ) + s(D2 ) = i=1 + i=m+1
m n−m
!"m "n #
xi + i=m+1 xi
= i=1
m+n−m
!"n #
xi
= i=1 = s(D).
n

From s(D), we can estimate the population mean using the sample mean
$ = s1 /s2 . The median is an example of a statistic that is not associative.
µ
For example, if D = {1, 2, 3, 4, 100}, D1 = {1, 2, 3, 4}, and D2 = {100}, then
median(D1 ) = 2.5, and there is no method of combining median(D1 ) and
median(D2 ) = 100 to arrive at median(D) = 3 that succeeds in the general
case.
3.4 Univariate Observations 55

3.4 Univariate Observations

Usually, a preliminary objective of data analysis is to describe the center


and spread of the distribution of a variable. This may be accomplished by
estimating the population mean µ and variance σ 2 from a sample of univariate
data D = {x1 , x2 , . . . , xn }. An estimator " of the variance σ 2 is needed in
addition to the estimator of the mean µ $= xi /n, say, the average squared
difference between the observations and the sample mean:
%
$2 = n−1
σ (xi − µ$)2
" & " '2 (3.1)
= n−1 x2i − n−1 xi .

From Eq. (3.1), we may deduce that the associative statistic


 "n 
i=1 xi
" 
s(D) =  ni=1 x2i  (3.2)
3×1
n

may be used to estimate the mean and variance. Let s(D) = [s1 s2 s3 ]T .
Then, the estimators are
s1
$=
µ ,
s3 . /
s2 s1
2 (3.3)
$2 =
σ − .
s3 s3

The statistic s(D) shown "n in Eq." (3.2) is associative


"n because addition is asso-
ciative; for example, i=1 xi = i=1 xi + i=m+1 xi for integers 1 ≤ m < n.
m

Thus, a scalable algorithm for computing estimators of the mean and variance
computes the associative statistic s(D) and then the estimates according to
Eq. (3.3). If the volume of D is too large for a single host, then D can be parti-
tioned as D1 , . . . , Dr and the subsets distributed to r network nodes. At node
j, s(Dj ) is computed. When all nodes have completed their respective "r tasks
and returned their statistic to a single host, we compute s(D) = j s(Dj )
followed by µ $ and σ $2 using formula (3.3).
Let’s consider a common situation. The data set D is too large to be stored
in memory but it’s not so large that it cannot be stored on a single computer.
These data may be a complete set or one subset of a larger partitioned set. In
any case, the data at hand D is still too large to be read at once into mem-
ory. The memory problem may be solved using two algorithmic approaches
sufficiently general to be applied to a wide range of problems. For simplicity
and concreteness, we describe the algorithms for computing the associative
statistic given in Eq. (3.2).
56 3 Scalable Algorithms and Associative Statistics

A block is a subset of D consisting of contiguous lines or records that is


sufficiently small to reside in memory. Suppose the blocks are D1 , D2 , . . . , Dr
and Dj contains nj observations. Then, we may write

D = {x1 , . . . , xn1 , xn1 +1 , . . . , xn1 +n2 , . . . , xn1 +···+nr−1 +1 , . . . , xn1 +···+nr }.


0 12 3 0 12 3 0 12 3
D1 D2 Dr
(3.4)
The blocks form a partition of D because every observation belongs to exactly
one subset. The algorithms are:
1. Process D one block at a time. Compute s(Dj ), j = 1, 2, . . . , r. At the
completion of processing Dj , store s(Dj ) in a dictionary using j as the
key and s(Dj ) as the value. Alternatively, store s(Dj ) as row j of a r × 3
matrix. At completion, compute s(D1 ) + s(D2 ) + · · · + s(Dr ) = s(D).
2. A slightly simpler algorithm builds s(D) as each line is read by up-
dating s. Before processing the data, initialize the associative statistic
s = [s1 s2 s3 ]T as a vector of zeros. The initialization step is written in
short as [0 0 0]T → s where a → b means assign a to b. Upon reading
the jth observation xj ∈ D, update s by computing

s1 + xj → s1
s2 + x2j → s2 (3.5)
s3 + 1 → s3 .

This one-line-at-a-time algorithm is a scalable algorithm. We can see this


by partitioning D as n singleton sets or blocks Dj = {xj }, j = 1, 2, . . . , n.
The statistic s applied to a datum xj is s({xj }) = [xj x2j 1]T . Therefore,
the process of updating s({x1 , x2 , . . . , xj−1 }) computes
4 5
s({x1 , x2 , . . . , xj−1 }) + s({xj }) = s ∪j−1 Di + s(Dj )
4 i=1 5
= s ∪i=1 Di .
j

The sets Dj are not created in practical applications, we just update


the associative statistic as shown in formula (3.5). If the algorithm is to
be used repeatedly, be aware that reading a data set one line at-a-time
may be slower than reading the data file in blocks. In our experience, one
line at-a-time reading is not noticeably slower using Python.
The next section provides an example of a statistic commonly used in
exploratory data analysis: the histogram. It’s often not realized as such, but
a histogram is a statistic that is rendered and interpreted visually. Moreover,
the histogram can be cast as an associative statistic and so it is useful in the
analysis of massively large data sets.
3.4 Univariate Observations 57

3.4.1 Histograms

A thorough analysis of a variable often involves a visual display of its empir-


ical, or sample, distribution. The term empirical is used to distinguish a dis-
tribution constructed from a sample versus the true distribution that would
be obtained if all values across a population were observed. Histograms and
the related boxplots are commonly used for this purpose. When data volume
is large, then the histogram becomes the visual of choice because the distri-
bution may be shown in fine detail. The level of detail is usually under the
control of the analyst. Visually, a histogram shows the empirical distribution
as a set of contiguous rectangles plotted across the range of the variable. The
height of a rectangle is proportional to the frequency (and relative frequency)
of values falling within the interval defined by the rectangle base.
Figure 3.1 shows a pair of overlain histograms built from sample distri-
butions of body mass index (kg/m2 ) of U.S. residents. Body mass index is
body weight scaled by the individual’s height. Scaling accounts for height
differences between individuals and allows for comparisons of weight regard-
less of height. The histograms were constructed using data collected in the
course of the U.S. Centers for Disease Control and Prevention’s Behavioral
Risk Factor Surveillance System survey (see Sect. 1.2 for more details). The
red histogram was constructed from 1,020,126 observations collected in the
years 2000, 2001, 2002, and 2003 and the blue histogram was collected from
2,848,199 observations collected in the years 2011, 2012, 2013, and 2014. The
histograms may be used to examine the assertion that the U.S. has suffered an
epidemic of obesity [26]. Since an individual is considered to be obese if their
body mass index is at least 30 kg/m2 , Fig. 3.1 provides visual confirmation
that the percent of obese adults has increased. The percent of obese individ-
uals in the year 2000 data set was 19.84% whereas 28.74% of the respondents
in the 2014 data set had a body mass index exceeding the threshold. The
increase in the percent obese was 44.8% = 100(28.74 − 19.84)/19.84.
The histograms provide more information, though. There’s been a shift
toward larger values of body mass index from the first to the second time
period. Otherwise, the distributions are similar in shape. Both histograms are
slightly skewed to the right having longer right tails than left tails. Skewness
to the right implies that there are relatively more individuals with very large
values of body mass index than individuals with very small body mass index.2

2
Right-skewness is common when a variable is bounded below as is the case with body
mass index since no one may have a body mass index less than or equal to zero.
58 3 Scalable Algorithms and Associative Statistics

Fig. 3.1 Histograms of body


mass index constructed from 0.09 First
two samples of U.S. residents.

Relative Frequency
Second
The first sample was collected
in the years 2000 through 2003 0.06
and the second sample was
collected in the years 2011
through 2014. All data were 0.03
collected by the U.S. Centers
for Disease Control and Pre-
vention, Behavioral Risk Fac- 0.00
tor Surveillance System 10 20 30 40 50
Body mass index

3.4.2 Histogram Construction

A histogram is a set of pairs. Each pair corresponds to one of the rectangles


that form the visual histogram. One element of the pair is the interval that
defines the base of the rectangle and the second element specifies the height.
The height is either the number of observations that fall in the interval or
the relative frequency of observations falling into the interval. The union of
the intervals span the observed range of a variable of interest. Accordingly,
we define a histogram (mathematically) as the set of pairs
H = {(b1 , p1 ), . . . , (bh , ph )}. (3.6)

The number of intervals is h. The first interval is b1 = [l1 , u1 ] and for i > 1,
the ith interval is bi = (li , ui ]. The second element of the pair, pi , is the
relative frequency of observations belonging to the interval. The term pi is
often used as an estimator of the proportion of the population belonging to
bi . The interval bi+1 takes its lower bound from the previous upper bound,
i.e., li+1 = ui . We show the intervals as open on the left and closed on the
right but there’s no reason not to define the intervals to be closed on the
left and open on the right instead. In any case, equal-length intervals are
formed by dividing the range into h segments. Usually, every observation in
the data set belongs to the base of the histogram, [l1 , uh ]. Admittedly, the
stipulation that all observations are included in the histogram sometimes is
a disadvantage as it may make it difficult to see some of the finer details.
Suppose that the data set D is massively large and cannot reside in mem-
ory. A scalable algorithm is needed to construct the histogram H, and formu-
lating the algorithm requires a precise description of the process of building
H. In brief, H is computed by counting the numbers of observations falling in
each interval. The intervals must be constructed before counting, and hence,
the data set must be processed twice, both times using a scalable algorithm.
The first pass determines the number and width of the intervals. The second
pass computes the numbers of observations each interval.
3.4 Univariate Observations 59

An algorithm for constructing a histogram begins with determining the


smallest and largest observations in D. Let x[1] = min(D) denote the smallest
observation and x[n] = max(D) denote the largest. Every observation in the
data set must be examined to determine x[1] and x[n] , and so the first pass
through the data does nothing more than compute x[1] and x[n] . Then, the
range is computed as x[n] − x[1] . The interval width is w = (x[n] − x[1] )/h,
where h is a choice for the number of intervals. The intervals are
6 7
b1 = x[1] , x[1] + w
..
& . 7
bi = x[1] + (i − 1)w, x[1] + iw
..
& . 7
bh = x[1] + (h − 1)w, x[n] .

In essence, the algorithm maps the data set D to a set of intervals, or bins,
B = {b1 , . . . , bh } and we may write D (→ B.
The second algorithm maps D and B to a dictionary C in which the keys
are the intervals and the values are the counts of observations falling into a
particular interval, and we may write (D, B) (→ C.
Computationally, the second algorithm fills the dictionary C by counting
the number of observations belonging to each interval. Determining which
interval an observation belongs to amounts to testing whether the observa-
tion value belongs to bi , for i = 1, . . . , h.3 When the interval is found that
contains the observation, the count of observations belonging to the interval
is incremented by one, and the next observation is processed. After all ob-
servations are processed, the relative frequency of observations belonging to
each interval is computed by dividing the interval count by n. The dictionary
may then be rendered as a visual.
If the algorithm is to be scalable, then the statistics from which H is built
must be associative. The key to associativity and scalability is that a single
set of intervals is used to form the histogram base.
As described above, the first pass through D computes the two-element
vector s(D) = [min(D) max(D)]T . We’ll argue that s(D) is associative by
supposing that D1 , D2 , . . . , Dr are a partition of D. Let
! #
min(Dj )
s(Dj ) = , j = 1, . . . , r. (3.7)
max(Dj )

Let s1 (D) = min(D) and s2 (D) = max(D) so that s(D) = [s1 (D) s2 (D)]T .
Then,

3
Of course, once it’s been determined that the observation belongs to an interval, there’s
no need to test any other intervals.
60 3 Scalable Algorithms and Associative Statistics

s1 (D) = min(D)
= min(D1 ∪ · · · ∪ Dr )
(3.8)
= min{min(D1 ), . . . , min(Dr )}
= min{s1 (D1 ), . . . , s1 (Dr )}.
Similarly, max(D) = max{max(D1 ), . . . , max(Dr )}. Changing the notation,
we see that s2 (D) = max{s2 (D1 ),. . . , s2 (Dr )}. Since s(D) can be computed
from s(D1 ), . . ., s(Dr ), the statistic s is associative.
The range and intervals of the histogram depend entirely on the associative
statistic s and the choice of h. Given h, the same set of intervals is created
no matter how D is partitioned. Thus, a scalable algorithm that computes B
is feasible.
The second algorithm fills the dictionary C = {(b1 , c1 ), . . . , (bh , ch )} by
determining cj , the count of observations in the data set D that belong to
bj , for j = 1, . . . , h. If D has been partitioned as r subsets, then B and
a partition Dj are used to construct the jth set of counts. Mathematically,
(Dj , B) (→ Cj . Then, the sets C1 , . . . , Cr are aggregated by adding the counts
across sets for each interval.

3.5 Functions

The next tutorial instructs the reader to create a user-defined function. Rea-
sons for using functions are to make a program easier to understand and to
avoid repeatedly programming the same operation. A function consists of a
code segment that is executed by a one line instruction such as y = f(x). The
code segment should have a clearly defined purpose and generally, the code
will be used more than once, say, in more than one location in a program, or
in multiple programs, or in a for loop.
The function definition is located not within the main program but at
the top of the program or in a separate file. However, it’s useful to write
the code that belongs in a function in the main program. Writing the code
in the location where the function will be called allows the programmer to
access the values of the variables within the function code for debugging.
Once the code segment is included in a function, the variables become local
and are undefined outside the function and cannot be inspected. When the
code segment executes correctly, then move the code out of the main program
and into a function.
The function is initialized by the keyword def and is ended (usually) by the
return statement. For example, the function product computes the product
of the two arguments x and y passed to the function:

def product(x,y):
xy = x*y
return xy
3.6 Tutorial: Histogram Construction 61

The function is called using the statement z = product(a,b). The vari-


able xy is local to the function and referencing it outside the program will
raise a NameError. A return statement is not necessary; for example, another
version of the function product prints the product of x and y:

def product(x,y):
print(x*y)

Note that xy will be undefined outside of the function because it’s not re-
turned.
The function must be compiled by the Python interpreter before the func-
tion is called. Therefore, it should be positioned at the top of the file; alter-
natively, put it in a file with a py extension, say functions.py, that contains
a collection of functions. The function product is imported in the main pro-
gram using the instruction

from functions import product

3.6 Tutorial: Histogram Construction

The objective of this tutorial ostensibly is to construct Fig. 3.1. In the pro-
cess of building the histograms, the reader will estimate the distribution of
body mass index of adult U.S. residents for two periods separated by approx-
imately 10 years. The tutorial also expands on the use of dictionaries for data
reduction and exposes the reader to weighted means. The data sets used in
the tutorial originate from the Centers for Disease Control and Prevention
(CDC) and are remarkable for the amount of largely untapped information
contained within.
The data were collected in the course of the Behavioral Risk Factor Surveil-
lance System’s (BRFSS) annual survey of U.S. adults. Telephone interviews
of a sample of adults were conducted in which a wide range of questions
on health and health-related behavior were asked of the respondents. The
respondents were selected by sampling land-line and cell phone numbers,
calling the numbers, and asking for interviews. Land-line and cell phone
numbers were sampled using different protocols and so the sampled indi-
viduals did not have the same probability of being sampled. Consequently,
certain sub-populations, say young land-line owners may be over- or under-
represented in the sample. Disregarding the sampling design may lead to
biased estimates of population parameters. To adjust for unequal sampling
probabilities, the CDC has attached a sampling weight to each observation.
The sampling weights may be incorporated into an estimator to correct for
62 3 Scalable Algorithms and Associative Statistics

unequal sampling probabilities, a subject which we expand on in Chap. 7. A


brief discussion is in order though.
First, consider estimation of a population mean µ. An estimator of µ can
be expressed as
"n
$ = j=1 wj xj ,
µ (3.9)
where wj is a weight reflecting the contribution of xj towards the estimator.
The traditional sample mean is a weighted mean—we see this if wj is defined
to be wj = 1/n for each j = 1, . . . , n. Of course, every observation has the
same weight and contribution towards the estimate. Any set of weights may
be used provided that all weights are non-negative and sum to one. Often,
weights are used to reduce the bias of an estimator.
The sample proportion is also a sample mean provided that the variable be-
ing averaged is an indicator variable. The indicator variable identifies whether
or not the jth sampling unit possesses a particular attribute. For instance,
we may define I≥30 (xj ) as an indicator variable identifying obese individuals.
Obesity is defined by a body mass index of 30 kg/m2 or more. Thus, the
indicator variable is
8
1, if x ≥ 30,
I≥30 (x) = (3.10)
0, if x < 30,
where x is the body mass index of the individual. We say that I is an indicator
variable for the event {x ≥ 30}. The sample mean of I≥30 (x1 ), . . . , I≥30 (xn )
is the proportion of individuals in the sample that are obese.
In the context of histograms, the property of interest is whether or not a
body mass index value xj is contained in interval bi . The proportion of the
population with membership in bi is estimated by the sample mean of the
indicator variables, say
%n
pi = n−1 Ii (xj ), (3.11)
j=1

where Ii (x) is an indicator variable of the event {x ∈ bi }. Since we want to


use sampling weights with the BRFSS data, we define the estimator"n of the
proportion of the population with membership in bi as pi = j=1 wj Ii (xj )
assuming that the weights are non-negative and sum to one. If the weights
are non-negative but do not sum to one, then we may scale the weights by
computing
"
j wj Ii (xj )
pi = " , (3.12)
j wj
" "
which amounts to using a set of weights v1 = w1 / wj , . . . , vn = wn / wj
that sum to one.
Since our aim is to compare adult distributions of body mass index from
two decades, we use 4 years of data from each decade and thus eight data
files in the analysis.
3.6 Tutorial: Histogram Construction 63

1. Create a directory for storing the data files. We recommend that you keep
the files related to this book in a single directory with sub-directories for
each chapter. Some data files and some functions will be used in more
than one chapter so it’s convenient to build a sub-directory to save these
data files and a sub-directory to contain the functions.4 The following
directory structure is suggested:

Algorithms
DataMaps
PythonScripts
RScripts
Data
ScalableAlgorithms
PythonScripts
RScripts
Data
Data
LLCP2014.ASC
LLCP2013.ASC
ModuleDir
functions.py

The directory ModuleDir is to contain user-written modules. A module


is a collection of functions that may be loaded and used by any Python
script. Using functions and modules promotes organized code and reduces
the likelihood that you’ll write code to perform the same operations more
than once.
2. Navigate to the Behavioral Risk Factor Surveillance System data portal
http://www.cdc.gov/brfss/annual_data/annual_data.htm. Retrieve
the files listed in Table 3.1 and place them in the data directory created
in instruction 1. Extract or decompress each file and delete the zip files.

Table 3.1 BRFSS data file names and sub-string positions of body mass index, sam-
pling weight, and gender. Positions are one-indexed
Body mass index Sampling weight Gender
Year File Start End Start End field
2000 cdbrfs00asc.ZIP 862 864 832 841 174
2001 cdbrfs01asc.ZIP 725 730 686 695 139
2002 cdbrfs02asc.ZIP 933 936 822 831 149
2003 CDBRFS03.ZIP 854 857 745 754 161
2011 LLCP2011.ZIP 1533 1536 1475 1484 151
2012 LLCP2012.ZIP 1644 1647 1449 1458 141
2013 LLCP2013.ZIP 2192 2195 1953 1962 178
2014 LLCP2014.ZIP 2247 2250 2007 2016 178

4
Chapter 7 uses these BRFSS data files in all of the tutorials.
64 3 Scalable Algorithms and Associative Statistics

3. If you want to look at the contents of one of the data files, open a Linux
terminal and submit a command of the form:

cat LLCP2011.ASC | more

Each record in a BRFSS data file is a character string and without de-
limiters to identify specific variables. The file format is fixed-width, im-
plying that variables are located according to an established and un-
changing position in the string (recall that the record is a character
string). Consequently, variables are extracted as substrings from each
record. Regrettably, string or field position depends on year and the
field positions must be determined anew each time a different year is
processed. Table 3.1 contains the field positions for several variables.
The field positions are exactly as presented in the BRFSS codebooks.
The codebooks describe the variables and field positions. For example,
https://www.cdc.gov/brfss/annual_data/2014/pdf/codebook14_llcp.pdf
is the year 2014 codebook.
Table 3.1 field positions are one-indexed. When one-indexing is used,
the first character in the string s is s[1]. Python uses zero-indexing to
reference characters in a string5 so we will have to adjust the values in
Table 3.1 accordingly.
4. Create a Python script. The first code segment creates a dictionary that
contains the field positions of body mass index and sampling weight.
We’ll create a dictionary of dictionaries. The outer dictionary name is
fieldDict and the keys of this dictionary are years, though we use only
the last two digits of the year rather than all four. The first instruction
in the following code segment initializes fieldDict. The keys are defined
when the dictionary is initialized.
The values of fieldDict are dictionaries in which the keys are the
variable names and the values of these inner dictionaries are pairs iden-
tifying the first and last field positions of the variable. The dictionaries
for year 2000 (field[0]) and 2001 (field[1]) are shown in the two lines
following the initialization of fieldDict:

fieldDict = dict.fromkeys([0, 1, 2, 3, 11, 12, 13, 14])


fieldDict[0] = {’bmi’:(862, 864), ’weight’:(832, 841)}
fieldDict[1] = {’bmi’:(725, 730), ’weight’:(686, 695)}

5. Using the information in Table 3.1, add the remaining inner dictionaries
entries to fieldDict, that is, for the years 2002, 2003, 2011, 2012, 2013,
5
The first character in the string in Python is s[0].
3.6 Tutorial: Histogram Construction 65

and 2014. Check your code by printing the contents of fieldDict and
comparing to Table 3.1. Iterate over year and print:

for year in fieldDict:


print(year, fieldDict[year])

6. We’ll use fieldDict in the several other tutorials and add additional
variables and years. To keep our scripts short and easy to read, move the
code segment that builds fieldDict to a function. For the moment, put
the function definition at the top of your Python script.

def fieldDictBuild():
fieldDict[0] = {’bmi’:(862,864),’weight’:(832,841)}
...
fieldDict[14] = {’bmi’:(2247,2250),’weight’:(2007,2016)}
return fieldDict

7. Call the function with the instruction fieldDict = fieldDictBuild().


Print fieldDict and check that it is agrees with the entries in Table 3.1.
8. The next two instructions direct the reader on building a module to con-
tain fieldDictBuild. Begin by creating a directory named ModuleDir
and a Python script named functions.py. Instruction 1 provides a sug-
gestion on the structure and names of the directories. Remove fieldDict
from its position at the top of the script and put it in functions.py. Now
functions.py is a module—a collection of definitions and functions that
may be called from other programs. The function fieldDictBuild cre-
ates fieldDict by executing the instruction

fieldDict = functions.fieldDictBuild()

However, before this function call will execute successfully, the functions
module must be imported using the instruction

from ModuleDir import functions

ModuleDir is the directory containing the file functions.py. The direc-


tory may contain other modules that are unrelated to the purposes of
functions.
9. Lastly, if the directory containing functions.py is not the same as the
location of the script being executed, then the interpreter must be in-
structed on where to search for functions.py. If this is the case, then
direct the compiler where to search for the module. Assuming that the full
66 3 Scalable Algorithms and Associative Statistics

path to functions.py is /home/Algorithms/ModuleDir/functions.py,


put the following instruction at the top of the script:

import os,sys

parentDir = r’/home/Algorithms/’
if parentDir not in set(sys.path):
sys.path.append(parentDir)
print(sys.path)
from ModuleDir import functions
dir(functions)

Your path, (parentDir), may be different.6 Notice that the path to the
directory omits the name of the directory containing the function. Adding
r in front of the path instructs Python to read backslashes literally. You’ll
probably need this if you’re working in a Windows environment.
When you modify functions.py, it will have to be reloaded by the
interpreter for the changes to take effect. You have to instruct the inter-
preter to reload it or else you have to restart the interpreter.7 You can
reload a module using a function from a library. If you’re using Python
3.4 or later, then import the library importlib using the instruction
import importlib. The instructions are

import importlib
reload(functions) # Python 3.4 or above

to reload functions. If reload does not update a change to


functions.py, then restart the console. Restarting a Python console will
also (re)load all of the modules. If your version of Python is 3.3 or less,
then import the library imp and reload functions with the instructions:

import imp
imp.reload(functions) # Python 2 and 3.2-3.3

6
It will not begin with /home/... if your operating system is Windows.
7
In Spyder, close the console, thereby killing the kernel, and start a new console to
restart the interpreter.
3.6 Tutorial: Histogram Construction 67

After reloading, check the contents of the module functions with the in-
struction dir(functions). The dir(functions) will list all of the func-
tions that are available including a number of built-in functions.8
10. The function fieldDictBuild will build fieldDict when called so:

fieldDict = functions.fieldDictBuild()

Add the instruction to the script after the reload(functions) instruc-


tion.
11. Build a for loop that will process all of the files located in the data
directory. First, create a list containing the names of the files in the
directory. Then iterate over the list and as the iteration progresses, test
whether the current file name is one of the BRFSS data files listed in
Table 3.1. This is accomplished by extracting the two character substring
occupying positions 6 and 7 from the file name. If the two-character
string cannot be converted to an integer, then the file name is not one of
the BRFSS data files and a ValueError will be thrown by the Python
interpreter. Execute the conversion of string to integer with an exception
handler so that any file that is in the directory and is not BRFSS data
file will be passed over without causing the program to terminate.

path = r’../Data/’ # Set the path to match your data directory.


fileList = os.listdir(path) # Creates a list of files in path
for filename in fileList:
try:
shortYear = int(filename[6:8])
year = 2000 + shortYear

fields = functions.fieldDict[shortYear]
sWt, eWt = fields[’weight’]
sBMI, eBMI = fields[’bmi’]

file = path+filename
print(file,sWt, eWt,sBMI, eBMI)
except(ValueError, KeyError):
pass

The field positions of the sampling weight and body mass index are ex-
tracted in the middle block of three instructions. The first instruction
extracts the fields dictionary from fieldDict using the two-digit year
as the key. Then, the starting and ending positions of the variables are
extracted. The starting and ending positions are the field positions trans-
lated from the BRFSS codebook. The BRFSS codebook for a specific year
8
Execute functions.py if your function in functions.py is not compiling despite calling
the reload function.
68 3 Scalable Algorithms and Associative Statistics

can be found on the same webpage as the data file.9 The codebook lists
the field positions using one-indexing. One-indexing identifies the first
field in the string as column 1. However, Python uses zero-indexing for
strings and so we will have to adjust when extracting the values.
12. The following code segment processes each data file as the program iter-
ates over the list of files. The code must be nested within the try branch
of the exception handler (instruction 11). Insert the code segment after
the print statement above.

with open(file, ’r’, encoding=’latin-1’) as f:


for record in f:
print(len(record))

In Python 3, the instruction with open forces the file to close when the
for loop is interrupted or when the end of the file is reached. We do not
need the instruction f.close(). Hence, all instructions that are to be
carried out while the file is being read must be nested below the with
open instruction.
13. In the for loop of instruction 12, extract body mass index and sampling
weight from each record by slicing. If a string named record is sliced using
the instruction record[a:b], then the result is a substring consisting of
the items in fields a, a + 1, . . . , b − 1. Note that the character in field b is
not included in the slice. Convert the sampling weight string to a float
using the one-index field positions sWt and eWt. Also extract the string
containing the body mass index value using sBMI and eBMI extracted in
instruction 11.

weight = float(record[sWt-1:eWt])
bmiString = record[sBMI-1:eBMI]

The next code segment converts bmiString to a float.


14. The BRFSS format and missing value code for body mass index depends
on year so there will be year-specific instructions for translating strings
to floats. Further, the decimal point has been left out of the string and
has to be inserted. The following code translates bmiString to a float
value if the missing value code is not encountered. If a missing value code
is encountered, then body mass index will be assigned the value 0. Since
none of the histogram intervals contain 0, a record with a missing value
code will be effectively omitted from the construction of the histograms.

9
The codebook contains a wealth of information about the data and data file structure.
3.6 Tutorial: Histogram Construction 69

bmi = 0
if shortYear == 0 and bmiString != ’999’:
bmi = .1*float(bmiString)
if shortYear == 1 and bmiString != ’999999’:
bmi = .0001*float(bmiString)
if 2 <= shortYear <= 10 and bmiString != ’9999’:
bmi = .01*float(bmiString)
if shortYear > 10 and bmiString != ’ ’:
bmi = .01*float(bmiString)
print(bmiString, bmi)

The length of the blank string must be the same as the length of
bmiString.
15. When the conversion of the string containing body mass index to the
decimal expression works correctly, then turn it into a function by placing
the declaration

def convertBMI(bmiString, shortYear):

before the code segment. Indent the code segment and add the instruction
return bmi at the end of the code segment.
16. Add the instruction to call the function:

bmi = convertBMI(bmiString, shortYear)

after the function. Run the program. If it is successful, then move the defi-
nition of convertBMI to functions.py. The function will not be available
until the functions is recompiled, so execute the script functions.py.
Call the function using the instruction

bmi = functions.convertBMI(bmiString, shortYear)

17. Go back to the beginning of the program and set up a dictionary to con-
tain the histograms. One histogram will be created for each year, and
each histogram will be represented by a dictionary that uses intervals as
keys and sums of sampling weights as values. (Ordinarily, the value would
be a count of observations that fell between the lower and upper bounds
of the interval). Each histogram interval is a tuple in which the tuple ele-
ments are the lower and upper bounds of the interval. The value is a float
since it is a sum of sampling weights. The set of intervals corresponding
to a histogram are created once as a list using list comprehension:
70 3 Scalable Algorithms and Associative Statistics

intervals = [(10+i, 10+(i+1)) for i in range(65)]

Place this instruction before the for loop that iterates over fileList.
The first and last keys are (10,11) and (74,75). It will become appar-
ent momentarily that the histogram spans the half-open interval (10, 75]
because of the way we test whether an observation falls in an interval. A
few individuals have a body mass index outside this range. We will ignore
these individuals since there are too few of them to affect the histogram
shape and including them interferes with readability.
18. Build a dictionary of histograms in which the keys are years and the
values are dictionaries.

years = [2000, 2001, 2002, 2003, 2011, 2012, 2013, 2014]


histDict = {}
for year in years:
histDict[year] = dict.fromkeys(intervals,0)

The value associated with the key year is a dictionary. The keys of these
inner dictionaries are the histogram intervals for the year. The values of
the inner dictionary are initialized as 0.
19. Return to the for loop that processes fileList. We’ll fill the histogram
corresponding to the data file or equivalently, the year, as the file is pro-
cessed. The first step is to identify the histogram to be filled by adding the
instruction histogram = histDict[year] immediately after extracting
the field positions for weight and body mass index (instruction 11).
20. We will assume that a ValueError has not be thrown and thus body
mass index and sampling weight have been extracted successfully from
the record. Increment the sum of the weights for the histogram interval
that contains the value of body mass index. The for loop below iter-
ates over each interval in histogram. The lower bound of the interval is
interval[0] and the upper bound is interval[1].

for interval in histogram:


if interval[0] < bmi <= interval[1]:
histogram[interval] += weight
break

The break instruction terminates the for loop when the correct interval
has been found.
This code segment must be located inside the for loop initiated by
for record in f so that it executes every time bmiString is converted
to the float bmi. Indentation should be the same as the statement if
shortYear > 10 and bmiString != ‘ ’:.
3.6 Tutorial: Histogram Construction 71

When the end of the file has been reached, histogram will con-
tain the sum of the weights shown in the numerator of Eq. (3.12).
The dictionary histDict[year] will also have been filled since we
set histogram = histDict[year] and the result of this instruction
is that histDict[year] and histogram reference the same location
in memory. You can test this by executing print(id(histogram),
id(histDict[year])). The function id reveals the unique identifier of
the object.10
21. It may be of interest to count the number of body mass index values
that are outside the interval (10, 75]. Initialize two variables before the
files are processed by setting them equal to zero. Give them the names
outCounter and n. Add the following instructions and indent them so
that they execute immediately after the code segment shown in instruc-
tion 20.

n += 1
outCounter += int(bmi < 10 or bmi > 75)
if n % 10000 == 0:
print(year,n,outCounter)

Thus far, the program functions as a mapper algorithm by mapping


records to annual histograms. The next code segment functions as a re-
ducer algorithm by mapping annual histograms to decadal histograms.
The code segment executes after all the data files have been processed.
22. Initialize a two-element list decadeWts to contain the sampling weight
totals for each decade. Create a dictionary named decadeDict with the
same structure as histDict except that the keys are decades instead of
years. The value associated with a decade key is a histogram dictionary.
The keys of the histogram dictionaries are the intervals and the values
are sampling weight totals. The code follows:

decadeWts = [0]*2
decades = [0, 1]
decadeDict = {}
for decade in decades:
decadeDict[decade] = dict.fromkeys(intervals, 0)

The list intervals was created earlier (instruction 17).


23. Construct a for loop that iterates over the list years and maps the
annual histograms to the appropriate decadal histograms:

10
It’s informative to submit the instruction a = b = 1 at the console. Then, submit a
= 2 and print the value of b. The moral of this lesson is be careful when you set two
variables equal.
72 3 Scalable Algorithms and Associative Statistics

for year in years:


decade = int(year/2005)
histogram = histDict[year]

The instruction decade = int(year/2005) produces the largest inte-


ger less than or equal to 2005/year, thereby determining the decade as
0 or 1.
24. As the for loop of instruction 23 iterates over year, compute the sum
of the sampling weights for the year. Specifically, extract the sampling
weight sum associated with each interval and increment the sampling
weight sum in the decade dictionary:

for interval in histogram:


weightSum = histogram[interval]
decadeDict[decade][interval] += weightSum
decadeWts[decade] += weightSum

Since the for loop is to execute for each year, it must be aligned with
the instruction histogram = histDict[year].
25. We may now scale the decade histograms to contain the estimated pro-
portions of the population with body mass index falling in a particular
interval.

for decade in decadeDict:


histogram = decadeDict[decade]
for interval in histogram:
histogram[interval] = histogram[interval]/decadeWts[decade]

Again, we’ve used the fact that assigning one variable equal to another
variable only generates two names for the same variable (or memory
location).
26. We will use the Python module matplotlib to graph the histograms for
the two decades. In preparation for plotting, import the plotting function
pyplot from matplotlib and create a list x containing the midpoints of
the histogram intervals. Also create a list y containing the estimated pro-
portions associated with each interval. Exclude from x and y the intervals
beyond 50 since relatively few individuals have a body mass index greater
than 50.
3.6 Tutorial: Histogram Construction 73

import matplotlib.pyplot as plt

x = [np.mean(pair) for pair in intervals if pair[0] < 50]


y = [decadeDict[0][pair] for pair in intervals if pair[0] < 50]
plt.plot(x, y)
y = [decadeDict[1][pair] for pair in intervals if pair[0] < 50]
plt.plot(x, y)
plt.legend([str(label) for label in range(2)], loc=’upper right’)

Two histograms are plotted on the same plot.


The histograms do not have the traditional rectangular or step form. If
you include the argument drawstyle=’steps’ in the call plt.plot(x,
y), then the histogram will appear as a series of steps. (We find that it
is not as easy to discriminate between the two histograms if they appear
as a series of steps). If the histograms are to appear on separate figures,
then set the figure number before the histogram is plotted.

x = [np.mean(pair) for pair in intervals if pair[0] < 50]


y = [decadeDict[0][pair] for pair in intervals if pair[0] < 50]
plt.figure(1)
plt.plot(x, y, drawstyle=’steps’)
y = [decadeDict[1][pair] for pair in intervals if pair[0] < 50]
plt.figure(2)
plt.plot(x, y, drawstyle=’steps’)

27. There is visual evidence of a difference between decades with respect


to the distributions of body mass index but it does not seem to us to
be very convincing evidence supporting the assertion that the U.S. is
suffering an obesity epidemic. Compute an estimate of the proportion of
the populations that are classified as obese, or equivalently, that have a
body mass index greater than or equal to 30 kg/m2 for the two decades.
The computation for the first decade is

print(sum([decadeDict[0][(a,b)] for a, b in decadeDict[0] if a >= 30]))

28. Compute the percent change relative to the earlier decade, say 100($ π2010 −
$2000 )/$
π π2000 %. Does this statistic support the assertion that an epidemic
is occurring?11

11
We think so.
74 3 Scalable Algorithms and Associative Statistics

3.6.1 Synopsis

We have reduced millions of observations on body mass index to a very


compact summary—two histograms consisting of the estimated proportion
of adult U.S. residents falling into 65 one-unit body mass index intervals.
From the two histograms, we have clear, empirical evidence of a shift in body
mass index over roughly a 10 year time span.
Often, boxplots are better than histograms when the objective is primar-
ily a visual contrast of two or more distributions. Boxplots are preferable to
histograms when the number of observations is small and it’s desirable to
visually identify outliers. The boxplot statistics are the 25th, 50th and 75th
percentiles, collectively referred to as the quartiles. Finding the quartiles re-
quires the data to be ordered, and if the data are partitioned as subsets
D1 , D2 , . . . , Dr , and separately processed to compute r sets of quartiles, then
there is no method that will aggregate the quartiles from each subset and ob-
tain the exact quartiles of D. Quartiles, and hence, boxplot statistics are not
associative. This raises the problem of determining an algorithm for comput-
ing the boxplot statistics from a massively large data set—there’s no obvious
path forward and the problem becomes mired in compromises and approxi-
mations. In contrast, the path for building histograms from massively large
data sets is straightforward and unambiguous.

3.7 Multivariate Data

In this section the reader is introduced to basic algorithms for multivariate


data processing. We continue to develop the theme of scalability and asso-
ciative statistics. It should be kept in mind that multivariate analysis is a
broad area, drawing on a wide variety of methods from statistics and numer-
ical linear algebra. Most the methods are sophisticated both mathematically
and computationally and thus beyond the scope of this text. In fact, one
cannot really get very far into the subject without some knowledge of matrix
algebra. Therefore, we proceed under the assumption that the reader has a
familiarity with matrix notation and arithmetic operations involving vectors
and matrices. Chapter 1, Sect. 1.10.1 provides a short review.
The data are presumed to be a set of multivariate observations collected
by sampling a population or observing a process. An example of a process is
the stream of price quotations generated by trading activity on the NASDAQ
stock exchange.12 In any minute of trading, a quotation may be produced on
any of the 3100 or so stocks, and if the quotations are aggregated by the
minute, then we may think of trading as a process generating a stream of

12
NASDAQ is the abbreviation for the National Association of Securities Dealers Au-
tomated Quotations system.
3.7 Multivariate Data 75

observations on a 3100-element vector. The elements of each vector are the


most recent asking price of the stocks. The analysis of this stream in real-
time has become both highly profitable and in some quarters, objectionable.
We’ll work with a univariate form of the NASDAQ stream and forecasting
in Chap. 11. The Behavioral Risk Factor Surveillance System discussed in
Sect. 3.6 exemplifies population sampling. From each respondent, the answers
to a number of questions are obtained, and so each question is in essence one
of many variables. The reader will use these data and the algorithms of this
section to investigate relationships between obesity, income, and education
in U.S. adults.
More generally, the objectives of multivariate data analysis routinely in-
clude estimating the mean level and variance of the variables, computing the
sample correlation between variables, and estimating the coefficients of a lin-
ear model that predicts one of the variables from one or more of the other
variables. Before developing scalable algorithms for accomplishing these ob-
jectives, some notation and terminology needs to be established.

3.7.1 Notation and Terminology

Suppose that the data consists of observations on n units and on each,


the values of p variables are observed. The data set is denoted as D =
{x1 , x2 , . . . , xn } and the ith observation in D is a vector

xi = [xi,1 xi,2 · · · xi,p ]T (3.13)


p×1

in which the jth element, xi,j , is an observation on the jth variable.


From a statistical perspective, the data often are treated as n realizations
of a multivariate random vector X = [X1 X2 · · · Xp ]T with expectation
E(X) = µ and variance matrix var(X) = Σ where

µ = [E(X1 ) E(X2 ) · · · E(Xp )] ,


T
p×1 (3.14)
= [µ1 µ2 · · · µp ]T .

We set µi = E(Xi ). The variance matrix is


 2 
σ1 σ12 · · · σ1p
 σ21 σ22 · · · σ2p 
 
Σ = . .. ..  .
p×p  .. . . 
σp1 σp2 · · · σp2

The diagonal elements σ12 , . . . , σp2 are the variances of each univariate variable.
The standard deviation σj = E[(Xj − µj )2 ]1/2 may be interpreted as the
76 3 Scalable Algorithms and Associative Statistics

average (absolute) difference between the mean µj and the realized values of
the variable. The off-diagonal elements of Σ are referred to as the covariances.
The principal use of the covariances is to describe the strength and direction
of association between two variables. Specifically, the population correlation
coefficient
σjk
ρjk = = ρkj (3.15)
σj σk
quantifies the strength and direction of linear association between the jth
and kth random variables. The correlation coefficient is bounded by −1 and
1, and values of ρjk near 1 or −1 indicate that the two variables are nearly
linear functions of each other. Problem 3.6 asks the reader to verify this
statement. When ρjk is positive, the variables are positively associated and
as the values of one variable increase, the second also tends to increase. If
ρjk < 0, the variables are negatively associated and as the values of one
variable increase, the second tends to decrease. Values of ρjk near 0 indicate
that the relationship, if any, is not linear.
The correlation matrix is
 
1 ρ12 · · · ρ1p
 ρ21 1 · · · ρ2p 
 
ρ = . . . ..  .
p×p  .. . . 
ρp1 ρp2 · · · 1

Together, µ, Σ, and ρ provide a substantial amount of information about


a population or process. However, these quantities rarely can be determined
precisely and must be estimated from the data. Multivariate data analysis
usually begins with estimation of these parameters.

3.7.2 Estimators

The mean vector µ is estimated by the sample mean vector


 "n 
"in xi,1
 
 i xi,2 
x = n−1  . .
.
"n.
p×1  
i xi,p

A scalable algorithm for computing x is straightforward since x is a func-


tion of a vector of sums, and the sum is an associative statistic. A scalable
algorithm for computing the sample
"n mean vector reads the data in blocks or
line-by-line to compute the sums i xi,j , j = 1, . . . , p. It’s not so obvious but
an estimator of σj2 may also be computed from an associative statistic using
a scalable algorithm.
3.7 Multivariate Data 77

We will not use the traditional moment estimator of σi2 but instead use
an estimator that is nearly equivalent and not as messy for developing the
estimator as a function of an associative statistic. Specifically, we use n as
the denominator instead of the traditional n − 1. The choice of n simplifies
computations but introduces a downward bias in the estimators. When n is
not small, but instead is at least 100, the bias is negligible. In any case, the
variance estimator is
"
(xi,j − xj )2
$j2 = i
σ
n
= n−1
&" 2 2
' (3.16)
i xi,j − nxj
"
= n−1 i x2i,j − x2j .

The estimator of the covariance between variables j and k is


"
(xi,j − xj )(xi,k − xk )
$jk = i
σ
n
"
= n−1 ( i xi,j xi,k − nxj xk ) (3.17)
"
= n−1 i xi,j xi,k − xj xk .

Our estimator σ $jk also differs from the traditional moment estimator of σjk
by using n as the denominator in place of n − 1 for the previous reason—
there’s no practical difference and the mathematics are simpler.
The variance matrix Σ is estimated by
 2 
$1 · · · σ
σ $1p
. . 
$ =
Σ  .. . . . .. 
$p2
$p1 · · · σ
σ
 % % 
n−1 x2i,1 − x21 · · · n−1 xi,1 xi,p − x1 xp (3.18)
 
 i i 

= .. .. .. 
. .
%.
.
 % 
 n−1 n−1 x2i,p − x2p 
x i,p xi,1 − xp x1 · · ·
i i

The outer product13 of x with itself is a p × p matrix given by


 2 
x1 · · · x1 xp
x x T =  ... . . . ...  .
 
p×11×p
xp x1 · · · x2p

13
The inner product of a vector w with itself is the scalar wT w (Chap. 1, Sect. 1.10.1).
78 3 Scalable Algorithms and Associative Statistics

The outer product leads to a simpler alternative formula to Eq. (3.18):

$ = n−1 M − xxT ,
Σ (3.19)

where
 " 2 " " 
" xi,1 "xi,1 xi,2 · · · " xi,1 xi,p
 xi,2 xi,1
 x2i,2 ··· xi,2 xi,p 

M = .. .. .. .. 
.
" . " . ". 2
p×p  
xi,p xi,1 xi,p xi,2 ··· xi,p

is the raw or uncentered moment matrix. The statistic M is composed of


sums and is associative. Thus, M and Σ $ can be computed using a scalable
algorithm. An algorithm for simultaneously computing M and x will be de-
scribed momentarily.
The estimator of ρ is the correlation matrix
 
1 r12 · · · r1p
 r21 1 · · · r2p 
 
R= . . . . ,
 .. .. . . .. 
rp1 rp2 · · · 1

where "
(xi,j − xj )(xi,k − xk )
rjk = . (3.20)
$j σ
σ $k
The matrix R is symmetric because rjk = rkj for each 1 ≤ j, k ≤ p. Further-
more, R is a product of two matrices D and Σ
$ given by

R = DΣD,
$ (3.21)

where D is the diagonal matrix


 
$1−1 0 · · · 0
σ
 0 σ $2−1 · · · 0 
 
D= . .. . . ..  .
 .. . . . 
0 0 ··· σ $p−1

Computing R is easy using formula (3.21); of course, this depends on the


programming language having implemented a matrix product operator.
The computation of x, Σ
$ and R is expedited if the data vectors are aug-
mented by concatenating 1 to each vector. The moment matrix obtained from
the augmented data vectors is the topic of the next section.
3.7 Multivariate Data 79

3.7.3 The Augmented Moment Matrix

Each data vector is augmented by concatenating a 1 before the data values.


The ith the augmented vector is then
 
9 : 1
1  xi,1 
 
wi = xi =  .  .
(p+1)×1  .
. 
p×1
xi,p

The outer product of wi with itself is a (p + 1) × (p + 1) matrix


   
1 1 xi,1 · · · xi,p
 xi,1  6 2
  7  
 xi,1 xi,1 · · · xi,1 xi,p 
wi wiT =  .  1 xi,1 · · · xi,p =  . .. .. .. .
 ..   .. . . . 
xi,p xi,p xi,p xi,1 · · · x2i,p

The sum of n outer products forms the augmented matrix A:


"
A = i wi wiT
(p+1)×(p+1)
 " " " 
"n " xi,1 " xi,2 · · · " xi,p
 xi,1 2
" xi,1 xi,1 xi,2 · · · " xi,1 xi,p 
" "  (3.22)
 xi,2
= xi,2 xi,1 x2i,2 · · · xi,2 xi,p 
.
 .. .. .. .. .. 
 . . . . . 
" " " " 2
xi,p xi,p xi,1 xi,p xi,2 · · · xi,p

The augmented moment matrix differs from the moment matrix M by


having an additional row along the top and left side that contains n and the
sums of each variable. Hence, the augmented matrix may be expressed as
 
n nxT
A =  1×1 1×p  . (3.23)
nx M
p×1 p×p

The augmented moment matrix is an associative statistic, and from A, the


statistics M and nx are readily extracted. With M and nx, the variance
matrix Σ $ can be computed using formula (3.19). Likewise, D, and R are
easily computed. A computationally efficient algorithm will compute A by
iterating over x1 , . . . , xn . The estimates x, Σ,
$ and R are computed from A
using matrix operations.
In a distributed environment, the data set D is partitioned as subsets
D1 , D2 , . . . , Dr and each subset is processed separately. Subset Dj yields an
80 3 Scalable Algorithms and Associative Statistics

augmented matrix Aj , and A1 , . . . , Ar are joined at completion of the r


processes as
%r
A= Aj .
j=1

Then, M and x are extracted from A and Σ,


$ D, and R are computed.

3.7.4 Synopsis

The sequence of computations used to compute the sample mean vector,


sample variance, and sample correlation matrix are
1. Partition the data set D as r subsets (if necessary) and distribute the sub-
sets on separate nodes. Carry out steps 2 and 3 on nodes j = 1, 2, . . . , r.
2. At node j, initialize Aj as a (p + 1) × (p + 1) matrix of zeros.
3. At node j, read Dj sequentially, one record at a time or in blocks. With
the ith record:
a. Extract xi,1 , . . . , xi,p .
b. Form the augmented observation vector wi from xi,1 , . . . , xi,p .
c. Update Aj by computing Aj + wi wiT → Aj .
"r
4. Transfer A1 , . . . , Ar to a single node and compute A = j=1 Aj .
5. Extract the first row of A and compute the sample mean vector x.
6. Extract M from A.
7. Compute Σ $ from n, x and M.
8. Construct D.
9. Compute R = DΣD. $

3.8 Tutorial: Computing the Correlation Matrix

Obesity is sometimes said to be a disease of the poor [35]. From a clinical


perspective, obesity is an indicator of excessive body fat, a condition that has
been observed to be associated with a number of chronic diseases. For the
sake of argument, we point out that claiming obesity to be a disease of the
poor is contradicted by national-level obesity rates. Specifically, a comparison
across countries reveals that obesity rates tend to be greater in high-income
countries compared to middle- and low-income countries [43]. An opportunity
to ascertain whether there is indeed a correlation between income and obesity
among adult residents of the United States presents itself in the databases of
Behavioral Risk Factor Surveillance System. If we entertain the hypothesis
that obesity is related to diet, and diet is related knowledge of nutrition,
3.8 Tutorial: Computing the Correlation Matrix 81

then educational level also may be associated with obesity. We may as well
include education in our investigation. The BRFSS asks survey respondents
to report household income, or more precisely to identify an income bracket
containing their income. Respondents also report their highest attained level
of education, body weight, and height. The CDC computes body mass index
from the reported weight and height of the respondents.
The clinical definition of obesity is a body mass index greater than or
equal to 30 kg/m2 . From the data analytic standpoint, obesity is a binary
variable and not suited for computing correlation coefficients.14 Furthermore
it’s derived from a quantitative variable, body mass index. Body mass index
generally is a better variable from an analytical standpoint than obesity. One
need only realize that a person is obese if their body mass index is 30 or 60
kg/m2 . The consequences of excessive body fat differ substantially between
the two levels of body mass index yet the binary indicator of obesity does
not reflect the differences. Our investigation will use body mass index and
examine associations by computing the (pair-wise) correlation coefficients
between body mass index, income level, and education level of respondents
using BRFSS data.
Income level is recorded in the BRFSS databases as an ordinal variable. An
ordinal variable has properties of both quantitative and categorical variables.
The values x and y of an ordinal variable are unambiguously ordered so
that there’s no debate of whether x < y or vice versa. On the other hand, the
practical importance of numerical difference x−y may depend on x and y. For
example, there are eight values of income: 1, 2, . . . , 8, each of which identifies
a range of annual household incomes. A value of 1 identifies incomes less than
$10,000, a value of two identifies incomes between $10,000 and $15,000, and
so on. It’s unclear whether a one-level difference has the same meaning at the
upper and lower range of the income variable. Education is also an ordinal
variable ranging from 1 to 6, with 1 identifying the respondent as never
having attended school and 6 representing 4 or more years of college. Values
of income and education outside these ranges denote a refusal to answer the
question or inability to answer the question. We will ignore such records.
Rather than computing each correlation coefficient associated with the
three pairs of variables one at a time, a single correlation matrix containing
all of the coefficients will be computed. The BRFSS data files for the years
2011, 2012, 2013, and 2014 will suffice for the investigation. We build on the
tutorial of Sect. 3.6. The tutorials of Sect. 3.10 and of Chaps. 7 and 8 build
on this tutorial, so as a practical matter, you should plan on re-using your
code.
1. Create a Python script and import the necessary Python modules:

14
Pearson’s correlation coefficient is a measure of linear association. Linear association
is meaningful when the variables are quantitative or ordinal.
82 3 Scalable Algorithms and Associative Statistics

import sys
import os
import importlib

parentDir = ’/home/Algorithms/’
if parentDir not in set(sys.path):
sys.path.append(parentDir)
print(sys.path)
from ModuleDir import functions
reload(functions)
dir(functions)

The full path to functions.py is assumed to be


/home/Algorithms/ModuleDir/functions.py.
2. Table 3.2 provides the field locations for the income and education vari-
ables for the years of interest. Add the field locations of the income and ed-
ucation entries to the dictionary fieldDict from the tutorial of Sect. 3.6.
If you worked through the tutorial of Sect. 3.6, then the field dictionary
definitions reside in the file functions.py. Add dictionary entries for in-
come and education for the years 2011 through 2014. For example, the
new specification for year 2014 should look like

fieldDict[14] = {’bmi’:(2247, 2250),’weight’:(2007, 2016),


’income’:(152, 153),’education’:150}

Table 3.2 BRFSS data file names and field locations of the income, education, and age
variables
Income Education Age
Year File Start End field Start End
2011 LLCP2011.ZIP 124 125 122 1518 1519
2012 LLCP2012.ZIP 116 117 114 1629 1630
2013 LLCP2013.ZIP 152 153 150 2177 2178
2014 LLCP2014.ZIP 152 153 150 2232 2233

3. Create the field dictionary using the function fieldDictBuild that was
built in the tutorial of Sect. 3.6.

fieldDict = functions.fieldDictBuild()

4. Read all the files in the directory containing the data files. You may be
able to use your code from the Tutorial of Sect. 3.6 for this purpose. Ignore
any files from a year before 2011 and any other files not listed in Table 3.2
by intentionally creating an exception of the type ZeroDivisionError.
3.8 Tutorial: Computing the Correlation Matrix 83

An exception is an error that is detected during execution. It’s not nec-


essarily fatal if trapped with an exception handler.

path = r’../Data/’
fileList = os.listdir(path)
for filename in fileList:
try:
shortYear = int(filename[6:8])
if shortYear < 11:
1/0
year = 2000 + shortYear
file = path + filename
fields = functions.fieldDict[shortYear]
except(ValueError, ZeroDivisionError):
pass

If shortYear is less than 11, then a ZeroDivisionError exception is


created by the instruction 1/0. The exception directs program flow to
the except branch and none of the remaining instructions in the try
branch are executed. The pass statement directs program flow back to
the top of the for loop and the next file name in fileList is processed.
A ValueError exception will be generated if the string filename[6:8]
does not consist of digits and again program flow returns to the next file
name in fileList.
5. Extract the field positions for income, body mass index, and education
from the dictionary fields:

sInc, eInc = fields[’income’]


sBMI, eBMI = fields[’bmi’]
fEduc = fields[’education’]

This code segment is to execute immediately after extracting fields


from fieldDict.
6. Now that the necessary information regarding data file field positions
have been determined, read the file by iterating over the file object (f)
and, as a test that the program is reading the data, print the first 10
characters in each record.

with open(file, encoding = "latin-1") as f:


for record in f:
print(record[:10])

This code segment executes immediately after extracting the field posi-
tions of the three variables.
84 3 Scalable Algorithms and Associative Statistics

7. Extract the income variable from record and test whether the income
string is missing. Income is missing if the field consists of two blanks, i.e.,
’ ’. If this is the case, then assign the integer 9 to income. We’ll use the
value 9 as a flag indicating that the record should be ignored.

incomeString = record[sInc-1:eInc]
if incomeString != ’ ’:
income = int(incomeString)
else:
income = 9

8. The next tutorial also uses income, and so to reduce effort, copy the
code segment and create a function for processing the income string. The
function and its call can be set up as

def getIncome(incomeString):
if incomeString != ’ ’:
income = int(incomeString)
else:
income = 9
return income

income = functions.getIncome(record[sInc-1:eInc])

Notice that the argument passed to getIncome is the string containing


the response to the income question. Compute and print income using the
old code and by calling the function. When the function works correctly,
move the function definition to the module functions.py and keep the
function call in place. Remove the code segment that translates the string
to integer from the main program.
9. Read the education character and test for blanks. The education field is a
single character in width and so record[fEduc-1] extracts the variable
from the string. Missing data is identified by testing for one blank rather
than the two-character blank used for income.
10. Move the code segment for processing education to a function named
getEducation and put the function in the module functions.py. Test
the function as you did with income. Insert a call to the function after
the call to getIncome.
11. Read the body mass index string and convert it to a float. The previous
tutorial created a function named convertBMI to perform the task of
converting the string to floating point type (see item 14 of Sect. 3.6).
12. Check that the code is functioning correctly by printing the values of
income, body mass index, and education as they are processed. Remove
the print statement when the code is functioning as expected.
3.8 Tutorial: Computing the Correlation Matrix 85

13. If the three variables, income, body mass index, and education, have
been successful converted to integers or floats, then create a vector w
containing values of the variables.

if education < 9 and income < 9 and 0 < bmi < 99:
w = np.matrix([1,income,bmi,education]).T

The Numpy function np.matrix creates a two-dimensional matrix. Passing


a list to Numpy produces a 1 × p matrix (mathematically, a row vector).
Assigning the .T attribute to np.matrix([1,income,bmi,education])
sets w to be the transpose of the row vector—therefore, a column vector
of length four. The vector w is created as a Numpy matrix rather than a
Numpy array so that we may easily compute matrix products using w.
14. Initialize the augmented matrix A before the for loop iterates over
fileList. The dimension of A is q × q where q = p + 1 = 4 is the
length of wi .

q = 4
A = np.zeros(shape=(q, q))

15. After computing w, add the outer product of w with itself to the aug-
mented matrix A:

A += w*w.T

The program flow should look like this:

for filename in os.listdir(path):


try:
...
with open(file, encoding = ’latin-1’) as f:
for record in f:
...
if education < 9 and income < 9 and 0 < bmi < 99:
w = np.matrix([1,income,bmi,education]).T
A += w*w.T
n = A[0,0] # Number of valid observations

16. Instead of waiting until all of the data has been processed to compute
the correlation matrix, we’ll compute it whenever the number of valid
observations is a multiple of 10,000 since it takes some time to process
all of the data. The following code segment computes the mean vector x
and extracts the moment matrix M from A:
86 3 Scalable Algorithms and Associative Statistics

if n % 10000 == 0:
M = A[1:,1:]
mean = np.matrix(A[1:,0]/n).T

The mean vector x is extracted from the first row of A as a 1 × q matrix.


The syntax A[1:,0] extracts a sub-vector from the first column of A
beginning with the second row and ending with the last row. Once again,
the .T operator is used to form mean as a column vector.
17. Compute the variance matrix estimate Σ $ = n−1 M − xxT using the
instruction

SigmaHat = M/n - mean*mean.T

18. Construct the diagonal matrix D with diagonal elements σ $p−1 :


$1−1 , . . . , σ

s = np.sqrt(np.diag(SigmaHat)) # Extract the diagonal from S and


# compute the square roots.
D = np.diag(1/s) # Create a diagonal matrix.

Note that the Numpy function diag has been used in two different ways.
In the first application, np.diag(SigmaHat) extracted the diagonal el-
ements as a vector s because diag was passed a matrix. In the second
application, diag was passed a vector containing the reciprocals of the
standard deviation estimates σ $i−1 , i = 1, . . . , p, and the function inserted
the vector as the diagonal of a p × p matrix of zeros.
19. The product of two conformable Numpy matrices A and B is computed by
the instruction A*B. Compute the correlation matrix R = DΣD. $
20. Verify that your code works correctly by computing R after the 10,000 ob-
servations have been processed. You may use the instruction sys.exit()
to stop execution immediately after computing R. The diagonal elements
of R must be equal to 1. and the off-diagonal elements must be between
−1 and 1; if not, then there is an error in the code.
If your correlation matrix does not conform to these constraints,
then it may be helpful to check the calculations of Σ $ and D using R.
First, initialize a matrix to contain the data using the instruction X =
np.zeros(shape = (10000,3)). Store the data as it is processed. Be-
cause X is zero-indexed, subtract one from n:

if n <= 10000:
X[n-1,:] = [x for x in w[1:]]
3.8 Tutorial: Computing the Correlation Matrix 87

21. Write the data to a file using the following instruction when n has reached
10,000:

np.savetxt(’../X.txt’,X,fmt=’%5.2f’)

The file will be space delimited. The field width is at least 5 and there
are two places to the right of the decimal point for each number. Using
R, read the data into a data frame using the instruction

X = read.table(’../X.txt’)

The R instructions var(X) and cor(X) will compute n−1 n $


Σ and R. The
R function diag is equivalent to the Numpy function of the same name.
22. Process the entire file and write the correlation matrix to an output file
using the Numpy function savetxt. The correlation matrix is shown in
Table 3.3.

Table 3.3 The sample correlation matrix between income, body mass index, and
education computed from BRFSS data files from years 2011, 2012, 2013, and 2014,
n = 1,587,668
Income Body mass index Education
Income 1 −.086 .457
Body mass index −.086 1 −.097
Education .457 −.097 1

There’s a very weak association between body mass index and income
since the correlation coefficient between the variables is −.086.

3.8.1 Conclusion

A correlation coefficient rij between −.3 and .3 indicates weak or little linear
association between variables i and j. Moderate levels of association are in-
dicated by .3 ≤ |rij | ≤ .7, and strong linear association is indicated by values
greater than .7 in magnitude. Based on Table 3.3, it is concluded that there
is little association between body mass index and income and between body
mass index and education. The negative association indicates that there is a
tendency for higher levels of income and education to be associated with lower
levels of body mass index. Considering the complex relationship between diet,
behavior, genetics, and body mass, the results are not unexpected. The data
do not directly measure these variables, but we might expect that income and
88 3 Scalable Algorithms and Associative Statistics

education would reflect these variables. In hindsight, these variables are in-
adequate proxies for diet quality and nutritional knowledge. We’re not ready
to give up on these data though. Let us investigate a related question with
the data: to what extent do income, education, and body mass index jointly
explain variation in a person’s perception of their health? The next section
introduces a method of investigating the question.

3.9 Introduction to Linear Regression

Linear regression is a key methodology of data science. It’s extremely use-


ful for investigating the relationships between variables and for prediction,
among other purposes. Methodological aspects of linear regression are dis-
cussed in Chap. 6. In this section, we solve the computational hurdle of how
to compute linear regression estimates from a massively large data set. The
principal algorithm is a straightforward extension of the previous algorithm
for computing correlation matrices. Though the emphasis of this section is
computing, a brief outline of the salient aspects of linear regression helps set
the stage.
In the linear regression setting, one of the variables is to be modeled or pre-
dicted as a function of p predictor variables. Specifically, a model is adopted
of the expected value of the target random variable Y given a concomitant
predictor vector x. For example, we may ask the question to what extent may
a person’s health be explained by demographic attributes? To investigate the
question, a measure of health is identified and used as the target variable and
measures of income, education, and body mass index are used as predictor
variables.
A linear regression model will provide a simple approximation of what
undoubtedly is a complex relationship. The extent to which the model ade-
quately explains the target variable takes on special importance in the context
of the question of interest. Our algorithm will compute a measure of adequacy
or model fit known as the adjusted coefficient of determination.
Before proceeding, let’s consider whether correlation—the correlation ma-
trix specifically—suffices for addressing the question posed above. The cor-
relation matrix measures the degree of linear association between pairs of
variables drawn from a set of variables. The notion of one variable being
jointly related to more than one other variable cannot be explored satisfacto-
rily through correlation. But linear regression revolves around the notion of
a target variable responding to, or being explained by a set of predictor vari-
ables. Thus, linear regression analysis is the path forward and almost always
yields more insight than an examination of the correlation matrix. Compu-
tationally, this additional insight comes cheaply since remarkably little has
to be done to the correlation matrix algorithm of Sect. 3.8 to compute linear
regression estimates and the coefficient of determination.
3.9 Introduction to Linear Regression 89

3.9.1 The Linear Regression Model

Our interest lies in the relationship between a target variable and a set of
predictor variables. The target has a preeminent role in the analysis whereas
the predictor variables are in a sense, subordinate to the target. The linear
regression model describes the target value as two terms: one describing the
expected value of the target as a function of the predictors and second being
a random term that cannot be explained by the predictors. In statistical
terms, the target variable is a random variable Y and the predictor vector
x is considered to be a vector of known values. The model is Y = f (x) + ε.
The term f (x) is unknown and needs to be estimated, and the term ε is
a random variable, commonly assumed to be normal in distribution, and
basically, a nuisance. A recurrent theme of regression analysis is minimizing
the importance of ε by finding a good form for the model f (x). We need not
be concerned with the distribution of ε at this point.
For example, Y may represent an individual’s health measured as a score
between 1 and 6, and x may represent concomitant measurements on variables
thought to be predictive of overall health. To investigate the relationship or
to predict an unobserved target value using observed predictor variables, a
linear model is adopted of the expected value of Y . We may think of observing
many individuals, all with the same set of predictor variable values. Their
health scores will vary about some mean value, and the linear regression
model describes that mean as a linear function of the predictor variables. It
is hoped that the magnitudes of the random terms are small so that Y ≈ f (x).
If this is the case, then the target can be predicted from the linear model with
some degree of accuracy.
The linear regression model specifies that E(Y |x), the expected value, or
mean of Y , is
E(Y | x) = β0 + β1 x1 + · · · + βp xp .
The predictor vector consists of the constant 1 and the predictor vari-
able values, hence, x = [1 x1 · · · xp ] . The parameter vector is β =
T

[β0 β1 · · · βp ] . The coefficients β0 , . . . , βp are unknown and must be es-


T

timated. The linear regression model may be expressed more compactly in


matrix form as
E(Y | x) = xT β. (3.24)
The inner product of x and β is said to be a linear combination of the p
variables comprising x. The model of Y can be written as Y = xT β + ε.
We turn now to the problem of estimating β using a set of data. The data
consist of pairs of observations on the target variable Y and the associated
predictor vector x. So, let D = {(y1 , x1 ), . . . , (yn , xn )} denote the data set.
The ith predictor vector is xi = [1 xi,1 · · · xi,p ] . If we are to choose be-
T

tween two estimators of β, say β


$ and β̃, we prefer the estimator that produces
predictions that are, on average, closer to the actual values.
90 3 Scalable Algorithms and Associative Statistics

With this in mind, the estimator of β is the parameter vector β $ that


minimizes the sum of the squared differences between the target values yi and
the predictions xiT β,
$ i = 1, . . . , n. The term residual is applied to describe
what has not been accounted for by the model and the parameter estimate
$ thus, the ith residual is yi − xT β.
β; $
i

3.9.2 The Estimator of β

The least squares estimator of β is determined by finding the vector that


minimizes the sum of the squared residuals
"n & '2
S(β) = i=1 yi − xiT β . (3.25)

Minimizing S(β) with respect to β is the least squares criterion. The objec-
tive function S(·) can be expressed in matrix form by stacking the predictor
vectors as rows of the matrix
 T
x1
 1×q 
X =  ...  .
 
n×q  T
xn
1×q

The matrix X is referred to as the design or model matrix. We also set up


the n-vector y = [y1 y2 · · · yn ] to contain the target observations. Then,
T

E(Y|X) = X β (3.26)
n×q q×1

is the n-vector of expected values. Furthermore, we may write the objective


function as an inner product of differences between the vector of observed
values and the vector of expectations:

S(β) = (y − Xβ)T (y − Xβ). (3.27)

Of course, β is unknown, and the task at hand is to determine the best


possible estimator. The best estimator is the vector β $ that minimizes S(β)
given that we judge estimators by the least squares criterion. The form of
the estimator can be determined by differentiating S(β) with respect to β,
setting the vector of partial derivatives equal to the zero vector, and solving
the resulting system of equations for β. The system, often referred to as the
normal equations, is
XT X β = XT y . (3.28)
q×q q×1 q×nn×1
3.9 Introduction to Linear Regression 91

Exercise 3.3 guides the reader through the derivation. The solution to the
normal equations, and therefore the least squares estimator of β, is
$ = (XT X)−1 XT y.
β (3.29)

The solution shown in Eq. (3.29) requires that XT X be invertible; if XT X


is singular, then the computation of an estimator of β is more difficult. The
immediate problem is that the solution for β$ expressed in formula (3.29) may
not be computationally tractable as it requires X, a matrix of n rows, and n
may be so large that X cannot easily be stored. A computational algorithm
for computing β $ that avoids constructing X is not difficult to develop since
we can use the same methods as were used for computing the correlation
matrix.
The algorithm for computing the correlation matrix solves the problem of
an intractably large design matrix because XT X = A (formula (3.22)) and
the dimension of A is q × q. The matrix A is the augmented matrix and an
associative statistic. The vector z = XT y is also associative, and so the two
terms in the solution shown in Eq. (3.29) can be computed without much
difficulty. Changing notation to write the solution involving the associative
statistics sets the solution to be
$ = A−1 z
β (3.30)
q×q q×1

Not only are the matrices A and z small, but they can be computed without
holding all of the data in memory. To understand why A = XT X (for-
mula (3.22)), let xk denote the n × 1 column vector of observations on the
kth predictor variable, k = 1, 2, . . . , p, and 1 denote a n × 1 vector of ones.
Then, XT X can be expressed as a matrix consisting of the q 2 inner products
since
 T
1 9 :
 x1T 
 
X X =  .  1 x 1 · · · xp
T
q×q  ..  n×q
xpT
q×n 
1 T 1 1 T x1 · · · 1 T xp
 x1T 1 x1T x1 · · · x1T xp 
 
= . .. .. ..  (3.31)
 .. . . . 
xpT 1 xpT x1 · · · xpT xp
 q×q
" " 
" n "i xi,1 · · · " i xi,p
 i xi,1 2
 i xi,1 · · · i xi,1 xi,p 
 %
n
= .. .. .. ..  = xi xiT .
.
". " . ". 2
  i=1
i xi,p i x i,p xi,1 · · · i xi,p
92 3 Scalable Algorithms and Associative Statistics

Hence, A = XT X, since A is the sum of the outer products. Formula (3.31)


shows that A is associative. Similarly, the second term z = XT y in Eq. (3.29)
also is an associative statistic since
z = XT y
q×1 q×nn×1
 T   " 
1 y " yi
 x1T y   xi,1 yi  " (3.32)
   
= . = .. = xi y i .
 ..   .  q×11×1
"
xpT y xi,p yi
q×1

The vector xiT is the ith row vector. (We’re using the index i for row vec-
tors and k column vectors.) Both A and z can be computed by iterating
over observation pairs (x1 , y1 ), . . ., (xn , yn ). It’s useful to have one further
expression for β
$ that explicitly shows the predictor vectors:

$ = ("n xi xT )−1 " xi yi .


β i=1 i (3.33)
q×11×1

Let t(D) = (A, z) denote the pair of statistics. It’s also associative since
A and z are both associative. A scalable algorithm for computing t(D) is
obtained by a minor modification of the algorithm for computing the corre-
lation matrix. As before, the algorithm iterates over the observations in the
data set D = {(y1 , x1 ), . . . , (yn , xn )}. The preliminary steps are:
1. Initialize A as a q × q matrix of zeros.
2. Initialize z as a q-vector of zeros.
Process D sequentially and carry out the following steps:
1. From the ith pair of D, extract yi , xi,1 , . . . , xi,p and construct xi according
to
{xi,1 · · · xi,p } → [1 xi,1 · · · xi,p ] = xi .
T
q×1

It’s assumed that the intercept β0 is to be included in the model. If not,


then xi is not augmented with 1.
2. Update A and z according to

A + xi xiT → A.

and
z + yi xi → z.
3. If the data has been partitioned as sets D1 , . . . , Dr , then compute
A1 , . . . , Ar and z1 , . . . , zr as described above. Upon completion, com-
pute
3.9 Introduction to Linear Regression 93
"r
A= Aj
(3.34)
j=1
"r
z= j=1 zj .
$ = A−1 z.
4. Compute β

3.9.3 Accuracy Assessment

In general, assessing the accuracy of a predictive function (not necessarily


a linear regression prediction function) is best accomplished using cross-
validation or bootstrapping. We’ll discuss cross-validation in Chap. 9. Fortu-
itously, elementary and informative accuracy estimates for regression-based
prediction functions can be computed essentially at the same time that the
parameter estimates are computed. We’ll develop one such measure of accu-
racy that provides a measure of the value of the predictor variables towards
explaining variation in the target variable. It will be used to determine the
value of income, education, and body mass index toward explaining variation
in health score. 6 " 71/2
Recall that standard deviation estimator σ $ = n−1 (yi − µ $)2 mea-
sures the average absolute error resulting from using µ $ as an estimator of the
expected value of Y . Without information about Yi in the form of a predictor
vector xi , µ is traditionally estimated by the sample mean y. A similar esti-
mator of the error is computed when regression is used to estimate the ith
expected value of the response variable. The regression mean squared error
2
σreg is the expected squared error associated with using xiT β$ as an estimator
of the expected value of Yi , and σ$reg is the estimated error incurred by using
xiT β
$ as an estimator of the expected value of Yi . An informative regression
model ought to yield a substantially smaller error than the sample mean y.
The next step is to develop a measure of model accuracy that is based on the
reduction in error provided by the regression model compared to the sample
mean.
Estimators of σ 2 and σreg
2
are
"n
2 i=1 (yi − y)2
$ =
σ
n
"n (3.35)
2 $i )2
i=1 (yi − y
and σ
$reg = ,
n−q

where y$i = xiT β$ is the ith fitted value. Except in special circumstances, it’s
2
difficult to interpret σ
$reg $2 . This difficulty motivates a
without reference to σ
relative measure of model fit, the adjusted coefficient of determination

2
$2 − σ
σ 2
$reg
Radjusted = . (3.36)
$2
σ
94 3 Scalable Algorithms and Associative Statistics

The adjusted coefficient of determination measures the relative reduction in


mean square error when comparing the fitted regression model to simplest of
2
data-based prediction functions, namely, y. The range of Radjusted is 0–1, and
2
values of Radjusted greater than .8 usually indicate that the fitted regression
2
model is useful for prediction. Furthermore, Radjusted may be interpreted as
the proportion of the variation in Y that is explained by the fitted regression
model.

3.9.4 Computing Radjusted


2

2
Turning now to the matter of computing Radjusted , formula (3.36) is a simple
2 2
function of the terms σ$ and σ $reg . Equation (3.35) suggests that the data
must be processed twice to compute the terms: the first time to compute y
and β,
$ and the second time to compute the sum of the squared deviations.
However, we saw earlier (formula (3.16)) that "σ$2 may be computed using a
single pass through the data since σ 2
$ =n −1
yi2 − y 2 . The regression mean
square error also can be computed without a second pass through the data
since
" 2
2 y − zT β
$
$reg = i i
σ , (3.37)
n−q
where
n
%
z = xi y i .
q×1 q×11×1
i=1

Exercise 3.8 guides the reader in a verification of formula (3.37).


As the algorithm iterates over observation pairs (y1 , x1 ), . . . , (yn , xn ) in
the calculation of β, $ we accumulate the sum of squares " y 2 . The term z
i i
is necessarily accumulated for the computation of β $ and so is available to
compute σ $reg when the algorithm completes.
2
If n is much larger than q so that n − q ≈ n, then computing Radjusted is
even simpler. Instead of using the correct denominator n − q in computing
2
$reg
σ , we’ll use n. Then,

2
$2 − σ
σ 2
$reg
Radjusted = 2
$"
σ 4" 5
2 2 2
iy −z β
−1 −1 T$
n iy −y −n (3.38)

$2
σ
$ − y2
n−1 zT β
= .
$2
σ
3.10 Tutorial: Computing β
$ 95
2
The alternative to computing σ$reg as described above utilizes a second
pass through the data file. The sole purpose is to compute the sum of the
squared prediction errors
n
%
$ 2 = (n − q)$
(yi − xiT β) 2
σreg . (3.39)
i=1

3.10 Tutorial: Computing β


$

For a long time it has been argued that income is related to health [16,
17]. The evidence supporting this contention is clouded by the difficulty of
quantifying a condition as complex as health. The problem is also confounded
with confidentiality issues that arise with potentially sensitive data on the
health of individuals. In this tutorial, we investigate the question by exploring
data from the Behavioral Risk Factor Surveillance System Survey (BRFSS).
Specifically, we build a regression model of a very simple measure of health,
the answer to the question would you say that in general your health is: The
question is multiple choice and the possible answers are shown in Table 3.4.

Table 3.4 Possible answers and codes to the question would you say that in general
your health is:

Code Descriptor
1 Excellent
2 Very good
3 Good
4 Fair
6 Poor
7 Don’t know or not sure
9 Refused
Blank Not asked or missing

After discarding records containing the codes 7, or 9, or an empty string,


the recorded variable is ordinal and suitable for regression analysis.
There is an abundance of demographic variables recorded in the BRFSS
data files but we limit the predictor variables to a few demographic descriptors
including of course, annual household income. Our objectives are limited to
assessing the relative importance of the variables income, body mass index,
and education toward predicting health. We also would like to judge whether
income is more important than the other variables. The objectives are pursued
by fitting a linear regression model of the health measure and examining the
estimated effect of a 1 unit change of each on health.
The BRFSS data files for the years 2011, 2012, 2013, and 2014 will
once again be used. In addition to income, body mass index, and education
96 3 Scalable Algorithms and Associative Statistics

extracted in the previous tutorial, we will need to extract responses to the


health question. Table 3.5 shows the field positions the variable. Valid re-
sponses are integers between 1 and 6 inclusive. Records with general health
responses that are entered in the data files as 7, 9, or blank will be ignored.

Table 3.5 BRFSS data file names and field positions of the general health variable.
The general health variable contains a respondent’s assessment of their health. Table 3.4
describes the general health codes and meaning of each
Year File Position
2011 LLCP2011.ZIP 73
2012 LLCP2012.ZIP 73
2013 LLCP2013.ZIP 80
2014 LLCP2014.ZIP 80

This tutorial builds on the correlation tutorial of Sect. 3.8. The instructions
that follow assume that the reader has written a script that computes a
correlation matrix for some of the BRFSS variables. If you do not have a
script, then you should go through the tutorial and create it.

1. Load modules and functions that were used in the previous tutorial:

import sys
import os
import importlib

parentDir = ’/home/Algorithms/’
if parentDir not in set(sys.path):
sys.path.append(parentDir)
print(sys.path)
from ModuleDir import functions
reload(functions)
dir(functions)

Execute the script and correct any errors.


2. Edit the module functions.py and add the field position entries for the
general health variable to fieldDict. The field positions are listed in
Table 3.5. The dictionary entry for the year 2011 may appear as

fieldDict[11] = {’genhlth’:73, ’bmi’:(1533, 1536), ’income’:124, 125),


’education’:122}

3. Create the field dictionary using the function fieldDictBuild.

fieldDict = functions.fieldDictBuild()
3.10 Tutorial: Computing β
$ 97

4. Create a list of files in your data directory and iterate through the list.
Process the files from the years 2011, 2012, 2013, and 2014.

path = r’.../Data/’
fileList = os.listdir(path)
print(fileList)
n = 0
for filename in fileList:
try:
shortYear = int(filename[6:8])
if shortYear < 11:
1/0
year = 2000 + shortYear

file = path+filename
print(filename)
fields = fieldDict[shortYear]
except(ValueError, ZeroDivisionError):
pass

Since there is an exception handler in the code to skip files that are
not data files, we also use the exception handler to skip data from years
preceding 2011. The last statement in the code segment retrieves the field
positions for the current year.
5. Extract the field positions for education, income, and body mass index
using the functions that you created in the previous tutorial. Get the field
position fGH for general health.

fEduc = fields[’education’]
sInc, eInc = fields[’income’]
sBMI, eBMI = fields[’bmi’]
fGH = fields[’genhlth’]

6. Process the data file by iterating over records. Extract the predictor vari-
able values:

file = path + filename


with open(file, encoding = "latin-1") as f:
for record in f:
education = functions.getEducation(record[fEduc-1])
income = functions.getIncome(record[sInc-1:eInc])
bmi = functions.convertBMI(record[sBMI-1:eBMI],shortYear)

This code segment executes within the for loop that iterates over
fileList. Therefore, the for record in f statement must have the
98 3 Scalable Algorithms and Associative Statistics

same indentation as the code segment of instruction 5. The next code


segment handles invalid values of general health.
7. Map the string genHlthString to the integer genHlth as follows. First
test whether the string is a blank. If it is, then assign −1 to genHlth.
If the string is not blank, translate the string to an integer. Then test
whether the integer is 7 or 9 (or simply greater than 6), and if so, assign
−1 to genHlth. No other transformation is necessary.
Print the values of genHlthString and genHlth as the program iter-
ates over records to check your code. No other values of genHlth besides
those in {−1, 1, 2, 3, 4, 5, 6} should be produced.
8. Encapsulate the code segment from instruction 7 as a function. The def-
inition and return statements are:

def getHlth(HlthString):
...
return genHlth

9. Call the function so:

y = functions.getHlth(record[fGH-1])

Check that the function getHlth works correctly by printing the return
value y. When the function works correctly, move it to the functions
module. Delete the code segment from the main program. Call getHlth
after computing bmi.
10. If the extracted values are all valid, then form the predictor vector x from
education, income, and body mass index. The vector x is created as a
4×1 Numpy matrix so that we may easily compute the outer product xxT .
If one or more extracted values are not valid, then a ZeroDivisionError
exception is created and program flow is directed to the next record.

try:
if education < 9 and income < 9 and 0 < bmi < 99 and y != -1:
x = np.matrix([1, income, education, bmi]).T
n += 1
else:
1/0
except(ZeroDivisionError):
pass

11. The next set of operations build the matrices A and z from x and y.
But before computing the matrices A and z, it is necessary to initialize
3.10 Tutorial: Computing β
$ 99

them as matrices containing zeros because they will be computed by


successively adding additional terms to the matrices. Initializations must
take place before any of the data files are processed. The initialization
code segment is

q = 4
A = np.zeros(shape = (q, q))
z = np.matrix(np.zeros(shape = (q, 1)))
sumSqrs = 0
n = 0

"
The variable sumSqrs will contain i yi2 (needed to compute σ$2 ).
12. Returning to the inner for loop, as each record is processed, update
A and z. This code follows the test for valid values of the target and
predictor variables.

A += x*x.T
z += x*y
sumSqrs += y**2
n += 1

These instructions are executed only if the test for valid values is True
(instruction 10). Thus, the code segment follows immediately after up-
dating A.
13. After processing a large number of observations, compute
$ = A−1 z.
β (3.40)

You can compute A−1 using the Numpy function linalg.inv(), say,
invA = np.linalg.inv(A), and then compute betaHat = invA*z. How-
ever, from a numerical standpoint it’s preferable not to compute A−1 but
instead solve the linear system of equations Aβ = z for β using a LU-
factorization optimized for symmetric matrices.15 The solution to the
system will be β.
$ Compute β $ after processing successive sets of 10,000
observations:

if n % 10000 == 0 and n != 0:
b = np.linalg.solve(A,z)
print(’\t’.join([str(float(bi)) for bi in b]))

15
The LU-factorization method is faster and more accurate than computing the inverse
and then multiplying.
100 3 Scalable Algorithms and Associative Statistics

The calculation of β$ shown here is carried out by means of an LU-


factorization. The join operator creates a string from the elements of
$ and inserts a tab character in between each element.
β
14. The next task is to compute

2
$2 − σ
σ 2
$reg
Radjusted = 2
(3.41)
$
σ
using the variance estimates
" 2
$2 = n−1
σ 2
i yi − y
4" 5 (3.42)
2
and σ
$reg = n−1 yi2 − zT β
$ .

2
All terms necessary to compute Radjusted have been computed except for
" "
y = n −1
i yi . The sum $2 is
i yi is stored in row zero of z. Hence, σ
computed using the instruction

ybar = z[0]/n
varEst = sumSqrs/n - ybar**2

15. Compute the adjusted coefficient of determination. We use the approxi-


2
mation formula for Radjusted from Eq. (3.38).

rAdj = (z.T.dot(b)/n - ybar**2)/varEst

The .dot operator is used to compute the inner product of Numpy ar-
rays z and b. Since the Numpy function linalg.solve does not return
a matrix (it returns an array), the matrix multiplication operator * will
not multiply z and b correctly. You can determine the type of an object
using the function type().
2
16. Print the value of Radjusted and β
$ whenever n mod 10,000 = 0 and n > 0
by putting the following code within the if branch of the conditional
statement of instruction 13.

bList = [str(round(float(bi),3)) for bi in b]


print(n,’\t’.join(bList),str(round(float(rAdj),3)))

The print statements and all of the intermediate calculations needed to


2
compute Radjusted should execute whenever β $ is computed. In the first
3.10 Tutorial: Computing β
$ 101

line of the code segment, b is converted from a Numpy matrix to list of


strings.16
17. The ith parameter estimate β$i can be interpreted as the estimated change
in E(Y |x) if ith predictor variable increases by one unit and all other
variables are held fixed.17 For example, the parameter estimate β$1 =
−.145 associated with income implies that a one unit change in the income
variable is associated with an average improvement in general health score
of −.145. We call this change an improvement because a general health
score 1 identifies excellent health and six identifies poor health. Recall
that the income variable is ordinal, and that a 1 unit change in the
income variable corresponds to roughly $5000 more in income per year.
A one unit increase in body mass index is associated with an increase of
β$3 = .037 in the health score. Body mass index is computed as weight
in kilograms divided by height2 and so, if an individual gains 10 kg (or
22 lbs), then we estimate the general health score to increase by .37. It’s
very difficult to say which variable is more important because the units
(dollars and kilograms are not comparable).
18. Remove education from the model and compute Radjusted . The difference
between Radjusted with all three values in the model and only income
and body mass index provides a measure of the importance of education.
As the difference is .016, we interpret education as accounting for 19.9 −
18.3 = 1.6% of the variation in general health score, not a very impressive
amount.

3.10.1 Conclusion

The tutorial found an adjusted coefficient of determination of .199, indicating


that the variables explained 19.9% of the variation in the general health
score. The model’s failure to explain much of the variation in general health
score is understandable because income and education do not directly affect
health. Despite there being a great deal of variation in general health score
that is not explained by this model, we see that differences in body mass
index and income are associated with differences in the perceived health of
respondents as measured by the health score.
If we remove income from the model, the coefficient of determination is
less—.134 and if we remove body mass index instead, Radjusted = .158. From
this comparison, we infer that income is a relatively more important determi-
nant of general health score than education or body mass index. This result
supports the notion that income and health are connected.

16
Numpy matrices and arrays cannot be rounded even if they are of length 1 or 1 × 1 in
dimension.
17
Interpretation of regression coefficients is discussed at length in Chap. 6.
102 3 Scalable Algorithms and Associative Statistics

3.11 Exercises

3.11.1 Conceptual

3.1. Suppose that s is a statistic and that for any two sets D1 and D2 that
form a partition of the set D, s(D1 ∪ D2 ) = s(D1 ) + s(D2 ). Suppose that
r > 2 and D1 , D2 , . . . , Dr is also a partition of D. Argue that s(D) = s(D1 )+
s(D2 ) + · · · + s(Dr ).
3.2. Show that the statistic t(D) = (A, z) (Eqs. (3.22) and (3.32)) is an
associative statistic. Recall that pairs are added component-wise; hence,
(x1 , y1 ) + (x2 , y2 ) = (x1 + x2 , y1 + y2 ).
3.3. (Requires multivariable calculus.) Show that the least squares estimator
is the solution to the normal equations (3.28). Specifically, minimize the ob-
jective function S(β) (Eq. (3.25)) with respect to β. Differentiate S(β) with
respect to β. Set the vector of partial derivatives equal to 0 and solve for
q×1
β. The solution is the least squares estimator.
$2 can be written as
3.4. Recall that σ
"
2 $)2
(xi − µ
$ =
σ ,
n
and consider the variance estimator presented in Eq. (3.16).
(a) Verify that Eq. (3.3) are correct; that is, verify that µ $2 can be
$ and σ
computed from s1 , s2 , and s3 according to the formulae.
(b) Conventional statistical texts advocate using the sample variance
"
2 $)2
(xi − µ
s = .
n−1
to estimate σ 2 because it is unbiased. In fact, σ
$2 is a biased estimator of
2
σ . Show that the difference between σ $ and s2 tends to 0 as n → ∞.
2

Consequently, when n is large, the bias of σ $2 is negligible.


3.5. Let  
1
 .. 
j =.
n×1
1
denote the summation vector of length n.
(a) Show that for any n-vector x, n−1 jT x = x.
(b) Suppose that the p-vector of sample means x is to be computed from the
data matrix X . Show that
n×p

xT = (jT j)−1 jT X . (3.43)


1×nn×p
3.11 Exercises 103

(c) Assuming that j and X have been constructed as Numpy matrices, give a
one-line Python instruction that will compute xT .
(d) Note the similarity between Eq. (3.43) and the least squares estimator of
β. What can you deduce about the sample mean as an estimator?
3.6. Suppose that X2 = aX1 + b, where X1 is a random variable with fi-
nite mean and variance and a += 0 and b are real numbers. Show that the
population correlation coefficient ρ12 = 1.
3.7. Show that
R = DΣD
$

by writing out some of the elements of ΣD


$ and then the elements of the
product D(ΣD).
$
"
3.8. Prove formula (3.37) by expanding (yi − y$i )2 as
"
(yi − y$i )2 = yT y − 2yT Xβ $ T XT Xβ.
$ +β $ (3.44)

$ T X T Xβ
Then, prove that β $ = zT β.
$

3.11.2 Computational

3.9. The standard deviation of the ith parameter estimator β$i is estimated
by the square root of the ith diagonal element of the matrix
2
var(
; β) $ =σ
$reg (XT X)−1 . (3.45)

An approximate large-sample 95% confidence interval for βi is


! < < #
β$i − 2 var(
; β$i ), β$i + 2 var(
; β$i ) . (3.46)

Compute confidence intervals for the parameters β1 , β2 , and β3 estimated in


Sect. 3.10.
3.10. Mokdat et al. [40] estimate the percent of obese U.S. adult residents to
be 19.8% and 21.5% in years 2000 and 2001 from an analysis of BRFSS data.
Determine whether their calculations are correct by modifying the tutorial
of Sect. 3.6 to compute the proportion of obese respondents for each of the 8
years listed in Table 3.1.
3.11. Use one of the BRFSS data sets to estimate the proportion of females in
the U.S. adult population. Compute an estimate as the sample proportion of
females among all observations. The gender variable field positions are listed
in the BRFSS codebooks. The year 2014 codebook can be viewed at the CDC
104 3 Scalable Algorithms and Associative Statistics

website https://www.cdc.gov/brfss/annual_data/2014/pdf/codebook14_llcp.pdf.
Males are identified in the BRFSS data files by the value 1 and females are
identified by 2. Let xj denote the gender of the jth respondent.
Compute two estimates: the conventional sample proportion, and a weighted
proportion using the BRFSS sampling weights. The two estimators are
"n
p1 = n−1 j=1 IF (xj )
"n
j=1 wj IF (xj )
and p2 = "n , (3.47)
j=1 wj

where IF (xj ) takes on the value 1 if the jth sampled individual is female and 0
otherwise, wj is the BRFSS sampling weight assigned to the jth observation,
and n is the number of observations. Compare to published estimates of the
proportion of females in the adult U.S. population. The U.S. Census Bureau
reports that in 2010, 50.8% of the population were female.

3.12. The University of California Irvine Machine Learning Repository [5]


maintains a data set related to household electric power consumption. Data
loggers were connected to three sub-meters that measured consumption on
three separate circuits. Data were collected by minute for approximately 47
months from December 2006 and November 2010. More information on the
study can be obtained at the University of California, Irvine Machine Learn-
ing Repository. Active power is the response variable. Reactive power largely
reflects energy loss due to resistance and is ignored in this exercise.
(a) Map the data from consumption per minute (of active power) to con-
sumption per hour (active power) by building a dictionary, hourlyDict
of key-value pairs where the keys are day and hour pairs, for instance,
(02/11/2007, 13) and values are lists containing the consumption for the
three sub-meters during the hour, e.g., [3, 16, 410].
(b) Compute the correlation matrix for the three circuits using the hourly
consumption data contained in hourlyDict.
(c) Map the hourly data contained in hourlyDict to hour. Do this by creating
an hour-of-the-day dictionary hourDict where the keys are 0, 1, . . . , 23
and the values are the means of all measurements obtained for a particular
hour of the day. On a single plot, graph mean consumption against hour
for each of the three sub-meters and describe the pattern of use by hour.

You might also like