Reflection paper Applied Business Analytics
Predictive Analytics for Business Strategy: Reasoning from Data to
Actionable Knowledge By Jeffrey T. Prince
Chapter 3: Reasoning from Sample to Population w/ Stata
Distributions and Sample Statistics Population – the entire collection of units of analysis, or observations, or elements, upon which
information is desired – i.e., a large collection of individuals or objects known to have similar
characteristics.
Population parameter – a numerical expression that summarizes some feature of the population.
Note: We can use samples to draw conclusions about population parameters. Further, we can use
confidence intervals and hypothesis testing to learn about population parameters.
For a given population consisting of numerical values, define 𝑋𝑖 as a random variable constituting a
single draw from that population. As a random variable, 𝑋𝑖 can take on multiple values, w/ any given
realization of the variable due to chance (or randomness).
A random variable can be discrete or continuous. A discrete random variable can take on only a
countable number of values, while a continuous random variable takes on an infinite number of values.
The probabilities of a discrete random variable are represented by a probability function. For a
continuous random variable, they are represented by a probability density function (pdf).
Example 1:
Consider the ages for a group of 10 people. Suppose three of them are 25, four are 30, two are 40, and
one is 45. Define 𝑌𝑖 as the age for a single draw from these 10 people; thus 𝑌𝑖 is a discrete random
variable.
Use Stata to write the probability function for 𝑌𝑖 .
. input height
25
25
25
30
30
30
30
40
. 40
. 45
. end
. histogram height, discrete percent
A normal random variable has a “bell shaped” pdf.
The pdf allows us to calculate the probabilities that the random variable falls in various ranges.
The probability that a random variable falls b/w two numbers A and B is the area under the pdf curve
b/w A and B.
For the normal distribution, the general formula for the pdf is:
𝑒 −(𝑥 − 𝜇)2
2𝜎2
𝜎√2𝜋
Example 2:
Suppose the number of daily visitors to a website is normally distributed w/ an expected value of 2,087
and a standard deviation of 316.
If we define 𝑋𝑖 as a random variable representing the number of daily visitors to a website, then given
the expected value and standard deviation of 𝑋𝑖 , we know the pdf is:
𝑒 −(𝑥 − 2087)2
2 ∗ 3162
316√2𝜋
Use Stata to determine the probability that the number of visitors is b/w 1,500 and 2,000? First, convert
each value for 𝑋𝑖 to a Z-value before solving for the difference in cumulative probabilities.
. 𝑍1500 = 1500−2087
316 = −1.86
𝑍2000 = 2000 − 2087
316 = −.28
. di normal(-.28) - normal(-1.86)
.35829599, or approximately 36%.
In solving for the Z-value, we subtract the population mean from the actual value before dividing by the
population standard deviation – i.e.,
𝑍𝑖 = 𝑋𝑖−𝜇
𝜎 .
For every X-value, there is a corresponding Z-value that expresses a variable value in terms of the
number of standard deviations it is from the population mean.
Example 3:
Calculate the corresponding Z-values for the following X-values of IQ, a normally distributed random
variable: 70, 85, 100, 115, 130, 145. Note: The population average, 𝜇, is equal to 100, and the
population standard deviation, 𝜎, is equal to 15.
𝑍70 = 𝑋𝑖 − 100
15 = ?, 𝑍85 =
𝑋𝑖 − 100
15 =?, 𝑍100 =
𝑋𝑖 − 100
15 = ?
𝑍115 = 𝑋𝑖 − 100
15 = ?, 𝑍130 =
𝑋𝑖 − 100
15 = ?, 𝑍145 =
𝑋𝑖 − 100
15 = ?
Question: Why would one convert the actual values of a random variable to Z-values?
Answer: We can “standardize” any normally distributed variable by converting the actual values of that
variable to Z-values (draw this out w/ IQ).
Hence, every normally distributed variable can be standardized so that its mean is 0 and its standard
deviation is equal to 1, making solving for probabilities of all normally distributed variables easier.
Finally, as you can see, there are 3 one-unit standard deviations to the L and 3 to the R of a standard
normal probability distribution w/ mean equal to 0 and standard deviation equal to 1.
By doing this, we can use a single probability table (the standard normal distribution) to solve
probability of observing any value or range of values of any random variable.
For any distribution, we are interested in its center and its spread – i.e., in the expected value and the
spread.
The expected value, aka population mean, 𝐸[𝑋𝑖 ], is the summation of each possible realization of 𝑋𝑖
multiplied by the probability of that realization.
A common measure for the spread of a distribution is the variance solved for as
𝑉𝑎𝑟[𝑋𝑖 ] = 𝐸[(𝑋𝑖 − 𝐸[𝑋𝑖 ]) 2].
Another common measure of spread is the standard deviation solved for as the square root of the
variance:
𝑠. 𝑑. [𝑋𝑖 ] = √𝑉𝑎𝑟[𝑋𝑖 ]
Data Samples and Sample Statistics For a random variable 𝑋𝑖 , we define a sample of size 𝑵 as a collection of 𝑁 realizations of 𝑋𝑖 .
Once we have a sample, we can calculate sample statistics, defined as single measures of some feature
of a sample.
The sample mean, a common measure of central tendency, can be solved for as
�̅� = 1
𝑁 ∑ 𝑥𝑖
𝑁 𝑖=1 .
The sample variance, a common measure of spread, of a sample of size 𝑁 for random variable 𝑋𝑖 is
solved for as
𝜎2 = 1
𝑁−1 ∑ (𝑥𝑖 − �̅�)
2𝑁 𝑖=1 .
The sample standard deviation, another common measure of spread, can be solved for as
𝜎 = √ 1
𝑁−1 ∑ (𝑥𝑖 − �̅�)
2𝑁 𝑖=1 .
Example 4:
Calculate the sample mean, variance, and standard deviation for the following sample of random
variable 𝑋𝑖 = {65, 73,70, 62, 79}.
. clear
. input X
65
73
70
62
79
end
. summarize, detail
Confidence Intervals
The sample mean is an example of an estimator – a calculation using sample data that is used to
provide information about a population parameter. Hence, the sample mean is an estimator for the
population mean.
A random sample is one where every member of the population has an equal chance of being selected.
If we have a random sample, the sample mean is a “reasonable guess” for the population mean.
We might inductively reason that the observed sample mean is equal to the population mean, but that
logic would be wrong nearly 100% of the time.
Since inductive arguments about population parameters that use a single number have little strength,
we should instead consider using a range of numbers.
A confidence interval is defined as a range of values such that there is a specified probability that they
contain a population parameter.
We can build a confidence interval for the population mean using the sample mean, sample standard
deviation, and sample size.
Note: For a given sample size of 𝑁, assume it is a random sample. This implies𝑋1, ⋯ , 𝑋𝑁 , whose
realizations constitute the sample, all have distributions mirroring the population distribution.
Consequently, each has a mean of 𝜇 and standard deviation 𝜎, and all are identically distributed. The
assumption of a random sample also implies that each random variable is independent, meaning the
distribution of one random variable does not depend on the realization of another.
Hence, we say that 𝑋1, ⋯ , 𝑋𝑁 are independent and identically distributed (i.e., one does not cause the
other and both come from the same distribution). To learn more about the i.i.d assumption, check out
the following videos:
https://www.youtube.com/watch?v=Ruz1wWkFUfM
https://www.youtube.com/watch?v=4UE3rUwibgg
https://www.youtube.com/watch?v=lhzndcgCXeo
https://www.statisticshowto.com/iid-statistics/
Still assuming a random sample, it can be shown that the mean of �̅� (that is, the mean of the sample
mean) is the population mean 𝜇. What do we mean by the mean of the mean?
A sampling distribution is a probability distribution of a statistic obtained from many samples drawn
from a population.
In other words, suppose we took many samples from a population and calculated the mean each time.
If we took the average of all sample means, it would equal the true population mean, 𝜇, that is
𝐸[�̅�] = 𝜇.
The standard deviation of a sampling distribution of a statistic like the mean, also known as the standard
error of the mean, will always be narrower than the standard deviation of a random sample of the
individual values of a variable.
Question:
So, what is the intuition? Again, explain w/ a commonly known variable w/ well-defined parameters,
like IQ. Individual values w/ include extremes, whereas the sample means will average out outliers.
Review sampling distributions (this allows us to understand the idea of “unbiased” statistics), the central
limit theorem (the sampling distribution for MOST statistics is NORMAL ), and the law of large
numbers (the sample mean will approach the true population value as the sample size gets bigger and
bigger) here:
https://www.khanacademy.org/math/ap-statistics/sampling-distribution-ap/what-is-sampling-
distribution/v/introduction-to-sampling-distributions?modal=1
https://www.khanacademy.org/math/ap-statistics/sampling-distribution-ap/sampling-distribution-
mean/v/sampling-distribution-of-the-sample-mean
https://www.khanacademy.org/math/statistics-probability/random-variables-stats-library/expected-
value-lib/v/law-of-large-numbers.
Now that you understand the idea of sampling distributions, the notion of unbiasedness is easy to
grasp.
An estimator whose mean is equal to the true population parameter it is used to estimate is known as a
unbiased estimator.
The sample mean is an unbiased estimator for the population mean. That is,
𝐸[�̅�] = 𝜇.
Likewise, the mean of the sample variance is the variance you would calculate if you collected the entire
population, i.e., the population variance solved for as
𝐸[𝑆 2] = 𝜎2.
The mean of the sample standard deviation is the population standard deviation solved for as
𝐸[𝑆] = 𝜎.
To construct a confidence interval for the population mean, we must know about the distribution of the
sample mean and its expected value.
As we have already learned, the sampling distribution of the mean is normally distributed and its
expected value is equal to the true population value.
The assumption that a data sample is a random sample implies the standard deviation of the sample
mean is
𝑠. 𝑑. [�̅�] = 𝜎
√𝑁 .
As you can see, the spread of the sample mean gets smaller as the sample size increases.
Hence, assuming that we have a random sample > 30, the central limit theorem states that the mean of
i.i.d. random variables is normally distributed w/ mean equal to 𝜇 and standard deviation equal to 𝜎
√𝑁 .
By knowing the distribution of the sample mean, we can (using probabilities from a normal distribution)
calculate the probability that the sample mean falls w/n any given distance from its mean 𝜇.
Distance is measured in terms of standard deviations.
Probabilities from the normal distribution worth committing to memory include the following:
90% of the time the normal random variable will be within 1.65 standard deviations of its mean.
95% of the time the normal random variable will be within 1.96 standard deviations of its mean.
99% of the time the normal random variable will be within 2.58 standard deviations of its mean.
These probabilities can be stated more formally as follows:
𝑃𝑟 (�̅� ∈ [𝜇 ± 1.65 ( 𝜎
√𝑁 ) ≈ 0.90])
𝑃𝑟 (�̅� ∈ [𝜇 ± 1.96 ( 𝜎
√𝑁 ) ≈ 0.95])
𝑃𝑟 (�̅� ∈ [𝜇 ± 2.58 ( 𝜎
√𝑁 ) ≈ 0.99])
For the above probabilities, we are taking the population mean and creating an interval around it to
capture a certain percentage of all possible draws for the sample mean.
Our objective is not to predict where the sample mean will fall.
Instead, we want to take an observed sample mean and create an interval around it that will capture the
population mean w/ some known probability.
With some algebra, it can be shown that for a random sample of size N > 30:
The sample mean +/- 1.65 standard deviations will contain the population mean 90% of the time.
The sample mean +/- 1.96 standard deviations will contain the population mean 95% of the time.
The sample mean +/- 2.58 standard deviations will contain the population mean 99% of the time.
This can be shown more formally as:
𝑃𝑟 (𝜇 ∈ [�̅� ± 1.65 ( 𝜎
√𝑁 ) ≈ 0.90])
𝑃𝑟 (𝜇 ∈ [�̅� ± 1.96 ( 𝜎
√𝑁 ) ≈ 0.95])
𝑃𝑟 (𝜇 ∈ [�̅� ± 2.58 ( 𝜎
√𝑁 ) ≈ 0.99])
If we take the sample mean and then add and subtract 1.96 standard deviations (standard errors), we
know this interval will contain the population mean approximately 95% of the time.
Since we do not observe the population standard deviation (𝜎), we can replace it w/ the sample
standard deviation (or standard error) (S) given that it is an unbiased estimator of 𝜎.
For a random sample of size N > 30, our formulas become:
𝑃𝑟 (𝜇 ∈ [�̅� ± 1.65 ( 𝑆
√𝑁 ) ≈ 0.90])
𝑃𝑟 (𝜇 ∈ [�̅� ± 1.96 ( 𝑆
√𝑁 ) ≈ 0.95])
𝑃𝑟 (𝜇 ∈ [�̅� ± 2.58 ( 𝑆
√𝑁 ) ≈ 0.99])
Note: When we replace S for 𝜎 for samples of size N < 30, instead of using the Z-values of 1.65, 1.96, and
2.58, we must use t-values obtained from the t-distribution.
Example 5:
Use the cars data to construct a 95% confidence interval for the mean age of cars driven in your
neighborhood.
. cd c:\projects\predictive_analytics\sample_to_pop
. import excel using cars.xlsx, firstrow
. summarize Age
. di 8.293103-(1.96*(4.966585/58^.5))
7.01
. di 8.293103+(1.96*(4.966585/58^.5))
9.57
Note: We can use the commands “ci mean Age” and “ameans Age,” just make sure that you use the t-
distribution. For a 95% confidence interval, the t-distribution requires use to replace 1.96 w/ 2.
. di 8.293103-(2*(4.966585/58^.5))
6.9888139
. di 8.293103+(2*(4.966585/58^.5))
9.5973921
Or we can use the “ci mean Age” and “ameans Age” commands as follows:
Hypothesis Testing
On many occasions, we have in mind a value for the population parameter, and we wish to see if this
value is plausible given the data we observe.
For example, a manager at a firm may believe the average age of the firm’s customers is 44.5. How do
we decide whether to believe this claim?
To make our decision, we can use the ages of the customers whom we observe in our data sample to
determine whether this claim is reasonable.
Age 58 8.293103 .6521446 6.987206 9.599
Variable Obs Mean Std. Err. [95% Conf. Interval]
. ci mean Age
Harmonic 53 5.40659 4.077445 8.021349
Geometric 53 7.554605 6.212164 9.187146
Age Arithmetic 58 8.293103 6.987206 9.599
Variable Type Obs Mean [95% Conf. Interval]
. ameans Age
Suppose that we have a data sample w/ 872 customers’ ages, where the sample mean is 43.61 and the
sample standard deviation is 12.72.
Based on this information, is the hypothesis that the mean age in the population is 44.5 credible, or
should we reject this idea?
The proposal that the average age of all customers is 44.5 is a hypothesis, and we wish to test this
hypothesis using a data sample.
Hence, a hypothesis test is the process of using sample data to assess the credibility of a hypothesis
about a population.
If a data sample is a random sample, then the sample mean is an unbiased estimate of the population
mean.
Consequently, 43.61 is our best guess of the population mean.
However, we know the sample mean almost never exactly equals the actual population mean, so no
matter how close the sample mean is to the hypothesized population mean, a small difference b/w the
two will not be sufficient for use to conclude that the hypothesized value is correct (so, we never
“accept” the null hypothesis).
In contrast, we can ask whether 43.61, our best guess for the population mean, is “too far” from 44.5 for
us to believe that 44.5 is a credible value for the population mean.
Conducting a hypothesis test consists of making such an assessment – that is, (1) rejecting a hypothesis
as noncredible, or (2) failing to reject the hypothesis.
In our example, we must choose b/w rejecting 44.5 as a noncredible value for the population mean or
failing to reject 44.5.
If we reject, then we are concluding that 44.5 is not the population mean; if we fail to reject, we believe
it is plausible for 44.5 to be the population mean but is not necessarily correct.
To see how a hypothesis test works, consider again the case where we have a sample of size 𝑁,
consisting of 𝑁 realizations of the random variable 𝑋𝑖 .
We know the sample mean is �̅�, and the sample standard deviation is 𝑆. Further, the population mean
is 𝜇 and the population standard deviation is 𝜎.
We also know that if a sample of size N > 30, then
�̅�~𝑁 (𝜇, 𝜎
√𝑁 )
For a hypothesis test, we hypothesize that a value for a population parameter; in our setting, we are
hypothesizing a value for the population mean.
The hypothesis about the population parameter’s value is called the null hypothesis, defined as the
hypothesis to be tested using a data sample. We can write this as follows:
𝐻0: 𝜇 = 𝐾
Our objective is to determine whether we believe this null hypothesis is credible given the data
observed.
By assuming the null hypothesis, we know the center of the distribution for the sample mean – it is
centered at 𝐾.
Because the sample mean is normally distributed, we know that it will:
• Fall w/n +/- 1.65 standard deviations of 𝐾 approximately 90% of the time.
• Fall w/n +/- 1.96 standard deviations of 𝐾 approximately 95% of the time.
• Fall w/n +/- 2.58 standard deviations of 𝐾 approximately 99% of the time.
We can again write this more formally for the sample mean w/ hypothesized population mean of 𝐾 as
follows (for a random sample of N < 30 and population mean = K):
𝑃𝑟 (�̅� ∈ [𝐾 ± 1.65 ( 𝜎
√𝑁 )] ≈ 0.90)
𝑃𝑟 (�̅� ∈ [𝐾 ± 1.96 ( 𝜎
√𝑁 )] ≈ 0.95)
𝑃𝑟 (�̅� ∈ [𝐾 ± 2.58 ( 𝜎
√𝑁 )] ≈ 0.99)
The next step in conducing our hypothesis test is to collect a data sample and calculate the sample
mean.
Upon observing the sample mean, we must decide whether to reject our deduced distribution for the
sample mean
�̅�~𝑁 (𝐾, 𝜎
√𝑁 ).
This decision hinges on whether we deem the observed sample mean as “reasonably likely,” and the
probabilities calculated above can be used to make the following arguments:
• Reject the distribution if the sample mean is more than 1.65 standard deviations from K,
otherwise fail to reject. This generates a degree of support of 90%.
• Reject the distribution if the sample mean is more than 1.96 standard deviations from K,
otherwise fail to reject. This generates a degree of support of 95%.
• Reject the distribution if the sample mean is more than 2.58 standard deviations from K,
otherwise fail to reject. This generates a degree of support of 99%.
So, to conduct a hypothesis test, we must (1) choose the desired degree of support (e.g., 90%, 95%, or
99%) and (2) measure how many standard deviations the sample mean is from the hypothesized value.
This difference, 𝑧, it can be solved for as follows:
𝑧 = �̅� − 𝐾
𝜎/√𝑁
We cannot calculate 𝑧 using our sample since we do not observe 𝜎; hence, simply replace 𝜎 w/ 𝑆.
By making this substitution, we rename the difference to be 𝑡, also called the t-stat, solved for as:
𝑡 = �̅� − 𝐾
𝑆/√𝑁
The t-stat is a test statistic, defined as any single value derived from a sample that can be used to
perform a hypothesis test.
Does replacing 𝜎 w/ 𝑆 affect our cutoff rules – i.e., does using a cutoff of 1.65, 1.96, and 2.58 still
correspond to a degree of support of 90%, 95%, and 99%?
The appropriate cutoffs using the t-stat come from the t-distribution, hence they are different.
However, the cutoffs for the t-distribution closely resemble those for the normal distribution when N >
30.
In our customer age example, the t-stat is
𝑡 = 43.61 − 44.5
12.72/√872 = −2.07.
Here, |𝑡| > 1.96, so we reject that �̅�~𝑁 (44.5, 12.72
√872 ) w/ 95% confidence. If we wanted to be 99%
confidence, we would fail to reject that �̅�~𝑁 (44.5, 12.72
√872 ). why?
Instead of computing the t-stat and comparing that to a cutoff value, we can solve for its p-value,
defined as the probability of attaining a test stat as extreme as the one that was observed.
If the p-value is < than the “significance value” defined as 1 – the degree of support, so in the case of
95%, 1-0.95 = 5%, then we reject the null hypothesis.
All these stats are instantly solved for in Stata, so let us do another example using the
Example 6:
To better assess the willingness to pay for advertising on others’ websites, you want to learn the mean
profit per visit for all visits to your website. To accomplish this, you have collected a random sample of
4,738 visits to your website over the past 6 months.
First, build a 99% confidence interval for the mean profit per visit for all your visitors.
. import excel using WebProfits.xlsx, firstrow clear
. ameans Profit, level(99)
Let the null hypothesis be that mean profit per visit for all your visitors is $11.50 – i.e.,
𝐻0: 𝜇 = 11.50
𝐻1: 𝜇 ≠ 11.50
. ttest Profit == 11.50, level(99)
The Stata results for this command are as follows:
As you can see, the absolute value of the calculated t-stat is equal to 12.08, which is much larger than
the critical value of 2.58, hence we reject the null hypothesis that the mean is equal to $11.50.
Finally, since the p-value = 0.00 is < 1-0.99, or 0.01, again, we reject the null hypothesis that the mean is
equal to $11.50.
I know, I know, things are much easier in Stata!!
Pr(T < t) = 0.0000 Pr(|T| > |t|) = 0.0000 Pr(T > t) = 1.0000
Ha: mean < 11.50 Ha: mean != 11.50 Ha: mean > 11.50
Ho: mean = 11.50 degrees of freedom = 4737
mean = mean(Profit) t = -12.0835
Profit 4,738 9.355635 .1774621 12.21528 8.898339 9.812932
Variable Obs Mean Std. Err. Std. Dev. [99% Conf. Interval]
One-sample t test