簡體   English   中英

模擬中的 Uniroot

[英]Uniroot in simulations

我試圖找到生成 function 的累積和為 0(或最接近 0 的值)的值。 我有一個簡單的計算:x - y(1-z) 其中 z 是關鍵變量。 問題是 x 和 y 是模擬的,因此它們不會保持不變。 這是可重現的示例:

set.seed(96)
#Create a dataframe with x and y. x = price_b;  y = price_a
month <- 1:120 #10 years
price_a <- runif(120, min = 9500, max = 12000) #random values for price_a
price_b <- runif(120, min = 4500, max = 6500) #random values for price_b
data <- data.frame(month, price_a, price_b)

#Function to calculate the stabilization cost
cumsum_cost <- function(z) {
  cost <- price_b - price_a*(1-z)
  cumsum <- cumsum(cost)
}

#Use function with z= 0.5 and create a column with the results
cumulative_cost <- cumsum_cost(0.5)
data$stabilization_cost <- cumulative_cost

#Try to use uniroot to find which value of z generates the last value of cumulative_cost to be 0.
uniroot(cumsum_cost, lower =0, upper = 1)

我知道,由於 function 的值 x 和 y 不斷變化,uniroot 將找不到解決方案。 有誰知道在這種情況下我能做什么? 提前致謝!

也許我誤解了,但你不能直接找到答案嗎?

z_zero <- 1-sum(price_b)/sum(price_a)
x <- cumsum_cost(z_zero)
tail(x,1)
#> [1] 6.093615e-11

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM