-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seminar9.R
115 lines (99 loc) · 3.91 KB
/
Seminar9.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
library(igraph)
library(decisionSupport)
# Path impact way model
hail_path <- graph.formula(HailNet -+ Yield,
HailNet -+ Cost,
HailEvent -+ Yield,
Yield -+ MarketPrice,
MarketPrice -+ NPV,
Cost -+ NPV,
Discount -+ NPV)
plot(hail_path)
# Input estimates
hail_estimates <- data.frame(variable = c("yield",
"var_CV",
"initial_investment",
"price",
"p_hail"),
lower = c(6000, 20, 500, 5, 0.02),
median = NA,
upper = c(14000, 20, 1000, 80, 0.2),
distribution = c("posnorm",
"const",
"posnorm",
"posnorm",
"posnorm"),
label = c("Yield (kg/ha)",
"Coefficient of variation",
"Investment cost (USD)",
"Market price (EUR/kg)",
"% chance hail"),
Description = c("Yield under normal conditions",
"Coefficient of variation (measure of relative variability)",
"Investment cost",
"Market price achieved for yields (EUR/kg)",
"Probability of the hail storm"))
hail_estimates
hail_function <- function(){
# use vv() to add variability to the
# random draws of yield and of price
# over a 20 year simulation
yields <- vv(var_mean = yield,
var_CV = var_CV,
n = 20)
prices <- vv(var_mean = price,
var_CV = var_CV,
n = 20)
# use rep() to simulate the initial_investment
# only in the first year (assuming the net lasts 20 years)
invest_costs <- c(initial_investment, rep(0, 19))
# use p_hail in the chance_event()
# to adjust yield for probability of hail
# assuming no yield at all in the event of hail
hail_adjusted_yield <- chance_event(chance = p_hail,
value_if = 0,
value_if_not = yield,
n = 20)
# calculate profit without net
profit_no_net <- hail_adjusted_yield*prices
# calculate profit with the net
profit_with_net <- (yields*prices)-invest_costs
# use 'discount' to calculate net present value
# 'discount_rate' is expressed in percent
NPV_no_net <- discount(profit_no_net, discount_rate = 5, calculate_NPV = TRUE)
NPV_net <- discount(profit_with_net, discount_rate = 5, calculate_NPV = TRUE)
# calculate the overall NPV of the decision (do - don't do)
NPV_decision <- NPV_net-NPV_no_net
return(list(NPV_no_net = NPV_no_net,
NPV_net = NPV_net,
NPV_decision = NPV_decision))
}
DT2[intersect(which(V3==1), 1:5), V2 := 1]
hail_estimates[intersect(which("variable" == "initial_investment"), c(2,4)),]
hail_estimates[hail_estimates$variable=="initial_investment", c(2,4)]
library(microbenchmark)
set.seed(1234)
x <- x <- c(0, 0, 20, 40, 80, 90)
microbenchmark(times = 1000,
filter = {
x[x < 10] <- 10
x[x > 55] <- 55
x
},
ifelse = {
x <- ifelse(x < 10, 10, x)
x <- ifelse(x > 55, 55, x)
x
},
for_loop = {
for (i in 1:length(x)){
if ((x[i]) < 10){
x[i] <- 10
}
if (x[i] > 55){
x[i] <- 55
}
}
x
}
)