Wednesday, March 27, 2013

“Building ractives is so addictive it should be illegal!”

clickme is an amazing R package. I was not sure what to expect when I first saw Nacho Caballero's announcement. I actually was both skeptical and intimidated, but neither reaction was justified. The examples prove its power, and his wiki tutorials ease the noobie difficulties. Very similar to shiny, clickme serves as an integration point for html, javascript (especially d3), and R. While clickme does not allow the R websocket interactivity that shiny does, its more concentrated focus on quick reproducibility and sharing makes it a very useful tool. This is very much in the spirit of http://dexvis.wordpress.com/ Reusable Charts. ractives defined as

(short for interactives-a hat tip to Neal Stephenson), which are simple folder structures that contain a template file used to populate the JS code with R input data

provide the structure for clickme to produce an html file from

  1. a template in R markdown (template.rmd)
  2. a translator R script (translator.r)
  3. a data source
  4. external scripts (probably javascript) and styles (.css).

Inspired by the clickme longitudinal heatmap example, I just had to try to create my own ractive. I thought Mike Bostock's line chart example would serve as a nice template for my first ractive. The data not surprisingly will come from the R finance package PerformanceAnalytics dataset named managers. With very minor modifications to the Bostock source and a simple custom R script translator (translator.R shown below), we have everything we need for this ractive, which I will call multiline.

#' Translate the data object to the format expected by current template
#'
#' @param data input data object
#' @param opts options of current template
#' @return The opts variable with the opts$data variable filled in
translate <- function(data, opts = NULL) {
require(df2json)

# I would like to generalize this to handle both price and return right
# now just handles return clickme template.Rmd javascript can handle
# prices or cumulative so we will send cumulative which can serve as price

# remove na
data[is.na(data)] <- 0
# get cumulative growth
data <- cumprod(1 + data)


# convert to data frame
data.df <- data.frame(cbind(format(index(data), "%Y-%m-%d"), coredata(data)))
colnames(data.df)[1] = "date"
# melt the data frame so we have our data in long form
data.melt <- melt(data.df, id.vars = 1, stringsAsFactors = FALSE)
colnames(data.melt) <- c("date", "indexname", "price")
# remove periods from indexnames to prevent javascript confusion these .
# usually come from spaces in the colnames when melted
data.melt[, "indexname"] <- apply(matrix(data.melt[, "indexname"]), 2, gsub,
pattern = "[.]", replacement = "")
opts$data <- df2json(data.melt)
opts
}


Now to create our first clickme html page, we just need a couple lines of code in R.

# if not already installed, uncomment the tow lines below
# library(devtools) install_github('clickme', 'nachocab')

require(clickme)
# set location where you put your multiline ractive
set_root_path("your-path-goes-here/ractives")
require(PerformanceAnalytics)
data(managers) #although I use managers, really any xts series of returns will work
clickme(managers, "multiline")

Then, we have a web page that will create an interactive d3 line chart using the cumulative growth of the managers return series. If you do not see the embed below, then please follow the link.


Eventually, it will be very nice to have an entire gallery of amazing ractives.


git repo

Friday, March 22, 2013

Production Quality Report with R and knitr on Yen

Sometimes I actually use my experiments for real work.  For example, I wanted to send an update  on the Japanese Yen.  This was a great opportunity to use the chart created in Shading and Points with xtsExtra plot.xts.

I was fairly pleased with the finished product (click here if embed does not show).


