Better R console fonts

The default installation of R on Windows uses Courier New for the console font. Unfortunately, this font offers low contrast between the letter ‘l’ and the number ‘1.’ There is also poor contrast between the letter ‘O’ and the number ‘0.’ The contrast between period and commas is OK.

Lucida Console is an improvement. It has high contrast between ‘l’ and ‘1’, though ‘O’ and ‘0’ are still hard to distinguish. But my favorite console font is Consolas. It offers strong contrast between ‘l’ and ‘1’, commas and periods, and especially between lower case ‘o’, upper case ‘O’, and the number ‘0.’

Consolas is more legible while also fitting more characters into the same horizontal space. It can do this because it uses ClearType anti-aliasing while the other two fonts do not. Here is a sample of the three fonts magnified 4x to show the anti-aliasing.

I found setting the default console font in R a little tricky. Clicking on the Edit -> GUI preferences menu brings up the Rgui Configuration Editor. From there it’s obvious how to change the font. However, what I found surprising is that clicking the “OK” button only changes the font for the current session. I can’t think of another application that behaves analogously. To set your choice of font for all future sessions, click “Save” rather than “OK.”

* * *

Help integrating R into your environment

The cost of breaking things down and putting them back together

The basic approach to solving large, complex problems is to break them down into smaller problems then re-assemble the solutions. The total cost of a solution has to account for not only the cost of solving the all the sub-problems, but also the cost of dividing the original problem into sub-problems and the cost of assembling the solutions to the sub-problems into a solution to the original problem. For example, the cost of writing a program is not simply the cost of each of the subroutines that comprise the final program. The cost has to include the cost of dividing the original task into subroutines and the cost of integrating these subroutines into the final product.

There is a theorem in computer science that quantifies the fuzzy statements above.

Let a ≥ 1 and b > 1 be constants. Let T(n) be defined on non-negative integers by the recurrence T(n) = a T(n/b) + f(n). In applications, T(n) is the total cost of solving a problem of size n. The recurrence relation says we have a way of breaking the original problem into b sub-problems of size n/b, each of which takes a units of work to solve. The cost of breaking the original problem into sub-problems and assembling the solutions into the final solution is described by f(n).

The theorem, which Introduction to Algorithms (ISBN 0262032937) calls the “master theorem,” says that the total cost T(n) of the solution can be bound according to three separate cases.

  1. If f(n) = O(nlogba – ε) for some constant ε > 0 then T(n) = O(nlogba).
  2. If f(n) = T(nlogba), then T(n) = T(nlogba lg(n)).
  3. If f(n) = (nlogba + ε) for some constant ε > 0, and if a f(n/b) = c f(n) for some constant c < 1 and for all sufficiently large n, then T(n) = O(f(n)).

The notation O, Ω, and Θ is explained in these notes on asymptotic order notation.

The main point here is that the term f(n), the one we tend to think less about, is the most important term in the theorem. T(n), the total time for the solution, breaks into three cases depending on whether grows at a rate slower, equal to, or faster than a multiple of nlogba.

Strictly speaking, the theorem applies to the cost (operation count) of running a program. However, you can draw an analogy from the theorem to the cost of developing a program as well. You have to consider the cost of the human subdividing the original problem (design) and the cost of making the pieces work together (integration).

Hubble and astronomy papers

The Hubble Space Telescope “has become the source of roughly 25 percent of all published astronomy research papers” since the telescope was fitted with corrective lenses in 1993 according to Discover magazine, September 2008.

Why there will always be programmers

The latest episode of the .NET Rocks podcast is an interview with Robert Martin. In the interview, Robert Martin gives his view of why there will always be programmers. Here’s my summary/paraphrase of his argument.

Systems are full of details, and somebody has to manage those details. That’s what programmers do. Maybe some day, for example, we will draw diagrams instead of typing lines of text in order to specify computer systems. Fine, but then those diagrams will have lots of detail, and we will need professionals who can manage that complexity. Call them programmers.

Carl Franklin, one of the hosts, added that part of a programmer’s responsibility is to know what needs to happen when things go wrong, which is part of managing the details of a system.

The essential challenge of programming is managing complexity. Those who think the difficulty is primarily translating ideas into computer language syntax line-by-line haven’t written large programs. Writing small programs is challenging, and not many people can do it. But far fewer people can write large programs.

To write a big program, you just break it into lots of small programs, right? Well, that’s true a sense, in the same sense that writing a book is merely a matter of writing chapters, which is merely a matter of writing paragraphs etc. But writing books is hard because the pieces have to hang together in a coherent whole. If part of a book doesn’t quite fit with the whole, the result is aesthetically disappointing. If a part of a program doesn’t quite fit in, it’s called a crash. Paper doesn’t abort, but programs do.

42nd Carnival of Mathematics

It’s the 42nd Carnival of Mathematics. Don’t panic!

First, the customary trivia about the number of the Carnival.

  • 42, Jackie Robinson’s jersey number, is the only number retired by all Major League Baseball teams.
  • 42 is a domino game, especially popular in Texas.
  • 42 is the top result for the Google search “answer to life, the universe, and everything.”

