library(tidyverse)
Given the below data, simulate the total cost, total revenue, and total profit for each day and for a 90-day season (via problem 3.5.17 in the text pg71):
nm_code <- c(1:4)
produce <- c("oats", "peas", "beans", "barley")
w_price <- c(1.05, 3.17, 1.99, 0.95) # per pound wholesale $
r_price <- c(1.29, 3.76, 2.23, 1.65) # per pound retail $
D_min <- c(rep(0,4)) # zero vector min demand
D_max <- c(10, 8, 14, 11) # upper-range demand
t_season <- 90 # number of days to run
my_df <- cbind(w_price, r_price, D_max) %>%
as.data.frame()
rownames(my_df) <- produce
my_df
## w_price r_price D_max
## oats 1.05 1.29 10
## peas 3.17 3.76 8
## beans 1.99 2.23 14
## barley 0.95 1.65 11
Below I’ve created a function that generates a single day’s business and captures all of day’s output in single format - a business day.
D_gen <- function(i, d, w, r, nms){
day <- i
dmnd_qty <- round(runif(length(d))*d, digits = 0)
cost <- dmnd_qty * w
rev <- dmnd_qty * r
prof <- dmnd_qty * (r-w)
# below is the raw data frame containing cost, rev, & profit
# PER produce item. Detail not shown.
biz_day_det <- cbind(nms, day, dmnd_qty, cost, rev, prof) %>%
as.data.frame()
biz_day <- c(day=day,
daily_cost = sum(biz_day_det$cost),
daily_rev = sum(biz_day_det$rev),
daily_profit = sum(biz_day_det$prof)) %>%
t() %>%
as.data.frame()
return(biz_day)
}
Here’s an example of it running on day’s 1 through 4 - the results are randomly generated and driven by the specified ranges:
## day daily_cost daily_rev daily_profit
## 1 1 23.00 28.55 5.55
## 2 2 28.79 37.38 8.59
## 3 3 44.00 52.63 8.63
## 4 4 29.74 34.63 4.89
To capture the information for a season, I’ve created the below function that re-runs the above function for a given number of days, sums the results, and produces the season totals among cost, revenue, and profit.
season_run <- function(season_t){
a_season <- NULL #empty season
for(i in 1:season_t){
a_season <- rbind(a_season,
D_gen(i, D_max, w_price, r_price, nm_code)) %>%
as.data.frame()
}
season_results<- c(cost = sum(a_season$daily_cost),
revenue = sum(a_season$daily_rev),
profit = sum(a_season$daily_profit))
return(season_results)
}
Below, please find 10 different 90-day season runs - i.e. each row represents the total season’s cost, revenue, and profit:
## cost revenue profit
## [1,] 3410.88 4256.15 845.27
## [2,] 3478.20 4319.31 841.11
## [3,] 3513.92 4351.77 837.85
## [4,] 3537.85 4383.23 845.38
## [5,] 3305.83 4111.16 805.33
## [6,] 3154.14 3936.89 782.75
## [7,] 3757.03 4626.99 869.96
## [8,] 3372.81 4198.38 825.57
## [9,] 3349.09 4184.70 835.61
## [10,] 3289.34 4116.93 827.59