R sweave file from GIST:
\documentclass[nohyper,justified]{tufte-handout}
\usepackage{xltxtra,fontspec,xunicode}
\usepackage{sectsty} %% change fonts for sections
\setmainfont{Source Sans Pro Light}
\begin{document}
\pagenumbering{gobble}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt}
<<include=FALSE>>=
opts_chunk$set(concordance=TRUE)#,dev='tikz')
@
<<eval=TRUE,echo=FALSE,results='hide',warning=FALSE,message=FALSE,error=FALSE>>=
require(RColorBrewer)
require(quantmod)
require(PerformanceAnalytics)
require(xtsExtra)
source("C:\\Users\\Kent.TLEAVELL_NT\\Dropbox\\Leavell\\2013-02 yen trade analysis\\plotxts_custom.r")
jpy <- getSymbols("DEXJPUS",src="FRED",auto.assign=FALSE)
startdates = c("2010-08-20",
"2011-02-07",
"2011-03-30",
"2011-10-14",
# "2012-04-03",
"2012-06-21",
"2012-10-25"
)
enddates = c("2010-10-08",
"2011-03-02",
"2011-06-01",
"2012-05-31",
"2012-08-22",
format(Sys.Date(),"%Y-%m-%d"))
custom.panel <- function(index,x,...) {
default.panel(index,x,...)
abline(h=pretty(c(par("yaxp")[1],par("yaxp")[2]),n=par("yaxp")[3]),col="gray60",lty=3)
abline(h=par("usr")[3], col="black")
axis(side=2,col="gray60",col.axis="black",lwd=0,lwd.ticks=FALSE,las=1,
at=pretty(c(par("yaxp")[1],par("yaxp")[2]),n=abs(par("yaxp")[3])),
labels=pretty(c(par("yaxp")[1],par("yaxp")[2]),n=abs(par("yaxp")[3])))
points(x=index[which(index(x) %in% as.Date(startdates))],
y=x[which(index(x) %in% as.Date(startdates)),],cex=1,pch=19,
col="darkolivegreen3")
points(x=index[which(index(x) %in% as.Date(enddates))],
y=x[which(index(x) %in% as.Date(enddates)),],cex=1,pch=19,
col="indianred3")
#to add reference lines to indicate entry level
#I'm not sure it is necessary but if you like it uncomment below
#for(i in 1:(length(startdates))) {
# segments(x0=index[which(index(x) == as.Date(startdates[i]))],
# x1=index[which(index(x) == as.Date(enddates[i]))],,
# y0=x[which(index(x) == as.Date(startdates[i])),],
# y1=x[which(index(x) == as.Date(startdates[i])),])
#}
}
@
{\huge Japanese Yen as a Macro Opportunity}
\newline
{\large ME}
\newline
{\large March 13, 2013}
\newline
\newline
\section{Opportunities by Time Scale}
We subdivide market opportunities into three time-scale categories--short-term, cyclical, and secular. The -------- philosophy is designed to focus on macro secular opportunities. -------- ignores short-term and only pursues cyclical in the absence of or as a supplement to secular. Macro or secular trades require patience and persistence. For this reason, these macro trades get missed by more mainstream and popular investment approaches. These opportunites are rare and infrequent but lucrative. I often say that one of the best gauges of the quality of a macro trade is how big a book I can write. The bigger the book the more the evidence I have and the more time I have had to write. This is certainly far different from the quick get-rich schemes that dominate the media and get popularized by market lore.
\section{Japanese Yen (\textyen) as an Example of Macro Opportunity}
The Japanese Yen (\textyen) provides a very instructive view of a secular macro opportunity. We certainly don't want to upset the fickle and vindictive market gods, but I would like to use this as a good example of how we approach these opportunities.
\noindent
The Japanese \textyen \hspace{1pt} strengthened by 370\% versus the US\$ from 1970 to 2009, but just because something goes up a lot over a long time does not mean it will necesarily come down.
\begin{figure}[!htb]
\caption{Japanese \textyen \hspace{1pt} / US\$ Since 1970\newline
\scriptsize{
While not specifically relevant to this chart, I include the shaded areas and dots to put the recent holding periods in the context of a much larger process. The light gray shaded areas represent holding periods for --------. Green dots are entry dates, and red dots are exit dates.\newline\newline\newline source: Federal Reserve Economic Data (FRED) from the Federal Reserve Bank of St. Louis}}
\vspace{-30pt}
<<echo=FALSE,eval=TRUE,fig.width=12,fig.height=6,warning=FALSE,message=FALSE,error=FALSE>>=
plot.xts(1/jpy,
col = brewer.pal(9,"Blues")[c(7)], #get two blues that will look ok
lwd = 2, #line width; will do 2
las = 1, #do not rotate y axis labels
bty="n",
auto.grid=FALSE,
major.format="%b %Y",
minor.ticks=FALSE,
col.axis="transparent",
yax.loc="none",
cex.axis=0.8,
panel=custom.panel,
# auto.legend = TRUE, #let plot.xts do the hard work on the legend
# legend.loc = c("topleft",NA, NA), #just do legend on the first screen
# legend.pars = list(bty = "n", horiz=TRUE), #make legend box transparent and legend horizontal
main = NA, #will do title later so we have more control
blocks = list(start.time=startdates, #overlay blocks for periods owned
end.time=enddates,col="gray90"))
@
\end{figure}
\vspace{-10pt}
\section{Catalysts for Reversal}
We must explore the underlying conditions for the 40 year trend, and then think through the potential catalysts for a reversal. Often, these initial catalysts are short-term mean reversion and fundamental overvaluation generated by the last wave of fickle buyers who buy just chasing momentum. During this last stage, the market prefers to extrapolate the trend ignoring slow moving barely perceptible environmental shifts. Throughout market history, we find numerous examples where the market misses these shifts.
\noindent
When the supporting conditions for the move have eroded while the market has overvalued/undervalued the instrument on both an absolute and relative basis, we begin to closely monitor the price for moves in the direction opposite to the trend. A secular move of this magnitude takes a very long time to turn. These short term moves are generally met with market disbelief or apathy as sellers have grown weary and reluctant. However, the combination of overvaluation and erosion of supportive conditions renders an opportunity ripe for positive or self-fulfilling feedback.
\section{Managing the Entry}
Since perfect timing of the top/bottom is impossible, we very systematically begin to trade. The last thing we want to happen is to lose too much money or lose confidence by the time the move finally develops, so we are very patient and persistent. We began this process in Fall 2010 selling the Japanese \textyen \hspace{1pt} as shown in the chart (reversed to US\$/\textyen) below, so up is good. Each time we shorted (shaded areas in the chart) we would accept very little risk and exit quickly if the price moved aversely.
\begin{figure}
\caption{US\$ / Japanese \textyen \hspace{1pt} Since Dec. 2009\newline
\scriptsize{
Light gray shaded areas represent holding periods for --------. Green dots are entry dates, and red dots are exit dates.\newline\newline\newline source: Federal Reserve Economic Data (FRED) from the Federal Reserve Bank of St. Louis}}
\vspace{-30pt}
<<echo=FALSE,eval=TRUE,fig.width=12,fig.height=6,warning=FALSE,message=FALSE,error=FALSE>>=
plot.xts(jpy["2009-12::"],
col = brewer.pal(9,"Blues")[c(7)], #get two blues that will look ok
lwd = 2, #line width; will do 2
las = 1, #do not rotate y axis labels
bty="n",
auto.grid=FALSE,
major.format="%b %Y",
major.ticks="years",
minor.ticks=FALSE,
col.axis="transparent",
yax.loc="none",
cex.axis=0.8,
panel=custom.panel,
# auto.legend = TRUE, #let plot.xts do the hard work on the legend
# legend.loc = c("topleft",NA, NA), #just do legend on the first screen
# legend.pars = list(bty = "n", horiz=TRUE), #make legend box transparent and legend horizontal
main = NA, #will do title later so we have more control
blocks = list(start.time=startdates, #overlay blocks for periods owned
end.time=enddates,col="gray90"))
@
\end{figure}
\vspace{-10pt}
\noindent
Remember for positive feedback to engage the first thing that needs to happen is a price move in the appropriate direction. Our current short is actually the 6th time we have shorted the \textyen \hspace{1pt}. Now that the first step in the positive feedback loop has occurred, we become much more stubborn and insist on fully benefitting from a much more substantial and long-term process. If we refer back to the long-term chart, the recent move is barely perceptible.
\section{Once In a Lifetime}
During an investing career of 10-30 years, macro opportunities might only occur 1-5 times. Unless an investor specifically focuses on studying these rare events, missing these "once-in-a-lifetime" opportunities is common. Fortunately, we have centuries of market history that can instruct us to discover, pursue, and hopefully profit from these unique occurrences.
\begin{quote}
After spending many years in Wall Street and after making and losing millions of dollars I want to tell you this: It never was my thinking that made big money for me. It was always my sitting. Got that? My sitting tight! It is no trick at all to be right on the market...Men who can both be right and sit tight are uncommon. I found it one of the hardest things to learn. \par\emph{Reminiscences of a Stock Operator\newline by Edwin Lefevre}
\end{quote}
\begin{center}
\line(1,0){450pt}
\end{center}
\small\textbf{Past performance does not guarantee future results. This should not be considered investment advice.}
\end{document}

#R knitr in Production for Client Report


An example of how I use R and knitr with latex to produce a client update in pdf.

To run:

require(knitr)

knit2pdf("/path/2013-03-08yen-anonymous.rnw", compiler = "xelatex")
view raw Readme.md hosted with ❤ by GitHub

Thursday, March 21, 2013

Dust off 130 Year Old Gold Books on Google Bookshelf

The very fine paper

The Golden Dilemma (January 8, 2013)
Erb, Claude B. and Harvey, Campbell R.
Available at SSRN: http://ssrn.com/abstract=2078535

discussed in this post How Did I Miss “The Golden Dilemma”? motivated me to revisit some really old books on gold that I had added to my Google bookshelf. I am always surprised by how centuries old finance and economics conversations still sound so relevant. Even more interesting is how many of the questions remain unanswered. My new favorite discussion on gold and deflation occurs in Gold and Prices Since 1873 by James Laurence Laughlin and starts like this (emphasis and links mine):

§ I. Much of the difference of opinion as to the significance of recent movements of prices is due to the fact that the value of gold is a ratio which varies with a variation in either of its terms. Whether commodities fall in relation to gold or gold rises in relation to commodities, in either case the value of gold has risen. The same phenomena, therefore, may be due to radically different causes. So that, admitting the fall of prices, it is said, on the one hand, that the rise in the value of gold is due to some cause affecting gold itself, such as scarcity; and, on the other hand, it is claimed that the fall in prices is due to causes connected solely with commodities, and not with gold.

