Get startedGet started for free

Optimizing Costs

Now we will use the functions you've built to optimize our cost of production. We are interested in maximizing average profits. However, our profits depend on a number of factors, while we only control cost. Thus, we can simulate the uncertainty in the other factors and vary cost to see how our profits are impacted.

Since you manage the small corn farm, you have the ability to choose your cost - from $100 to $5,000. You want to choose the cost that gives you the maximum average profit. In this exercise, we will simulate multiple outcomes for each cost level and calculate an average. We will then choose the cost that gives us the maximum mean profit. Upon completion, you will have a framework for selecting optimal inputs for business decisions.

This exercise is part of the course

Statistical Simulation in Python

View Course

Exercise instructions

  • Initialize the empty dictionary results.
  • For each cost level, simulate profits using the pre-loaded profits() function and append them to tmp_profits.
  • Store the average of tmp_profits for each cost level in the results dictionary.
  • Find the cost level cost_max that has the maximum average profit by running results through the list comprehension.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Initialize results and cost_levels variables
sims, results = 1000, ____
cost_levels = np.arange(100, 5100, 100)

# For each cost level, simulate profits and store mean profit
for cost in cost_levels:
    tmp_profits = []
    for i in range(sims):
        tmp_profits.append(____)
    results[cost] = np.mean(____)
    
# Get the cost that maximizes average profit
cost_max = [x for x in ____.keys() if ____[x] == max(____.values())][0]
print("Average profit is maximized when cost = {}".format(cost_max))
Edit and Run Code