text:
Data validation with the assertr package

Version 2.0 of my data set validation package assertr hit CRAN just this weekend. It has some pretty great improvements over version 1. For those new to the package, what follows is a short and new introduction. For those who are already using assertr, the text below will point out the improvements.

I can (and have) go on and on about the treachery of messy/bad datasets. Though its substantially less exciting than… pretty much everything else, I believe (proportional to the heartache and stress it causes) we don’t spend enough time talking about it or building solutions around it. No matter how new and fancy your ML algorithm is, it’s success is predicated upon a properly sanitized dataset. If you are using bad data, your approach will fail—either flagrantly (best case), or unnoticeably (considerably more probable and considerably more pernicious).

assertr is a R package to help you identify common dataset errors. More specifically, it helps you easily spell out your assumptions about how the data should look and alert you of any deviation from those assumptions.

I’ll return to this point later in the post when we have more background, but I want to be up front about the goals of the package; assertr is not (and can never be) a “one-stop shop” for all of your data validation needs. The specific kind of checks individuals or teams have to perform any particular dataset are often far too idiosyncratic to ever be exhaustively addressed by a single package (although, the assertive meta-package may come very close!) But all of these checks will reuse motifs and follow the same patterns. So, instead, I’m trying to sell assertr as a way of thinking about dataset validations—a set of common dataset validation actions. If we think of these actions as verbs, you could say that assertr attempts to impose a grammar of error checking for datasets.

In my experience, the overwhelming majority of data validation tasks fall into only five different patterns:

• For every element in a column, you want to make sure it fits certain criteria. Examples of this strain of error checking would be to make sure every element is a valid credit card number, or fits a certain regex pattern, or represents a date between two other dates. assertr calls this verb assert.
• For every element in a column, you want to make sure certain criteria are met but the criteria can only be decided only after looking at the entire column as a whole. For example, testing whether each element is within n standard deviations of the mean of the elements requires computation on the elements prior to inform the criteria to check for. assertr calls this verb insist.
• For every row of a dataset, you want to make sure certain assumptions hold. Examples include ensuring that no row has more than n number of missing values, or that a group of columns are jointly unique and never duplicated. assertr calls this verb assert_rows.
• For every row of a dataset, you want to make sure certain assumptions hold but the criteria can only be decided only after looking at the entire column as a whole. This closely mirrors the distinction between assert and insist, but for entire rows (not individual elements). An example of using this would be checking to make sure that the Mahalanobis distance between each row and all other rows are within n number of standard deviations of the mean distance. assertr calls this verb insist_rows.
• You want to check some property of the dataset as a whole object. Examples include making sure the dataset has more than n columns, making sure the dataset has some specified column names, etc… assertr calls this last verb verify.

Some of this might sound a little complicated, but I promise this is a worthwhile way to look at dataset validation. Now we can begin with an example of what can be achieved with these verbs. The following example is borrowed from the package vignette and README…

Pretend that, before finding the average miles per gallon for each number of engine cylinders in the mtcars dataset, we wanted to confirm the following dataset assumptions…

• that it has the columns mpg, vs, and am
• that the dataset contains more than 10 observations
• that the column for 'miles per gallon' (mpg) is a positive number
• that the column for ‘miles per gallon’ (mpg) does not contain a datum that is outside 4 standard deviations from its mean
• that the am and vs columns (automatic/manual and v/straight engine, respectively) contain 0s and 1s only
• each row contains at most 2 NAs
• each row is unique jointly between the mpg, am, and wt columns
• each row's mahalanobis distance is within 10 median absolute deviations of all the distances (for outlier detection)
library(dplyr)
library(assertr)

mtcars %>%
verify(has_all_names("mpg", "vs", "am", "wt")) %>%
verify(nrow(.) > 10) %>%
verify(mpg > 0) %>%
insist(within_n_sds(4), mpg) %>%
assert(in_set(0,1), am, vs) %>%
assert_rows(num_row_NAs, within_bounds(0,2), everything()) %>%
assert_rows(col_concat, is_uniq, mpg, am, wt) %>%
group_by(cyl) %>%
summarise(avg.mpg=mean(mpg))

Before assertr version 2, the pipeline would immediately terminate at the first failure. Sometimes this is a good thing. However, sometimes we’d like to run a dataset through our entire suite of checks and record all failures. The latest version includes the chain_start and chain_end functions; all assumptions within a chain (below a call to chain_start and above chain_end) will run from beginning to end and accumulate errors along the way. At the end of the chain, a specific action can be taken but the default is to halt execution and display a comprehensive report of what failed including line numbers and the offending datum, where applicable.

Another major improvement since the last version of assertr of CRAN is that assertr errors are now S3 classes (instead of dumb strings). Additionally, the behavior of each assertion statement on success (no error) and failure can now be flexibly customized. For example, you can now tell assertr to just return TRUE and FALSE instead of returning the data passed in or halting execution, respectively. Alternatively, you can instruct assertr to just give a warning instead of throwing a fatal error. For more information on this, see help("success_and_error_functions")

Beyond these examples

Since the package was initially published on CRAN (almost exactly two years ago) many people have asked me how they should go about using assertr to test a particular assumption (and I’m very happy to help if you have one of your own, dear reader!) In every single one of these cases, I’ve been able to express it as an incantation using one of these 5 verbs. It also underscored, to me, that creating specialized functions for every need is a pipe dream. There is, however, two good pieces of news.

The first is that there is another package, assertive (vignette here) that greatly enhances the assertr experience. The predicates (functions that start with “is_”) from this (meta)package can be used in assertr pipelines just as easily as assertr’s own predicates. And assertive has an enormous amount of them! Some specialized and exciting examples include is_hex_color, is_ip_address, and is_isbn_code!

The second is if assertive doesn’t have what you’re looking for, with just a little bit of studying the assertr grammar, you can whip up your own predicates with relative ease. Using some these basic constructs and a little effort, I’m confident that the grammar is expressive enough to completely adapt to your needs.

If this package interests you, I urge you to read the most recent package vignette here. If you're a assertr old-timer, I point you to this NEWS file that list the changes from the previous version.

The Bayesian approach to ridge regression

In a previous post, we demonstrated that ridge regression (a form of regularized linear regression that attempts to shrink the beta coefficients toward zero) can be super-effective at combating overfitting and lead to a greatly more generalizable model. This approach to regularization used penalized maximum likelihood estimation (for which we used the amazing glmnet package). There is, however, another approach... an equivalent approach... but one that allows us greater flexibility in model construction and lends itself more easily to an intuitive interpretation of the uncertainty of our beta coefficient estimates. I'm speaking, of course, of the bayesian approach.

As it turns out, careful selection of the type and shape of our prior distributions with respect to the coefficients can mimic different types of frequentist linear model regularization. For ridge regression, we use normal priors of varying width.

Though it can be shown analytically that shifting the width of normal priors on the beta coefficients is equivalent to L2 penalized maximum likelihood estimation, the math is scary and hard to follow. In this post, we are going to be taking a computational approach to demonstrating the equivalence of the bayesian approach and ridge regression.

This post is going to be a part of a multi-post series investigating other bayesian approaches to linear model regularization including lasso regression facsimiles and hybrid approaches.

mtcars

We are going to be using the venerable mtcars dataset for this demonstration because (a) it's multicollinearity and high number of potential predictors relative to its sample size lends itself fairly well to ridge regression, and (b) we used it in the elastic net blog post :)

Before, you lose interest... here! have a figure! An explanation will follow.

After scaling the predictor variables to be 0-centered and have a standard deviation of 1, I described a model predicting mpg using all available predictors and placed normal priors on the beta coefficients with a standard deviation for each value from 0.05 to 5 (by 0.025). To fit the model, instead of MCMC estimation via JAGS or Stan, I used quadratic approximation performed by the awesome rethinking package written by Richard McElreath written for his excellent book, Statistical Rethinking. Quadratic approximation uses an optimization algorithm to find the maximum a priori (MAP) point of the posterior distribution and approximates the rest of the posterior with a normal distribution about the MAP estimate. I use this method chiefly because as long as it took to run these simulations using quadratic approximation, it would have taken many orders of magnitude longer to use MCMC. Various spot checks confirmed that the quadratic approximation was comparable to the posterior as told by Stan.

As you can see from the figure, as the prior on the coefficients gets tighter, the model performance (as measured by the leave-one-out cross-validated mean squared error) improves—at least until the priors become too strong to be influenced sufficiently by the evidence. The ribbon about the MSE is the 95% credible interval (using a normal likelihood). I know, I know... it's pretty damn wide.

The dashed vertical line is at the prior width that minimizes the LOOCV MSE. The minimum MSE is, for all practical purposes, identical to that of the highest performing ridge regression model using glmnet. This is good.

Another really fun thing to do with the results is to visualize the movement of the beta coefficient estimates and different penalties. The figure below depicts this. Again, the dashed vertical line is the highest performing prior width.

One last thing: we've heretofore only demonstrated that the bayesian approach can perform as well as the L2 penalized MLE... but it's conceivable that it achieves this by finding a completely different coefficient vector. The figure below shows the same figure as above but I overlaid the coefficient estimates (for each predictor) of the top-performing glmnet model. These are shown as the dashed colored horizontal lines.

These results are pretty exciting! (if you're the type to not get invited to parties). Notice that, at the highest performing prior width, the coefficients of the bayesian approach and the glmnet approach are virtually identical.

Sooooo, not only did the bayesian variety produce an equivalently generalizable model (as evinced by equivalent cross-validated MSEs) but also yielded a vector of beta coefficient estimates nearly identical to those estimated by glmnet. This suggests that both the bayesian approach and glmnet's approach, using different methods, regularize the model via the same underlying mechanism.

A drawback of the bayesian approach is that its solution takes many orders of magnitude more time to arrive at. Two advantages of the Bayesian approach are (a) the ability to study the posterior distributions of the coefficient estimates and ease of interpretation that they allows, and (b) the enhanced flexibility in model design and the ease by which you can, for example, swap out likelihood functions or construct more complicated hierarchal models.

If you are even the least bit interested in this, I urge you to look at the code (in this git repository) because (a) I worked really hard on it and, (b) it demonstrates cool use of meta-programming, parallelization, and progress bars... if I do say so myself :)

Genre-based Music Recommendations Using Open Data (and the problem with recommender systems)

After a long 12 months of pouring my soul into it, my book, Data Analysis with R, was finally published. After the requisite 2-4 day breather, I started thinking about how I was going to get back into the swing of regular blog posts and decided that the easier and softer way is to cannibalize and expand on an example in the book.