The believers in the scarcity value of gold substantiate their position by reference to the falling off in the annual production of gold; the unusual demands for gold since 1873, by Germany, Italy, and the United States; stringencies in the money market; the increased use of gold in the arts; the claim that the fall of prices is general; the exceptional character of the depression of trade since 1873; the general existence of low wages, profits, and rents; and the absence of any progress since 1873 in the means of economizing gold and silver. These opinions have been prominently associated with Mr. Robert Giffen, the statistician of the English Board of Trade, and Mr. Goschen, the present Chancellor of the Exchequtr; while the evident connection of the main proposition with bimetallism has given it a semi-political character, and many supporters in both Europe and America.

§ II. Inasmuch as the rise in the value of gold since 1873 is in proportion to the fall of prices, it is a matter of some importance to look critically at the facts in regard to prices. With this object in view, the more important tables of prices since 1850 have been collected in the Appendix, with explanations as to the methods of computation, sources, and reliability. It is hoped that a comparison of the diverse methods and results of these tables will serve a useful purpose.

Hitherto, the figures of the London Economist for twenty-two articles have been almost universally used as evidence in regard to the movement of prices; but it is time that the worship of this fetich should cease.* Of late, much more trustworthy tables have been published.

I very much like the author's approach, which is to consider both the numerator and denominator in a price fraction and to gather as much data as possible rather than rely on one potentially flawed dataset. By page 3 we are already presented with this wonderful chart on prices/inflation in Great Britain, France, Germany, and the United States. These extra sources of price data allow us to frame the discussion much more objectively.

[Chart 1 from the Table]

 

In the spirit of the author's approach, I wanted to reproduce the chart presented above in R and also compare the US Burchard's table with CPI and GDP deflator data from MeasuringWorth. I tried to take the easy route with OCR, but eventually manually entered the price tables from the appendix.

plot of chunk unnamed-chunk-1

The chart was well done and really could not be improved beyond color and labels.

As I have mentioned before, MeasuringWorth is a wonderful source of long-term economic and financial data. I wanted to compare Burchard's US table of prices to MeasuringWorth's US GDP deflator and CPI. The Civil War significantly destabilized prices. The effect is much more pronounced in the MeasuringWorth data.

plot of chunk unnamed-chunk-2

The author discusses the shortcomings of the Burchard prices in Appendix Table M.

An examination of the Finance Report tables indicates that they were not compiled with great care. The price of an article will run along without change from month to month; then, suddenly, it will rise or fall sharply. The prices of articles that normally would fluctuate together (pig and bar iron, butter and cheese) show a very loose correspondence. Moreover, the same articles do not appear from year to year. An article will be quoted for a number of years, will then disappear, and later, perhaps, will reappear…Notwithstanding these serious imperfections, we reprint Mr. Burchard's figures for the years 1850-84, since they are the only continuous figures of average prices in the United States.

The author organizes his remaining arguments into 8 additional sections, which I have titled. Also, I will give my favorite passage from each section. Interestingly, many of these same arguments apply directly to the MMT world of today with the only exception being the absence of a gold standard.

 

Section 3: The Supply of Both Money and Credit Must Be Considered

In truth, in society as it exists to-day, the general level of prices for considerable periods (sufficiently long to permit the effect of changes in the business habits of the community, or changes in the existing stock of gold, to be felt) must depend upon a combination of the quantity of money with the various forms of credit. The two are inextricably bound together. So, therefore, the level of prices (so far as it is affected by the offer of purchasing power) depends on the expansion or contraction of two factors, quantity of money and credit, each of which may change to a considerable extent independently of each other. Both may increase or diminish together, or the gain of one may offset the loss of the other.

Section 4: Other Events Affecting Prices

The extraordinary and exceptional demand for commodities in periods of war, at the very time of the great destruction of wealth, produced an unhealthy state of affairs; but on the outside all seemed fair, and men had begun to believe that prices were fated always to rise. The speculation in metals (see Chart II.) in 1873 was of an unparalleled kind…There were all the evidences of an unhealthy and abnormal condition of affairs. But the unchecked demand, when the actual power to buy had been greatly impaired, could not go on forever. When it was once found that men had been creating liabilities beyond their means to meet them, the end had come. The crisis of 1873 was the painful return to a consciousness of the real situation, after a prolonged fever of speculation for nearly twenty years, which had spread over many countries. The effects were the more serious because the disease had got such great headway.

Section 5: Improvements in Industrial Process and New Supplies of Commodities

The period following a great financial upheaval is naturally crowded with improvements in processes and in methods of lowering the cost of production. Necessity becomes the mother of invention. The extent to which producers have been driven by the fierce competition since 1873 to cheapen production leads to the inquiry how far the fall of prices can be accounted for by influences connected solely with commodities, and not with gold. If these influences have been widely extended, it will be strong evidence that the scarcity of gold has had less effect than some suppose.

Section 6: Analysis of Prices by Commodity Type

Whether to draw inferences as to a scarcity of gold from forty-nine articles, or to infer that gold was abundant, according to the prices of fifty-one articles, is an awkward dilemma for those who think that prices give direct evidence as to the quantity of money. As Forsell remarks, the theory of a scarcity of gold is incompatible with the rise * in price of so many commodities.

Section 7: Supply of Gold

From these figures, it will be seen that the reserves in the banks of the civilized world show a very remarkable increase in gold. Although the total note circulation was increased 29 per cent., the gold in the reserves was increased 75 per cent., while the silver was also increased 25 per cent. In 1870-74, the gold reserves amounted to 28 per cent. of the total note circulation, and constituted 64 per cent. of all the specie reserves. In 1885, the gold bore a larger ratio to a larger issue of paper, or 41 per cent. of the total note circulation; and, in spite of unusual accumulations of silver (in the Bank of France, for example), the gold formed 71 per cent. of the specie reserves. This is a very significant showing. What it means, without a shadow of doubt, is that the supply of gold is so abundant that the character and safety of the note circulation have been improved in a signal manner. In 1871-74 there was $1 of gold for every $3.60 of paper circulation.* In 1885 there was 11 of gold for every $2.40.

Section 8: Supply of Credit

Specie to the amount of 1800,000,000 has gone into circulation in the form of note issues, representing an equivalent amount of specie; but gold has not been economized by the use of credit in the form of notes. While the total circulation of these countries has increased 35 per cent., the paper has been much better protected…The paper currency of every country except Russia has gained in security, together with a large increase in many of the countries. The gold supplies have not merely permitted an enlarged note circulation, but have furnished a much better protection to that increased issue.

Section 9: Price of Land Labor

It will be recalled at once, in regard to rents, that a marked characteristic of the period since 1873 has been the opening up of new and fertile lands, whose products have been transported at a greatly diminished rate. But this in itself is a reason why lands in the older countries should be thrown out of cultivation, and why rents should be lowered. This phenomenon, then, can be accounted for on other grounds than the scarcity of gold…The fact that wages have risen tends to confirm the belief that the fall of prices is due chiefly to the introduction of improvements.

Section 10: Conclusion