And now on to the posts.

TwoPi at the 360 blog presents two fun articles using advanced techniques to prove elementary results, Using calculus to derive the quadratic equation and Integration by parts via iterated integrals.

Next, Barry Leiba discusses the use of logic in mathematics in Modus ponens, modus tollens, and proof by contradiction posted at Staring At Empty Pages. The footnotes at the end have some interesting information on prime numbers, factorization, and cryptography.

Moving from philosophical logic to hardware logic, Himadri Choudhury illustrates how a seemingly trivial problem becomes interesting when you look into it deeply enough. He presents a clever algorithm for simultaneously adding three integers more efficiently in a microchip. Check out Multiple Operand Adder posted at Thread Pool for an insight into the work that goes into operations most of us take for granted.

Moving higher up the computing levels of abstraction, Samuel Jack combines mathematics and computer science in presenting his solutions to problems 18 and 67 from Project Euler. See Project Euler Problems 18 and 67: Finding the Maximal Route through a Triangle posted at Functional Fun. He also presents his solution to problem 15 in the same series in City Grids and Pascal’s Triangle.

Finally, MBB explains how the check-sum algorithm works on major credit cards in Generation Of Luhn Algorithm Credit Card Numbers posted at Money Blue Book Finance Blog. This algorithm will detect any error that changes a single digit and will detect most errors that transpose consecutive digits.

Jason Dyer will host the 43rd Carnival of Mathematics at The Number Warrior on November 7. Submit your articles for the next Carnival here.

Three math diagrams

Here are three pages containing diagrams that each summarize several theorems.

The distribution relationships page summarizing around 40 relationships between around 20 statistical distributions.

The modes of convergence page has three diagrams like the following that explain when one kind of convergence implies another: almost everywhere, almost uniform, Lp, and convergence in measure.

The page conjugate prior relationships is similar to the probability distribution page, but concentrates on relationships in Bayesian statistics.

What are some of your favorite diagrams? Do you have any suggestions for diagrams that you’d like to see someone make?

Five kinds of subscripts in R

Five kinds of things can be subscripts in R, and they all behave differently.

  1. Positive integers
  2. Negative integers
  3. Zero
  4. Booleans
  5. Nothing

For all examples below, let x be the vector (3, 1, 4, 1, 5, 9).

Positive integers

Ordinary vector subscripts in R start with 1, like FORTRAN and unlike C and its descendants. So for the vector above, x[1] is 3, x[2] is 1, etc. R doesn’t actually have scalar types; everything is a vector, so subscripts are vectors. In the expression x[2], the subscript is a vector containing a single element equal to 2. But you could use the vector (2, 3) as a subscript of x, and you’d get (1, 4).

Negative integers

Although subscripts that reference particular elements are positive, negative subscripts are legal. However, they may not do what you’d expect. In scripting languages, it is conventional for negative subscripts to indicate indexing from the end of the array. So in Python or Perl, for example, the statement y = x[-1] would set y equal to 9 and y = x[-2] would set y equal to 5.

In R, a negative is an instruction to remove an element from a vector. So y = x[-2] would set y equal to the vector (3, 4, 1, 5, 9), i.e. the vector x with the element x[2] removed.

While R’s use of negative subscripts is unconventional, it makes sense in context. In some ways vectors in R are more like sets than arrays. Removing elements is probably a more common task than iterating backward.

Zero

So if positive subscripts index elements, and negative subscripts remove elements, what does a zero subscript do? Nothing. It doesn’t even produce an error. It is silently ignored. See Radford Neal’s blog post about zero subscripts in R for examples of how bad this can be.

Booleans

Boolean subscripts are very handy, but look very strange to the uninitiated. Ask a C programmer to guess what x[x>3] would be and I doubt they would have an idea. A Boolean expression with a vector evaluates to a vector of Boolean values, the results of evaluating the expression componentwise. So for our value of x, the expression x>3 evaluates to the vector (FALSE, FALSE, TRUE, FALSE, TRUE, TRUE). When you use a Boolean array as a subscript, the result is the subset of elements whose index corresponds to an index in the Boolean array containing a TRUE value. So x[x>3] is the subset of x consisting of elements larger than 3, i.e. x[x>3] equals (4, 5, 9).

When a vector with a Boolean subscript appears in an assignment, the assignment applies to the elements that would have been extracted if there had been no assignment. For example, x[x > 3] <- 7 turns (3, 1, 4, 1, 5, 9) into (3, 1, 7, 1, 7, 7). Also, x[x > 3] <- c(10, 11, 12) would produce (3, 1, 10, 1, 11, 12).

Nothing

A subscript can be left out entirely. So x[] would simply return x. In multi-dimensional arrays, missing subscripts are interpreted as wildcards. For example, M[3,] would return the third row of the matrix M.

Mixtures

Fortunately, mixing positive and negative values in a single subscript array is illegal. But you can, for example, mix zeros and positive numbers. And since numbers can be NA, you can even include NA as a component of a subscript.

Related resources