# Load the 'tidyverse' library
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Define n
n <- 1000

# Empty data frame for 1000 simulated days
data <- data.frame(day = seq(1:n),
                demand = NA, # demand in Rockport
                quantity = NA, # quantity sold
                price = NA, # price per pound
                cost = 10000, # cost of daily operations
                earnings = NA)
head(data)
##   day demand quantity price  cost earnings
## 1   1     NA       NA    NA 10000       NA
## 2   2     NA       NA    NA 10000       NA
## 3   3     NA       NA    NA 10000       NA
## 4   4     NA       NA    NA 10000       NA
## 5   5     NA       NA    NA 10000       NA
## 6   6     NA       NA    NA 10000       NA

Fill in the following dataset with random numbers drawn from the probability distributions defined in the case description—distributions for demand, quantity and price.

# Empty data frame for 1000 simulated days
set.seed(123)
#Define n
n <- 1000
# Fill the dataset using the mutate function
sim <- data |> 
  mutate( demand = sample(c(0,1000, 2000, 3000, 4000, 5000, 6000 ), # Randomly sample demand values
                                n,# Number of samples
                                replace = T,
                                # Probabilities for each demand value
                                prob = c(0.02, 0.03, 0.05, 0.08, 0.33, 0.29, 0.20)),
          quantity = ifelse(demand > 3500, 3500,demand),#max of 3500 or actual demand
          price = rnorm(n, mean = 3.65, sd =.2 ),# Generate normally distributed price values
          earnings = price * quantity - cost)# Calculate earnings

# Ensure price is not negative
sim$price[sim$price < 0] <- 0

# View the first few rows of the simulation data
head(sim)
##   day demand quantity    price  cost  earnings
## 1   1   4000     3500 3.529621 10000  2353.675
## 2   2   6000     3500 3.451260 10000  2079.411
## 3   3   5000     3500 3.855357 10000  3493.750
## 4   4   3000     3000 3.800212 10000  1400.637
## 5   5   2000     2000 3.348167 10000 -3303.667
## 6   6   4000     3500 3.630971 10000  2708.397

Q1

Plot simulated earnings at Rockport.

# Load the ggplot2 library
library(ggplot2)

# Create a histogram of the simulated earnings
ggplot(sim, aes(x = earnings)) +                
       geom_histogram(binwidth = 600, fill = "pink", color = "white") +  # Add a histogram
       labs(title = "Histogram of Simulated Earnings at Rockport",  # Add a title to the plot
            x = "Earnings ($)",                            # Label for the x-axis
            y = "Frequency")                              # Label for the y-axis

Q2

Earnings at Gloucester are fixed at $1375 ($3.25 x 3500 - $10,000). What is P(F > 1375) at Rockport? Write down the answer.

# Calculate the probability that earnings exceed $1,375
prob <- mean( sim$earnings >1375)
prob
## [1] 0.826

Q3

What is the probability that Mr. Conley will lose money? We can express this as P(F < 0). Write down the answer.

# Calculate the probability that earnings are less than $0 (indicating a loss)
prob <- mean( sim$earnings < 0)
prob
## [1] 0.099

Q4

What is the mean of F? Write down the answer.

# Calculate the mean of the simulated earnings
F <- mean(sim$earnings)
print(F)
## [1] 1879.699

Q5

What is your advice to Mr. Conley? Write one paragraph in which you argue a position. In your answer please incorporate the quantitative details from your simulation, and consider in particular the trade-off between risk and reward.

After analyzing the data, I advise Mr. Conley to take the risk and sell his daily codfish catch at Rockport. The probability of incurring a loss is only 9.9%, which is relatively low. Furthermore, there is an 83% chance of earning more in Rockport than in Gloucester. By choosing to sell his codfish catch in Rockport, Mr. Conley can expect an average earning of approximately $1,879, significantly higher than the fixed earnings of $1,375 he would make in Gloucester. This analysis suggests that the potential rewards in Rockport outweigh the associated risks, making it a more favorable option for his business.