To assume that because the fall of prices coincided with the demonetization of silver it was due to an appreciation of gold, without considering whether the coincident phenomena were traceable to entirely distinct causes, is to fall into the fallacy of post hoc propter hoc. The forces which fix the level of prices at any time, moreover, are far too complex to admit of the inference that, because prices have fallen seriously, gold has become scarce.


Gold and Prices Since 1873 by James Laurence Laughlin is a reminder of how our current is wedded to our past. I often feel like the unprecedented actions of the world's central banks have broken the link to centuries of financial history. Rather, it seems they have simply temporarily suspended the immutable laws of humans and money and maybe “The effects were the more serious because the disease had got such great headway.” will haunt us.


I ran out of time and could not do the visualization in d3. Anybody that is motivated, here is a way to get the JSON:

# for the ambitious and more motivated than me, here is how to easily get
# the JSON to translate the graph to an interactive http://d3js.org
require(rjson)
toJSON(list(Date = format(index(priceTables), "%Y-%m-%d"), as.list(priceTables)))




Post entirely generated from R markdown for reproducibility:

Dust off 130 Year Old Gold Books on Google Bookshelf
========================================================
The very fine paper
<BLOCKQUOTE><P><STRONG>The Golden Dilemma</STRONG> (January 8, 2013)<BR>Erb, Claude B. and Harvey, Campbell R.<BR>Available at SSRN: <A href="http://ssrn.com/abstract=2078535">http://ssrn.com/abstract=2078535</A></P></BLOCKQUOTE>
discussed in this post [How Did I Miss “The Golden Dilemma”?](http://timelyportfolio.blogspot.com/2013/03/how-did-i-miss-golden-dilemma.html) motivated me to revisit some really old books on gold that I had added to my Google bookshelf. I am always surprised by how centuries old finance and economics conversations still sound so relevant. Even more interesting is how many of the questions remain unanswered. My new favorite discussion on gold and deflation occurs in [Gold and Prices Since 1873 by James Laurence Laughlin](http://books.google.com/books?id=UFMuAAAAYAAJ&printsec=frontcover#v=thumbnail&q&f=false) and starts like this (emphasis and links mine):
>§ I. Much of the difference of opinion as to the significance of recent movements of prices is due to the fact that the **value of gold is a ratio which varies with a variation in either of its terms**. Whether commodities fall in relation to gold or gold rises in relation to commodities, in either case the value of gold has risen. The same phenomena, therefore, may be due to radically different causes. So that, admitting the fall of prices, it is said, on the one hand, that the **rise in the value of gold is due to some cause affecting gold itself, such as scarcity; and, on the other hand, it is claimed that the fall in prices is due to causes connected solely with commodities, and not with gold.**
<br>
The **believers in the scarcity value of gold substantiate their position by reference to the falling off in the annual production of gold; the unusual demands for gold since 1873, by Germany, Italy, and the United States; stringencies in the money market; the increased use of gold in the arts; the claim that the fall of prices is general; the exceptional character of the depression of trade since 1873; the general existence of low wages, profits, and rents; and the absence of any progress since 1873 in the means of economizing gold and silver**. These opinions have been prominently associated with [Mr. Robert Giffen, the statistician of the English Board of Trade](http://books.google.com/books?id=tgzxAAAAMAAJ&printsec=frontcover&dq=editions:Zo11ySnomUYC&hl=en&sa=X&ei=PBJLUaaLLsHc0QGCl4CYCA&ved=0CEQQ6AEwAw#v=twopage&q&f=false), and [Mr. Goschen, the present Chancellor of the Exchequtr](http://books.google.com/books?id=0iU5AAAAMAAJ&printsec=frontcover&dq=editions:vBmzMXPNwLYC&hl=en&sa=X&ei=6RJLUefZLe-70QGm6oDwDg&ved=0CDEQ6AEwAA#v=twopage&q&f=false); while the evident connection of the main proposition with bimetallism has given it a semi-political character, and many supporters in both Europe and America.
<br>
§ II. Inasmuch as the rise in the value of gold since 1873 is in proportion to the fall of prices, it is a matter of some importance to look critically at the facts in regard to prices. With this object in view, the more important tables of prices since 1850 have been collected in the Appendix, with explanations as to the methods of computation, sources, and reliability. It is hoped that a comparison of the diverse methods and results of these tables will serve a useful purpose.
<br>
Hitherto, the figures of the London Economist for twenty-two articles have been almost universally used as evidence in regard to the movement of prices; **but it is time that the worship of this fetich should cease.* Of late, much more trustworthy tables have been published.**
I very much like the author's approach, which is to consider both the numerator and denominator in a price fraction and to gather as much data as possible rather than rely on one potentially flawed dataset. By page 3 we are already presented with this wonderful chart on prices/inflation in Great Britain, France, Germany, and the United States. These extra sources of price data allow us to frame the discussion much more objectively.
<div style="height: 290px;">
<img alt="[Chart 1 from the Table]" src="http://books.google.com/books?id=UFMuAAAAYAAJ&amp;output=text&amp;pg=PA3&amp;img=1&amp;zoom=3&amp;hl=en&amp;q=&amp;cds=1&amp;sig=ACfU3U00BpGcjHmleF3Z_uaeMV3hJzOOoQ&amp;edge=0&amp;edge=stretch&amp;ci=124,256,722,1174" width="290" height="471" style="-webkit-transform: rotate(270deg); -moz-transform: rotate(270deg); -ms-transform: rotate(270deg); -ms-transform-origin:70% 20%; -webkit-transform-origin:70% 20%; -moz-transform-origin:70% 20%;">
</div>
<br/>
In the spirit of the author's approach, I wanted to reproduce the chart presented above in R and also compare the US Burchard's table with data CPI and GDP deflator data from [MeasuringWorth](http://measuringworth.com). I tried to take the easy route with OCR, but eventually manually entered the price tables from the appendix.
```{r error=FALSE,message=FALSE,warning=FALSE,echo=FALSE,fig.height=6,fig.width=10}
#manually reproduce price tables from
#Google digitized http://books.google.com/books?id=UFMuAAAAYAAJ&pg=PA12&dq=gold+price+1873&hl=en&sa=X&ei=F9FIUYOyLbWo4AP5-YCoBA&ved=0CDoQ6AEwAA#v=onepage&q=gold%20price%201873&f=false
#Gold and Prices Since 1873
#by J. Laurence Laughlin
#Reprinted from Quarterly Journal of Economics April 1887
#The University of Chicago Press 1895
require(xts)
priceTables <- merge(
#EconomistTable
xts(
c(2200,2293,2451,2996,2612,2543,2692,2727,2878,3492,3787,3575,3564,
3024,2682,2666,2689,2590,2835,2947,2891,2778,2711,2723,2529,2202,
2538,2376,2435,2342,2220,2221,2170,2098,2048,2023),
order.by=as.Date(c("1850-01-01",
"1851-01-01",
"1853-07-01",
"1857-07-01",
paste(1858:1883,"-01-01",sep=""),
"1883-07-01",
"1884-01-01",
"1884-07-01",
"1885-01-01",
"1885-07-01",
"1886-01-01"))),
#EconomistTablePercent
xts(
c(100,104,111,136,118,115,122,123,130,158,172,162,161,137,122,121,
122,118,129,134,131,126,123,124,115,100,115,108,111,107,101,101,
98,95,93,92),
order.by=as.Date(c("1850-01-01",
"1851-01-01",
"1853-07-01",
"1857-07-01",
paste(1858:1883,"-01-01",sep=""),
"1883-07-01",
"1884-01-01",
"1884-07-01",
"1885-01-01",
"1885-07-01",
"1886-01-01"))),
#EconomistTableBourne
xts(
c(103,114,140,123,118,123,124,125,144,151,138,141,128,122,118,119,
118,133,142,136,130,123,126,118,106),
order.by = as.Date(c("1851-01-01",
"1853-07-01",
"1857-07-01",
paste(1858:1879,"-01-01",sep="") ))),
#BourneTableHome
xts(
c(94,94,95,109,115,105,107,98,93,90,91,90,101,108,103,99,94,96,90,80),
order.by = as.Date(c(paste(1860:1879,"-01-01",sep="") ))),
#BourneTableWorld
xts(
c(113,116,143,170,175,132,120,116,111,111,105,108,107,99,94,95,97),
order.by = as.Date(c(paste(1861:1877,"-01-01",sep="") ))),
#PalgraveTableEconomist
xts(
c(2200,1995,1981,2132,2237,2207,2098,2044,2064,1910,1676,1918,1782,
1830,1755,1660,1562,1509),
order.by = as.Date(c(paste(1869:1886,"-01-01",sep="") ))),
#PalgraveTableEconomistPercent
xts(
c(100,91,90,97,102,100,95,93,94,87,76,87,81,83,80,75,70,69),
order.by = as.Date(c(paste(1869:1886,"-01-01",sep="") ))),
#PalgraveTableRelativeImportance
xts(
c(2366,2434,2179,2058,1963,1975,2046,2197,2298,2378,2125,2186,2205,
2081,1806,1967,2054,1908,1924,1750,1669),
order.by = as.Date(c(paste(1865:1885,"-01-01",sep="") ))),
#PalgraveTableRelativeImportancePercent
xts(
c(108,111,99,93,89,90,93,100,104,108,97,99,100,95,82,89,93,87,88,80,76),
order.by = as.Date(c(paste(1865:1885,"-01-01",sep="") ))),
#BurchardsUSTable
xts(
c(102.6,103.1,106.1,114.8,123.5,128.3,129.6,138.3,115.1,
116.1,115.8,113.1,128.8,137.8,127.0,142.0,137.5,130.5,
124.2,124.9,136.5,124.8,130.0,124.1,123.3,113.5,108.9,
113.2,101.6,109.1,104.3,118.4,119.1,115.6,105.3),
order.by = as.Date(c(paste(1850:1884,"-01-01",sep="") ))),
#PalgraveFranceTable
xts(
c(103,105,100,97,95,97,104,111,110,101,102,100,102,89,86,88,85,84,82,76),
order.by = as.Date(c(paste(1865:1884,"-01-01",sep="") ))),
#PalgraveFranceTableRelativeImportance
xts(
c(106,108,97,96,93,91,102,105,105,97,95,95,96,91,87,88,86,84,80),
order.by = as.Date(c(paste(1865:1883,"-01-01",sep="") ))),
#SauerbeckTable
xts(
c(89,95,78,74,77,75,78,95,102,101,101,105,91,94,99,98,101,103,
105,101,102,100,99,98,96,100,109,111,112,96,95,94,87,83,88,
85,84,82,76,72),
order.by = as.Date(c(paste(1846:1885,"-01-01",sep="") ))),
#SoetbeerGermanTable
xts(
c(100.00,100.21,101.69,113.69,121.25,124.23,123.27,130.11,
113.52,116.34,120.98,118.1,122.65,125.49,129.28,122.63,
125.85,124.44,121.99,123.38,122.87,127.03,135.62,138.28,136.20,129.85,
128.33,127.70,120.60,117.10,121.89,121.07,122.14,122.24,114.25,108.72),
order.by = as.Date(c(paste(1850:1885,"-01-01",sep="") )))
)
colnames(priceTables) <- c("EconomistTable",
"EconomistTablePercent",
"EconomistTableBourne",
"BourneTableHome",
"BourneTableWorld",
"PalgraveTableEconomist",
"PalgraveTableEconomistPercent",
"PalgraveTableRelativeImportance",
"PalgraveTableRelativeImportancePercent",
"BurchardsUSTable",
"PalgraveFranceTable",
"PalgraveFranceTableRelativeImportance",
"SauerbeckTable",
"SoetbeerGermanTable")
#since Palgrave's France Table starts in 1865, the author splices
#what appears to be his favorite price series (Soetbeer German)
#to the series by standardizing the Palgrave table 100 to
#the average 1865-1869 of Soetbeer German values
#we'll make this adjustment also
#the author does not specify which Palgrave France table is used
#but from the graph it appears to be Palgrave France not Relative Importance
#since the final value is close to 102
priceTables = merge(priceTables,priceTables[,"PalgraveFranceTable"] * 1.236)
colnames(priceTables)[NCOL(priceTables)] = "PalgraveFranceSpliced"
xyplot(priceTables)
#we'll also adjust the Sauerbeck table to start cumulative at 1850
priceTables[,"SauerbeckTable"] = priceTables[,"SauerbeckTable"]/as.numeric(priceTables["1850-01-01","SauerbeckTable"])*100
require(latticeExtra)
require(directlabels)
direct.label(
#use update since direct.label tries to use theEconomist colors which we override with col
update(
xyplot(priceTables[which(format(index(priceTables),"%m")=="01"),
c("SoetbeerGermanTable",
"EconomistTablePercent",
"PalgraveFranceSpliced",
"SauerbeckTable",
"BurchardsUSTable")]["1850::",],
screens=1,ylim=c(80,180),
scales=list(x=list(tck=c(1,0)),y=list(rot=0)),
xlab=NULL,
main="UK, France, German, and US Price Tables from 1850 to 1885",
lattice.options=theEconomist.opts(),
par.settings=theEconomist.theme(box="transparent"),
lwd=3,
col=c("#882E72", "#5289C7", "#7BAFDE", "#4EB265", "#90C987", "#CAE0AB", "#F7EE55", "#F6C141", "#F1932D", "#E8601C", "#DC050C")), #thanks to http://tradeblotter.wordpress.com/2013/02/28/the-paul-tol-21-color-salute/#more-277
par.settings = simpleTheme(col=c("#882E72", "#5289C7", "#7BAFDE", "#4EB265", "#90C987", "#CAE0AB", "#F7EE55", "#F6C141", "#F1932D", "#E8601C", "#DC050C"))),
list("last.bumpup",hjust=1))
```
The chart was well done and really could not be improved beyond color and labels.
<br/>
As I have mentioned before, [MeasuringWorth](http://measuringworth.com) is a wonderful source of long-term economic and financial data. I wanted to compare Burchard's US table of prices to MeasuringWorth's US GDP deflator and CPI. The Civil War significantly destabilized prices. The effect is much more pronounced in the MeasuringWorth data.
```{r error=FALSE,message=FALSE,warning=FALSE,echo=FALSE,fig.height=6,fig.width=10}
#now get some data for comparison from MeasuringWorth for comparison
#if you use and like their data, I strongly encourage donating to the cause http://www.measuringworth.com/contribute.php
#ugly way to specify url for prices you would like
#shiny would provide a very nice interface once I get motivated
#this particular example will download US cpi from 1850 to 1885
#to compare to the Burchards US Table
url = paste("http://www.measuringworth.com/datasets/uscpi/export.php?",
"year_source=1850&", #data starts at 1774
"year_result=1885", #data goes through 2012
sep="")
mwUScpi <- apply(read.csv(url,stringsAsFactors=FALSE),MARGIN=2,FUN=as.numeric)
#get in xts format for easier time-series handling
mwUScpi.xts <- na.omit(xts(mwUScpi[,2:ncol(mwUScpi)]/mwUScpi[1,2]*100,
order.by = as.Date(paste(mwUScpi[,1],"-01-01",sep="")))) #set each year to end of year 1/1/Year to match
colnames(mwUScpi.xts) = "MeasWorth.USCPI"
url = paste("http://www.measuringworth.com/datasets/usgdp/export.php?",
"year_source=1850&",
"year_result=1885&",
"use[]=GDPDEFLATION",
sep="")
mwUSgdpdef <- apply(read.csv(url,stringsAsFactors=FALSE),MARGIN=2,FUN=as.numeric)
#get in xts format for easier time-series handling
mwUSgdpdef.xts <- na.omit(xts(mwUSgdpdef[,2:ncol(mwUSgdpdef)]/mwUSgdpdef[1,2]*100,
order.by = as.Date(paste(mwUSgdpdef[,1],"-01-01",sep="")))) #set each year to end of year 1/1/Year to match
colnames(mwUSgdpdef.xts) = "MeasWorthUS.GDPDef"
direct.label(
update(
asTheEconomist(
xyplot(na.omit(merge(mwUScpi.xts,mwUSgdpdef.xts,priceTables[,"BurchardsUSTable"])),
screens=1,
scales=list(y=list(rot=0)),
main="US Price Inflation by Various Sources 1850 to 1884",
col=c("#5289C7", "#7BAFDE", "#4EB265"))), #thanks to http://tradeblotter.wordpress.com/2013/02/28/the-paul-tol-21-color-salute/#more-277
par.settings = simpleTheme(col=c("#5289C7", "#7BAFDE", "#4EB265"))),
list("last.bumpup",hjust=0.7)
)
```
The author discusses the shortcomings of the Burchard prices in [Appendix Table M](http://books.google.com/books?id=UFMuAAAAYAAJ&pg=PR14#v=onepage&q&f=false).
>An examination of the Finance Report tables indicates that they were not compiled with great care. The price of an article will run along without change from month to month; then, suddenly, it will rise or fall sharply. The prices of articles that normally would fluctuate together (pig and bar iron, butter and cheese) show a very loose correspondence. Moreover, the same articles do not appear from year to year. An article will be quoted for a number of years, will then disappear, and later, perhaps, will reappear...Notwithstanding these serious imperfections, we reprint Mr. Burchard's figures for the years 1850-84, since they are the only continuous figures of average prices in the United States.
The author organizes his arguments into 8 additional sections, which I have titled. Also, I will give my favorite passage from each section. Interestingly, many of these same arguments apply directly to the MMT world of today with the only exception being fiat money today.
<br/>
#### Section 3: The Supply of Both Money and Credit Must Be Considered
>In truth, in society as it exists to-day, the general level of prices for considerable periods (sufficiently long to permit the effect of changes in the business habits of the community, or changes in the existing stock of gold, to be felt) must depend upon a combination of the quantity of money with the various forms of credit. The two are inextricably bound together. So, therefore, the level of prices (so far as it is affected by the offer of purchasing power) depends on the expansion or contraction of two factors, quantity of money and credit, each of which may change to a considerable extent independently of each other. Both may increase or diminish together, or the gain of one may offset the loss of the other.
#### Section 4: Other Events Affecting Prices
>The extraordinary and exceptional demand for commodities in periods of war, at the very time of the great destruction of wealth, produced an unhealthy state of affairs; but on the outside all seemed fair, and men had begun to believe that prices were fated always to rise. The speculation in metals (see Chart II.) in 1873 was of an unparalleled kind...There were all the evidences of an unhealthy and abnormal condition of affairs. But the unchecked demand, when the actual power to buy had been greatly impaired, could not go on forever. When it was once found that men had been creating liabilities beyond their means to meet them, the end had come. The crisis of 1873 was the painful return to a consciousness of the real situation, after a prolonged fever of speculation for nearly twenty years, which had spread over many countries. The effects were the more serious because the disease had got such great headway.
#### Section 5: Improvements in Industrial Process and New Supplies of Commodities
>The period following a great financial upheaval is naturally crowded with improvements in processes and in methods of lowering the cost of production. Necessity becomes the mother of invention. The extent to which producers have been driven by the fierce competition since 1873 to cheapen production leads to the inquiry how far the fall of prices can be accounted for by influences connected solely with commodities, and not with gold. If these influences have been widely extended, it will be strong evidence that the scarcity of gold has had less effect than some suppose.
#### Section 6: Analysis of Prices by Commodity Type
>Whether to draw inferences as to a scarcity of gold from forty-nine articles, or to infer that gold was abundant, according to the prices of fifty-one articles, is an awkward dilemma for those who think that prices give direct evidence as to the quantity of money. As Forsell remarks, the theory of a scarcity of gold is incompatible with the rise * in price of so many commodities.
#### Section 7: Supply of Gold
>From these figures, it will be seen that the reserves in the banks of the civilized world show a very remarkable increase in gold. Although the total note circulation was increased 29 per cent., the gold in the reserves was increased 75 per cent., while the silver was also increased 25 per cent. In 1870-74, the gold reserves amounted to 28 per cent. of the total note circulation, and constituted 64 per cent. of all the specie reserves. In 1885, the gold bore a larger ratio to a larger issue of paper, or 41 per cent. of the total note circulation; and, in spite of unusual accumulations of silver (in the Bank of France, for example), the gold formed 71 per cent. of the specie reserves. This is a very significant showing. What it means, without a shadow of doubt, is that the supply of gold is so abundant that the character and safety of the note circulation have been improved in a signal manner. In 1871-74 there was $1 of gold for every $3.60 of paper circulation.* In 1885 there was 11 of gold for every $2.40.
#### Section 8: Supply of Credit
>Specie to the amount of 1800,000,000 has gone into circulation in the form of note issues, representing an equivalent amount of specie; but gold has not been economized by the use of credit in the form of notes. While the total circulation of these countries has increased 35 per cent., the paper has been much better protected...The paper currency of every country except Russia has gained in security, together with a large increase in many of the countries. The gold supplies have not merely permitted an enlarged note circulation, but have furnished a much better protection to that increased issue.
#### Section 9: Price of Land Labor
>It will be recalled at once, in regard to rents, that a marked characteristic of the period since 1873 has been the opening up of new and fertile lands, whose products have been transported at a greatly diminished rate. But this in itself is a reason why lands in the older countries should be thrown out of cultivation, and why rents should be lowered. This phenomenon, then, can be accounted for on other grounds than the scarcity of gold...The fact that wages have risen tends to confirm the belief that the fall of prices is due chiefly to the introduction of improvements.
#### Section 10: Conclusion
>To assume that because the fall of prices coincided with the demonetization of silver it was due to an appreciation of gold, without considering whether the coincident phenomena were traceable to entirely distinct causes, is to fall into the fallacy of post hoc propter hoc. The forces which fix the level of prices at any time, moreover, are far too complex to admit of the inference that, because prices have fallen seriously, gold has become scarce.
<br>
[Gold and Prices Since 1873 by James Laurence Laughlin](http://books.google.com/books?id=UFMuAAAAYAAJ&printsec=frontcover#v=thumbnail&q&f=false) is a reminder of how our current is wedded to our past. I often feel like the unprecedented actions of the world's central banks have broken the link to centuries of financial history. Rather, it seems they have simply temporarily suspended the immutable laws of humans and money and maybe "The effects were the more serious because the disease had got such great headway." will haunt us.
<br>
I ran out of time and could not do the visualization in d3. Anybody that is motivated, here is a way to get the JSON:
```{r eval=FALSE}
#for the ambitious and more motivated than me, here is how to easily get the JSON
#to translate the graph to an interactive http://d3js.org
require(rjson)
toJSON(list(Date=format(index(priceTables),"%Y-%m-%d"),as.list(priceTables)))
```
****
[Post entirely generated from R markdown for reproducibility:]()

Monday, March 18, 2013

One Pager Performance Report with knitr, R, and a Different Font

Although I suffer from complete ignorance of typography, with a little help from a post from Hyndsight and post from mages' blog, I wanted to try a different font on the one-pager performance report that we created in Onepager Now with knitR. I do not think Open Sans Light is the best choice for this report, but since it is the most popular font on Google I figured I could not be criticized too heavily by those more knowledgeable than me in typography.

options(tikzDefaultEngine = "xetex")

require(knitr)

knit2pdf("onepager_text_graphics-knitr.rnw", compiler = "xelatex")



And the result (go to Box if the embed does not appear below):


R code from GIST:

One Pager Performance Report with knitr, R, and a Different Font

Although I suffer from complete ignorance of typography, with a little help from a post from Hyndsight and post from mages' blog, I wanted to try a different font on the one-pager performance report that we created in Onepager Now with knitR. I do not think Open Sans Light is the best choice for this report, but since it is the most popular font on Google I figured I could not be criticized too heavily by those more knowledgeable than me in typography.

options(tikzDefaultEngine = "xetex")

require(knitr)

knit2pdf("onepager_text_graphics-knitr.rnw", compiler = "xelatex")
view raw Readme.md hosted with ❤ by GitHub
\documentclass[nohyper,justified]{tufte-handout}
%\documentclass{article}
%\usepackage[absolute,showboxes]{textpos}
\usepackage{fontspec}
\usepackage[absolute]{textpos}
\usepackage{sidecap}
%\usepackage{color}
%\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
%get Open Sans Light from Google Webfonts
\setmainfont{Open Sans Light}
\begin{document}
\pagestyle{empty}
<<include=FALSE>>=
opts_chunk$set(concordance=TRUE, dev="tikz")
@
\begin{wide}
\section{\Huge Performance Summary with knitR and R}
{\large Here is a little experiment with R, knitr, and XeLaTeX to produce a performance report. I have done some samples in the past, but I wanted to iterate through a couple more, especially to evaluate other options for what has been started in the PApages package. Of course, this text could be easily replaced with some commentary from a manager about opportunities, thoughts, or current allocation. A dashboard set of charts also might be very helpful here.}
\hrulefill
\end{wide}
<<eval=TRUE,echo=FALSE,results='hide',warning=FALSE,message=FALSE,error=FALSE>>=
#do requires and set up environment for reporting
require(xtable)
require(ggplot2)
require(directlabels)
require(reshape2)
require(latticeExtra)
require(quantmod)
require(PerformanceAnalytics)
data(managers)
#get xts in df form so that we can melt with the reshape package
#will use just manager 1, sp500, and 10y treasury
managers <- managers[,c(1,8,9)]
#add 0 at beginning so cumulative returns start at 1
#also cumulative will match up datewise with returns
managers <- as.xts(rbind(rep(0,NCOL(managers)),coredata(managers)),
order.by=c(as.Date(format(index(managers)[1],"%Y-%m-01"))-1,index(managers)))
managers.df <- as.data.frame(cbind(index(managers),coredata(managers)),stringsAsFactors=FALSE)
#melt data which puts in a form that lattice and ggplot enjoy
managers.melt <- melt(managers.df,id.vars=1)
colnames(managers.melt) <- c("date","account","return")
managers.melt[,1] <- as.Date(managers.melt[,1])
#get cumulative returns starting at 1
managers.cumul <- as.xts(
apply(managers+1,MARGIN=2,FUN=cumprod),
#add end of first month to accommodate the 1 that we add
order.by=index(managers))
managers.cumul.df <- as.data.frame(cbind(index(managers.cumul),
coredata(managers.cumul)),
stringsAsFactors=FALSE)
managers.cumul.melt <- melt(managers.cumul.df,id.vars=1)
colnames(managers.cumul.melt) <- c("date","account","return")
managers.cumul.melt[,1] <- as.Date(managers.cumul.melt[,1])
#get rolling returns for 1y, 3y, 5y, since inception
trailing <- table.TrailingPeriods(managers, periods=c(12,36,60,NROW(managers)),FUNCS=c("mean"),funcs.names=c("return"))
trailing.df <- as.data.frame(cbind(c("1y","3y","5y","SinceIncep"),
c(rep("return",4)),
coredata(trailing)),
stringsAsFactors=FALSE)
trailing.melt <- melt(trailing.df,id.vars=1:2)
colnames(trailing.melt) <- c("period","measure","account","return")
#function to get numbers in percent format
#will use \\ to play well with tikz
percent <- function(x, digits = 2, format = "f", ...)
{
paste(formatC(100 * x, format = format, digits = digits, ...), "%", sep = "")
}
@
\begin{textblock*}{105mm}(15mm,70mm)
\begin{figure}
\vspace{0pt}
<<echo=FALSE,eval=TRUE,fig.width=8,fig.height=12>>=
charts.PerformanceSummary(managers,
colorset=c(brewer.pal(9,"Blues")[6],brewer.pal(8,"Greys")[c(5,4)]),
xlab=NULL)
@
\end{figure}
\end{textblock*}
\begin{textblock*}{75mm}(120mm,100mm)
\small Cumulative returns offer one of the best methods to evaluate the ability of a manager to achieve long term returns. Ultimately, the cumulative return is often one of the primary objectives of our clients.
\normalsize
\newline
\begin{figure}
\vspace{0pt}
<<echo=FALSE,eval=TRUE,results='asis'>>=
trailingtable <- apply(trailing,MARGIN=2,FUN=percent)
rownames(trailingtable) <- c("1y","3y","5y",paste("Since Inception ",format(index(managers)[1],"%b %Y")))
print(xtable(trailingtable), floating=FALSE, scalebox=0.7)
@
\end{figure}
\end{textblock*}
\begin{textblock*}{75mm}(120mm,152mm)
\small However, cumulative returns must also be evaluated with reference to the risks incurred to generate those returns. Below are multiple risk measures. We are most concerned with limiting drawdowns shown in the bottom left chart.
\normalsize
\newline
\begin{figure}
\vspace{0pt}
<<echo=FALSE,eval=TRUE,results='asis'>>=
risktable <- table.DownsideRisk(managers)
print(xtable(risktable), floating=FALSE, scalebox=0.6)
@
\end{figure}
\end{textblock*}
\end{document}

Friday, March 15, 2013

How Did I Miss “The Golden Dilemma”?

I am ashamed to admit that I am way behind (about 10,127 downloads) in discovering this wonderful paper:

The Golden Dilemma (January 8, 2013)
Erb, Claude B. and Harvey, Campbell R.
Available at SSRN: http://ssrn.com/abstract=2078535

image

Here are the authors presenting the concept in July 2012 if you prefer slideshow format (thanks http://abnormalreturns.com/saturday-links-the-gold-dilemma/).

A presentation on “The Gold Dilemma” by Claude Erb and Campbell Harvey. (Duke)

I have struggled with the various reasons I have heard for owning gold, which often conflict with each other, and how gold cannot universally work in all environments.  The authors enumerate and explore 6 arguments for owning gold:

    1. “gold provides an inflation hedge”
    2. “gold serves as a currency hedge”
    3. “gold is an attractive alternative to assets with low real returns”
    4. “gold a safe haven in times of stress”
    5. “gold should be held because we are returning to a de facto world gold standard”
    6. “gold is underowned”

In previous posts from 2010 and 2011, I focused on the gold as a very expensive inflation hedge, but I was nowhere near as exhaustive as the authors on this point and did not even touch on the five others.

I very much liked Exhibit 6 from the paper outlining scenarios based on varying 10 year forward inflation and a gold/CPI constant (3.2 average since 1975 and 7.1 as March 2012).

image

Although the gold/CPI ratio averaged 3.2 1975-2012, the authors extend the analysis to 1791 using the very fine data gathered at MeasuringWorth (more later in the post on importing this data to R for the R geeks).  The gold/CPI ratio has averaged 2 since 1791, which is clearly much lower than the almost current 8.  Using the table above would suggest a negative nominal return unless annualized inflation exceeds 15% for the next ten years.  Just think what happens to other markets in this environment, and gold might not even be the worse.

image

I was also delighted to see the authors reflect my concern that argument 2 could easily be the same as argument 1 in a debasement environment.

“A second way to interpret the “gold as a currency hedge” argument sees “gold as a hedge of my own currency, spent in my own country, when the local government is printing money with abandon”. This is also sometimes referred to as “currency debasement”. If this debasement is a result of inflation, then this interpretation is just another version of the “gold as an inflation hedge” argument.”

I highly recommend readers work through the other arguments with the authors by reading the original paper, but (spoiler alert) if you do not want to do that, I think the conclusion very simply is that after a mammoth increase in gold prices, none of the 6 arguments can justify the current price.  The “gold is underowned” argument is the strongest but even then requires a unique marginal buyer “[who] are not focused on ‘valuation’.”  Of course, we do have a lot of central bank buyers now of every financial asset “not focused on ‘valuation’.”

Now almost for the R as promised, but first the data at MeasuringWorth is truly special, and I would liken it to the other unbelievably generous set of financial data at http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html.  Although no one has asked that I do this, please consider making a donation at http://www.measuringworth.com/contribute.php to insure the continuation of this data and hopefully maybe the collection of more.  Non-US financial data prior to 1900 is nearly impossible to find.

MeasuringWorth data is extremely easy to access in R. As a simple example, I wanted to pull in the price of gold since the 1700s.  I did it in 2 lines of R.  In 5 I got this graph.

 image

R code from GIST:

#R example of using prices from http://www.measuringworth.com
#if you use and like their data, I strongly encourage donating to the cause http://www.measuringworth.com/contribute.php
require(latticeExtra)
require(xts)
#ugly way to specify url for prices you would like
#shiny would provide a very nice interface once I get motivated
#this particular example will download prices from 1718 to 2013 for New York and London market
#redundant since London market dataset switched to New York in 1950
url = paste("http://www.measuringworth.com/datasets/gold/export.php?",
"year_source=1791&", #1257, 1786, 1791, 1687, or 1718
"year_result=",format(Sys.Date(),"%Y"),"&", #any year to current except for 1945 for British official
#set on for the following data sources
#time range set above must fall within the years specified
#can specify multiple sources
"British=&", #British official price (1257-1945)
"us=&", #U.S. official price (1786-Present)
"newyork=on&", #New York market price (1791-Present)
"goldsilver=&", #Gold/silver price ratio (1687-Present)
"london=on", #London market price (1718-Present) switches to New York 1950
sep="")
goldprices <- apply(read.csv(url,stringsAsFactors=FALSE),MARGIN=2,FUN=as.numeric)
#get in xts format for easier time-series handling
goldprices.xts <- na.omit(xts(goldprices[,2:ncol(goldprices)],
order.by = as.Date(paste(goldprices[,1],"-12-31",sep="")))) #set each year to end of year 12/31/Year
xyplot(goldprices.xts,
scales=list(x=list(tck=c(1,0)),y=list(log=10,rot=0)),
yscale.components = yscale.components.log10ticks,
xlab=NULL,
main="Gold Prices from MeasuringWorth (http://measuringworth.com)")

Thursday, March 14, 2013

Doing Things the Hard Way with d3

Although embarrassing, I thought this might help some folks learning d3.js.  Most of my struggles with d3.js center around getting the data in proper form.  I think most of it is mindset.  d3.js offers a full range of helper functions to easily accomplish this task, but it seems my brain has been conditioned to think a different way.  In some ways, I liken it to my initial difficulty with R’s set of apply functions.  With a lot of practice, I think once you get it you get it.

Take for example a recent experiment with zui53 and d3 on index data.  I spent more than an hour coming up with this mess:

var indexkeys = d3.keys(d3.nest().key(function (d) { return d.indexname; }).map(data))
var
indexdata = indexkeys
.map(function (name) {
return {
name: name,
values: data.filter(function (d) { return d.indexname == name; })
};
});


The original data is a csv in long format
date,indexname,price
1976-01-30,SP500,100.86
1976-02-27,SP500,99.71
...
2013-01-31,BarAgg,1831.49
2013-02-28,BarAgg,1840.67


which I would like to look like.
image
Although my ugly mess works, after a couple days away from the code, I had a thought after reading D3 Nest Tutorial and examples for a fifth time.  I could have just simply done this.
indexdata = d3.nest().key(function(d) {return d.indexname;}).entries(data)

I thought I had it all figured out after https://groups.google.com/forum/?fromgroups=#!topic/d3-js/biALMQnYUgo, but as you can see not even close.