In the chapter "Sources of Data" I show how to consume web data of different formats in R. The motivating example is to build a simple recommendation system that uses user-supplied "tags" (genres/labels) submitted to Last.fm and MusicBrainz to quantify musical artist "similarity". The example in the book stops at the construction and sorting of the similarity matrix but, in this post, we're going to make a really fly D3 visualization of the musical similarity network and provide recommendations in the tooltips. The code, including the Javascript and HTML, I used for this post was hastily thrown into a git repo and is available here. If you're uninterested in the detailed methodology, I suggest you skip to the section labeled "Outcome".

Methodology

Although in the book tags from both Last.fm and MusicBrainz are used, we'll just be using Last.fm here. (In additional contrast to the book, the code here is, as you might imagine, substantially faster-paced.)

The first step is to make a character vector of all the artists that you'd like to be included. If you were building a real system, you'd probably want all Last.fm artists. Since we're not, I just used 70 of my most played artists on my Last.fm. Since I got the list straight from the source, I didn't have to worry that any of the API requests would return "No Artist Found".

The following is a function that takes an artist and returns the properly formatted Last.fm API call to get the tags in JSON format.

create_artist_query_url_lfm <- function(artist_name){
prefix <- "http://ws.audioscrobbler.com/2.0/?method=artist.gettoptags&artist="
postfix <- "&api_key=c2e57923a25c03f3d8b317b3c8622b43&format=json"
encoded_artist <- URLencode(artist_name)
return(paste0(prefix, encoded_artist, postfix))
}

This is an example of the JSON payload from my favorite merengue artist.

We only want the tag names--curiously, attempts to factor in degree of tag fit (the "count" attribute) resulted in (what I interpreted as) substantially poorer recommendations.

The following is a function that will return a vector of all the tags.

library(jsonlite)

get_tag_frame_lfm <- function(an_artist){
print(paste0("Attempting to fetch: ", an_artist))
artist_url <- create_artist_query_url_lfm(an_artist)
json <- fromJSON(artist_url)
return(as.vector(json$toptags$tag[,"name"]))
}

Since the above function is referentially transparent, and it involves using resources that aren't yours, it's a good idea to memoize the function so that if you (accidentally or otherwise) call the function with the same artist, the function will return the cached result instead of making the web request again. This can be achieved quite easily with the memoise package.

library(memoise)
mem_get_tag_frame_lfm <- memoise(get_tag_frame_lfm)

To get the tags from all the artists in our custom ARTIST_LIST vector...

artists_tags <- sapply(ARTIST_LIST, mem_get_tag_frame_lfm)
names(artists_tags) <- ARTIST_LIST

To get a list of all pairs of artists to compute the similarity for, we can use the combn function to create a 2 by 2,415 character matrix of all possible combinations (choose 2). Let’s get that into a 2,415 by 2 data.frame with the name "artist1" and "artist2"...

cmbs <- combn(ARTIST_LIST, 2)
comparisons <- data.frame(t(cmbs))
names(comparisons) <- c("artist1", "artist2")

The similarity metric we’ll be using is simple as all get-out: the Jaccard index. Assuming we put the tags from both artists into two sets, it is the cardinality of the sets' intersection divided by the sets' union...

jaccard_index <- function(tags1, tags2){
length(intersect(tags1, tags2))/length(union(tags1, tags2))
}

