# Confidence Bands for Universal Scalability Models

**The Pith of Performance**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In the recent GDAT class, confidence intervals (CI) for performance data were discussed. Their generalization to confidence bands (CB) for scalability projections using the USL model also came up informally. I showed a prototype plot but it was an ugly hack. Later requests from GDAT attendees to apply CBs to their own data meant I had to do something about that. I tried a lot of things in R that didn’t produce the expected results. Ultimately, I was led to explore the ggplot2 package—the “gg” stands for *grammar of graphics*. A set of ggplots, corresponding to the VAMOOS stages of USL analysis, is shown in Figure 1.

For a long time I’ve been on a crusade about including error reporting and display in computer performance data. ggplot2 has a lot of inbuilt features to easily enable such displays—CBs being one of them. Unfortunately, many of the ggplot2 examples assume the application of linear models or data smoothers.

The USL requires confidence bands for a *nonlinear model* that is solved using the `nls` function in R. The distinction can be seen in Figure 1. The first plot (*upper left*) shows the raw data taken from Chapter 5 and Appendix B in my GCaP book. The independent variable (p) is the number of physical processors and the dependent variable (Xp) is the measured throughput for that processor configuration. The second plot (*upper right*) shows what happens if you apply an inbuilt ggplot2 data-smoother like this:

<br />qplot(p, Xp, data=bmark, geom=c("point", "smooth"), main = "Data smoother")<br />where the invocation is made through the

`geom`or geometrical object—a language construct that tells ggplot2 you wish to display both the data

*points*and a

*smooth*curve through those data.

This is very convenient if you are just trying to visualize underlying trends or patterns in the data. For the proper analysis of scalability data, however, we need a predictive (nonlinear) model, and that’s role of the USL—a *physically constrained* model. To apply the USL, we first normalize the raw performance data to get USL coefficients (σ and κ) and then fit the USL model to the original data, which looks like this in R:

<br />X1 <- bmark$Xp[1]<br />Cp <- bmark$Xp / X1<br />usl <- nls( Cp ~ p/(1 + A * (p-1) + B * p * (p-1)), data=bmark, start=c(A=0.1, B=0.01) )<br /><br />curves <- with(bmark, expand.grid(p=seq(min(p), max(p), length=100)))<br />curves$fit <- X1 * predict(usl, newdata=curves)<br />qplot(p, Xp, data=bmark) + geom_smooth(aes(y=fit), data=curves, stat="identity") <br />The third plot (

*lower left*) shows this fit and the fourth plot (

*lower right*) shows the USL fit together with the corresponding 95% CBs. Figure 2 summarizes the complete USL regression analysis, which is in complete numerical agreement with the Mathematica analysis in Appendix B.

It is often preferable to place important information, such as the fitted USL parameters, right in the plot panel itself. This might not be considered aesthetic by graphics purists but it saves looking for that information separately—especially when it might not be at hand—and it is not semantically equivalent to a conventional legend. Although this kind of embedded text is generally shunned for aesthetic reasons in ggplot2, the newer `annotate` construct does facilitate it. As you can see in Figure 2, the most recent version is also capable of parsing math notation.

Here is the complete R script:

<br /># USL confidence bands using ggplot2 functions<br />#<br /># Created by NJG on Wed Sep 1 08:47:21 2010<br /># Updated by NJG on Sat Sep 4 22:14:25 2010<br /><br />library(ggplot2)<br />library(gridExtra)<br /><br /># Benchmark data from Table 5.1 in GCaP book www.perfdynamics.com/iBook/gcap.html<br />bmark <- read.table(textConnection("p Xp<br />1 20<br />4 78<br />8 130<br />12 170 <br />16 190<br />20 200<br />24 210<br />28 230<br />32 260<br />48 280<br />64 310"), header=TRUE)<br />closeAllConnections()<br /><br /># Look at raw data and cf. standard smoother fit<br />p1 <- qplot(p, Xp, data=bmark, main="Raw bench data")<br />p2 <- qplot(p, Xp, data=bmark, geom=c("point", "smooth"), main = "Data smoother")<br /><br />samples <- dim(bmark)[1]<br />degfree <- samples - 1<br /><br /># Normalize bench data to get USL coeffs<br />X1 <- bmark$Xp[1]<br />Cp <- bmark$Xp / X1<br />usl <- nls( Cp ~ p/(1 + A * (p-1) + B * p * (p-1)), data=bmark, start=c(A=0.1, B=0.01) )<br /><br /># Rescale to bench data for stat calcs<br />y.usl <- predict(usl) * X1<br />y.mu <- mean(bmark$Xp)<br />y.se <- sqrt(sum((bmark$Xp-y.usl)^2) / degfree)<br />sse <- sum((bmark$Xp-y.usl)^2)<br />sst <- sum((bmark$Xp-y.mu)^2)<br />y.r2 <- 1 - sse / sst<br />y.ci <- y.se * qt(0.95, degfree)<br /><br />curves <- with(bmark, expand.grid(p=seq(min(p), max(p), length=100)))<br />curves$fit <- X1 * predict(usl, newdata=curves)<br />curves$ucl <- curves$fit + y.ci<br />curves$lcl <- curves$fit - y.ci<br /><br />p3 <- qplot(p, Xp, data=bmark, main="USL fit") + geom_smooth(aes(y=fit), data=curves, stat="identity") <br />p4 <- qplot(p, Xp, data=bmark, main="USL fit + CI bands") + <br /> geom_smooth(aes(y=fit, ymin=lcl, ymax=ucl), data=curves, stat="identity") <br />grid.newpage()<br />print(grid.arrange(p1, p2, p3, p4, nrow=2, ncol=2))<br /><br />p <- qplot(p, Xp, data=bmark, main="USL Analysis of SGI Origin 2000",xlab="Processors", ylab="Benchmark throughput (Krays/s)") + <br /> geom_smooth(aes(y=fit, ymin=lcl, ymax=ucl), data=curves, stat="identity") + <br /> annotate("text", x=30, y=100, label="See GCaP book Chap. 5 & App. B", hjust=0, vjust=0, size=4)<br />p <- p + annotate("text", x=30, y=88, label=paste("sigma == ", coef(usl)['A']), parse=T, hjust=0, size=4)<br />p <- p + annotate("text", x=30, y=75, label=paste("kappa == ", coef(usl)['B']), parse=T, hjust=0, size=4)<br />p <- p + annotate("text", x=30, y=62, label=paste("R^2 == ",y.r2), parse=T, hjust=0, size=4)<br />print(p)<br />Arguably, ggplot2 does offer a more compact grammar for plotting statistical data but, as usual, there are some trade-offs relative to the native plot functions in R:

- The syntax is very different and it takes a while to become familiar with it.
- Rendering is painfully slow, although I understand this is being addressed.
- Divining the author’s intent when customizing certain constructs almost requires divine intervention.

**leave a comment**for the author, please follow the link and comment on their blog:

**The Pith of Performance**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.