14
Adding recession bands to Mathematica economic data plots
No comments · Posted by wildebeest in mathematica
It is relatively common to see economic data plotted with shaded bands highlighting periods of recession. It would be nice to have this feature built into Mathematica, actually it would be nice to have banded plots built into Mathematica. In the meantime here is how you do it.
Firstly have a read of an earlier article about creating banded plots.
Next, head to the National Bureau of Economic Research and get a list of official recession dates. Take those dates and make them into Mathematica date lists.
Next, take some data and pick out the segments that match recessions. This may not be straight forward because economic data is normally reported at the end of the month whereas the recession dating is from the first of the month. You could subtract a day from all the recession dates. Alternatively a work around is shown below where end of month data is converted, for the purposes of matching, to start of the next month:
tmp = data;
tmp1 = Cases[tmp, x_ /; MemberQ[#[[All, {1, 2, 3}]], x[[1, {1, 2, 3}]] /. {a_, b_, c_} -> {a, b + 1, 1}]] & /@ recessions;
tmp1 = DeleteCases[tmp1, {}];
The result is a set of lists, which are subsets of your data, that match recessions periods. You then plot them as described in the article about making banded plots. The advantage of doing it this way is that in addition to making shaded bands you can also control the color of the line, i.e. set a specific color in the banded region. Here is what the end result looks like:
An easier way however is simply to Prolog some rectangle primitives that match recession dates. Firstly, for those not aware, the x axis scale in DateListPlot is in absolute time (seconds). So make a list of absolute time recessions:
absoluteRecessions = AbsoluteTime /@ # & /@ recessions
Once you have that list make a list of “recession rectangles”:
recessionBands1= Rectangle[{First@#, yMin}, {Last@#, yMax}] & /@ absoluteRecessions
or make a function to generate the rectangles:
recessionBands2[yMin_, yMax_] := Rectangle[{First@#, yMin}, {Last@#, yMax}] & /@ absoluteRecessions
Save the list recessionBands1 because you can use it anytime by using a rule replacement to stick in the minimum and maximum y values. Here is the result:
DateListPlot[data,
ImageSize -> 600,
Prolog -> {Yellow, recessionBands /. {yMin -> -4.5, yMax -> 2.5}},
PlotRange -> All
]
If you choose to use a function recessionBands2 to make the bands then just stick in the minimum and maximum y values:
DateListPlot[data[[All, {1, 7}]],
ImageSize -> 600,
Prolog -> {Yellow, recessionBands[-4.5, 2.5]},
PlotRange -> All
]
That’s the end, but below I’ve pasted in the recession date lists and rectangles to save you all some time.
recessions={{{1857, 6, 1}, {1857, 7, 1}, {1857, 8, 1}, {1857, 9, 1}, {1857, 10,
1}, {1857, 11, 1}, {1857, 12, 1}, {1858, 1, 1}, {1858, 2,
1}, {1858, 3, 1}, {1858, 4, 1}, {1858, 5, 1}, {1858, 6, 1}, {1858,
7, 1}, {1858, 8, 1}, {1858, 9, 1}, {1858, 10, 1}, {1858, 11,
1}, {1858, 12, 1}}, {{1860, 10, 1}, {1860, 11, 1}, {1860, 12,
1}, {1861, 1, 1}, {1861, 2, 1}, {1861, 3, 1}, {1861, 4, 1}, {1861,
5, 1}, {1861, 6, 1}}, {{1865, 4, 1}, {1865, 5, 1}, {1865, 6,
1}, {1865, 7, 1}, {1865, 8, 1}, {1865, 9, 1}, {1865, 10, 1}, {1865,
11, 1}, {1865, 12, 1}, {1866, 1, 1}, {1866, 2, 1}, {1866, 3,
1}, {1866, 4, 1}, {1866, 5, 1}, {1866, 6, 1}, {1866, 7, 1}, {1866,
8, 1}, {1866, 9, 1}, {1866, 10, 1}, {1866, 11, 1}, {1866, 12,
1}, {1867, 1, 1}, {1867, 2, 1}, {1867, 3, 1}, {1867, 4, 1}, {1867,
5, 1}, {1867, 6, 1}, {1867, 7, 1}, {1867, 8, 1}, {1867, 9,
1}, {1867, 10, 1}, {1867, 11, 1}, {1867, 12, 1}}, {{1869, 6,
1}, {1869, 7, 1}, {1869, 8, 1}, {1869, 9, 1}, {1869, 10, 1}, {1869,
11, 1}, {1869, 12, 1}, {1870, 1, 1}, {1870, 2, 1}, {1870, 3,
1}, {1870, 4, 1}, {1870, 5, 1}, {1870, 6, 1}, {1870, 7, 1}, {1870,
8, 1}, {1870, 9, 1}, {1870, 10, 1}, {1870, 11, 1}, {1870, 12,
1}}, {{1873, 10, 1}, {1873, 11, 1}, {1873, 12, 1}, {1874, 1,
1}, {1874, 2, 1}, {1874, 3, 1}, {1874, 4, 1}, {1874, 5, 1}, {1874,
6, 1}, {1874, 7, 1}, {1874, 8, 1}, {1874, 9, 1}, {1874, 10,
1}, {1874, 11, 1}, {1874, 12, 1}, {1875, 1, 1}, {1875, 2,
1}, {1875, 3, 1}, {1875, 4, 1}, {1875, 5, 1}, {1875, 6, 1}, {1875,
7, 1}, {1875, 8, 1}, {1875, 9, 1}, {1875, 10, 1}, {1875, 11,
1}, {1875, 12, 1}, {1876, 1, 1}, {1876, 2, 1}, {1876, 3, 1}, {1876,
4, 1}, {1876, 5, 1}, {1876, 6, 1}, {1876, 7, 1}, {1876, 8,
1}, {1876, 9, 1}, {1876, 10, 1}, {1876, 11, 1}, {1876, 12,
1}, {1877, 1, 1}, {1877, 2, 1}, {1877, 3, 1}, {1877, 4, 1}, {1877,
5, 1}, {1877, 6, 1}, {1877, 7, 1}, {1877, 8, 1}, {1877, 9,
1}, {1877, 10, 1}, {1877, 11, 1}, {1877, 12, 1}, {1878, 1,
1}, {1878, 2, 1}, {1878, 3, 1}, {1878, 4, 1}, {1878, 5, 1}, {1878,
6, 1}, {1878, 7, 1}, {1878, 8, 1}, {1878, 9, 1}, {1878, 10,
1}, {1878, 11, 1}, {1878, 12, 1}, {1879, 1, 1}, {1879, 2,
1}, {1879, 3, 1}}, {{1882, 3, 1}, {1882, 4, 1}, {1882, 5,
1}, {1882, 6, 1}, {1882, 7, 1}, {1882, 8, 1}, {1882, 9, 1}, {1882,
10, 1}, {1882, 11, 1}, {1882, 12, 1}, {1883, 1, 1}, {1883, 2,
1}, {1883, 3, 1}, {1883, 4, 1}, {1883, 5, 1}, {1883, 6, 1}, {1883,
7, 1}, {1883, 8, 1}, {1883, 9, 1}, {1883, 10, 1}, {1883, 11,
1}, {1883, 12, 1}, {1884, 1, 1}, {1884, 2, 1}, {1884, 3, 1}, {1884,
4, 1}, {1884, 5, 1}, {1884, 6, 1}, {1884, 7, 1}, {1884, 8,
1}, {1884, 9, 1}, {1884, 10, 1}, {1884, 11, 1}, {1884, 12,
1}, {1885, 1, 1}, {1885, 2, 1}, {1885, 3, 1}, {1885, 4, 1}, {1885,
5, 1}}, {{1887, 3, 1}, {1887, 4, 1}, {1887, 5, 1}, {1887, 6,
1}, {1887, 7, 1}, {1887, 8, 1}, {1887, 9, 1}, {1887, 10, 1}, {1887,
11, 1}, {1887, 12, 1}, {1888, 1, 1}, {1888, 2, 1}, {1888, 3,
1}, {1888, 4, 1}}, {{1890, 7, 1}, {1890, 8, 1}, {1890, 9,
1}, {1890, 10, 1}, {1890, 11, 1}, {1890, 12, 1}, {1891, 1,
1}, {1891, 2, 1}, {1891, 3, 1}, {1891, 4, 1}, {1891, 5,
1}}, {{1893, 1, 1}, {1893, 2, 1}, {1893, 3, 1}, {1893, 4,
1}, {1893, 5, 1}, {1893, 6, 1}, {1893, 7, 1}, {1893, 8, 1}, {1893,
9, 1}, {1893, 10, 1}, {1893, 11, 1}, {1893, 12, 1}, {1894, 1,
1}, {1894, 2, 1}, {1894, 3, 1}, {1894, 4, 1}, {1894, 5, 1}, {1894,
6, 1}}, {{1895, 12, 1}, {1896, 1, 1}, {1896, 2, 1}, {1896, 3,
1}, {1896, 4, 1}, {1896, 5, 1}, {1896, 6, 1}, {1896, 7, 1}, {1896,
8, 1}, {1896, 9, 1}, {1896, 10, 1}, {1896, 11, 1}, {1896, 12,
1}, {1897, 1, 1}, {1897, 2, 1}, {1897, 3, 1}, {1897, 4, 1}, {1897,
5, 1}, {1897, 6, 1}}, {{1899, 6, 1}, {1899, 7, 1}, {1899, 8,
1}, {1899, 9, 1}, {1899, 10, 1}, {1899, 11, 1}, {1899, 12,
1}, {1900, 1, 1}, {1900, 2, 1}, {1900, 3, 1}, {1900, 4, 1}, {1900,
5, 1}, {1900, 6, 1}, {1900, 7, 1}, {1900, 8, 1}, {1900, 9,
1}, {1900, 10, 1}, {1900, 11, 1}, {1900, 12, 1}}, {{1902, 9,
1}, {1902, 10, 1}, {1902, 11, 1}, {1902, 12, 1}, {1903, 1,
1}, {1903, 2, 1}, {1903, 3, 1}, {1903, 4, 1}, {1903, 5, 1}, {1903,
6, 1}, {1903, 7, 1}, {1903, 8, 1}, {1903, 9, 1}, {1903, 10,
1}, {1903, 11, 1}, {1903, 12, 1}, {1904, 1, 1}, {1904, 2,
1}, {1904, 3, 1}, {1904, 4, 1}, {1904, 5, 1}, {1904, 6, 1}, {1904,
7, 1}, {1904, 8, 1}}, {{1907, 5, 1}, {1907, 6, 1}, {1907, 7,
1}, {1907, 8, 1}, {1907, 9, 1}, {1907, 10, 1}, {1907, 11,
1}, {1907, 12, 1}, {1908, 1, 1}, {1908, 2, 1}, {1908, 3, 1}, {1908,
4, 1}, {1908, 5, 1}, {1908, 6, 1}}, {{1910, 1, 1}, {1910, 2,
1}, {1910, 3, 1}, {1910, 4, 1}, {1910, 5, 1}, {1910, 6, 1}, {1910,
7, 1}, {1910, 8, 1}, {1910, 9, 1}, {1910, 10, 1}, {1910, 11,
1}, {1910, 12, 1}, {1911, 1, 1}, {1911, 2, 1}, {1911, 3, 1}, {1911,
4, 1}, {1911, 5, 1}, {1911, 6, 1}, {1911, 7, 1}, {1911, 8,
1}, {1911, 9, 1}, {1911, 10, 1}, {1911, 11, 1}, {1911, 12,
1}, {1912, 1, 1}}, {{1913, 1, 1}, {1913, 2, 1}, {1913, 3,
1}, {1913, 4, 1}, {1913, 5, 1}, {1913, 6, 1}, {1913, 7, 1}, {1913,
8, 1}, {1913, 9, 1}, {1913, 10, 1}, {1913, 11, 1}, {1913, 12,
1}, {1914, 1, 1}, {1914, 2, 1}, {1914, 3, 1}, {1914, 4, 1}, {1914,
5, 1}, {1914, 6, 1}, {1914, 7, 1}, {1914, 8, 1}, {1914, 9,
1}, {1914, 10, 1}, {1914, 11, 1}, {1914, 12, 1}}, {{1918, 8,
1}, {1918, 9, 1}, {1918, 10, 1}, {1918, 11, 1}, {1918, 12,
1}, {1919, 1, 1}, {1919, 2, 1}, {1919, 3, 1}}, {{1920, 1,
1}, {1920, 2, 1}, {1920, 3, 1}, {1920, 4, 1}, {1920, 5, 1}, {1920,
6, 1}, {1920, 7, 1}, {1920, 8, 1}, {1920, 9, 1}, {1920, 10,
1}, {1920, 11, 1}, {1920, 12, 1}, {1921, 1, 1}, {1921, 2,
1}, {1921, 3, 1}, {1921, 4, 1}, {1921, 5, 1}, {1921, 6, 1}, {1921,
7, 1}}, {{1923, 5, 1}, {1923, 6, 1}, {1923, 7, 1}, {1923, 8,
1}, {1923, 9, 1}, {1923, 10, 1}, {1923, 11, 1}, {1923, 12,
1}, {1924, 1, 1}, {1924, 2, 1}, {1924, 3, 1}, {1924, 4, 1}, {1924,
5, 1}, {1924, 6, 1}, {1924, 7, 1}}, {{1926, 10, 1}, {1926, 11,
1}, {1926, 12, 1}, {1927, 1, 1}, {1927, 2, 1}, {1927, 3, 1}, {1927,
4, 1}, {1927, 5, 1}, {1927, 6, 1}, {1927, 7, 1}, {1927, 8,
1}, {1927, 9, 1}, {1927, 10, 1}, {1927, 11, 1}}, {{1929, 8,
1}, {1929, 9, 1}, {1929, 10, 1}, {1929, 11, 1}, {1929, 12,
1}, {1930, 1, 1}, {1930, 2, 1}, {1930, 3, 1}, {1930, 4, 1}, {1930,
5, 1}, {1930, 6, 1}, {1930, 7, 1}, {1930, 8, 1}, {1930, 9,
1}, {1930, 10, 1}, {1930, 11, 1}, {1930, 12, 1}, {1931, 1,
1}, {1931, 2, 1}, {1931, 3, 1}, {1931, 4, 1}, {1931, 5, 1}, {1931,
6, 1}, {1931, 7, 1}, {1931, 8, 1}, {1931, 9, 1}, {1931, 10,
1}, {1931, 11, 1}, {1931, 12, 1}, {1932, 1, 1}, {1932, 2,
1}, {1932, 3, 1}, {1932, 4, 1}, {1932, 5, 1}, {1932, 6, 1}, {1932,
7, 1}, {1932, 8, 1}, {1932, 9, 1}, {1932, 10, 1}, {1932, 11,
1}, {1932, 12, 1}, {1933, 1, 1}, {1933, 2, 1}, {1933, 3,
1}}, {{1937, 5, 1}, {1937, 6, 1}, {1937, 7, 1}, {1937, 8,
1}, {1937, 9, 1}, {1937, 10, 1}, {1937, 11, 1}, {1937, 12,
1}, {1938, 1, 1}, {1938, 2, 1}, {1938, 3, 1}, {1938, 4, 1}, {1938,
5, 1}, {1938, 6, 1}}, {{1945, 2, 1}, {1945, 3, 1}, {1945, 4,
1}, {1945, 5, 1}, {1945, 6, 1}, {1945, 7, 1}, {1945, 8, 1}, {1945,
9, 1}, {1945, 10, 1}}, {{1948, 11, 1}, {1948, 12, 1}, {1949, 1,
1}, {1949, 2, 1}, {1949, 3, 1}, {1949, 4, 1}, {1949, 5, 1}, {1949,
6, 1}, {1949, 7, 1}, {1949, 8, 1}, {1949, 9, 1}, {1949, 10,
1}}, {{1953, 7, 1}, {1953, 8, 1}, {1953, 9, 1}, {1953, 10,
1}, {1953, 11, 1}, {1953, 12, 1}, {1954, 1, 1}, {1954, 2,
1}, {1954, 3, 1}, {1954, 4, 1}, {1954, 5, 1}}, {{1957, 8,
1}, {1957, 9, 1}, {1957, 10, 1}, {1957, 11, 1}, {1957, 12,
1}, {1958, 1, 1}, {1958, 2, 1}, {1958, 3, 1}, {1958, 4,
1}}, {{1960, 4, 1}, {1960, 5, 1}, {1960, 6, 1}, {1960, 7,
1}, {1960, 8, 1}, {1960, 9, 1}, {1960, 10, 1}, {1960, 11,
1}, {1960, 12, 1}, {1961, 1, 1}, {1961, 2, 1}}, {{1969, 12,
1}, {1970, 1, 1}, {1970, 2, 1}, {1970, 3, 1}, {1970, 4, 1}, {1970,
5, 1}, {1970, 6, 1}, {1970, 7, 1}, {1970, 8, 1}, {1970, 9,
1}, {1970, 10, 1}, {1970, 11, 1}}, {{1973, 11, 1}, {1973, 12,
1}, {1974, 1, 1}, {1974, 2, 1}, {1974, 3, 1}, {1974, 4, 1}, {1974,
5, 1}, {1974, 6, 1}, {1974, 7, 1}, {1974, 8, 1}, {1974, 9,
1}, {1974, 10, 1}, {1974, 11, 1}, {1974, 12, 1}, {1975, 1,
1}, {1975, 2, 1}, {1975, 3, 1}}, {{1980, 1, 1}, {1980, 2,
1}, {1980, 3, 1}, {1980, 4, 1}, {1980, 5, 1}, {1980, 6, 1}, {1980,
7, 1}}, {{1981, 7, 1}, {1981, 8, 1}, {1981, 9, 1}, {1981, 10,
1}, {1981, 11, 1}, {1981, 12, 1}, {1982, 1, 1}, {1982, 2,
1}, {1982, 3, 1}, {1982, 4, 1}, {1982, 5, 1}, {1982, 6, 1}, {1982,
7, 1}, {1982, 8, 1}, {1982, 9, 1}, {1982, 10, 1}, {1982, 11,
1}}, {{1990, 7, 1}, {1990, 8, 1}, {1990, 9, 1}, {1990, 10,
1}, {1990, 11, 1}, {1990, 12, 1}, {1991, 1, 1}, {1991, 2,
1}, {1991, 3, 1}}, {{2001, 3, 1}, {2001, 4, 1}, {2001, 5,
1}, {2001, 6, 1}, {2001, 7, 1}, {2001, 8, 1}, {2001, 9, 1}, {2001,
10, 1}, {2001, 11, 1}}, {{2007, 12, 1}, {2008, 1, 1}, {2008, 2,
1}, {2008, 3, 1}, {2008, 4, 1}, {2008, 5, 1}, {2008, 6, 1}, {2008,
7, 1}, {2008, 8, 1}, {2008, 9, 1}, {2008, 10, 1}, {2008, 11,
1}, {2008, 12, 1}, {2009, 1, 1}, {2009, 2, 1}, {2009, 3, 1}, {2009,
4, 1}, {2009, 5, 1}, {2009, 6, 1}, {2009, 7, 1}, {2009, 8,
1}, {2009, 9, 1}, {2009, 10, 1}, {2009, 11, 1}, {2009, 12,
1}, {2010, 1, 1}, {2010, 2, 1}, {2010, 3, 1}}};
recessionBands[yMin_, yMax_] := {Rectangle[{-1343865600, yMin}, {-1296518400, yMax}],
Rectangle[{-1238630400, yMin}, {-1217635200, yMax}],
Rectangle[{-1096675200, yMin}, {-1012521600, yMax}],
Rectangle[{-965174400, yMin}, {-917827200, yMax}],
Rectangle[{-828403200, yMin}, {-657590400, yMax}],
Rectangle[{-562896000, yMin}, {-462931200, yMax}],
Rectangle[{-405129600, yMin}, {-370828800, yMax}],
Rectangle[{-299894400, yMin}, {-273628800, yMax}],
Rectangle[{-220838400, yMin}, {-176256000, yMax}],
Rectangle[{-128908800, yMin}, {-81561600, yMax}],
Rectangle[{-18489600, yMin}, {28857600, yMax}],
Rectangle[{84067200, yMin}, {144547200, yMax}],
Rectangle[{231206400, yMin}, {265507200, yMax}],
Rectangle[{315532800, yMin}, {378604800, yMax}],
Rectangle[{410227200, yMin}, {470620800, yMax}],
Rectangle[{586310400, yMin}, {604627200, yMax}],
Rectangle[{631065600, yMin}, {678326400, yMax}],
Rectangle[{736128000, yMin}, {773020800, yMax}],
Rectangle[{844041600, yMin}, {878256000, yMax}],
Rectangle[{933465600, yMin}, {1046476800, yMax}],
Rectangle[{1177977600, yMin}, {1212192000, yMax}],
Rectangle[{1422748800, yMin}, {1443657600, yMax}],
Rectangle[{1541030400, yMin}, {1569888000, yMax}],
Rectangle[{1688169600, yMin}, {1714435200, yMax}],
Rectangle[{1817078400, yMin}, {1838073600, yMax}],
Rectangle[{1901232000, yMin}, {1927670400, yMax}],
Rectangle[{2206310400, yMin}, {2235254400, yMax}],
Rectangle[{2329948800, yMin}, {2371852800, yMax}],
Rectangle[{2524521600, yMin}, {2540246400, yMax}],
Rectangle[{2571782400, yMin}, {2613945600, yMax}],
Rectangle[{2855779200, yMin}, {2876774400, yMax}],
Rectangle[{3192393600, yMin}, {3213561600, yMax}],
Rectangle[{3405456000, yMin}, {3476390400, yMax}]};