comparisons$similarity <- apply(comparisons, 1, function(arow){ jaccard_index(artists_tags[[unlist(arow[1])]], artists_tags[[unlist(arow[2])]]) })  Now we've added a new column to our previously 2,415 by 2 data.frame, "similarity" that contains the Jaccard index. Our D3 visualization expects a JSON with two top level attributes: "nodes" and "links". The "nodes" attribute is an array of x number of 5 key-value pairs (where x is the number of nodes). The 5 keys are "name" (the name of the artist) "group" (a number that affects the coloring of the node in the visualization that we will be setting to "1"), and "first", "second", and "third", which are the top 3 most similar artists and will serve as the recommendations that pop-up in a tool-tip when you mouse over an artist node in the visualization. This is some code to get the top 3 most similar artists. It takes the 2,415 by 3 comparisons data.frame, the number of "most similar artists" to return, an artist, and an arbitrary threshold for "similar-ness" as arguments. Any similarity below this threshold will not be considered a viable recommendation. library(dplyr) get_top_n <- function(comparisons, N, artist, threshold){ comparisons %<>% filter(artist1==artist | artist2==artist) %>% arrange(desc(similarity)) other_artist <- ifelse(comparisons$similarity>threshold,
ifelse(comparisons$artist1==artist, comparisons$artist2, comparisons$artist1), "None") return(other_artist[1:N]) }  The inner ifelse clause has to handle the fact that the "similar" artist can be in the first column or the second column. The outer ifelse returns "None" for every similarity value that is not above the threshold. Let's make the data.frame that will serve as the "nodes" attribute in the final JSON... nodes <- sapply(ARTIST_LIST, function(x) get_top_n(comparisons, 3, x, 0.25)) nodes <- data.frame(t(nodes)) names(nodes) <- c("first", "second", "third") nodes$name <- row.names(nodes)
row.names(nodes) <- NULL
nodes$group <- 1  For the other top-level JSON attribute, "links", we need an array of y number of 5 key-value pairs where y is the number of sufficiently strong similarities between the artists. The 5 keys are "node1" (the name of the first artist), "source" (the 0-indexed index of the artist with respect to the array in the "nodes" attribute), "node2" (the name of the second artist), "target" (the index of the second artist) and "weight", which is the degree of similarity between the two artists; this will translate into thicker "edges" in the similarity graph. # find the 0-indexed index lookup_number <- function(name) which(name==ARTIST_LIST)-1 strong_links <- comparisons %>% filter(similarity > 0.25) %>% rename(node1 = artist1, node2 = artist2, weight=similarity) strong_links$source <- sapply(strong_links$node1, lookup_number) strong_links$target <- sapply(strong_links$node2, lookup_number)  Finally, we can create the properly formatted JSON and send it to the file "artists.json" thusly... object <- list("nodes"=nodes, "links"=strong_links) sink("artists.json") toJSON(object, dataframe="rows", pretty=TRUE) sink()  Outcome Using "artists.json" and the "index.html" that can be found here, the similarity graph looks a little like this. (Make sure you scroll to see the whole thing.) For illustrative purposes, I pre-labeled the artists' "group" with labels that correspond to what I view as the artist's primary genre. This is why the nodes in the linked visualization have different colors. Note that, independently, the genres that I indicated tend to cluster together in the network. For example, Reggae (light green), Hip-Hop (green), and Punk (orange) all form almost completely connected graphs, though unconnected to each other (disjoint subgraphs). Indie rock (blue), post-punk (light blue) and classic rock (light orange) together form a rather tightly-connected subgraph. Curiously, the Sex Pistols (that I labeled "Punk") are not part of the Punk cluster but part of the Indie-rock/post-punk/classic-rock component. There are three orphan nodes (no edges), "Johann Sebastian Bach", "P:ano", and "No Kids". Bach is orphaned because he's the only Baroque artist in my top 70 artists :( --P:ano and No Kids are obscure... you’ve probably never heard of them. The recommendations, prima facie, appear to be on point. For example, without direct knowledge of association, "KRS-One" recommends "Boogie Down Productions" (the group that KRS-One comes from) most highly. Similarly, "The Smiths" and "Morrissey" recommend each other, and "De La Soul" and "A Tribe Called Quest" (part of a positive, Afrocentric hip-hop collective known as the Native Tongues together with Queen Latifah, et al.) recommend each other. Appropriately, Joy Division and New Order, whose Jaccard index of band members is 0.6 but whose music style is somewhat distinct, don't recommend each other. Lastly, subgenred artists appear to recommend other artists in the subgenre. For example, goth band "The Sisters of Mercy" appropriately recommends other goth-esque bands "Bauhaus", "And Also The Trees", and "Joy Division". Afterword Using this similarity measure to drive recommendations seems successful. It should be noted, though, that my ability to assess the effectiveness of using the Jaccard index as the sole arbiter of musical similarity is hampered; judging an algorithm on the basis that the system recommends other bands that I necessarily like is prejudicial, to say the least. This stands even if the system makes good theoretical sense. This still stands even if the system, quite independently, indicates that associated acts—that are objectively and incontrovertibly similar—are good recommendations. This raises a larger question on how to accurately measure the effectiveness of recommender systems; do you tell people what they want to hear, or do you pledge allegiance to a particular theoretical interpretation of similarity? If it's the latter, how do you iterate and improve the system? If it's the former, is your only criterion for success positive user-provided feedback? share this: Kickin' it with elastic net regression With the kind of data that I usually work with, overfitting regression models can be a huge problem if I'm not careful. Ridge regression is a really effective technique for thwarting overfitting. It does this by penalizing the L2 norm (euclidean distance) of the coefficient vector which results in "shrinking" the beta coefficients. The aggressiveness of the penalty is controlled by a parameter $\lambda$. Lasso regression is a related regularization method. Instead of using the L2 norm, though, it penalizes the L1 norm (manhattan distance) of the coefficient vector. Because it uses the L1 norm, some of the coefficients will shrink to zero while lambda increases. A similar effect would be achieved in Bayesian linear regression using a Laplacian prior (strongly peaked at zero) on each of the beta coefficients. Because some of the coefficients shrink to zero, the lasso doubles as a crackerjack feature selection technique in addition to a solid shrinkage method. This property gives it a leg up on ridge regression. On the other hand, the lasso will occasionally achieve poor results when there's a high degree of collinearity in the features and ridge regression will perform better. Further, the L1 norm is underdetermined when the number of predictors exceeds the number of observations while ridge regression can handle this. Elastic net regression is a hybrid approach that blends both penalization of the L2 and L1 norms. Specifically, elastic net regression minimizes the following... $\lVert y - X\beta \rVert + \lambda[(1-\alpha)\lvert \beta \rvert_2^2 + \alpha\lvert \beta \rvert_1]$ the $\alpha$ hyper-parameter is between 0 and 1 and controls how much L2 or L1 penalization is used (0 is ridge, 1 is lasso). The usual approach to optimizing the lambda hyper-parameter is through cross-validation—by minimizing the cross-validated mean squared prediction error—but in elastic net regression, the optimal lambda hyper-parameter also depends upon and is heavily dependent on the alpha hyper-parameter (hyper-hyper-parameter?). This blog post takes a cross-validated approach that uses grid search to find the optimal alpha hyper-parameter while also optimizing the lambda hyper-parameter for three different data sets. I also compare the performances against the stepwise regression and showcase some of the dangers of using stepwise feature selection. mtcars In this example, I try to predict “miles per gallon” from the other available attributes. The design matrix has 32 observations and 10 predictors and there is a high degree of collinearity (as measured by the variance inflation factors). The left panel above shows the leave-one-out cross validation (LOOCV) mean squared error of the model with the optimal lambda (as determined again by LOOCV) for each alpha parameter from 0 to 1. This panel indicates that if our objective is to purely minimize MSE (with no regard for model complexity) than pure ridge regression outperforms any blended elastic-net model. This is probably because of the substantial collinearity. Interestingly, the lasso outperforms blended elastic net models that weight the lasso heavily. The right panel puts things in perspective by plotting the LOOCV MSEs along with the MSE of the "kitchen sink" regression (the blue line) that includes all features in the model. As you can see, any degree of regularization offers a substantial improvement in model generalizability. It is also plotted with two estimates of the MSE for models that blindly use the coefficients from automated bi-directional stepwise regression. The first uses the features selected by performing the stepwise procedure on the whole dataset and then assesses the model performance (the red line). The second estimate uses the step procedure and resulting features on only the training set for each fold of the cross validations. This is the estimate without the subtle but treacherous "knowledge leaking" eloquently described in this plot post. This should be considered the more correct assessment of the model. As you can see, if we weren't careful about interpreting the stepwise regression, we would have gotten an incredibly inflated and inaccurate view of the model performance. Forest Fires The second example uses a very-difficult-to-model dataset from University of California, Irvine machine learning repository. The task is to predict the burnt area from a forest fire given 11 predictors. It has 517 observations. Further, there is a relatively low degree of collinearity between predictors. Again, highest performing model is the pure ridge regression. This time, the performance asymptotes as the alpha hyper-parameter increases. The variability in the MSE estimates is due to the fact that I didn't use LOOCV and used 400-k CV instead because I'm impatient. As with the last example, the properly measured stepwise regression performance isn't so great, and the kitchen sink model outperforms it. However, in contrast to the previous example, there was a lot less variability in the selected features across folds—this is probably because of the significantly larger number of observations. "QuickStartExample" This dataset is a contrived one that is included with the excellent glmnet package (the one I'm using for the elastic net regression). This dataset has a relatively low degree of collinearity, has 20 features and 100 observations. I have no idea how the package authors created this dataset. Finally, an example where the lasso outperforms ridge regression! I think this is because the dataset was specifically manufactured to have a small number of genuine predictors with large effects (as opposed to many weak predictors). Interestingly, stepwise progression far outperforms both—probably for the very same reason. From fold to fold, there was virtually no variation in the features that the stepwise method automatically chose. Conclusion So, there you have it. Elastic net regression is awesome because it can perform at worst as good as the lasso or ridge and—though it didn’t on these examples—can sometimes substantially outperform both. Also, be careful with step-wise feature selection! PS: If, for some reason, you are interested in the R code I used to run these simulations, you can find it on this GitHub Gist. share this: Lessons learned in high-performance R On this blog, I've had a long running investigation/demonstration of how to make a "embarrassingly-parallel" but computationally intractable (on commodity hardware, at least) R problem more performant by using parallel computation and Rcpp. The example problem is to find the mean distance between every airport in the United States. This silly example was chosen because it exhibits polynomial growth in running time as a function of the number of airports and, thus, quickly becomes intractable without sampling. It is also easy to parallelize. The first post used the (now-deprecated in favor of 'parallel') multicore package to achieve a substantial speedup. The second post used Rcpp to achieve a statistically significant but, functionally, trivial speedup by replacing the inner loop (the distance calculation using the Haversine formula) with a version written in C++ using Rcpp. Though I was disappointed in the results, it should be noted that porting the function to C++ took virtually no extra work. By necessity, I've learned a lot more about high-performance R since writing those two posts (part of this is by trying to make my own R package as performant as possible). In particular, I did the Rcpp version all wrong, and I'd like to rectify that in this post. I also compare the running times of approaches that use both parallelism and Rcpp. Lesson 1: use Rcpp correctly The biggest lesson I learned, is that it isn’t sufficient to just replace inner loops with C++ code; the repeated transferring of data from R to C++ comes with a lot of overhead. By actually coding the loop in C++, the speedups to be had are often astounding. In this example, the pure R version, that takes a matrix of longitude/latitude pairs and computed the mean distance between all combinations, looks like this... just.R <- function(dframe){ numrows <- nrow(dframe) combns <- combn(1:nrow(dframe), 2) numcombs <- ncol(combns) combns %>% {mapply(function(x,y){ haversine(dframe[x,1], dframe[x,2], dframe[y,1], dframe[y,2]) }, .[1,], .[2,])} %>% sum %>% (function(x) x/(numrows*(numrows-1)/2)) }  The naive usage of Rcpp (and the one I used in the second blog post on this topic) simply replaces the call to "haversine" with a call to "haversine_cpp", which is written in C++. Again, a small speedup was obtained, but it was functionally trivial. The better solution is to completely replace the combinations/"mapply" construct with a C++ version. Mine looks like this... double all_cpp(Rcpp::NumericMatrix& mat){ int nrow = mat.nrow(); int numcomps = nrow*(nrow-1)/2; double running_sum = 0; for( int i = 0; i < nrow; i++ ){ for( int j = i+1; j < nrow; j++){ running_sum += haversine_cpp(mat(i,0), mat(i,1), mat(j,0), mat(j,1)); } } return running_sum / numcomps; }  The difference is incredible… res <- benchmark(R.calling.cpp.naive(air.locs[,-1]), just.R(air.locs[,-1]), all_cpp(as.matrix(air.locs[,-1])), columns = c("test", "replications", "elapsed", "relative"), order="relative", replications=10) res # test replications elapsed relative # 3 all_cpp(as.matrix(air.locs[, -1])) 10 0.021 1.000 # 1 R.calling.cpp.naive(air.locs[, -1]) 10 14.419 686.619 # 2 just.R(air.locs[, -1]) 10 15.068 717.524  The properly written solution in Rcpp is 718 times faster than the native R version and 687 times faster than the naive Rcpp solution (using 200 airports). Lesson 2: Use mclapply/mcmapply In the first blog post, I used a messy solution that explicitly called two parallel processes. I’ve learned that using mclapply/mcmapply is a lot cleaner and easier to intregrate into idiomatic/functional R routines. In order to parallelize the native R version above, all I had to do is replace the call to "mapply" to "mcmapply" and set the number of cores (now I have a 4-core machine!). Here are the benchmarks:  test replications elapsed relative 2 R.calling.cpp.naive.parallel(air.locs[, -1]) 10 10.433 1.000 4 just.R.parallel(air.locs[, -1]) 10 11.809 1.132 1 R.calling.cpp.naive(air.locs[, -1]) 10 15.855 1.520 3 just.R(air.locs[, -1]) 10 17.221 1.651  Lesson 3: Smelly combinations of Rcpp and parallelism are sometimes counterproductive Because of the nature of the problem and the way I chose to solve it, the solution that uses Rcpp correctly is not easily parallelize-able. I wrote some *extremely* smelly code that used explicit parallelism to use the proper Rcpp solution in a parallel fashion; the results were interesting:  test replications elapsed relative 5 all_cpp(as.matrix(air.locs[, -1])) 10 0.023 1.000 4 just.R.parallel(air.locs[, -1]) 10 11.515 500.652 6 all.cpp.parallel(air.locs[, -1]) 10 14.027 609.870 2 R.calling.cpp.naive.parallel(air.locs[, -1]) 10 17.580 764.348 1 R.calling.cpp.naive(air.locs[, -1]) 10 21.215 922.391 3 just.R(air.locs[, -1]) 10 32.907 1430.739  The parallelized proper Rcpp solution (all.cpp.parallel) was outperformed by the parallelized native R version. Further the parallelized native R version was much easier to write and was idiomatic R. How does it scale? Two quick things... • The "all_cpp" solution doesn't appear to exhibit polynomial growth; it does, it's just so much faster than the rest that it looks completely flat • It's hard to tell, but that's "just.R.parallel" that is tied with "R.calling.cpp.naive.parallel" Too long, didn’t read: If you know C++, try using Rcpp (but correctly). If you don't, try multicore versions of lapply and mapply, if applicable, for great good. If it’s fast enough, leave well enough alone. PS: I way overstated how "intractable" this problem is. According to my curve fitting, the vanilla R solution would take somewhere between 2.5 and 3.5 hours. The fastest version of these methods, the non-parallelized proper Rcpp one, took 9 seconds to run. In case you were wondering, the answer is 1,869.7 km (1,161 miles). The geometric mean might have been more meaningful in this case, though. share this: I'm all about that bootstrap ('bout that bootstrap) As some of my regular readers may know, I'm in the middle of writing a book on introductory data analysis with R. I'm at the point in the writing of the book now where I have to make some hard choices about how I'm going to broach to topic of statistical inference and hypothesis testing. Given the current climate against NHST (the journal Basic and Applied Social Psychology banned it) and my own personal preferences, I wasn't sure just how much to focus on classical hypothesis testing. I didn't want to burden my readers with spending weeks trying to learn the intricacies of NHST just to have them being told to forget everything they know about it and not be able to use it without people making fun of them. So I posed a question to twitter: "Is it too outlandish to not include the topic of parametric HTs in an intro book about data analysis. Asking for a friend.. named Tony…. You know, in favor of bootstrapped CIs, permutation tests, etc…" To which my friend Zach Jones (@JonesZM) replied: "they could at least be better integrated with monte-carlo methods. i think they'd make it easier to understand". I agreed, which is why I'm proceeding with my original plan to introduce classical tests after and within the context of Monte Carlo bootstrapping (as opposed to exhaustive bootstrapping). Even though I'm a huge fan of the bootstrap, I want to be careful not to further any misconceptions about it—chiefly, that bootstrapping is a cure-all for having a small sample size. To be able to show how this isn’t the case, I wrote an R script to take 1,000 samples from a population, calculate 95% confidence intervals using various methods and record the proportion of times the population mean was within the CIs. The four ways I created the CIs were: • the z interval method: which assumes that the sampling distribution is normal around the sample mean (1.96 * the standard error) • the t interval method: which assumes that the population is normally distributed and the sampling distribution is normally distributed around the sample mean (t-distribution quantile at .975 [with appropriate degrees of freedom] * standard error) • basic bootstrap CI estimation (with boot() and boot.CI() from the boot R package) • adjusted percentile CI estimation (with boot() and boot.CI() from the boot R package) I did this for various sample sizes and two different distributions, the normal and the very non-normal beta distribution (alpha=0.5, beta=0.5). Below is a plot depicting all of this information. So, clearly the normal (basic) boot doesn’t make up for small sample sizes. It's no surprise the the t interval method blows everything else out of the water when sampling from a normal distribution. It even performs reasonably well with the beta distribution, although the adjusted bootstrap wins out for most sample sizes. In addition to recording the proportion of the times the population mean was within the confidence intervals, I also kept track of the range of these intervals. All things being equal, narrower intervals are far preferable to wide ones. Check out this plot depicting the mean ranges of the estimated CIs: The t interval method always produces huge ranges. The adjusted bootstrap produces ranges that are more or less on par with the other three methods BUT it outperforms the t interval method for non-normal populations. This suggests the the adjustments to the percentiles of the bootstrap distribution do a really good job at correcting for bias. It also shows that, if we are dealing with a non-normal population (common!), we should use adjusted percentile bootstrapped CIs. Some final thoughts: • The bootstrap is not a panacea for small sample sizes • The bootstrap is cool because it doesn’t assume anything about the population distribution, unlike the z and t interval methods • Basic bootstrap intervals are whack. They’re pathologically narrow for small sample sizes. • Adjusted percentile intervals are great! You should always use them instead. Thanks Bradley Efron! Also, if you're not using Windows, you can parallelize your bootstrap calculations really easily in R; below is the way I bootstrapped the mean for this project: library(boot) dasboot <- boot(a.sample, function(x, i){mean(x[i])}, 10000, parallel="multicore", ncpus=4)  which uses 4 cores to perform the bootstrap in almost one fourth the time. In later post, I plan to further demonstrate the value of the bootstrap by testing difference in means and show why permutation tests comparing means between two samples is always better than t-testing. share this: Playing around with #rstats twitter data As a bit of weekend fun, I decided to briefly look into the #rstats twitter data that Stephen Turner collected and made available (thanks!). Essentially, this data set contains some basic information about over 100,000 tweets that contain the hashtag "#rstats" that denotes that a tweeter is tweeting about R. As a warning, I don't know much about how these data were collected, whether it was collected and random times during the day or whether it was biased toward particular times and, therefore, locations. I wouldn't really read too much into this. Most common co-occuring hashtags When a tweet uses a hashtag at all, it very often uses more than one. To extract the co-occuring hashtags, I used the following perl script: #!/usr/bin/perl while(<>){ chomp;$_ = lc($_);$_ =~ s/#rstats//g;
my @matches;
push @matches, /(#\w+)/;
print join "\n" => @matches if @matches;
}

