Friday, March 30, 2012

David MacKay on Sustainable Energy

I've linked to MacKay's book "Without Hot Air" before. You can buy it or read it on the web for free.

I like the way he makes numbers on the issue of sustainable energy and climate change, available and accessible to a general audience. Given the persistent numerical illiteracy (or apathy) around the issue, such outreach may be crucial.

Here is a video-lecture for those who'd like a (hour-long) summary in a different format!


Wednesday, March 28, 2012

Writing multiple column vectors to file in Matlab and Octave

Consider a simple example. If x and y are two vectors of the same size, one obvious way of writing them to file is:

n  = 1e5;
x  = 1:n;
y  = x.^2;
f1 = fopen('xy.dat','w');
for i = 1:length(x)
    fprintf(f1,'%d\t%d\n',x(i),y(i));
end
fclose(f1);

[Side Note: Matlab does not have a command like Octave's "printf" - if "fprintf" is used without a file handle, then it outputs to the screen.]

I ran timings for the "red" writing part of the code above in Matlab 7.4 R2011b and Octave 3.4.2 on my Desktop, and found that it took 3.4s and 2.6s, respectively. It isn't usual for Octave to outrun Matlab, but this may have something to do with the peculiarities of my installation.

I replaced the "for-loop" in the write operation with a single command:

fprintf(f1,'%d\t%d\n',x,y);

and my timings improved. It took 0.13s and 0.11s for Matlab and Octave, respectively. So clearly getting rid of the for loop helps (no surprise!).

Sometimes, you may be in a hurry and not care so much for the control over formatting that fprintf offers. If you want avoid all the fopen and fclose business, you could instead use, something like:

xy = [x' y'];
save('xy.dat', 'xy','-ascii');

Note that I need to define a "matrix", with the columns side by side to use this.  It took 0.23s and 0.28s for Matlab and Octave, respectively - so there is a small performance hit.

Friday, March 23, 2012

TexMaths: A LaTeX plugin for Open/LibreOffice

I have been a big fan of an OpenOffice extension called OOoLaTeX - which lets you typeset equations in OpenOffice Writer and Impress (the word-processing and presentation software, respectively).
I love it because equations look beautiful, and I can cut-paste from my LaTeX manuscripts (and wikipedia sources for lectures).
Getting fonts to work, does require some gymnastics - but is not a deal-breaker. The trouble with OOoLaTeX however is that it is no longer actively maintained (the last updates date back to late 2007), and getting it to work with newer releases has started to get increasingly painful. In fact, late last year, I reluctantly gave the LaTeX presentation class "beamer" a shot, and actually found it very usable.

I found out last week that there is a new LaTeX plugin for LibreOffice (version >= 3.3) called TeXMaths
TexMaths is a LaTeX equation editor for LibreOffice. It is derived from OOoLatex, originally developed by Geoffroy Piroux (see here). 
As its predecessor, TexMaths is a LibreOffice extension that allows you to enter and edit LaTeX equations directly into LibreOffice documents. All you need is a working LaTeX installation, at least one of the two programs dvipng or dvisvgm and of course the TexMaths LibreOffice extension.
It looks like what OOoLaTeX should have looked like by now, if it were maintained. The primary advantage of TexMaths over OOoLaTeX is that equations are rendered either in png or svg (instead of emf). This simplifies the installation process a great deal, because you don't have to wrestle with the issue of getting fonts right.

SVG is scalable (so you can resize the equation without screwing it up), and you can drag and drop it into other programs (Inkscape has its own native LaTeX support).

The whole episode points to the both the pitfalls and strength of open source software. One can get accustomed to programs (especially those with single authors) that subsequently become obsolete, because the principal coder has moved on to better things. However, if the program fills an important demand, often, someone else steps up, and builds and improves the older program.


Tuesday, March 20, 2012

The Agony and Ecstasy of Mike Daisey

A couple of months ago, I linked to an episode on "This American Life" called "Mr. Daisey and the Apple Factory". Apparently, it went on to become the most downloaded episode in the program's history.

Well, turns out, Mr. Daisey made stuff up.

In his defense, he argues that he is a theater person, not a journalist, but that strikes me as being at least somewhat disingenuous. It must not be forgotten that many of the claims he makes in the episode have been reported elsewhere. They may still be true. However, this does not help our disappointment upon learning that his "first-person" account was a fabricated exaggeration of a third-person news-story.

To his credit, Ira Glass devoted the entire episode of "This American Life" to the retraction.


Thursday, March 15, 2012

Happy Pi Day

I know this is a day late, but I think the following physics-based puzzle was the awesomest thing I saw on Pi-day (H/T Sol)!

This has to be the best problem I've seen so far this year!

Spoiler alert: the video contains an outline of the solution, but it us sufficiently vague to make you want to work it out anyway.

Links: Finance

1. A sympathetic portrait of Ben Bernanke by Roger Lowenstein in the Atlantic entitled "The Villan". It is a long article, but presents an articulate summary of the pressures of a thankless but powerful job.
The left hates him. The right hates him even more. But Ben Bernanke saved the economy—and has navigated masterfully through the most trying of times.
... 
Ultimately, Bernanke’s legacy will depend on whether he can fully exit from the mortgage debacle without bequeathing a new one, or lighting an inflationary fire that becomes uncontainable. Alan Greenspan retired as the prince of central banking, but saw his reputation wither because of the bubble that burst on Bernanke’s watch. In office, Paul Volcker was highly controversial because of his hawkish policies; today he is practically canonized. Vincent Reinhart, who served under both Greenspan and Bernanke as the senior monetary deputy and is now retired, told me I was writing about Bernanke “five years too early.” For sure, no one knows where either inflation or unemployment will be in five years’ time. Forgoing a guess, I would offer the appraisal by Hank Paulson, the former treasury secretary, who told me recently: ”I don’t know what people expect Ben to do. To me, it’s pretty amazing. Who would have guessed when he came to Washington we would be so fortunate to get someone who was willing to think outside the box and deal with this unprecedented crisis?”
2. Greg Smith writes a scathing opinion in the NYT "Why I am leaving Goldman Sachs" telling us what we always suspected we knew.

Tuesday, March 13, 2012

Subfunctions in Octave/Matlab

Consider the contents of the following Matlab program "mainFunction.m".

%
% A function that integrates the function
% given by the routine subFunction(x)
% over the range a and b
%

function I = mainFunction(a,b)
   a = -5;
   b = 5;
   I = quad('subFunction', a, b)
%  keyboard  % uncomment if needed
endfunction
 
%
% some nontrivial function
%

function y=subFunction(x)
   if( x > 0)
      y = x^2;
   else
      y = x;
   endif
endfunction

The function to be integrated, subFunction(x), can be saved as a separate file called subFunction.m, or, as in this case, it can be included in the same file as the main function. There are advantages to both these techniques.

If I store functions in their own individual files, I can call them from any other function. However, I can very easily end up with a lot of files. This may not necessarily be a bad thing, but it can be a challenge beyond a point.

If I use the subfunctions (as above), then the subfunctions themselves remain invisible outside the file in which they are contained. They may be accessed by the main function and other subfunctions within the same file.

Unfortunately, you cannot use "subfunctions" with script files. A useful workaround is to wrap a function declaration around your script, and use the command keyboard (as shown in the commented line in the example above) to transfer control out.

From here you can examine all the variables local to the "script".

When you are done, say dbquit, or return.

Saturday, March 10, 2012

Links: Tech Edition

1. Bill Gates' bookshelf: The library makes for an interesting "to-read" list, with plenty of reviews written by Bill Gates.

2. Is Jeff Bezos the next Steve Jobs? Probably not, but you've got to admit that Amazon has become a lot more than an online retailer.
“A big piece of the story we tell ourselves about who we are is that we are willing to invent,” Mr Bezos told shareholders at Amazon’s annual meeting last year. “And very importantly, we are willing to be misunderstood for long periods of time.” 
... 
That passion for invention has not deserted Mr Bezos, who last year filed a patent for a system of tiny airbags that can be incorporated into smartphones, to prevent them from being damaged if dropped.
3.  LINux from Abstruse Goose.

Thursday, March 8, 2012

Efficiency and Friction in Academia

After reading Barry Schwartz's opinion column in the NYT, I wondered if his "efficiency-friction" metaphor had any resonance in an academic setting. To paraphrase poorly, Schwartz thinks efficiency is generally a good thing, while friction sometimes provides a useful counter-balance.

Almost instantly after I had framed that question, I recalled how hard it was to carry out some routine research tasks when I first "started", less than 15 years ago.

As an undergraduate student doing some literature survey, I remember the hurdles one had to jump over - first, one needed to check out a Chemical Abstracts or Inspec CD, and try to hit the static databases with meaningful queries. After studying the abstracts, one hoped to come up with some leads. I shudder to imagine what people had to do before CDs became cool.

Then, one had to figure out where the particular journal was archived (if it was at all) to hunt down the article.

After spending some time reading the article, one had to figure out if it was worthwhile to "save" it. If it was, then one headed with the big tome (usually several) to the photocopying unit, usually on a different level in the library.

The process of coming back from the library with a couple of useful articles was a full afternoon's workout.

Contrast those times with today.

I sit in my office, do a quick google search, and a couple of mouse clicks later, I have downloaded a PDF on my computer.

In rare cases, the article is very old, or the journal is not housed in our library.

No problem! I simply fill out simple form on my library's webpage, and usually I get a PDF emailed to me in a couple of days.

I can open up the PDF, read it, annotate it, and upload the annotated copy on a site like CiteULike, with some useful tags. I don't even have to store a copy on my Desktop. I can search (re-"search") for it whenever I like, I can read it from anywhere, and I can easily cite the paper.

If "Efficiency" ever wrote an autobiography, it would be littered with such anecdotes.

So no, I don't see any role for friction in literature surveys.

The only places that I can think of where efficiency sometimes becomes too much of a good thing  are classes with an over-reliance on Powerpoint (link to comics) lectures.

It is no wonder that some of the most popular material on the web (including Khan Academy, or lectures on MIT's OCW) tends to be very "old-school" chalkboard talks.

Sunday, March 4, 2012

Incentivizing Good Citizenship

Richard Thaler has a column in the NYT on making good citizenship fun. "Fun" can be used as a strong positive incentive to encourage a certain type of behavior, as in this viral video:

I liked this particular example:
Over in mainland China, lotteries are used for a different purpose: tax compliance. As in many parts of the world, China has a thriving cash economy, and it is common for small businesses like restaurants to evade paying sales tax. To combat this behavior, the government printed up special receipts that are supposed to be given to restaurant customers when they pay. Cleverly, each receipt includes a scratch-off lottery ticket, giving customers an incentive to ask for a receipt. Finance ministers in Southern Europe might take note.
You may also want to check out this, this, and this video. 

Thursday, March 1, 2012

Nonsingular and Nondefective Random Matrices

In a previous post, I posed the folllowing question:

Consider again, a random square n by n matrix A, whose entries are restricted to the set of integers {-p, -p+1, ..., 0, ... p-1, p}. Each of the 2p+1 values are equally probable.
  • What is the probability that this matrix is nonsingular?
  • What is the probability that this matrix is nondefective?
For n = 3 and 4, using a simple Monte Carlo method, this is what I get. As p increases, we approach the continuous distribution of entries asymptotically.
Blue lines are for n=3, and green for n=4. Triangles and squares denote the probability that a random matrix is nonsingular, and nondefective, respectively
As we can see, both singular and defective matrices are extremely rare for large p. Between the two, defectiveness is a rarer feature.