#set document(title: "8.1 Q-Q Plots", author: "OpenStax") #set page(width: 8.5in, height: auto, margin: 1in) #import "@preview/cetz:0.5.2" #set text(font: ("STIX Two Text", "Libertinus Serif", "New Computer Modern"), size: 10.5pt, lang: "en") #show math.equation: set text(font: ("STIX Two Math", "New Computer Modern Math")) #set par(justify: true, leading: 0.62em, spacing: 0.9em) #set enum(spacing: 1.1em) // room between list items so tall inline fractions don't collide #set list(spacing: 1.1em) #set table(stroke: 0.5pt + rgb("#c7ccd3")) #let BLUE = rgb("#183B6F") // brand navy — section bars + example/solution labels (white on navy 11.09:1) #let ORANGE = rgb("#A94509") // brand primary-700 — AA-safe deep orange for TEXT (5.93:1 on white; raw brand #F37021 is 2.94:1 and must never carry text) #let RED = rgb("#DC2626") // brand error-600 #let GREEN = rgb("#059669") // brand success-600 (decoration only; small green text uses green-text #007942) #show heading.where(level: 1): it => block(width: 100%, above: 0pt, below: 16pt, fill: gradient.linear(BLUE, rgb("#2C5AA0")), inset: (x: 14pt, y: 12pt), radius: 3pt, text(fill: white, weight: "bold", size: 19pt, it.body)) #show heading.where(level: 2): it => block(width: 100%, above: 18pt, below: 10pt, fill: BLUE, inset: (x: 10pt, y: 6pt), radius: 2pt, text(fill: white, weight: "bold", size: 12pt, it.body)) #show heading.where(level: 3): it => text(fill: ORANGE, weight: "bold", size: 12.5pt, it.body) #show heading.where(level: 4): it => text(fill: BLUE, weight: "bold", size: 10.5pt, it.body) #let examplebox(label, title, body) = block(width: 100%, breakable: true, fill: rgb("#EFF1F5"), stroke: 0.5pt + rgb("#CFDDF0"), radius: 4pt, inset: 10pt, above: 12pt, below: 12pt)[ #block(below: 6pt)[#box(fill: BLUE, inset: (x: 6pt, y: 2pt), radius: 2pt, text(fill: white, weight: "bold", size: 8.5pt, label)) #h(0.4em) #strong[#title]] #body] // rail = decorative left rule (raw brand token); labelcolor = AA-safe label text shade #let notebox(label, rail, labelcolor, tint, body) = block(width: 100%, breakable: true, fill: tint, stroke: (left: 3pt + rail), inset: (left: 10pt, rest: 8pt), radius: (right: 4pt), above: 11pt, below: 11pt)[ #text(fill: labelcolor, weight: "bold", size: 7.5pt, tracking: 0.5pt)[#upper(label)] #linebreak() #body] #let solutionbox(body) = block(above: 4pt, below: 8pt)[ #text(fill: BLUE, weight: "bold", size: 8.5pt)[Solution] #linebreak() #body] #let figph(msg) = block(width: 100%, height: 60pt, fill: rgb("#f6f7f9"), stroke: (paint: rgb("#c7ccd3"), dash: "dashed"), radius: 4pt, inset: 10pt)[ #align(center + horizon, text(fill: rgb("#889"), style: "italic", size: 9pt, msg))] // Standardize inlined figure sizes: measure the natural CeTZ canvas, then scale to a // consistent envelope (aspect-aware; see build_typst.py FIG_* constants). Unlike the // print preamble, dimensions are FLOORED: in an editor a user can trim a figure to a // degenerate 1-D shape (a bare line), and w/h or tw/w would then divide by zero. #let _STD_W = 3.5 #let _WIDE_W = 5.6 #let _MAX_H = 3.4 #let _ASPECT_WIDE = 2.2 #let _UPSCALE_MAX = 1.15 #let stdfig(body) = context { let m = measure(body) let w = calc.max(m.width / 1in, 0.01) let h = calc.max(m.height / 1in, 0.01) let tw = if w / h > _ASPECT_WIDE { _WIDE_W } else { _STD_W } let s = calc.min(tw / w, _MAX_H / h, _UPSCALE_MAX) align(center, box(scale(x: s * 100%, y: s * 100%, reflow: true, body))) } #show figure: set block(breakable: false) #set figure(gap: 8pt) #show figure.caption: set text(size: 8.5pt, fill: rgb("#555")) == 8.1#h(0.6em)Q-Q Plots #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Prerequisites] Histograms, Distributions, Percentiles, #link("https://onlinestatbook.com/2/describing_bivariate_data/bivariate.html")[Describing Bivariate Data], #link("https://onlinestatbook.com/2/normal_distribution/normal_distribution.html")[Normal Distributions] #linebreak() #linebreak() ] #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Learning Objectives] + State what q-q plots are used for. + Describe the shape of a q-q plot when the distributional assumption is met. + Be able to create a normal q-q plot. ] Introduction #linebreak() The quantile-quantile or q-q plot is an exploratory graphical device used to check the validity of a distributional assumption for a data set. In general, the basic idea is to compute the theoretically expected value for each data point based on the distribution in question. If the data indeed follow the assumed distribution, then the points on the q-q plot will fall approximately on a straight line. Before delving into the details of q-q plots, we first describe two related graphical methods for assessing distributional assumptions: the histogram and the cumulative distribution function (CDF). As will be seen, q-q plots are more general than these alternatives. #linebreak() Assessing Distributional Assumptions #linebreak() As an example, consider data measured from a physical device such as the spinner depicted in Figure 1. The red arrow is spun around the center, and when the arrow stops spinning, the number between 0 and 1 is recorded. Can we determine if the spinner is fair? #figure(figph[Drawing of a spinner: a circular dial marked 0 at the top and running clockwise through 0.1, 0.2, ... to 0.9, with a red arrow from the center pointing between 0.8 and 0.9. A callout labels the landing point u = 0.876. The spinner is the source of the uniform (0, 1) data used throughout the q-q plot section.], alt: "Drawing of a spinner: a circular dial marked 0 at the top and running clockwise through 0.1, 0.2, ... to 0.9, with a red arrow from the center pointing between 0.8 and 0.9. A callout labels the landing point u = 0.876. The spinner is the source of the uniform (0, 1) data used throughout the q-q plot section.", caption: [Figure 1. A physical device that gives samples from a uniform distribution.]) #linebreak() If the spinner is fair, then these numbers should follow a uniform distribution. To investigate whether the spinner is fair, spin the arrow n times, and record the measurements by {μ#sub[1], μ#sub[2], ..., μ#sub[n]}. In this example, we collect n = 100 samples. The histogram provides a useful visualization of these data. In Figure 2, we display three different histograms on a probability scale. The histogram should be flat for a uniform sample, but the visual perception varies depending on whether the histogram has 10, 5, or 3 bins. The last histogram looks flat, but the other two histograms are not obviously flat. It is not clear which histogram we should base our conclusion on. #figure(figph[Three histograms of the same sample of 100 spinner values u, drawn with different bin widths: 10 bins on the left, 5 in the middle, 3 on the right. The 10-bin version is very ragged, with bar heights bouncing between about 0.5 and 1.4; the 5-bin version is less so; the 3-bin version is almost perfectly flat at 1.0. The underlying density really is flat — the raggedness is an artifact of narrow bins, which is why the histogram is a poor tool for judging the shape here.], alt: "Three histograms of the same sample of 100 spinner values u, drawn with different bin widths: 10 bins on the left, 5 in the middle, 3 on the right. The 10-bin version is very ragged, with bar heights bouncing between about 0.5 and 1.4; the 5-bin version is less so; the 3-bin version is almost perfectly flat at 1.0. The underlying density really is flat — the raggedness is an artifact of narrow bins, which is why the histogram is a poor tool for judging the shape here.", caption: [Figure 2. Three histograms of a sample of 100 uniform points.]) Alternatively, we might use the cumulative distribution function (CDF), which is denoted by F(μ). The CDF gives the probability that the spinner gives a value less than or equal to μ, that is, the probability that the red arrow lands in the interval \[0, μ\]. By simple arithmetic, F(μ) = μ, which is the diagonal straight line y = x. The CDF based upon the sample data is called the empirical CDF (ECDF), is denoted by #math.equation(block: false, alt: "F hat sub n open parenthesis u close parenthesis")[$hat(F)_(n) ( u )$], and is defined to be the fraction of the data less than or equal to μ; that is, #linebreak() #math.equation(block: true, alt: "F hat sub n open parenthesis u close parenthesis equals the fraction # u sub i less than or equal to u over n")[$hat(F)_(n) ( u ) = frac(\# #h(0.167em) u_(i) ≤ u, n)$] In general, the ECDF takes on a ragged staircase appearance. #linebreak() For the spinner sample analyzed in Figure 2, we computed the ECDF and CDF, which are displayed in Figure 3. In the left frame, the ECDF appears close to the line y = x, shown in the middle frame. In the right frame, we overlay these two curves and verify that they are indeed quite close to each other. Observe that we do not need to specify the number of bins as with the histogram. #figure(figph[Three panels on a common u axis from 0 to 1 and probability axis from 0 to 1. Left, the Empirical CDF of the 100 spinner values: a ragged rising staircase. Middle, the Theoretical CDF: the straight diagonal line y = x. Right, Overlaid CDFs: the staircase drawn on top of the line, with the gaps between them filled in red — the red slivers are small and fall on both sides, showing close agreement.], alt: "Three panels on a common u axis from 0 to 1 and probability axis from 0 to 1. Left, the Empirical CDF of the 100 spinner values: a ragged rising staircase. Middle, the Theoretical CDF: the straight diagonal line y = x. Right, Overlaid CDFs: the staircase drawn on top of the line, with the gaps between them filled in red — the red slivers are small and fall on both sides, showing close agreement.", caption: [Figure 3. The empirical and theoretical cumulative distribution functions of a sample of 100 uniform points.]) === q-q plot for uniform data The q-q plot for uniform data is very similar to the empirical CDF graphic, except with the axes reversed. The q-q plot provides a visual comparison of the sample quantiles to the corresponding theoretical quantiles. In general, if the points in a q-q plot depart from a straight line, then the assumed distribution is called into question. Here we define the qth quantile of a batch of n numbers as a number ξ#sub[q] such that a fraction q x n of the sample is less than ξ#sub[q], while a fraction (1 - q) x n of the sample is greater than ξ#sub[q]. The best known quantile is the median, ξ#sub[0.5], which is located in the middle of the sample. Consider a small sample of 5 numbers from the spinner: #linebreak() μ#sub[1] = 0.41, μ#sub[2] =0.24, μ#sub[3] =0.59, μ#sub[4] =0.03,and μ#sub[5] =0.67. #linebreak() Based upon our description of the spinner, we expect a uniform distribution to model these data. If the sample data were “perfect,” then on average there would be an observation in the middle of each of the 5 intervals: 0 to .2, .2 to .4, .4 to .6, and so on. Table 1 shows the 5 data points (sorted in ascending order) and the theoretically expected value of each based on the assumption that the distribution is uniform (the middle of the interval). #linebreak() Table 1. Computing the Expected Quantile Values. #figure(table( columns: 3, align: left, inset: 6pt, table.header([Data (μ)], [Rank (#emph[i])], [Middle of the #emph[#linebreak() i]th Interval]), [.03 #linebreak() .24 #linebreak() .41 #linebreak() .59 #linebreak() .67], [1 #linebreak() 2 #linebreak() 3 #linebreak() 4 #linebreak() 5], [.1 #linebreak() .3 #linebreak() .5 #linebreak() .7 #linebreak() .9], )) The theoretical and empirical CDFs are shown in Figure 4 and the q-q plot is shown in the left frame of Figure 5. #linebreak() #figure(figph[Three panels illustrating quantiles for a small sample of n = 5 spinner values. Left, the Theoretical CDF, the diagonal line y = x. Middle, the Empirical CDF, a five-step staircase jumping by 0.2 at each of the five data points (marked as red dots along the bottom near 0.03, 0.23, 0.41, 0.58 and 0.67). Right, the same diagonal with horizontal red arrows drawn in from probabilities 0.1, 0.3, 0.5, 0.7 and 0.9 — the midpoints of the five intervals — and vertical red arrows dropping to the corresponding theoretical quantiles 0.1, 0.3, 0.5, 0.7 and 0.9 on the u axis.], alt: "Three panels illustrating quantiles for a small sample of n = 5 spinner values. Left, the Theoretical CDF, the diagonal line y = x. Middle, the Empirical CDF, a five-step staircase jumping by 0.2 at each of the five data points (marked as red dots along the bottom near 0.03, 0.23, 0.41, 0.58 and 0.67). Right, the same diagonal with horizontal red arrows drawn in from probabilities 0.1, 0.3, 0.5, 0.7 and 0.9 — the midpoints of the five intervals — and vertical red arrows dropping to the corresponding theoretical quantiles 0.1, 0.3, 0.5, 0.7 and 0.9 on the u axis.", caption: [Figure 4. The theoretical and empirical CDFs of a small sample of 5 uniform points, together with the expected values of the 5 points (red dots in the right frame).]) In general, we consider the full set of sample quantiles to be the sorted data values μ#sub[(1)] \< μ#sub[(2)] \< μ#sub[(3)] \< ··· \< μ#sub[(n-1)] \< μ#sub[(n)] , where the parentheses in the subscript indicate the data have been ordered. Roughly speaking, we expect the first ordered value to be in the middle of the interval (0, 1/n), the second to be in the middle of the interval (1/n, 2/n), and the last to be in the middle of the interval ((n - 1)/n, 1). Thus, we take as the theoretical quantile the value #math.equation(block: true, alt: "ξ sub q equals q approximately equals the fraction i minus 0.5 over n")[$ξ_(q) = q ≈ frac(i − 0.5, n)$] where q corresponds to the ith ordered sample value. We subtract the quantity 0.5 so that we are exactly in the middle of the interval ((i - 1)/n, i/n). These ideas are depicted in the right frame of Figure 4 for our small sample of size n = 5. We are now prepared to define the q-q plot precisely. First, we compute the n expected values of the data, which we pair with the n data points sorted in ascending order. For the uniform density, the q-q plot is composed of the n ordered pairs #math.equation(block: true, alt: "open parenthesis the fraction i minus 0.5 over n , u sub open parenthesis i close parenthesis close parenthesis for i equals 1 , 2 , and so on , n .")[$( frac(i − 0.5, n) , #h(0.167em) u_(( i )) ) #h(1em) "for " i = 1 , 2 , … , n .$] #linebreak() This definition is slightly different from the ECDF, which includes the points (u#sub[(i)], i/n). In the left frame of Figure 5, we display the q-q plot of the 5 points in Table 1. In the right two frames of Figure 5, we display the q-q plot of the same batch of numbers used in Figure 2. In the final frame, we add the diagonal line y = x as a point of reference. #figure(figph[Three q-q plots of sample quantile against theoretical quantile, both axes 0 to 1. Left, the n = 5 sample: five dots at roughly (0.1, 0.02), (0.3, 0.23), (0.5, 0.41), (0.7, 0.59), (0.9, 0.67), rising but bowing below a straight line. Middle, the n = 100 sample as a dense rising trail of dots. Right, the same 100 points with the reference line y = x drawn in red: the trail runs just above the line for most of its length and crosses back near 0.4 and 1.0.], alt: "Three q-q plots of sample quantile against theoretical quantile, both axes 0 to 1. Left, the n = 5 sample: five dots at roughly (0.1, 0.02), (0.3, 0.23), (0.5, 0.41), (0.7, 0.59), (0.9, 0.67), rising but bowing below a straight line. Middle, the n = 100 sample as a dense rising trail of dots. Right, the same 100 points with the reference line y = x drawn in red: the trail runs just above the line for most of its length and crosses back near 0.4 and 1.0.", caption: [Figure 5. (Left) q-q plot of the 5 uniform points. (Right) q-q plot of a sample of 100 uniform points.]) The sample size should be taken into account when judging how close the q-q plot is to the straight line. We show two other uniform samples of size n = 10 and n = 1000 in Figure 6. Observe that the q-q plot when n = 1000 is almost identical to the line y = x, while such is not the case when the sample size is only n = 10. #figure(figph[Two q-q plots against the uniform theoretical quantile with the red y = x reference line. Left, a sample of 10 points that all sit ABOVE the line, from (0.05, 0.35) up to (0.95, 0.90) — the sample quantiles are systematically too large. Right, a large sample whose dense trail hugs the reference line closely along its whole length.], alt: "Two q-q plots against the uniform theoretical quantile with the red y = x reference line. Left, a sample of 10 points that all sit ABOVE the line, from (0.05, 0.35) up to (0.95, 0.90) — the sample quantiles are systematically too large. Right, a large sample whose dense trail hugs the reference line closely along its whole length.", caption: [Figure 6. q-q plots of a sample of 10 and 1000 uniform points.]) In Figure 7, we show the q-q plots of two random samples that are not uniform. In both examples, the sample quantiles match the theoretical quantiles only at the median and at the extremes. Both samples seem to be symmetric around the median. But the data in the left frame are closer to the median than would be expected if the data were uniform. The data in the right frame are further from the median than would be expected if the data were uniform. #figure(figph[Two q-q plots against the uniform theoretical quantile with the red y = x line. Left, the trail rises steeply at both ends and flattens in the middle, crossing the line once near 0.48 — an S laid on its side, the signature of a distribution more concentrated in the middle than the uniform. Right, the mirror-image pattern: the trail is flat at both ends and steep through the middle, again crossing once near 0.5.], alt: "Two q-q plots against the uniform theoretical quantile with the red y = x line. Left, the trail rises steeply at both ends and flattens in the middle, crossing the line once near 0.48 — an S laid on its side, the signature of a distribution more concentrated in the middle than the uniform. Right, the mirror-image pattern: the trail is flat at both ends and steep through the middle, again crossing once near 0.5.", caption: [Figure 7. q-q plots of two samples of size 1000 that are not uniform.]) #linebreak() In fact, the data were generated in the R language from beta distributions with parameters a = b = 3 on the left and a = b =0.4 on the right. In Figure 8 we display histograms of these two data sets, which serve to clarify the true shapes of the densities. These are clearly non-uniform. #figure(figph[Two histograms of samples on the interval 0 to 1, both with frequency axes to about 250. Left, a mound-shaped distribution rising from about 8 at 0.0-0.1 to a peak of about 207 at 0.4-0.5 and falling back to about 9 at 0.9-1.0. Right, a U-shaped distribution: about 233 in the lowest bin and 243 in the highest, with a flat floor of roughly 50 to 80 in between. Neither is uniform, and the two shapes are what produce the two opposite S-curves in the previous q-q figure.], alt: "Two histograms of samples on the interval 0 to 1, both with frequency axes to about 250. Left, a mound-shaped distribution rising from about 8 at 0.0-0.1 to a peak of about 207 at 0.4-0.5 and falling back to about 9 at 0.9-1.0. Right, a U-shaped distribution: about 233 in the lowest bin and 243 in the highest, with a flat floor of roughly 50 to 80 in between. Neither is uniform, and the two shapes are what produce the two opposite S-curves in the previous q-q figure.", caption: [Figure 8. Histograms of the two non-uniform data sets.]) #strong[#linebreak()]q-q plot for normal data The definition of the q-q plot may be extended to any continuous density. The q-q plot will be close to a straight line if the assumed density is correct. Because the cumulative distribution function of the uniform density was a straight line, the q-q plot was very easy to construct. For data that are not uniform, the theoretical quantiles must be computed in a different manner. Let {z#sub[1], z#sub[2], ..., z#sub[n]} denote a random sample from a normal distribution #linebreak() with mean μ = 0 and standard deviation σ = 1. Let the ordered values be #linebreak() denoted by z#sub[{1)] \< z#sub[(2)] \< z#sub[(3)] \< ... \< z#sub[(n-1)]\