R: solving a small system of equations fast

My google-fu failed me on this one, so for the next person googling “stupid fast library for solving a system of equations in R such that the trade off between speed and ‘robustness’ is way on the speed side”, you’re welcome.

I’m on the final leg of my dissertation and I have some code that loops through a discreet state-space with about 300k points. Each of these iterations requires a two-equation non-linear system to be solved. Because all of this is contained in a loop that is (supposed to be) converging on a pair of value functions, its not super critical that the solutions to this system are “correct” every time. I want speed, baby.

Here’s the R libraries I tried:

  • nleqslv with the functions written to have a high penalty: each iteration took 0.020 seconds with a standard deviation of 0.007 seconds
  • optim using the “make the system a single quadratic” trick and having a high penalty: mean 0.253 seconds with a standard deviation of 0.130 seconds
  • optim using the “make the system a single quadratic” trick and using the “L-BFGS-B” algorithm to deal with boundedness: mean 0.014 seconds with a standard deviation of 0.032 seconds
  • pso (partical swarm optimization) with bounds: mean 20 seconds!

I was kind of excited about particle swarm optimization but it was waaaaaay slow (at least with the default settings). The best was optim (available with the R core installation) using the quadratic trick. The quadratic trick is just defining all your functions in the system with the right hand side equal to zero and then having the algorithm minimize the sum of squares of those equations. Interestingly the nleqslv library, which is built for solving “medium-sized” systems, did almost as well on average but had had a much better variance. I’m not sure if this is because nleqslv gives up earlier or if it does a better job staying out of pathological parts of the parameter space relative to optim.

Anyway, there you go. If you find your self solving a small system of equations lots and lots of times over and over again, optim should do the trick for you in terms of speed.

PS – R seems to be the wrong environment for this task. Fortran code that does a very similar thing runs about 10 times faster.

A link everyone will find useful

I was at a practice job talk at a nearby university on Friday. As preface to my work ((I’ll post on it as soon as I get my job applications sent)), I pointed out that workers change careers many times, 3 or 4 times, over their lifetimes where career change is defined as an occupation change that requires different tasks performed on the job, e.g. taxi driver to nurses assistant. BTW, workers change occupations about once every five years so only about a third of these are career changes. One of the audience members, an academic, objected saying that nobody he knows has ever changed occupations (not just careers, occupations).

On that note, I am sure everyone would find this MR linked paper useful: goofy titles get cited less often. At the link, its suggested that this is because “science is serious business”, but in my experience this result stems from the fact that most of these titles are “funny” but not funny. The problem is that the paper only looks at the effect of average “funny” titles. Since the average “funny” title isn’t funny, just “funny”, its picking up the effect of being “funny” not funny. I’m sure there are some really funny titles that get extra cites because of it.

Which paper titles are funny and not just “funny”?

Setting expectations

Evans and Honkapohja’s work on learning in macro is important. Watch this to get a sense for what they’re doing:

But learning dynamics are not well understood empirically. Prof. Evans describes the dynamics under adaptive learning. Under rational expectations the economy would just pop to the good equilibrium. Under other learnings schemes the dynamics would be different. How do we know which dynamics describe the actual economy? We’d need to have empirical tests of the different learning regimes. We don’t have these tests.

In the same vein, Prof. Evans is just speculating about where the economy is right now in the phase diagram. He suggest we’re close to to the deflationary zone where things go to hell if the Fed raises interest rates. In fact, there’s no a priori way to know where we’re at in the diagram — we don’t have good measures of expectations.

(ht Thoma. BTW, Thoma says that Evan’s model shows increasing interest rates “increases rather than decreases the chance of a deflationary spiral”. They do no such thing. The little arrows on the phase diagram aren’t invariant to policy, see the paper figure 1. Suppose you’re close to the deflationary zone. Where you were before the policy may be in the deflationary zone after the policy, but your location after the policy is determined, in part, by the policy and it doesn’t have to be in the deflationary zone. Anyway, this probability could be computed as simulated comparative static, but Evan’s didn’t do this in his paper.)

Economists: the King’s alchemists?


