DATA 604: Week 3 Discussion

Problem 3.5.17

Walther has a roadside produce stand where he sells oats, peas, beans, and barley. He buys these products at per-pound wholesale prices of, respectively, $1.05, $3.17, $1.99, and $0.95; he sells them at per-pound retail prices of, respectively, $1.29, $3.76, $2.33, and $1.65. Each day the amount demanded (in pounds) could be as little as zero for each product, and as much as 10, 8, 14, and 11 for oats, peas, beans, and barley, respectively; he sells only whole-pound amounts, no partial pounds. Assume a discrete uniform distribution for daily demand for each product over its range; assume as well that Walther always has enough inventory to satisfy all demand. The summer selling season is 90 days, and demand each day is independent of demand on other days. Create a spreadsheet simulation that will, for each day as well as for the whole season, simulate Walther’s total cost, total revenue and total profit.

set.seed(123)

crop_data <- 
  data.frame(
     crop = c("oats", "peas", "beans", "barley"),
     wac = c(1.05, 3.17, 1.99, 0.95),     # acquisition cost
     sell = c(1.29, 3.76, 2.23, 1.65),    # selling price
     max_demand = c(10, 8, 14, 11),       # maximum demand for this crop
     sim_demand = 0,                      # simulated daily demand for the given crop; uniform distribution
     cost = 0,                            # crop simulated cost (wac * simulated demand) 
     revenue = 0,                         # crop simulated revenue value (selling price * simulated demand)  
     profit = 0,                          # crop simulated profit value (selling price * simulated demand)
     stringsAsFactors = F) 
                        
         

sim_crop_season <- function(run_num, days) {
  
  # replicate each crop entry by the number of days in the season to simulate
  crop__sim <- as.data.frame(lapply(crop_data, rep, days))
  
  crop__sim$run_num <- run_num
  
  # set the simulated daily demand for the crop
  # This is a discrete uniform distribution for daily demand for each product over its range
  crop__sim$sim_demand <- round(runif(days, 0, crop__sim$max_demand), digits=0)  

  # calcualate the daily simulated cost, revenue, and profit for each crop  
  crop__sim <- 
               within(crop__sim,
                       { cost = wac * sim_demand
                         revenue = sell * sim_demand
                         profit = (sell * sim_demand) - (wac * sim_demand)
                       }
               )
  
}

Run the 90-day Simulation 100 times

sim <-  rbindlist(lapply(1:100, function(x) sim_crop_season(x, 90))) %>% as.data.frame()

Simulation results of a 90-day crop season (daily):

Crop Season Aggregate Metrics

sim %>% 
       group_by(run_num) %>% 
       summarise(cost    = sum(cost),
                 revenue = sum(revenue),
                  profit = sum(profit)) -> agg
  gather(agg, variable, value, -run_num) %>% arrange(run_num, variable) %>%
  ggplot( aes(x=variable, y = value, fill = variable)) +  geom_boxplot( alpha=0.7 ) +
          ggtitle("Crop Simulation Results") +
        theme_bw() + labs(x = "Metric", y="Total ($)") +
        theme(plot.title = element_text(size = 14, face = "bold"),
              text = element_text(size = 12),
              axis.title = element_text(face="bold"),
              axis.text.x=element_text(size = 11)) +
        scale_fill_brewer(palette = "Accent")

Details