Malthus.js

Classical population dynamics in a form you can play with (02.12.13)

Below is an attempt at animating the changes to equilibrium levels of population and income per capita under the Malthusian model of population dynamics. The sliders let you tweak the values of the models parameters, and the current equilibrium is shown in yellow...

Exogenous Model Parameters:

Income Per Capita
Population Level
Income Elasticity of the Birth Rate (ε):
Income Elasticity of the Death Rate ():
Fertility Rate (f):
Mortality Rate (m):
Labor Elasticity of Income (-α):
Total Factor Productivity (A):

Read More...


Indexing Dictionaries with namedtuple

A basic class for an easily subsetable dictionary using Python's namedtuple as keys for multidimensional indexing (01.04.14)

Dictionaries/HashMaps/HashTables are great. Having items stored and returned at constant time complexity makes building up data structures, and finding items within them, a breeze.

But what if my item has multiple features? What if I want to get all the items that have a certain values for some of those features? Ideally, I could index a dictionary with a few different attributes (in the form of another dictionary) and get back the item (if a full key is passed), or a set of items if a partial key is passed. An example usage of what I want might look like this...

    
>>> T = {}
>>> T[{"a":1,"b":True, "c":"string"}] = 4
>>> T[{"a":2,"b":True, "c":"string"}] = 5
>>> T[{"a":3,"b":False,"c":"string"}] = 6
>>> T[{"b":True}]
{{"a":1, "c":"string"}: 4, {"a":2, "c":"string"}: 5}
>>> T[{"a":3,"b":False,"c":"string"}]
6
    

If the above code is executed, the interpreter will inform you that dictionaries are unhashable, as they are mutable, and thus cannot be used as keys. However, if the set of attributes are known beforehand, this can be fixed with a special Python data structure. Bringforth the elegance of the namedtuple.

Read More...


Constructing Color Gradients with Python

Investigating a few "Pythonic" ways to calculate gradients across given input colors (12.03.13)

Color is one of the most powerful tools for conveying information about data. Differences in color can inspire or imply emotions (positive or negative), give a sense of magnitude (dark and dense, or light and sparse), or even hint at political persuasion (redness" and "blueness" of states on a map).

image/svg+xml Control Colors

3D Quadratic Bezier Curve with Corresponding Gradient

One way to convey continuous variation through colors is by using a gradient. Most graphics applications provide an easy and intuitive way to apply a gradient to a project or dataset. The ubiquitous Microsoft Excel is an easy example, with its suprisingly useful conditional formatting. Interested in how these spectra are actually constructed, I decided to try out a few ways of manually calculating color gradients using Python, given some desired input colors. Here's what I came up with!

Read More...


Dynamic reports with "StaTeX"

Data analysis, LaTeX source generation, and pdf report compiling all in a single Stata .do file (10.01.13)

As a big fan of LaTeX and daily slave to Stata, I've put a little thought into finding easy ways to bring the aesthetics and clarity of the fromer to the output of the later. In particular, there are two great Stata modules (estout and texdoc, both available through SSC) which, when combined with Stata's shell interface, make it easy to compile professional and organized output. In this post, I present a sample (OSX oriented) code structure for a single .do file which conducts analysis, generates TeX source code, and then compiles that source to pdf.

Streamlined TeX'n Data Report

The ".do it all" file:   Data → {Stata + LaTeX + Shell} → Report

Recently, I've been working on a few projects for Urban which involve constructing large multivariate models. The process includes constant tweaking of various parameters such as sample restrictions and control variables. As a project progresses, keeping track of these changes can be difficult. Keeping track of the evolving components and outcomes of a model as it is modified can make the construction process simpler. As shown here, this can be easily done by creating a LaTeX output document upon each run of the analysis do file.

Read More...


10 million steps with R

Random walk data generation and plotting in R3 (9.01.13)

I have a particular fascination with code that is computationally intense, yet very dense in terms of lines. A sort of David / Goliath kinda thing, where a few choice keystrokes can take hours to process. Additionally, when the computation can result in aesthetically pleasing output, like a 3D plot of a random walk using R's rgl package, it's hard to resist tapping away on the keyboard for a bit.

Plotting a 3D random walk with plot3d()

One simple and easy example of this sort of David-script is random walk data generation. With a bunch of free time this Labor-Day Weekend, I decided to take R for a long walk. Here is the code for a single step:

Read More...