which uses the regular expression "(#\w+)" to search for hashtags after removing "#rstats" from every tweet.

On the unix command-line, I put these other hashtags into a file and sorted via these commands:

cat data/R-hashtag-data.txt | ./PERL_SCRIPT_ABOVE.pl | tee other-hashtags.txt

sort other-hashtags.txt | uniq -c | sort -n -r > sorted-other-hashtags.txt

After running these commands, I get a numbered list of co-occuring hashtags, sorted in descending order. The top 10 co-occuring hashtags were as follows (you can see the rest here :

5258 #datascience
1665 #python
1625 #bigdata
1542 #r
1451 #dataviz
1360 #ggplot2
852 #statistics
783 #dplyr
749 #machinelearning
743 #analytics

Neat-o. The presence of "#python" and "#ggplot2" in the top 10 made me wonder what the top 10 programming language and R package related hashtags were. Here they are, respectively:

1665 #python
423 #d3js (plus 72 for #d3) (plus 2 for #js)
343 #sas
312 #julialang (plus 43 for #julia)
240 #fsharp
140 #spss  (plus 7 for #ibmspss)
102 #stata
75 #matlab
55 #sql
38 #java

1360 #ggplot2  (plus 298 for ggplot)  (plus for 6 #gglot2) (plus 4 for #ggpot)
783 #dplyr
663 #shiny
557 #rcpp (plus 22 for rcpp11)
251 #knitr
156 #magrittr
105 #lme4
93 #ggvis   (plus 11 for #ggivs)
65 #datatable
46 #rneo4j

You can view the full list here and here.

I was happy to see my favorite languages (python, perl, clojure, lisp, haskell, c) besides R being represented in the first list. Additionally, most of my favorite packages were fairly well tweeted about--at least as far as hashtags-applied-to-a-package go.

#strangehashtags
Before moving on to the next section, I wanted to share my favorite co-occuring hashtags that I found while sifting through the data: #rcatladies, #rdogfella, #bayesianbootycall, #dontbeaplyrhater, #overlyhonestmethods, #rickshaw (??), #statafail, and #monkeysinfrontoftypewriters.

Most prolific #rstats tweeters
One of the first things I did with these data is a simple aggregation and sort to find the tweeters that used the hashtag most often:

library(dplyr)
THE_DATA %>%
group_by(User) %>%
summarise(count = n()) %>%
arrange(desc(count)) -> prolific.rstats.tweeters

Here is the top 10 (you can see the rest here.)

@Rbloggers	1081
@timelyportfolio	427
@recology_	419
@revodavid	210
@chlalanne	209
@RLangTip	175
@jmgomez	160

Nothing terribly surprising here.

Normalizing by total tweets
In a twitter discussion about these data, a twitter friend Tim Hopper posited that though he had fewer #rstats tweets than another mutual friend, Trey Causey, he would have a higher number of #rstats tweets if you control for total tweet volume. I wondered how this sorting would look.

Answering this question gave me an excuse to use Hadley Wickham's new package, rvest (I literally just got why the package is named as much while typing this out) which makes web scraping easier--in part by leveraging the expressive power of the magrittr package.

To get the total number of tweets for a particular tweeter, I wrote the following function:

library(rvest)
library(magrittr)
get.num.tweets <- function(handle){
tryCatch({
unraw <- function(raw_str){
raw_str <- sub(",", "", raw_str)    # remove commas if any
if(grepl("K", raw_str)){
return(as.numeric(sub("K", "", raw_str))*1000)   # in thousands
}
return(as.numeric(raw_str))
}
html_nodes(".is-active .ProfileNav-value") %>%
html_text() %>%
unraw
},
error=function(cond){return(NA)})
}

The real logic (and beauty) of which is contained only in the last few lines:

    html(paste0("http://twitter.com/", sub("@", "", TWITTER_HANDLE))) %>%
html_nodes(".is-active .ProfileNav-value") %>%
html_text()

The CSS element that houses the number of total tweets from a useR's twitter page was found easily using SelectorGadget.

After scraping the number of tweets for almost 10,000 #rstats tweeters (waiting a few seconds between each request because I'm considerate) I divided number of #rstats tweets by the total number of tweets to come up with a normalized value.

The top 10 tweeteRs were as follows:

              User count num.of.tweets     ratio
1     @medzihorsky     9            28 0.3214286
2        @statworx     5            16 0.3125000
4  @RforExcelUsers     4            15 0.2666667
5     @showmeshiny    27           102 0.2647059
6           @tcrug     6            25 0.2400000
7   @DailyRpackage   155           666 0.2327327
8   @R_Programming    49           250 0.1960000
10     @Deep_RHelp    11            58 0.1896552

In case you were wondering, Trey Causey still "won" by a long shot:

> tweeters[which(tweeters$User=="@tdhopper"),] Source: local data frame [1 x 4] User count num.of.tweets ratio 1 @tdhopper 8 26700 0.0002996255 > tweeters[which(tweeters$User=="@treycausey"),]
Source: local data frame [1 x 4]

User count num.of.tweets      ratio
1 @treycausey    50         28700 0.00174216

Before ending this post, I feel compelled to issue an almost certainly unnecessary but customary warning against using number of #rstats tweets as a proxy for who likes R the most or who are the biggest R "thought leaders" (whatever that is). Most tweets about R don't use the #rstats hashtag, anyway.

Again, I would't read too much into this :)

Assertive R programming in dplyr/magrittr pipelines

A lot of my job–and side projects, for that matter–involve running R scripts on updates of open government data. While I’m infinitely grateful to have access to any interesting open datasets in the first place, I can’t ignore that dealing with open data is often a messy affair. In fact this seems to be characteristic of most data sets I work with, open access or otherwise.

So... let's say I have a labyrinthine analysis workflow that uses a wide array of government sources to answer an interesting question. The workflow is full of analyses that return components that are components of still other analyses.

Then there’s an update of the data! Whoopee! I rerun the scripts/workflow on updated (or partially updated) data. Then one of four things happen:

• In the best case scenario, everything works because there were no errors in the data.
• In the likely scenario, something very late in this labyrinthine analysis workflow breaks and it’s not clear what datum caused this error.
• In the worst case scenario, nothing breaks and the error is only caught when the results–or part of them–are nonsensical.
• In the worst worst case scenario, the results or some of the results are wrong but it looks ok and it goes undetected.

In an effort to help solve this common problem–and inspired by the elegance of dplyr/magrittr pipelines–I created a R package called assertr.

assertr works by adding two new verbs to the pipeline, verify and assert, and a couple of predicate functions. Early on in the pipeline, you make certain assertions about how the data should look. If the data conform to these assertions, then we go on with the pipeline. If not, the verbs produce errors that terminate any further pipeline computations. The benefit of the verbs, over the truth assurance functions already in R (like stopifnot) is that they needn’t interrupt the flow of the pipeline.

Take, for example, the following contrived snippet making sure that there are only 0s and 1s (automatic and manual, respectively) in R’s Motor Trend Car Road Test built-in dataset before calculating the average miles per gallon per category.

mtcars %>%
verify(am %in% c(0,1)) %>%
group_by(cyl) %>%
summarise(mean.mpg=mean(mpg))

#   am     mean.mpg
#   0      17.14737
#   1      24.39231

Let’s say this dataset was much bigger, not built in to R, and curated and disseminated by someone with less perfectionistic (read obsessive/compulsive) tendencies than yours truly. If we wanted to find the average miles per gallon aggregated by number of engine cylinders, we might first want to check if the number of cylinders is reasonable (either 4, 6, or 8) and that the miles per gallon was a reasonable number (between 10 and 40 mpg) and not a data entry error that would greatly throw off our non-robust estimator:

mtcars %>%
assert(in_set(4, 6, 8), cyl) %>%
assert(within_bounds(10, 40), mpg) %>%
group_by(cyl) %>%
summarise(mean.mpg=mean(mpg))

#  cyl   mean.mpg
#   4     26.66364
#   6     19.74286
#   8     15.10000

Perhaps one day there will be cars that have more than 8 cylinders or less than 2. We might want to only check if there are an even number of cylinders (since it has to be even, I think); we can change the first assert line to:

assert(function(x) x%%2==0, cyl) %>%

assertr subscribes to the general idea that it is better to fail fast to spot data errors early. The benefit of assertr’s particular approach is that it’s friendly to the pipeline paradigm used by magrittr and dplyr.

The best thing about assertr’s approach, though, is that it forces you to state your assumptions up front. When your assumptions are stated clearly and verified, errors from messy data tend to disappear.

To learn more about assertr and the kinds of assertions that you can make with it, visit its page on github.

You can also read the vignette here.

Why is my OS X Yosemite install taking so long?: an analysis

Why?
Since the latest Mac OS X update, 10.10 "Yosemite", was released last Thursday, there have been complaints springing up online of the progress bar woefully underestimating the actual time to complete installation. More specifically, it appeared as if, for a certain group of people (myself included), the installer would stall out at "two minutes remaining" or "less than a minute remaining"–sometimes for hours.

In the vast majority of these cases, though, the installation process didn't hang, it was just performing a bunch of unexpected tasks that it couldn't predict.

During the install, striking "Command" + "L" would bring up the install logs. In my case, the logs indicated that the installer was busy right until the very last minute.

Not knowing very much about OS X's installation process and wanting to learn more, and wanting to answer why the installation was taking longer than the progress bar expected, I saved the log to a file on my disk with the intention of analyzing it before the installer automatically restarted my computer.

Cleaning
The log file from the Yosemite installer wasn't in a format that R (or any program) could handle natively so before we can use it, we have to clean/munge it. To do this, we'll write a program in the queen of all text-processing languages: perl.

This script will read the log file, line-by-line from standard input (for easy shell piping), and spit out nicely formatted tab-delimited lines.

#!/usr/bin/perl

use strict;
use warnings;

while(<>){
chomp;
my $line =$_;
my ($not_message,$message) = split ': ', $line, 2; # skip lines with blank messages next if$message =~ m/^\s*$/; my ($month, $day,$time, $machine,$service) = split " ", $not_message; print join("\t",$month, $day,$time, $machine,$service, $message) . "\n"; }  We can output the cleaned log file with this shell command: echo "Month\tDay\tTime\tMachine\tService\tMessage" > cleaned.log grep '^Oct' ./YosemiteInstall.log | grep -v ']: ' | grep -v ': }' | ./clean-log.pl >> cleaned.log  This cleaned log contains 6 fields: 'Month', 'Day', 'Time', 'Machine (host)', 'Service', and 'Message'. The installation didn't span days (it didn't even span an hour) so technically I didn't need the 'Month' and 'Day' fields, but I left them in for completeness' sake. Analysis Let's set some options and load the libraries we are going to use: # options options(echo=TRUE) options(stringsAsFactors=FALSE) # libraries library(dplyr) library(ggplot2) library(lubridate) library(reshape2)  Now we read the log file that I cleaned and add a few columns with correctly parsed timestamps using lubridate’s "parse_date_time()" function yos.log <- read.delim("./cleaned.log", sep="\t") %>% mutate(nice.date=paste(Month, Day, "2014", Time)) %>% mutate(lub.time=parse_date_time(nice.date, "%b %d! %Y! %H!:%M!:%S!", tz="EST"))  And remove the rows of dates that didn't parse correctly yos.log <- yos.log[!is.na(yos.log$lub.time),]

##   Month Day     Time   Machine        Service
## 1   Oct  18 11:28:23 localhost opendirectoryd
## 2   Oct  18 11:28:23 localhost opendirectoryd
## 3   Oct  18 11:28:23 localhost opendirectoryd
## 4   Oct  18 11:28:23 localhost opendirectoryd
## 5   Oct  18 11:28:23 localhost opendirectoryd
## 6   Oct  18 11:28:23 localhost opendirectoryd
##                                                                    Message
## 1                   opendirectoryd (build 382.0) launched - installer mode
## 2                                  Logging level limit changed to 'notice'
## 3                                               Initialize trigger support
## 4 created endpoint for mach service 'com.apple.private.opendirectoryd.rpc'
## 5                                set default handler for RPC 'reset_cache'
## 6                           set default handler for RPC 'reset_statistics'
##              nice.date            lub.time
## 1 Oct 18 2014 11:28:23 2014-10-18 11:28:23
## 2 Oct 18 2014 11:28:23 2014-10-18 11:28:23
## 3 Oct 18 2014 11:28:23 2014-10-18 11:28:23
## 4 Oct 18 2014 11:28:23 2014-10-18 11:28:23
## 5 Oct 18 2014 11:28:23 2014-10-18 11:28:23
## 6 Oct 18 2014 11:28:23 2014-10-18 11:28:23

The first question I had was how long the installation process took

install.time <- yos.log[nrow(yos.log), "lub.time"] - yos.log[1, "lub.time"]
(as.duration(install.time))
## [1] "1848s (~30.8 minutes)"

Let's make a column for cumulative time by subtracting each row's time by the start time

yos.log$cumulative <- yos.log$lub.time - min(yos.log$lub.time, na.rm=TRUE)  In order to see what processes were taking the longest, we have to make a column for elapsed time. To do this, we can subtract each row's time from the time of the subsequent row. yos.log$elapsed <- lead(yos.log$lub.time) - yos.log$lub.time

# remove last row
yos.log <- yos.log[-nrow(yos.log),]

Which services were responsible for the most writes to the log and what services took the longest? We can find out with the following elegant dplyr construct. While we're at it, we should add columns for percentange of the whole for easy plotting.

counts <- yos.log %>%
group_by(Service) %>%
summarise(n=n(), totalTime=sum(elapsed)) %>%
arrange(desc(n)) %>%
top_n(8, n) %>%
mutate(percent.n = n/sum(n)) %>%
mutate(percent.totalTime = as.numeric(totalTime)/sum(as.numeric(totalTime)))
(counts)

## Source: local data frame [8 x 5]
##
##           Service     n totalTime percent.n percent.totalTime
## 1     OSInstaller 42400 1586 secs 0.9197197          0.867615
## 2  opendirectoryd  3263   43 secs 0.0707794          0.023523
## 3         Unknown   236  157 secs 0.0051192          0.085886
## 4  _mdnsresponder    52   17 secs 0.0011280          0.009300
## 5              OS    49    1 secs 0.0010629          0.000547
## 6 diskmanagementd    47    7 secs 0.0010195          0.003829
## 7     storagekitd    29    2 secs 0.0006291          0.001094
## 8         configd    25   15 secs 0.0005423          0.008206

Ok, the "OSInstaller" is responsible for the vast majority of the writes to the log and to the total time of the installation. "opendirectoryd" was the next most verbose process, but its processes were relatively quick compared to the "Unknown" process' as evidenced by "Unknown" taking almost 4 times longer, in aggregate, in spite of having only 7% of "opendirectoryd"'s log entries.

We can more intuitively view the number-of-entries/time-taken mismatch thusly:

melted <- melt(as.data.frame(counts[,c("Service",
"percent.n",
"percent.totalTime")]))

ggplot(melted, aes(x=Service, y=as.numeric(value), fill=factor(variable))) +
geom_bar(width=.8, stat="identity", position = "dodge",) +
ggtitle("Breakdown of services during installation by writes to log") +
ylab("percent") + xlab("service") +
scale_fill_discrete(name="Percent of",
breaks=c("percent.n", "percent.totalTime"),
labels=c("writes to logfile", "time elapsed"))

As you can see, the "Unknown" process took a disproportionately long time for its relatively few log entries; the opposite behavior is observed with "opendirectoryd". The other processes contribute very little to both the number of log entries and the total time in the installation process.

What were the 5 most lengthy processes?

yos.log %>%
arrange(desc(elapsed)) %>%
select(Service, Message, elapsed) %>%

##       Service
## 1 OSInstaller
## 2 OSInstaller
## 3     Unknown
## 4 OSInstaller
## 5 OSInstaller
##                                                                                Message
## 1 PackageKit: Extracting file:///System/Installation/Packages/Essentials.pkg (destination=/Volumes/Macintosh HD/.OSInstallSandboxPath/Root, uid=0)
## 2                                    System Reaper: Archiving previous system logs to /Volumes/Macintosh HD/private/var/db/PreviousSystemLogs.cpgz
## 3                       kext file:///Volumes/Macintosh%20HD/System/Library/Extensions/JMicronATA.kext/ is in hash exception list, allowing to load
## 4                                                                   Folder Manager is being asked to create a folder (down) while running as uid 0
## 5                                                                                Checking catalog hierarchy.
##    elapsed
## 1 169 secs
## 2 149 secs
## 3  70 secs
## 4  46 secs
## 5  44 secs

The top processes were:

• Unpacking and moving the contents of "Essentials.pkg" into what is to become the newsystem directory structure. This ostensibly contains items like all the updated applications (Safari, Mail, etc..). (almost three minutes)
• Archiving the old system logs (two and a half minutes)
• Loading the kernel module that allows the onboard serial ATA controller to work (a little over a minute)

Let's view a density plot of the number of writes to the log file during installation.

ggplot(yos.log, aes(x=lub.time)) +
ggtitle("Density plot of number of writes to log file during installation") +
xlab("time") + ylab("")

This graph is very illuminating; the vast majority of log file writes were the result of very quick processes that took place in the last 15 minutes of the install, which is when the progress bar read that only two minutes were remaining.

In particular, there were a very large number of log file writes between 11:47 and 11:48; what was going on here?

# if the first time is in between the second two, this returns TRUE
is.in <- function(time, start, end){
if(time > start && time < end)
return(TRUE)
return(FALSE)
}

the.start <- ymd_hms("14-10-18 11:47:00", tz="EST")
the.end <- ymd_hms("14-10-18 11:48:00", tz="EST")

# logical vector containing indices of writes in time interval
is.in.interval <- sapply(yos.loglub.time, is.in, the.start, the.end) # extract only these rows in.interval <- yos.log[is.in.interval, ] # what do they look like? silence <- in.interval %>% select(Message) %>% sample_n(7) %>% apply(1, function (x){cat("\n");cat(x);cat("\n")})  ## ## (NodeOp) Move /Volumes/Macintosh HD/Recovered Items/usr/local/texlive/2013/tlpkg/tlpobj/featpost.tlpobj -> /Volumes/Macintosh HD/usr/local/texlive/2013/tlpkg/tlpobj Final name: featpost.tlpobj (Flags used: kFSFileOperationDefaultOptions,kFSFileOperationSkipSourcePermissionErrors,kFSFileOperationCopyExactPermissions,kFSFileOperationSkipPreflight,k_FSFileOperationSuppressConversionCopy) ## ## (NodeOp) Move /Volumes/Macintosh HD/Recovered Items/usr/local/texlive/2013/texmf-dist/tex/generic/pst-eucl/pst-eucl.tex -> /Volumes/Macintosh HD/usr/local/texlive/2013/texmf-dist/tex/generic/pst-eucl Final name: pst-eucl.tex (Flags used: kFSFileOperationDefaultOptions,kFSFileOperationSkipSourcePermissionErrors,kFSFileOperationCopyExactPermissions,kFSFileOperationSkipPreflight,k_FSFileOperationSuppressConversionCopy) ## ## (NodeOp) Move /Volumes/Macintosh HD/Recovered Items/Library/Python/2.7/site-packages/pandas-0.12.0_943_gaef5061-py2.7-macosx-10.9-intel.egg/pandas/tests/test_groupby.py -> /Volumes/Macintosh HD/Library/Python/2.7/site-packages/pandas-0.12.0_943_gaef5061-py2.7-macosx-10.9-intel.egg/pandas/tests Final name: test_groupby.py (Flags used: kFSFileOperationDefaultOptions,kFSFileOperationSkipSourcePermissionErrors,kFSFileOperationCopyExactPermissions,kFSFileOperationSkipPreflight,k_FSFileOperationSuppressConversionCopy) ## ## (NodeOp) Move /Volumes/Macintosh HD/Recovered Items/usr/local/texlive/2013/texmf-dist/tex/latex/ucthesis/uct10.clo -> /Volumes/Macintosh HD/usr/local/texlive/2013/texmf-dist/tex/latex/ucthesis Final name: uct10.clo (Flags used: kFSFileOperationDefaultOptions,kFSFileOperationSkipSourcePermissionErrors,kFSFileOperationCopyExactPermissions,kFSFileOperationSkipPreflight,k_FSFileOperationSuppressConversionCopy) ## ## (NodeOp) Move /Volumes/Macintosh HD/Recovered Items/usr/local/texlive/2013/texmf-dist/doc/latex/przechlewski-book/wkmgr1.tex -> /Volumes/Macintosh HD/usr/local/texlive/2013/texmf-dist/doc/latex/przechlewski-book Final name: wkmgr1.tex (Flags used: kFSFileOperationDefaultOptions,kFSFileOperationSkipSourcePermissionErrors,kFSFileOperationCopyExactPermissions,kFSFileOperationSkipPreflight,k_FSFileOperationSuppressConversionCopy) ## ## WARNING : ensureParentPathExists: Created /Volumes/Macintosh HD/usr/local/texlive/2013/texmf-dist/doc/latex/moderntimeline' w/ { ## ## (NodeOp) Move /Volumes/Macintosh HD/Recovered Items/usr/local/texlive/2013/texmf-dist/fonts/type1/wadalab/mrj/mrjkx.pfb -> /Volumes/Macintosh HD/usr/local/texlive/2013/texmf-dist/fonts/type1/wadalab/mrj Final name: mrjkx.pfb (Flags used: kFSFileOperationDefaultOptions,kFSFileOperationSkipSourcePermissionErrors,kFSFileOperationCopyExactPermissions,kFSFileOperationSkipPreflight,k_FSFileOperationSuppressConversionCopy)  Ah, so these processes are the result of the installer having to move files back into the new installation directory structure. In particular, the vast majority of these move operations are moving files related to a program called "texlive". I'll explain why this is to blame for the inaccurate projected time to completion in the next section. But lastly, let's view a faceted density plot of the number of log files writes by process. This might give us a sense of what steps go on as the installation progresses by showing us with processes are most active. # reduce number of service to a select few of the most active smaller <- yos.log %>% filter(Service %in% c("OSInstaller", "opendirectoryd", "Unknown", "OS")) ggplot(smaller, aes(x=lub.time, color=Service)) + geom_density(aes( y = ..scaled..)) + ggtitle("Faceted density of log file writes by process (scaled)") + xlab("time") + ylab("")  This shows that no one process runs consistently throughout the entire installation process, but rather that the process run in spurts. the answer The vast majority of Mac users don't place strange files in certain special system-critical locations like '/usr/local/' and '/Library/'. Among those who do, though, these directories are littered with hundreds and hundreds of custom files that the installer doesn't and can't have prior knowledge of. In my case, and probably many others, the estimated time-to-completion was inaccurate because the installer couldn't anticipate needing to copy back so many files to certain special directories after unpacking the contents of the new OS. Additionally, for each of these copied files, the installer had to make sure the subdirectories had the exact same meta-data (permissions, owner, reference count, creation date, etc…) as before the installation began. This entire process added many minutes to the procedure at a point when the installer thought it was pretty much done. What were some of the files that the installer needed to copy back? The answer will be different for each system but, as mentioned above, anything placed '/usr/local' and '/Library' directories that wasn't Apple-supplied needed to be moved and moved back. /usr/local/ /usr/local/ is used chiefly for user-installed software that isn't part of the OS distribution. In my case, my /usr/local contained a custom compliled Vim; ClamXAV, a lightweight virus scanner that I use only for the benefit of my Windows-using friends; and texlive, software for the TeX typesetting system. texlive was, by far, the biggest time-sink since it had over 123,491 files. In addition to these programs, many users might find that the Homebrew package manager is to blame for their long installation process, since this software also uses the /usr/local prefix (although it probably should not). /Library/ Among other things, this directory holds (subdirectories that hold) modules and packages that the Apple-supplied Python, Ruby, and Perl uses. If you use these Apple-supplied versions of these languages and you install your own packages/modules using super-user privileges, the new packages will go into this directory and it will appear foreign to the Yosemite installer. To get around this issue, either install packages/modules in a local (non-system) library, or use alternate versions of these programming languages that you either download and install yourself, or use MacPorts to install. --- You can find all the code and logs that I used for this analysis in this git repository This post is also available as a RMarkdown report here share this: Fun with .Rprofile and customizing R startup Over the years, I've meticulously compiled–and version controlled–massive and extensive configuration files for virtually all of my most used utilities, most notably vim, tmux, and zsh. In fact, one of the only configurable utilities for which I had no special configuration schema was R. This is extremely surprising, given that I use R everyday. One reason I think that this was the case is because I came to R from using general-purpose programming languages for which there is no provision to configure the language in a way that would actually change results or program output. I only vaguely knew that .Rprofile was a configuration file that some people used, and that others warned against using, but it never occurred to me to actually use it for myself. Because I never used it, I developed odd habits and rituals in my interactive R programming including adding "stringsAsFactors=FALSE" to all of my "read.csv" function calls and making frequent calls to the "option()" function. Since I actually began to use and expand my R configuration, though, I've realized how much I've been missing. I pre-set all my preferred options (saving time) and I've even made provisions for some cool tricks and hacks. That being said, there's a certain danger in using a custom R profile but we'll talk about how to thwart that later. The R Startup Process In the absence of any command-line flags being used, when R starts up, it will "source" (run) the site-wide R startup configuration file/script if it exists. In a fresh install of R, this will rarely exist, but if it does, it will usually be in '/Library/Frameworks/R.framework/Resources/etc/' on OS X, 'C:\Program Files\R\R-***\etc\' on Windows, or '/etc/R/' on Debian. Next, it will check for a .Rprofile hidden file in the current working directory (the directory where R is started on the command-line) to source. Failing that, it will check your home directory for the .Rprofile hidden file. You can check if you have a site-wide R configuration script by running R.home(component = "home")  in the R console and then checking for the presence of Rprofile.site in that directory. The presence of the user-defined R configuration can be checked for in the directory of whichever path path.expand("~")  indicates. More information on the R startup process can be found here and here. The site-wide R configuration script For most R installations on primarily single-user systems, using the site-wide R configuration script should be given up in favor of the user-specific configuration. That being said, a look at the boilerplate site-wide R profile that Debian furnishes (but comments out by default) provides some good insight into what might be a good idea to include in this file, if you choose to use it. ## Emacs please make this -*- R -*- ## empty Rprofile.site for R on Debian ## ## Copyright (C) 2008 Dirk Eddelbuettel and GPL'ed ## ## see help(Startup) for documentation on ~/.Rprofile and Rprofile.site # ## Example of .Rprofile # options(width=65, digits=5) # options(show.signif.stars=FALSE) # setHook(packageEvent("grDevices", "onLoad"), # function(...) grDevices::ps.options(horizontal=FALSE)) # set.seed(1234) # .First <- function() cat("\n Welcome to R!\n\n") # .Last <- function() cat("\n Goodbye!\n\n") # ## Example of Rprofile.site # local({ # # add MASS to the default packages, set a CRAN mirror # old <- getOption("defaultPackages"); r <- getOption("repos") # r["CRAN"] <- "http://my.local.cran" # options(defaultPackages = c(old, "MASS"), repos = r) #})  Two things you might want to do in a site-wide R configuration file is add other packages to the default packages list and set a preferred CRAN mirror. Other things that the above snippet indicates you can do is set various width and number display options, setting a random-number seed (making random number generation deterministic for reproducible analysis), and hiding the stars that R shows for different significance levels (ostensibly because of their connection to the much-maligned NHST paradigm). The user-specific .Rprofile In contrast to the site-wide config (that will be used for all users on the system), the user-specific R configuration file is a place to put more personal preferences, shortcuts, aliases, and hacks. Immediately below is my .Rprofile. local({r <- getOption("repos") r["CRAN"] <- "http://cran.revolutionanalytics.com" options(repos=r)}) options(stringsAsFactors=FALSE) options(max.print=100) options(scipen=10) options(editor="vim") # options(show.signif.stars=FALSE) options(menu.graphics=FALSE) options(prompt="> ") options(continue="... ") options(width = 80) q <- function (save="no", ...) { quit(save=save, ...) } utils::rc.settings(ipck=TRUE) .First <- function(){ if(interactive()){ library(utils) timestamp(,prefix=paste("##------ [",getwd(),"] ",sep="")) } } .Last <- function(){ if(interactive()){ hist_file <- Sys.getenv("R_HISTFILE") if(hist_file=="") hist_file <- "~/.RHistory" savehistory(hist_file) } } if(Sys.getenv("TERM") == "xterm-256color") library("colorout") sshhh <- function(a.package){ suppressWarnings(suppressPackageStartupMessages( library(a.package, character.only=TRUE))) } auto.loads <-c("dplyr", "ggplot2") if(interactive()){ invisible(sapply(auto.loads, sshhh)) } .env <- new.env() .envunrowname <- function(x) {
rownames(x) <- NULL
x
}

.env\$unfactor <- function(df){
id <- sapply(df, is.factor)
df[id] <- lapply(df[id], as.character)
df
}

attach(.env)

[Lines 1-3]: First, because I don't have a site-wide R configuration script, I set my local CRAN mirror here. My particular choice of mirror is largely arbitrary.

[Line 5]: If stringsAsFactors hasn't bitten you yet, it will.

[Line 9]: Setting 'scipen=10' effectively forces R to never use scientific notation to express very small or large numbers.

[Line 13]: I included the snippet to turn off significance stars because it is a popular choice, but I have it commented out because ever since 1st grade I've used number of stars as a proxy for my worth as a person.

[Line 15]: I don't have time for Tk to load; I'd prefer to use the console, if possible.

[Lines 17-18]: Often, when working in the interactive console I forget a closing brace or paren. When I start a new line, R changes the prompt to "+" to indicate that it is expecting a continuation. Because "+" and ">" are the same width, though, I often don't notice and really screw things up. These two lines make the R REPL mimic the Python REPL by changing the continuation prompt to the wider "...".

[Lines 22-24]: Change the default behavior of "q()" to quit immediately and not save workspace.

[Line 26]: This snippet allows you to tab-complete package names for use in "library()" or "require()" calls. Credit for this one goes to @mikelove.

[Lines 28-34]: There are three main reasons I like to have R save every command I run in the console into a history file.

• Occasionally I come up with a clever way to solve a problem in an interactive session that I may want to remember for later use; instead of it getting lost in the ether, if I save it to a history file, I can look it up later.
• Sometimes I need an alibi for what I was doing at a particular time
• I ran a bunch of commands in the console to perform an analysis not realizing that I would have to repeat this analysis later. I can retrieve the commands from a history file and put it into a script where it belongs.

These lines instruct R to, before anything else, echo a timestamp to the console and to my R history file. The timestamp greatly improves my ability to search through my history for relevant commands.

[Lines 36-42]: These lines instruct R, right before exiting, to write all commands I used in that session to my R command history file. I usually have this set in an environment variable called "R_HISTFILE" on most of my systems, but in case I don't have this defined, I write it to a file in my home directory called .Rhistory.

[Line 44]: Enables the colorized output from R (provided by the colorout package) on appropriate consoles.

[Lines 47-50]: This defines a function that loads a libary into the namespace without any warning or startup messages clobbering my console.

[Line 52]: I often want to autoload the 'dplyr' and 'ggplot2' packages (particularly 'dplyr' as it is now an integral part of my R experience).

[Lines 54-56]: This loads the packages in my "auto.loads" vector if the R session is interactive.

[Lines 58-59]: This creates a new hidden namespace that we can store some functions in. We need to do this in order for these functions to survive a call to "rm(list=ls())" which will remove everything in the current namespace. This is described wonderfully in this blog post.

[Lines 61-64]: This defines a simple function to remove any row names a data.frame might have. This was stolen from Stephen Turner (which was in turn stolen from plyr).

[Lines 66-70]: This defines a function to sanely undo a "factor()" call. This was stolen from Dason Kurkiewicz.

There are some compelling reasons to abstain from using an R configuration file at all. The most persuasive argument against using it is the portability issue: As you begin to rely more and more on shortcuts and options you define in your .Rprofile, your R scripts will depend on them more and more. If a script is then transferred to a friend or colleague, often it won't work; in the worst case scenario, it will run without error but produce wrong results.

There are several ways this pitfall can be avoided, though:

• For R sessions/scripts that might be shared or used on systems without your .Rprofile, make sure to start the R interpreter with the --vanilla option, or add/change your shebang lines to "#!/usr/bin/Rscript --vanilla". The "--vanilla" option will tell R to ignore any configuration files. Writing scripts that will conform to a vanilla R startup environment is a great thing to do for portability.
• Use your .Rprofile everywhere! This is a bit of an untenable solution because you can't put your .Rprofile everywhere. However, if you put your .Rprofile on GitHub, you can easily clone it on any system that needs it. You can find mine here.
• Save your .Rprofile to another file name and, at the start of every R session where you want to use your custom configuration, manually source the file. This will behave just as it would if it were automatically sourced by R but removes the potential for the .Rprofile to be sourced when it is unwanted.

A variation on this theme is to create a shell alias to use R with your special configuration. For example, adding a shell alias like this:

alias aR="R_PROFILE_USER=~/.myR/aR.profile R"
`

will make it so that when "R" is run, it will run as before (without special configuration). In order to have R startup and auto-source your configuration you now have to run "aR". When 'aR' is run, the shell temporarily assigns an environment variable that R will follow to source a config script. In this case, it will source a config file called aR.profile in a hidden .myR subdirectory of my home folder. This path can be changed to anything, though.

This is the solution I have settled on because it is very easy to live with and invalidates concerns about portability.


text:
# Why the frequency response plots (of causal filters) only show positive frequency?

Take an example of the below plot for an LPF (Source : WikiPedia)

The plot starts from $$0$$. We know that the fourier transform of any signal brings in negative frequencies due to complex exponentials (which are just as real as positive frequencies, just to be clear) but most of the frequency response plots I have observed for practical causal filters, they start from $$0$$.

Indeed, the frequency response of an ideal LPF is a rectangular function symmetric about the $$jw$$ axis and the impulse response is a non-causal sinc function.

I have the following questions: Why do we represent/plot the frequency response of a causal filter with only positive frequency plane? Does it have to do something with making the non-causal ideal sinc filter, a causal one? (But don't we have to make the impulse response [time domain] causal in order to achieve that? [although I don't know what's the frequency response of a causal sinc]) or is it understood that there is a symmetric negative frequency part and we just ignore it?

This has absolutely nothing to do with causality. The frequency response of a real-valued filter (i.e., one with a real-valued impulse response) is (conjugate) symmetric, i.e., the negative frequencies are redundant. That's why it is sufficient to show the frequency response at non-negative frequencies only.

You can easily see that symmetry as follows. The frequency response of a system with impulse response $$h(t)$$ is given by

$$H(\omega)=\int_{-\infty}^{\infty}h(t)e^{-j\omega t}dt\tag{1}$$

If $$h(t)$$ is real-valued, taking the complex conjugate of $$(1)$$ gives

$$H^*(\omega)=\int_{-\infty}^{\infty}h^*(t)e^{j\omega t}dt=\int_{-\infty}^{\infty}h(t)e^{j\omega t}dt=H(-\omega)\tag{2}$$

So, for real-valued $$h(t)$$, we have

$$H(\omega)=H^*(-\omega)\tag{3}$$

With $$H(\omega)=|H(\omega)|e^{j\phi(\omega)}$$ we get

$$|H(\omega)|=|H(-\omega)|\tag{4}$$

i.e., the magnitude of the frequency response is an even function of frequency, and

$$\phi(\omega)=-\phi(-\omega)\tag{5}$$

i.e., the phase is an odd function of frequency.

The same is true for discrete-time systems.

• Yes, this was amongst one of conclusions I had but I wasn't sure. Thanks a lot for the confirmation! It's always a pleasure to read your answers :-) – paulplusx Nov 9 '18 at 13:12

format: plain


text:
# Are quantum computers just a variant on Analog computers of the 50's & 60's that many have never seen nor used?

In the recent Question "Is Quantum Computing just Pie in the Sky" there are many responses regarding the improvements in quantum capabilities, however all are focussed on the current 'digital' computing view of the world.

Analog computers of old could simulate and compute many complex problems that fitted their operating modes that were not suitable for digital computing for many many years (and some are still 'difficult'). Before the wars (~I & II) everything was considered to be 'clockwork' with mechanical Turk brains. Have we fallen into the same 'everything digital' bandwagon trap that keeps recurring (there are no tags related to 'analog')?

What work has been done on the mapping of quantum phenomena to analog computing, and learning from that analogy? Or is it all a problem of folk having no real idea how to program the beasts.

• I asked a similarish question: quantumcomputing.stackexchange.com/questions/2595/… – Steven Sagona Jul 7 '18 at 19:16
• I just want to clarify that their is the potential distinction between Network based analog computers where connections are bi-directional, and Amplifier based analog computers where there was feedback (slow & sluggish..) based connections. It is the speed around the nodes, and the floor 'noise' that drives the interconnected nodes to their final state. It just feels like 'Quantum' is just a method of miniaturisation and speed up... – Philip Oakley Nov 25 '19 at 14:44

Here is a quick list of notable differences between analog and quantum computers:

1. Analog computers can't pass Bell tests.

2. The state space of an analog computer with N sliders is N dimensional. The state space of a quantum computer with N qubits is $2^N$ dimensional.

3. Error correct an analog computer and what you've got is a digital computer (i.e. not fundamentally analog anymore). Quantum computers are still quantum after being error corrected.

4. Analog computers aren't sensitive to decoherence errors. They don't break if you make accidental copies of the data. Quantum computations do break if that happens.

5. Analog computers can't (efficiently) run Shor's algorithm. Or Grover's algorithm. Or basically any other quantum algorithm.

• This is confusing to me. You seem to suggest that "analog" and "quantum" are two different things, but in reality they are not mutually exclusive: You have have (1) analog-classical (2) analog-quantum (3) digital-classical (4) digital-quantum. So for example, "analog computers" can pass Bell tests if they are analog quantum computers. Same goes for the rest of your points. – user1271772 Jul 10 '18 at 21:03
• @user1271772 In the context of the question, it is clear that I am referring to classical analog computers. – Craig Gidney Jul 10 '18 at 23:08

What work has been done on the mapping of quantum phenomena to analog computing, and learning from that analogy?

A starting place (with a lot of good references) to learn about analog quantum computing (also known as "quantum analogue computing" and "continuous variable quantum computing") is here. Note that analog classical computing is not as powerful as analog quantum computing, for a reason similar to what I explained in my answer to this question: quantum computers (whether digital or analog) can take advantage of quantum entanglement.

Have we fallen into the same 'everything digital' bandwagon trap that keeps recurring (there are no tags related to 'analog')?

A lot of people unfortunately have, and this might be part of the reason why "adiabatic quantum computing" struggled to get the respect it deserved in its early years (and even now). Adiabatic quantum computing is a specific type of analog quantum computing which certainly does have a tag on this Stack Exchange and a fair number of questions (but not enough, in my opinion). It has been proven that "adiabatic quantum computing", which is completely analog and does not involve any gates, can do anything that a digital quantum computer can do with the same computational efficiency, so while it is true that many people in quantum computing have fallen into the 'everything digital' bandwagon trap, there are some people that appreciate analog quantum computing (for example adiabatic quantum computing).

• Thanks for the extra tags, the links and the terminology clarification. For myself, I was comparing electrical mesh networks to quantum networks, where historically the electronic networks were 'instant', just as quantum is now, and both have similar physics on their side. – Philip Oakley Jul 10 '18 at 13:21

Are quantum computers just a variant on Analog computers of the 50's & 60's that many have never seen nor used?

No, they are not.

The digital vs analog factor is not the point here, the difference between quantum and classical devices lies at a more fundamental level.

A quantum device cannot, in general, be simulated efficiently by a classical device, be it "analog" or "digital" (or at least, this is strongly believed to be the case). In this sense, quantum computers are really radically different from any variation of classical analog computers, or other forms of classical computing for that matter.

Indeed, the most popularized architectures for quantum computing, those operating on sets of "qubits", are the quantum counterparts of digital classical computers. Analog devices also have their quantum counterparts (see for example continuous-variable quantum information).

• The aspect I had in mind was the way interactions are seen. In digital there is a presumed certainty, while in analog there is 'noise' (fluctuation, probability, ..). It's the latter that Quantum tends to be presented as, hence the suggestion of my Q (plus there are few left who really remember such analog methods!) – Philip Oakley Jul 10 '18 at 16:33
• @PhilipOakley I'm not sure I understand. It's the latter that Quantum tends to be presented as <- I don't understand this sentence – glS Jul 10 '18 at 16:40
• The 'latter' (for QM) being "probability distributions" and the like. So noise in an analog system is a multi-dimensional probability problem (as per Shannon) and Qubits would appear to be a similar multi-dimensional probability problem, hence the similarity of the conceptual abstractions. One key difference is the spatial extent such that old fashion analog networks rarely got to a MHz BW, and millisecond responses over cm, but QM hopes for much much higher frequencies over microns and less. – Philip Oakley Jul 10 '18 at 16:49
• Qubits would appear to be a similar multi-dimensional probability problem: but they are not really, or at least, not the same way the classical analog devices are. A qubit can be in a continuum of states, that's true, but every time you measure it you always observe it in one of two positions, so it's something fundamentally different than what you have classically. Another big difference is that the number of possible states in which quantum systems can be is exponentially larger than what is possible classically, thus allowing for a much richer dynamics – glS Jul 10 '18 at 16:56

Have we fallen into the same 'everything digital' bandwagon trap that keeps recurring?

What I have noticed is more the 'everything binary' bandwagon trap; which reminds me of the Grandma's cooking secret:

Once upon a time, a mother was teaching her daughter the family recipe for making a whole baked ham. It was the very best ham anybody had ever had so they always followed that recipe carefully.

They prepared the marinade, scored the skin, put in the cloves, and then came a step the daughter didn't understand.

"Why do we cut off the ends of the ham?" she said. "Doesn't that make it dry out?"

"You know, I don't know," said the mother. "That's just the way grandma taught me. We should call grandma and ask."

So they called grandma and asked, "why do we cut off the ends of the ham? Is it to let the marinade in, or what?"

"No," said Grandma. "To be honest, I cut the ends off because that's how my mother taught me. I added the marinade step later, because I was worried about the ham drying out. Let's call great grandma and ask her."

So they called the assisted living facility where great grandma was living, and the old woman listend to their questions, and then said.

"Oh, for land sakes! I cut off the ends because I didn't have a pan big enough for a whole ham!"

I was recently thinking about qubytes & wondering if they really needed to be defined as 8 qubits. An 8-level quantum system (qunit) would have an 8 dimensional space & could in theory encode a byte (8 bits). Is this a better definition of a qubyte (quantum byte)?

Or is it all a problem of folk having no real idea how to program the beasts.

• I'd agree that the "everything binary/digital" has become a mantra that many are embedded within (that then being above). We explain brains and everything as if it's like a computer. There was a period in the early electronics days where its theories/techniques could be applied to big analog issues, such as resistive (impedance) meshes. It's mostly the same old Maxwell, apart from the mistaken(?;-) Gibbs formulation, that QM uses, so a bit of provocation about a bit of lateral thinking is in order, maybe. For the 'byte', have a look at baud rate, which is not bit rate. – Philip Oakley Jul 10 '18 at 16:40
• 'Symbol rates' - nice! I think the binary issue predates everything as a computer. See: the tree of knowledge of good & evil ;P – meowzz Jul 10 '18 at 17:41
• for the 8d space, have a look at C Furey's PhD "Standard model physics from an algebra?", and the 2 minute YouTube lectures. Has a lot of plausibility relative to our need for a mathematics to represent science .. (can't let things become voodoo maths/science - other theologies available) – Philip Oakley Jun 5 '19 at 14:55


text:
# Vertical bar for “evaluated at”

I would like to create a vertical "evaluation" bar similar to the one in this formula:

This was produced by the code:

df\,_{\big\rvert x} \colon T_x M \to T_y N

It comes close to what I want, but I am not entirely satisfied: I would like the 'x' to be lower (e.g. aligned with the bottom of the vertical bar). Also maybe have the vertical bar slightly less tall.

Suggestions? Thank you!

Like this?

To get the vertical bar a bit larger, you can use \raisebox. This way, the \vert will be in the normal math size and x will be in the \scriptsize style. Alternatively, you can use \mbox if you don't like \raisebox. In this case, the usual font sizes can be used (tiny, scriptsize, footnotesize, small, normalsize, large, Large, LARGE, huge, Huge).

\documentclass{article}
\usepackage{amsmath,mathptmx}
\begin{document}

$df\,\raisebox{-.5em}{\vert_{x}} \colon T_x M \to T_y N$
%
$df\,_{\mbox{\vert_{x}}} \colon T_x M \to T_y N$

\end{document}

• Thank you, that works. For my own taste, I inserted a \big before the \vert. – Seub Jun 24 '17 at 2:17
• +1. Two suggestions. First, consider moving the \, directive inside the argument of \raisebox or \mbox. Second, \, may provide to much whitespace; consider inserting half as much whitespace by issuing the directive \mkern1.5mu instead. – Mico Jun 24 '17 at 5:32
• @Mico: Perfect, thank you! So, for those interested, I did: \newcommand{\evalat}[1]{_{\mbox{${\mkern1.5mu}\big\vert_{#1}$}}} – Seub Jun 24 '17 at 18:48
• @Seub - You may also want to give \newcommand\evalat[1]{_{\mkern1.5mu\big\vert_{\scriptstyle #1}}} a try. – Mico Jun 24 '17 at 19:07
• @Mico Yep, I'm keeping your suggestion! – Seub Jun 24 '17 at 20:01


text:
# beamer blocks in ordinary article-style document

Is there a way to emulate beamer blocks, e.g., alert/example, in ordinary pdflatex in article documents? Is there a pure tikz way of doing this?

-
What exactly do you want to achieve? Colored text? The box around theorem environments? –  Caramdir Aug 29 '10 at 19:23
Rounded corners, shadows, two colors (title/body). I have seen some tikz examples, but they are not 100% satisfactory. –  E.Verbitskiy Aug 29 '10 at 19:36

With version 2.00 of tcolorbox and its skin library it's possible to include beamer blocks in non-beamer documents. And now this blocks can break between columns and pages.

Here you have an example:

\PassOptionsToPackage{svgnames}{xcolor}
\documentclass[twocolumn,a4paper]{article}
\usepackage{tcolorbox}
\usepackage{lipsum}
\tcbuselibrary{skins,breakable}

\title{Beamer blocks in \texttt{article.cls} with \texttt{tcolorbox (v 2.00)}}
\author{xxxxxx}

\newenvironment{myexampleblock}[1]{%
\tcolorbox[beamer,%
noparskip,breakable,
colback=LightGreen,colframe=DarkGreen,%
colbacklower=LimeGreen!75!LightGreen,%
title=#1]}%
{\endtcolorbox}

\tcolorbox[beamer,%
noparskip,breakable,
colback=LightCoral,colframe=DarkRed,%
colbacklower=Tomato!75!LightCoral,%
title=#1]}%
{\endtcolorbox}

\newenvironment{myblock}[1]{%
\tcolorbox[beamer,%
noparskip,breakable,
colback=LightBlue,colframe=DarkBlue,%
colbacklower=DarkBlue!75!LightBlue,%
title=#1]}%
{\endtcolorbox}

\begin{document}
\maketitle

\section{Section}

\lipsum[2]

\begin{myblock}{Example of \texttt{myblock}}
\lipsum[2-3]
\end{myblock}

\begin{myexampleblock}{Example of \texttt{myexampleblock}}
\lipsum[2]
\end{myexampleblock}

It is relatively easy to reproduce similar boxes using TikZ in a non-beamer document. For example, see here. Do you really need it to look exactly the same as in beamer? If so, you can just look at how it is implemented within beamer; I think it is defined either in beamerbaselocalstructure.sty or beamerinnerthemeinmargin.sty.


text:
# nLab refinement

In general, let $U = (U_i)_{i: I}$ and $V = (V_j)_{j: J}$ be two families of objects of some category $C$. We say that $V$ is a refinement of $U$ if there are a function $f: J \to I$ of indices and a morphism $V_j \to U_{f(j)}$ for each $j$.

## Examples

Very often we do this in the slice category $C/X$ for some object $X$. If you spell this out, then you have families $(U_i \to X)_i$ and $(V_j \to X)_j$ of morphisms to $X$; $V$ is a refinement of $U$ if there are a function $f: J \to I$ and a commutative diagram

(1)$\array{ V_j &&\to&& U_{f(j)} \\ & \searrow && \swarrow \\ && X }$

for each $j$.

More specifically, apply this to the poset of subobjects of $X$. Then you have families $(U_i \hookrightarrow X)_i$ and $(V_j \hookrightarrow X)_j$ of subobjects of $X$; $V$ is a refinement of $U$ if there are a function $f: J \to I$ and a commutative diagram (1) for each $j$.

Yet more specifically, apply this to the lattice of subsets of some set $X$. Then you have families $(U_i \subseteq X)_i$ and $(V_j \subseteq X)_j$ of subsets of $X$; $V$ is a refinement of $U$ if there is a function $f: J \to I$ such that each $V_j$ is contained in $U_{f(j)}$.

Yet more specifically, let the families of subsets be indexed by themselves. Then have collections $U \subseteq \mathcal{P}X$ and $V \subseteq \mathcal{P}X$ of subsets of $X$; $V$ is a refinement of $U$ if for each $j$ there is an $i$ such that $V_j$ is contained in $U_i$.

Actually, this definition is slightly weaker than the previous one in the absence of the axiom of choice. Perhaps in that case the general definition should say that for each $j$ there is an $i$ and a morphism $V_j \to U_i$.

Special cases of this last example include refinement of filters and refinement of open covers.

On the other hand, you might want to generalise the case of open covers to covers or covering sieves on a site. In that case, the general definition still applies; you have covering families $(U_i \to X)_i$ and $(V_j \to X)_j$ of some object $X$; $V$ is a refinement of $U$ if there are a map $f: J \to I$ and a commutative diagram (1) for each $j$.

Revised on July 19, 2009 20:24:22 by Toby Bartels (71.104.230.172)