One of the embarrassing dirty little secrets of economics is that there is no such thing as economic theory properly so-called. There is simply no set of foundational bedrock principles on which one can base calculations that illuminate situations in the real world. Biologists know that every cell runs off instructions for protein synthesis encoded in its DNA. Chemists start with what the Heisenberg and Pauli principles plus the three-dimensionality of space tell us about stable electron configurations. Physicists start with the four fundamental forces of nature. Economists have none of that. The “economic principles” underpinning their theories are a fraud–not bedrock truths but mere knobs twiddled and tunes so that th right conclusions come out of the analysis.

What are the “right” conclusions? It depends on what type of economist you are, for [there] are two types. One type chooses, for non-economic and non-scientific reasons, a political stance and a political set of allies, and twiddles and tunes their assumptions until they come out with conclusions that please their allies and their stance. The other type takes the carcass of history, throws it into the pot, turns up the heat, and boils it down, hoping that the bones and the skeleton that emerge will teach lessons and suggest principles that will be useful to voters, bureaucrats, and politicians as they try to guide our civilization as it slouches toward utopia.

There’s a third type of economist that cooks a pot of history to discover interesting patterns for the sake of discovering interesting patterns. Ironically, given the existence of the first type of economist, you should only listen to the third type for policy advice.

A little better

Here’s the cross-state plot of the effect of immigrants on native wages:
This time you’ll notice that I plotted the changes in the proportion of immigrants against the change in wages (these are changes from the 1990 to 2000 censuses). In effect, I’m controlling for fixed features of states. This makes the effect size (a slope of about 0.25 and statistically insignificant) a little closer to the average effect size reported by Longhi, Nijkamp and Poot.

Substitutability as default?

I’m jumping ahead of my own narrative, but why does it seem ingrained in people (including economists) that immigrants must be competing with native workers? Why is complementarity so much more unlikely in people’s priors?

We don’t have this bias when it comes to new technologies. In fact, we have the opposite bias. Despite the popularity of the phrase “creative destruction”, quality ladder models are much less common than expanding variety models of growth. Anyone who suggests there may be a negative externality of R&D in competitive industries gets jumped all over.

Can’t immigration be seen as a type of innovation? Immigrants bring more with them than their L.

The computer thinks for us

Angus Deaton:

Structural estimation is useful, not only for the estimates (whose credibility is often undercut by the panoply of supporting assumptions that are required to obtain them), but for understanding the empirical predictions of the theory.

Do we do this with DSGEs? We try to replicate known patterns in the data, but I don’t think we use these models to find potential new patterns.

In one of my myriad of yet-unfinished papers I report things I call “simulated comparative statics”. I can’t solve the model, so I have the computer generate numerical derivatives. People don’t like ’em. Can I market them better? “Simulated theorems”? “Generated predictions”? “Computed hypotheses”?

The positive nature of normative analysis

When you first take a grad level economics class, it occurs to you the discipline appears to be “social physics”. Where you thought the discipline was “a tool for understanding and social criticism and an instrument for intellectual enlightenment” you begin to suspect its “a tool for social engineering and an instrument for progressive politics”. The suspicion morphs into a full blown conspiracy theory when you learn the math behind the welfare theorems.

But look at how normative analysis is actually done in economics. Consumers of models compare their intuitions about how policy should work against the prescriptions of the model and use this difference to evaluate the model. “Wow, this model says money is too loose right now. I know that’s wrong, why does the model get it wrong?”

One of the reasons to reject real business cycle models is because they provide no room for macroeconomic management. The dissatisfaction with exogenous growth models is that they’re silent about institutions. There’s a heavy bias against models of wage inequality that rely on transient features of workers; the problems *must* be structural. Unemployment must be responsive to aggregate demand manipulations and so they can’t be a result of real frictions. And so on.

Despite these examples, after a couple years of doing this, I’ve come to believe that this is an acceptable way to evaluate models. There is no THE MODEL of the economy and our intuitions are basically trustworthy when it comes to social arrangements. Our intuitions are data. If a model gives counterintuitive policy implications, it bears the burden of showing us why our intuitions are incorrect.