開始使用免費開始

利潤建模

在上一個練習中,你已經建立了玉米產量的模型。對小型農場而言,你通常無法掌控玉米的價格或需求。假設價格服從常態分佈,平均數為 40、標準差為 10。你已獲得一個函式 corn_demanded(),它會根據價格決定玉米的需求量。這很合理,因為需求通常由市場決定,不在你的掌控範圍內。

在這個練習中,你要撰寫一個函式,把先前模擬的各個變數整合起來,用來計算利潤。這個函式唯一的輸入是固定的生產成本。完成後,你會得到一個函式,能在給定成本下回傳一次模擬的利潤結果。之後你就可以用這個函式來規劃你的成本。

本練習屬於課程

Python 的統計模擬

檢視課程

練習說明

  • price 建模為一個常態隨機變數,平均數 40、標準差 10。
  • 呼叫你在上一個練習中設計的函式 corn_produced(rain, cost) 以取得玉米的 supply
  • price 作為輸入呼叫 corn_demanded(),取得 demand
  • Profit \(=\) quantity \(\times\) price \(-\) cost。 如果玉米的產量多於需求(supply > demand),則實際賣出的數量為 demand,否則為 supply

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Function to calculate profits
def profits(cost):
    rain = np.random.normal(50, 15)
    price = np.random.____
    supply = ____
    demand = ____
    equil_short = supply <= demand
    if equil_short == True:
        tmp = ____*price - cost
        return tmp
    else: 
        tmp2 = ____*price - cost
        return tmp2
result = profits(cost)
print("Simulated profit = {}".format(result))
編輯並執行程式碼