mini-ppppject
--- title: "Econ/Demog C175 Lab 4: The Bet -- Over and over again" output: html_notebook --- Julian Simon liked to proclaim that > The only true measure of scarcity is price ... In this lab we are going to take advantage of a database put together by David Jacks. The data for his paper "From Boom to Bust: A Typology of Real Commodity Prices in the Long Run" (2013, NBER Working Paper 18874) has consistent series of commodity prices for everything ranging from barley to zinc. For our lab we've selected prices since 1900. We're going to ask the following: 1. What has been the long-term trend in commodity prices? 2. Was Simon lucky to have won the bet? 3. Do commodity price trends differ by commodity in a way that we can understand? (This section will be more exploratory than in past labs. Here the important point is to report _what_ you find, not to find a specific "right" answer.) 4. Finally, we will use our Malthusian "harvest" model to learn about renewable resources. (We may or may not get to this in class this week. If not, then this part of the lab will be pushed into next week, when we have no lab). ```{r} # Do not edit this chunk, but *do* press the green button to the answer key for the quiz info (the unreadable string below) tot = 0 answer.key = "eJzNVk2P4zYMvedXEDmMd4FASZzMYHeBwSIzLdBeiqKz94EiM7YaWfLqYz359yVlJ/Phaw69GKFok3yP5FPET5vaPfq1WMM90HMmlPMeVRTShv7smP82n4lG2ziaf3utMID0CNpW+IIVRAfrr6sVSFvlc1n9m0Ikx8F5eulgZNTOUhhtQ/RJsRXGcP9gZ6RCiA3CXzvodWzg5JKHoQb4hKIWCyh2BUWCn8lFDJ8pFL7QdzYHvkSSBrqxulzBJP8FcZkRl1PE7Jg/XBBns4mxC9+WS7Si10fdYaWlcL5esrV8bLxrdWon8MqrwcuRdopqrbStme5LHQv4cgsdeoU2gibgXUcdsNGcIAWsZoIpiA15nEXoku9cQAE7n0uiZ0j7EHVMlDfT1XkqgSBQHmohmu+vtJWZ53I6KINj/jjSNppP9M5pAdYBH36gp7xa98dIPxhNEUAZlJ7QSzAugjvAL8ftNzqeCLaJjUt1k/PlUeGw5Wp9y9w1uqYg5JOWj3mkFwN3lcNgi0gh3RGMPtKID4kgEtnVAoK2FIv5a5NqOC1nKFcEhzuT/OlNyl4GitR/SCXeEl1moifzOTjmF56z9WdtHS9diq4lpApaDEHWdLJ3Kb6jc4/c14KKHcMWM5GJY/jUqGI8L8YPBDykTKKiIIFR03gcKDwB7V0yFRAJdiai66WvoHgsJm2+1hb8T8FeOrbJHdtMO7Z5qyijuTMmC6VHGZyVe4MLwBeFXYRH6BtNI0Q1vlYOP2iAZC2JHH1gkFDpigdyhPogJrxvrsb7UPFFV2gl6N1RZ3umUzkbvTOGBJ890kfY8wIq+oWGGSUiq6TQBwFPiHAW1L7vhY5eE2ciHZf5OhFd0313Hae+V659bmnD8CbKcLyXMT5XrrfGyerGaHt81tX99uvd6vZGHfh3uc0rKImdoFIIFAMOpM6Qt6yiG4Fo9BhocYnhQEQofLN2m6wkm6m+DY5LE0fzj0EuRiZamg1oeTYD4VYsNx96srma5I2RdoxK8QxlGRsLCamuMcQwgL64Z6I4F1YsaMZkhN8bb3jUWI/2GLPmO/uOkDITMtGhzXglXQgZdtMGpN6T8mqL36Cggmz85LyutZVGUApB5CcTP2doTIByFT2aZI98qwUcaBneOqsofzeh8lqyckYCFms6+IUwQM/9DK9jTrcrj94i/8cJDp50S8M1yEIj6bP+HXPb3KHtdJS270dpNEfmhhuZhWG9umPcga/PSNbqIwPbqw3TGOmuXHJS/guRlXQlbr+I+X+1gFbM" library(quizify) source.coded.txt(answer.key) ``` > Note: the most helpful reading for this lab is David Lam (2011), > "How the World Survived the Population Bomb" # Part 1. Read in data and graph trends in commodity prices These commodity prices are modified in two ways. First, the nominal prices have been adjusted for inflation. Second, all prices are indexed to 100 in 1900. This makes it easy for us to compare across commodities and over time. ```{r} ## get commodity data from a file on our server file <- "/data175/Real_commodity_prices_1900_2015.csv" commod <- read.table(file, sep = ",") # read the data into, with column header head(commod) # looks at first few lines ``` You can scroll to the right and left using the black arrows in the display of the data. Q1.1 What does the number 110.51 under Zinc mean? A. Zinc costs 110.51 cents per pound B. Zinc prices increased by about 110 percent between 1900 and 1904 C. Zinc cost about 10.51 percent more in *nominal* terms in 1904 than in 1900 D. Zinc cost 10.51 percent more in *real* terms in 1904 than in 1900 ```{r} ## "Replace the NA with your answer (e.g., 'A' in quotes)" answer1.1 = 'D' quiz.check(answer1.1) ``` Q1.2 What is the major use of Chromium? A. For browsing the internet B. As Chrome plating and stainless steel C. As vitamin supplement for breakfast cereal D. As a precious metal for storing value ```{r} ## "Replace the NA with your answer (e.g., 'A' in quotes)" answer1.2 = 'B' quiz.check(answer1.2) ``` To select a value from this matrix of data you can index by the label of the row and column. For example, ```{r} commod["1963", "Chromium"] ## note for those interested in R: ## the index "1963" needs to be in quotes, otherwise R will think it ## is the 1963rd row of the matrix. Here we're telling it that it is ## the row labeled "1963". ``` tells us that the real price of Chromium in 1963 was 55 percent of that in 1900. > (Note: in the rest of the lab, when we say "price" we mean "real > price", unless otherwise stated.) # Part 2. Visualizing commodity prices Let's see how the price of Chromium varies over time. ```{r} year.vec <- rownames(commod) plot(year.vec, commod$Chromium, type = "l") abline(v = 1980) # the date of "the bet" ``` Q2.1 How would you describe the history of Chromium prices? A. Steadily rising B. Steadily falling C. Very volatile, with little clear long-term trend D. Very volatile, with a clear upward trend ```{r} ## "Replace the NA with your answer (e.g., 'A' in quotes)" answer2.1 = 'C' quiz.check(answer2.1) ``` ## What does "trendless" data look like? Imagine a "random walk", in which prices go up or down each year by some random amount that averages zero. Each realization of this random walk will have, by luck, some trend, but the underlying process generating the randomness has no trend -- on average the change is "zero". It is very easy to falsely interpret a random walk as having an underlying trend. The subject of how to make inferences about the trend of a time series is covered in an econometrics or statistics class. For now, we will just look at some realizations of the random walk to get a feeling for what random realizations of trendless data can look like. ```{r} ## here's the function plot.random.walk <- function(seed) { set.seed(seed) # this initializes the random # number generator used in # sample(). If we use the same # seed, we will get the same # set of "random" numbers. random.steps <- sample(x = c(-1,1), size = 40, replace = TRUE) ## this selects the number -1 or 1 randomly 40 times random.walk <- cumsum(random.steps) ## this turns the random steps into a walk by summing them up. ## cumsum(c(1,2,3)) returns a vector with values 1, 3, and 6. plot(1:40, random.walk, type = "l") } ``` Here's an example ```{r} plot.random.walk(seed = 23) # Here we set seed to 23 ``` Modify the code below to try 1,2, 3, and 4 as "seeds" ```{r} plot.random.walk(seed = 23) plot.random.walk(seed = 23) plot.random.walk(seed = 23) plot.random.walk(seed = 23) ``` Q.2.2 Which of the following seems to be true A. Even if price changes are random, averaging zero, the realization of a random process can look like it has a trend. B. A historical trend is the one reality we observe and we shouldn't let anyone confuse us with alternative random realizations. C. Thinking about randomness may help us understand and interpret the reality we see and thus is a subject worth of study. ```{r} ## "Replace the NA with your answer (e.g., 'A' in quotes)" answer2.2 = 'C' quiz.check(answer2.2) ``` ## Repeating for Tin Plot the price of tin. You can use the same code we used for Chromium, modifying just slightly. ```{r} ## put your code for Tin here year.vec <- rownames(commod) plot(year.vec, commod$Tin, type = "l") abline(v = 1980) ## (Hint: you can copy and modify the commands we used for Chromium) ``` Q2.3 Why do you think Ehrlich chose Tin as one of his commodities to bet on? A. Tin is difficult to substitute for B. Tin had shown a sharp increase in price for the decade or so before 1980 and so it looked like it would continue. C. The price had been going down before 1980 and was ready for a recovery D. There was evidence that the tin mines were running out of ore ```{r} ## "Replace the NA with your answer (e.g., 'A' in quotes)" answer2.3 = 'B' quiz.check(answer2.3) ``` # Part 3. Replicating actual bet In the original bet, Ehrlich was allowed to choose any commodities he wished. He chose Chromium, Copper, Tin, Tungsten, and Nickel. In our data, we don't have Tungsten. We can check to see if Simon still wins with the four commodities we have. > See the Lam reading, p. 1242 for an account of the bet. We will use a function to compare commodities over time. This uses the indexing by label we did above. It returns the change in value of a $1000 purchase of the commodity basket in the start year. So if the function returns +100, this means that the value of the commodity basket increased by $100. Q3.1 If the value of the basket increased by $100, who would win? A. Simon B. Ehrlich C. Neither, because the value of the basket doesn't tell us if *all* of the commodities increased in price at the same time. ```{r} ## "Replace the NA with your answer (e.g., 'A' in quotes)" answer3.1 = 'B' quiz.check(answer3.1) ``` ```{r} bet.fun <- function(start, end, basket.vec, data) { ## start = 1980 ## end = 1990 ## data = commod ## basket.vec <- c("Copper", "Chromium", "Nickel", "Tin") year <- rownames(data) start.price <- data[year == start, basket.vec] end.price <- data[year == end, basket.vec] ## we invest 1000, buyin an equal $ amount in each commodity. start.funds <- 1000 dollars.per.commod <- start.funds/length(basket.vec) # e.g. $250 with 4 ## amount purchased of each commodity holdings.vec <- dollars.per.commod/start.price ## value at the end value.at.end <- sum(holdings.vec * end.price) change.in.value <- value.at.end - start.funds return(change.in.value) } who.wins.fun <- function(change.in.value) { ifelse(change.in.value > 0, "ehrlich", "simon") } ``` Let's try this for 1980 to 1990 with Ehrlich's basket. ```{r} ehrlich.basket.vec <- c("Copper", "Chromium", "Nickel", "Tin") original.bet.result <- bet.fun("start" = 1980, "end" = 1990, "basket.vec" = ehrlich.basket.vec, "data" = commod) who.wins.fun(original.bet.result) ``` Q3.2 How much would Simon have won with our version of the "bet"? A. About 300 dollars B. He wouldn't have won. He would have lost about 300 dollars C. About 500 dollars D. About 100 dollars ```{r} ## "Replace the NA with your answer (e.g., 'A' in quotes)" answer3.2 = 'A' quiz.check(answer3.2) ``` # Part 4. Does Simon win in other periods? Commodities are highly volatile. Even if commodities were generally rising, Ehrlich could have been unlucky about the timing of his bet. Let's check over all years since 1900. We can construct a "loop" using R that will repeat the bet according to our instructions. We can then save the output in a vector and make a judgement about whether Ehrlich was simply unlucky. Here's a simple example of a loop, in which we repeat an action 7 times, incrementing the value of "i", and printing this value along with a label "iteration". ```{r} for (i in 1:7) ## the variable "i" takes the value 1, 2, 3, ... { ## anything in between {} is done once ## for each value that "i" takes print(c("iteration", i)) } ``` Now let's do a loop to see who won the bet in every year of the last century. ```{r} start.vec <- 1900:2005 ## a vector of years to start the bet bet.result.vec <- rep(NA, length(start.vec)) ## an empty vector to be ## used to store the ## results names(bet.result.vec) <- start.vec ## labeling the elements (you can ignore) for (i in 1:length(start.vec)) { bet.result.vec[i] <- bet.fun(start = start.vec[i], end = start.vec[i] + 10, basket.vec = ehrlich.basket.vec, data = commod) } print(bet.result.vec) ``` To summarize our results, let's look at them in several ways. Plot the numbers ```{r} plot(start.vec, bet.result.vec) abline(h = 0) text(1960, 1500, "Ehrlich wins", col = "blue") text(1960, -250, "Simon wins", col = "red") ``` In the graph, it looks like Ehrlich would have won more of the time. But not a lot more. Let's tabulate how many times Ehrlich and Simon would have won: ```{r} ## Convert to a vector of "simon" and "ehrlich", depending on who won winners.vec <- who.wins.fun(bet.result.vec) print(winners.vec) ``` ```{r} ## count number of times each won using the "table()" function table(winners.vec) ``` Q4.1 What percent of the time would Ehrlich have won of the 106 simulated bets from 1900 to 2005? A. About 62 percent B. About 58 percent ```{r} ## "Replace the NA with your answer (e.g., 'A' in quotes)" answer4.1 = 'B' quiz.check(answer4.1) ``` Make a fancier plot. (You don't need to understand this code.) ```{r} plot(start.vec, bet.result.vec, type = "n") #an empty plot abline(h = 0) e <- which(winners.vec == "ehrlich") # T or F, to subset text() s <- which(winners.vec == "simon") text(x = start.vec[e], y = bet.result.vec[e], labels = "e", col = "blue") text(x = start.vec[s], y = bet.result.vec[s], labels = "s", col = "red") text(1960, 1500, "Ehrlich wins", col = "blue") text(1960, -250, "Simon wins", col = "red") ``` # 5. Generalizing the bet to other commodities A change in the supply of a particular commodity may or may not cause a large price change -- depending on the availability of substitutes. (Note: the results from this section will be useful for your write-up of the graded questions at the end of the lab.) The following code provides a template for a single commodity, in this case "Chromium". ```{r} start.vec <- 1900:2005 result.vec <- rep(NA, length(start.vec)) for (i in 1:length(start.vec)) { result.vec[i] <- bet.fun(start = start.vec[i], end = start.vec[i] + 10, basket.vec = c("Chromium"), data = commod) } winner.vec <- who.wins.fun(result.vec) cat("counts:\n") table(winner.vec) cat("proportions:\n") prop.table(table(winner.vec)) ``` Well done. You are finished with the computing portion of Lab 4. # Part 5. Graded Questions # 1. What relationship did Ehrlich expect to hold between commodity prices and population growth? > Ehrlich expected population growth to invoke scarcity (and thus driving up commodity prices). > As a Forbes article puts it, "Ehrlich insisted that commodities would become more expensive: they were running out in the face of the population explosion. Simon asserted the opposite: more people meant more brains meant better methods of extraction and lower usage per unit of production. Thus prices should fall. Simon won: but that's not quite the end of the matter. With different commodities, or over different timescales with the same ones, Ehrlich could have." # 2. Who won the bet in 1990? Given our analysis of other time periods, would you say that the winner was “lucky” or “right”, or both? [Explain your answer in 50 to 100 words.] > Simon won in 1990, which was the end of the decade-duration bet. I agree with the Forbes article in that Simon was "lucky" in the time period the bet happened to occur in, and the commodities being betted upon. In our computational part of the lab, we found that over our investigated time period, Ehrlich had won about 58% of the time. So suppose Simon won to pure chance, at 42% (< 50%) chance. I'd call that lucky for a bet with money involved. (As noted in the lab, this section is for you to explore and describe what you found. Whether what you find is conclusive or inconclusive, you can still get full credit by reporting whatever you found.) ## 3. Choose another easily substitutable commodity from the database. Conduct the analysis of its price as the previous part of the lab and answer the following questions. # 3.1) Why do you think this commodity would be easily substitutable, explaining what “service” it provides with 1 or 2 sentences. > I chose Coffee as the commodity to investigate, with a meta particular interest in whether or not this commodity is easily substitutable. Although this may cause difficulty in our analysis, I argue that there are many consumers who 'require' coffee to function, but theoretically it can be skipped upon. In fact, nowadays some people search for other sources of caffeine or taurine for this purpose. One could expect tea (another commodity in our data set) to be positively or negatively correlated with our coffee price trends. # 3.2) Attach a plot of the change of prices (“bet.result”) of the chosen commodity. ```{r} ## get commodity data from a file on our server file <- "/data175/Real_commodity_prices_1900_2015.csv" commod <- read.table(file, sep = ",") # read the data into, with column header # head(commod) # looks at first few lines year.vec <- rownames(commod) plot(year.vec, commod$Coffee, type = "l") abline(v = 1980) # the date of "the bet" start.vec <- 1900:2005 ## a vector of years to start the bet bet.result.vec <- rep(NA, length(start.vec)) ## an empty vector to be ## used to store the ## results bet.fun <- function(start, end, basket.vec, data) { ## start = 1980 ## end = 1990 ## data = commod ## basket.vec <- c("Coffee") # for this analysis we only look at a single commodity basket, of Coffee year <- rownames(data) start.price <- data[year == start, basket.vec] end.price <- data[year == end, basket.vec] ## we invest 1000, buyin an equal $ amount in each commodity. start.funds <- 1000 dollars.per.commod <- start.funds/length(basket.vec) # e.g. $250 with 4 ; unnecessary for a single commodity basket ## amount purchased of each commodity holdings.vec <- dollars.per.commod/start.price ## value at the end value.at.end <- sum(holdings.vec * end.price) change.in.value <- value.at.end - start.funds return(change.in.value) } who.wins.fun <- function(change.in.value) { ifelse(change.in.value > 0, "ehrlich", "simon") } # # names(bet.result.vec) <- start.vec ## labeling the elements (you can ignore) # for (i in 1:length(start.vec)) # { # bet.result.vec[i] <- bet.fun(start = start.vec[i], # end = start.vec[i] + 10, # basket.vec = ehrlich.basket.vec, # data = commod) # } # print(bet.result.vec) winners.vec <- who.wins.fun(bet.result.vec) plot(start.vec, bet.result.vec, type = "n") #an empty plot abline(h = 0) e <- which(winners.vec == "ehrlich") # T or F, to subset text() s <- which(winners.vec == "simon") text(x = start.vec[e], y = bet.result.vec[e], labels = "e", col = "blue") text(x = start.vec[s], y = bet.result.vec[s], labels = "s", col = "red") text(1960, 1500, "Ehrlich wins", col = "blue") text(1960, -250, "Simon wins", col = "red") ``` > Hint 1: there would only be your chosen commodity in the > “basket.vec”. You can either modify the original > “ehrlich.basket.vec” or create your own “basket.vec”. Remember to > modify for-loop consistently. > Hint 2: you don’t need to show whether Ehrlich or Simon win about > this commodity. But labelling the plot with different colors can > help you understand the general trend. > Hint 3: NOT REQUIRED. To make it a better graph, you can add a title > to the graph indicating the commodity you choose adding ‘main = > “commodity name”’ to the syntax. # 4. Choose a commodity from the database which you think would be hard to substitute for. And explain why you think this commodity would NOT be easily substituted, explaining what “service” it provides. (In 1 or 2 sentences) [Note: you don't need to analyze the data for this problem.] The most prime commodity that comes directly to mind is gold. As a commodity, it always has its uses from fashion to utility from electronics to other applications. Suppose demand for the utility for gold goes down. Then if the price drops, we can imagine more people would buy the dip for fashion purposes. It extends both ways. Moreover, we can't forget the saying "it's as good as gold." # 5. What are two reasons prices might not reflect the long-term availability of a commodity? [A sentence on each.] Firstly, we only have current and past (historical) data on prices. Predicted or forecasted prices are dependent on future events, so we cannot use forecasted prices to implicate or draw inferences about the long-term availability of a commodity. Secondly, we do not know how future technologies will affect the availability or demand for that given commodity. It may happen that a technology makes it so a competing commodity is much more useful than our first commodity, almost depleting demand for our first commodity, which would create a higher actual availability than predicted. Additionally, we could have a technology create new and better ways to provide more of our commodity, making the actual availability higher than projected and predicted. Alternatively, we could have new technologies actually require this commodity, which drives demand for the commodity higher and may make the actual long-term availability of the commodity much lower than projected. # 6. For the following questions, use the "app" available http://shiny.demog.berkeley.edu/josh/harvest/ to gather data on the relationship between harvest rate "h" and the sustainable (steady-state) yield. You can do this by moving the slider from 0 upwards by increments of 0.02. # 6.1 There is a slight bug in the app, so that it reports sustainable yields even when harvest rate is unsustainable. How can you inspect the graphs to check on sustainability? > The app shows a harvest over time even when our harvest rate h is absurdly high. If the bottom-right chart says sustainable yield = 0, then we know we are over the harvest rate. It would end up looking like (due to our restrictions on zoom) our graph is discontinuous. # 6.2 When h = 0.01, the sustainable yield is 0.0678, what is the sustainable yield when h = 0.02 and h = 0.04? > h = 0.02 => sustainable yield = 0.108 ; h = 0.04 => sustainable yield = 0.0239 # 6.3 What harvest rate maximizes the sustainable yield? [Hint: try all of the slider values] > At all other variables at default values (as in 6.2), we see that the sustainable yield increases as x goes from 0 to 0.022, then starts decreasing indefinitely. So the harvest rate h = 0.022 gives the maximum sustainable yield for our discrete measures. # 6.4 What harvest rate maximizes the yield during the first year of harvesting (this is about year 10 on the slider, or year 0 on the time axes of the graph)? (You can either report the value you find with the "app" or your theoretical answer) > Considering only the sustainable yield at t= 10 on the slider (t = 0 on graph), we see the yield is constant at 0 for all values of h. Perhaps the questions should ask us to look at the range t = 10 to t = 20, and take the "area under the curve" for this region. Investigating this, we see that the first year's harvest yield strictly increases as we increase h (even when inducing the "bug" where it's past sustainability). > So for the harvest rate that maximizes the first year's total yield from harvesting, we take the highest h value that is still sustainable, which is at about h = 0.076. # 6.5 Would harvest rates be higher in year "1" if we harvested at the sustainable rate found in part 6.3, or if they were .02 higher than the sustainable rate? > The harvest rates would be higher at h = 0.022 + 0.02 compared to h = 0.022, based on the trend we discussed the previous problem (that the harvest rates in year 1 increase with higher values of h). We verify this in the app. If we are comparing h = 0.022 to h = 0.076 + 0.02, then we can expect the first year's harvest to be higher at 0.02 past the sustainable rate. Although our results are good for the first year, we can expect our harvest rates to be worse eventually. # 6.6 When Becker advocates for attention to sustainability, how did Lam reply to his warning? (In 1 or 2 sentences). Lam notes "Although I agree with Becker’s concern about water, I am less concerned that we will run out of oil or any other resource that has clear property rights and active markets. ( https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4298828/ )". Lam draws a distinction between protected/restricted resources and free resources like water, in terms of attention to sustainability. On discussing the illegal loging problem in Indonesia, he writes "The point is that it will take institutional change, not simply the end of population growth, to solve the problem." Essentially, Lam agrees with the call for attention to sustainability, but he adds notes that stopping population growth is not the only factor involved in the solution. Congratulations! You are finished with Lab 4.