-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy_simulations.R
More file actions
380 lines (318 loc) · 11.8 KB
/
Copy pathpolicy_simulations.R
File metadata and controls
380 lines (318 loc) · 11.8 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
###############################################################################
# (A) INSTALL / LOAD REQUIRED PACKAGES
###############################################################################
if (!require("mnonr")) install.packages("mnonr", dependencies = TRUE)
if (!require("moments")) install.packages("moments", dependencies = TRUE)
if (!require("survival")) install.packages("survival", dependencies = TRUE)
if (!require("evd")) install.packages("evd", dependencies = TRUE)
if (!require("dplyr")) install.packages("dplyr", dependencies = TRUE)
library(mnonr)
library(moments)
library(survival)
library(evd)
library(dplyr)
###############################################################################
# (B) DEMAND GENERATION (Appendix B style)
###############################################################################
generate_data <- function(n = 50,
mu = c(100, 0),
variance = 100,
skewness = c(2.8, 0),
kurtosis = c(20, 3),
seed = 123) {
set.seed(seed)
# 2x2 covariance matrix with 'variance' for the first dimension
Sigma <- matrix(c(variance, 0,
0, 1),
nrow = 2,
ncol = 2)
generated_data <- unonr(
n = n,
mu = mu,
Sigma = Sigma,
skewness = skewness,
kurtosis = kurtosis
)
# Return only the first column as univariate demand
demands <- generated_data[, 1]
return(demands)
}
###############################################################################
# (C) KAPLAN–MEIER ESTIMATOR FUNCTIONS
###############################################################################
km_fit <- function(time, event) {
survfit(Surv(time, event) ~ 1, type = "kaplan-meier")
}
km_to_stepfun <- function(sf) {
timeVec <- sf$time
survVec <- sf$surv
cdfVec <- 1 - survVec
Sfun <- function(t) {
if (length(timeVec) == 0) {
return(1)
}
if (t < timeVec[1]) {
return(1)
}
if (t >= timeVec[length(timeVec)]) {
return(survVec[length(survVec)])
}
idx <- which(timeVec <= t)
survVec[max(idx)]
}
Ffun <- function(t) 1 - Sfun(t)
list(timeVec = timeVec, survVec = survVec, cdfVec = cdfVec,
Sfun = Sfun, Ffun = Ffun)
}
# Compute expected cost from a KM step function
km_expected_cost <- function(Y, timeVec, cdfVec, b, g) {
# b=shortage cost, g=holding cost
if (length(timeVec) < 1) return(NA)
pmf <- numeric(length(timeVec))
pmf[1] <- cdfVec[1]
if (length(timeVec) > 1) {
for (i in 2:length(timeVec)) {
pmf[i] <- cdfVec[i] - cdfVec[i-1]
}
}
overage <- pmax(Y - timeVec, 0)
underage <- pmax(timeVec - Y, 0)
E_over <- sum(overage * pmf)
E_under <- sum(underage * pmf)
cost <- g * E_over + b * E_under
cost
}
# We do a grid search from 0..(1.2 * max(timeVec))
km_optimal_cost <- function(time, event, b, g) {
# If no uncensored data => fallback
if (!any(event == 1)) {
return(list(Y_star = 0, cost_min = NA))
}
sf <- km_fit(time, event)
if (length(sf$time) < 1) {
return(list(Y_star = 0, cost_min = NA))
}
km_steps <- km_to_stepfun(sf)
if (length(km_steps$timeVec) < 1) {
return(list(Y_star = 0, cost_min = NA))
}
max_t <- max(km_steps$timeVec)
Y_grid <- seq(0, max_t * 1.2, length.out = 100)
cost_grid <- sapply(Y_grid, function(yy) {
km_expected_cost(yy, km_steps$timeVec, km_steps$cdfVec, b, g)
})
idx_min <- which.min(cost_grid)
list(Y_star = Y_grid[idx_min],
cost_min = cost_grid[idx_min])
}
###############################################################################
# (D) GPD FITTING & COST (Appendix B)
###############################################################################
gpd_negLL <- function(params, y) {
xi <- params[1]
beta <- params[2]
if (beta <= 0) return(1e10)
inside <- 1 + (xi * y) / beta
if (any(inside <= 0)) return(1e10)
log_f <- -log(beta) - (1/xi + 1)* log(inside)
-sum(log_f)
}
gpd_fit <- function(y_data, start_params = c(xi = 0.1, beta = 1)) {
optim(par = start_params, fn = gpd_negLL, y = y_data, method = "BFGS")
}
gpd_cdf <- function(y, xi, beta) {
out <- numeric(length(y))
for (i in seq_along(y)) {
if (y[i] < 0) {
out[i] <- 0
} else {
out[i] <- 1 - (1 + xi * y[i] / beta)^(-1/xi)
}
}
out
}
gpd_expected_cost <- function(Y, data, threshold, b, g, xi, beta) {
# b=shortage, g=holding
n <- length(data)
d_leq <- data[data <= threshold]
p_leq <- length(d_leq) / n
over_leq <- pmax(Y - d_leq, 0)
under_leq <- pmax(d_leq - Y, 0)
E_over_leq <- mean(over_leq)
E_under_leq <- mean(under_leq)
xMax <- max(data) - threshold
if (xMax < 1e-6) {
return(g * p_leq * E_over_leq + b * p_leq * E_under_leq)
}
nGrid <- 200
xGrid <- seq(0, xMax, length.out = nGrid)
cdfVals <- gpd_cdf(xGrid, xi, beta)
pmfVals <- c(cdfVals[1], diff(cdfVals))
if (sum(pmfVals) <= 1e-15) {
return(g * p_leq * E_over_leq + b * p_leq * E_under_leq)
}
pmfVals <- pmfVals / sum(pmfVals)
over_exceed <- pmax(Y - (threshold + xGrid), 0)
under_exceed <- pmax((threshold + xGrid) - Y, 0)
E_over_exceed <- sum(over_exceed * pmfVals)
E_under_exceed <- sum(under_exceed * pmfVals)
E_over <- p_leq * E_over_leq + (1 - p_leq)*E_over_exceed
E_under <- p_leq * E_under_leq + (1 - p_leq)*E_under_exceed
g * E_over + b * E_under
}
gpd_optimal_cost <- function(demands, b, g, thresh_prob = 0.8) {
if (length(demands) < 2) {
return(list(Y_star = mean(demands), cost_min = NA))
}
threshold <- quantile(demands, probs = thresh_prob)
exceedances <- demands[demands > threshold] - threshold
if (length(exceedances) < 2) {
return(list(Y_star = mean(demands), cost_min = NA))
}
fit <- gpd_fit(exceedances)
xi_hat <- fit$par[1]
beta_hat <- fit$par[2]
Y_grid <- seq(0, max(demands)*1.2, length.out = 100)
cost_grid <- sapply(Y_grid, function(yy) {
gpd_expected_cost(yy, demands, threshold, b, g, xi_hat, beta_hat)
})
idx_min <- which.min(cost_grid)
list(Y_star = Y_grid[idx_min],
cost_min = cost_grid[idx_min])
}
###############################################################################
# (E) ADDITIONAL POLICIES: (s,S), Base-Stock, EOQ
###############################################################################
# For these examples, we fix certain policy parameters.
# In practice, you'd fine-tune s,S or base_stock, ordering_cost, etc.
s_const <- 90
S_const <- 120
base_stock_const <- 110
ordering_cost <- 50
compute_sS_policy <- function(demands, s, S, b, g) {
# We'll define the final inventory = last demand
current_inventory <- tail(demands, 1)
reorder_amount <- ifelse(current_inventory < s, S - current_inventory, 0)
overage <- pmax(reorder_amount - demands, 0)
underage <- pmax(demands - reorder_amount, 0)
cost <- g * mean(overage) + b * mean(underage)
list(Y_star = reorder_amount, cost_min = cost)
}
compute_base_stock <- function(demands, base_stock, b, g) {
current_inventory <- tail(demands, 1)
reorder_amount <- max(0, base_stock - current_inventory)
overage <- pmax(reorder_amount - demands, 0)
underage <- pmax(demands - reorder_amount, 0)
cost <- g * mean(overage) + b * mean(underage)
list(Y_star = reorder_amount, cost_min = cost)
}
compute_eoq <- function(demands, b, g, ordering_cost) {
demand_rate <- mean(demands)
reorder_amount <- sqrt((2 * demand_rate * ordering_cost) / g)
overage <- pmax(reorder_amount - demands, 0)
underage <- pmax(demands - reorder_amount, 0)
cost <- g * mean(overage) + b * mean(underage)
list(Y_star = reorder_amount, cost_min = cost)
}
###############################################################################
# (F) MAIN TIME-SERIES SIMULATION
###############################################################################
# 1) Parameter combinations
b_vals <- c(0,1,2) # shortage cost
g_vals <- c(0,1,2) # holding cost
variance_vals <- c(100, 10000)
skewness_vals <- c(0.2, 0, 2, 3)
kurtosis_vals <- c(5, 3, 1)
n_sims <- 30
n_periods <- 50
# Data frame to accumulate final results
all_results <- data.frame()
param_grid <- expand.grid(
b = b_vals,
g = g_vals,
variance = variance_vals,
skewness = skewness_vals,
kurtosis = kurtosis_vals
)
for (i in seq_len(nrow(param_grid))) {
b_par <- param_grid$b[i]
g_par <- param_grid$g[i]
var_par <- param_grid$variance[i]
skew_par <- param_grid$skewness[i]
kurt_par <- param_grid$kurtosis[i]
# Storage matrices: 30 sims x 50 periods
Ystar_KM_mat <- matrix(NA, n_sims, n_periods)
Cost_KM_mat <- matrix(NA, n_sims, n_periods)
Ystar_GPD_mat <- matrix(NA, n_sims, n_periods)
Cost_GPD_mat <- matrix(NA, n_sims, n_periods)
Ystar_sS_mat <- matrix(NA, n_sims, n_periods)
Cost_sS_mat <- matrix(NA, n_sims, n_periods)
Ystar_Base_mat <- matrix(NA, n_sims, n_periods)
Cost_Base_mat <- matrix(NA, n_sims, n_periods)
Ystar_EOQ_mat <- matrix(NA, n_sims, n_periods)
Cost_EOQ_mat <- matrix(NA, n_sims, n_periods)
for (sim in 1:n_sims) {
# Generate a 50-period demand series
set_seed <- 100000*i + sim # unique seed offset
demands <- generate_data(
n = n_periods,
mu = c(100,0),
variance = var_par,
skewness = c(skew_par, 0),
kurtosis = c(kurt_par, 3),
seed = set_seed
)
for (t in 1:n_periods) {
d_sub <- demands[1:t]
# == (A) KM
time_t <- ifelse(d_sub >= 150, 150, d_sub)
event_t <- ifelse(d_sub >= 150, 0, 1)
km_res <- km_optimal_cost(time_t, event_t, b_par, g_par)
Ystar_KM_mat[sim, t] <- km_res$Y_star
Cost_KM_mat[sim, t] <- km_res$cost_min
# == (B) GPD
gpd_res <- gpd_optimal_cost(d_sub, b_par, g_par, thresh_prob = 0.8)
Ystar_GPD_mat[sim, t] <- gpd_res$Y_star
Cost_GPD_mat[sim, t] <- gpd_res$cost_min
# == (C) (s,S)
sS_res <- compute_sS_policy(d_sub, s_const, S_const, b_par, g_par)
Ystar_sS_mat[sim, t] <- sS_res$Y_star
Cost_sS_mat[sim, t] <- sS_res$cost_min
# == (D) Base-Stock
base_res <- compute_base_stock(d_sub, base_stock_const, b_par, g_par)
Ystar_Base_mat[sim, t] <- base_res$Y_star
Cost_Base_mat[sim, t] <- base_res$cost_min
# == (E) EOQ
eoq_res <- compute_eoq(d_sub, b_par, g_par, ordering_cost)
Ystar_EOQ_mat[sim, t] <- eoq_res$Y_star
Cost_EOQ_mat[sim, t] <- eoq_res$cost_min
}
}
# Now compute the mean at each period across the 30 simulations
for (t in 1:n_periods) {
row_out <- data.frame(
b = b_par,
g = g_par,
variance = var_par,
skewness = skew_par,
kurtosis = kurt_par,
t = t,
Mean_Ystar_KM = mean(Ystar_KM_mat[, t], na.rm = TRUE),
Mean_Cost_KM = mean(Cost_KM_mat[, t], na.rm = TRUE),
Mean_Ystar_GPD = mean(Ystar_GPD_mat[, t], na.rm = TRUE),
Mean_Cost_GPD = mean(Cost_GPD_mat[, t], na.rm = TRUE),
Mean_Ystar_sS = mean(Ystar_sS_mat[, t], na.rm = TRUE),
Mean_Cost_sS = mean(Cost_sS_mat[, t], na.rm = TRUE),
Mean_Ystar_Base = mean(Ystar_Base_mat[, t], na.rm = TRUE),
Mean_Cost_Base = mean(Cost_Base_mat[, t], na.rm = TRUE),
Mean_Ystar_EOQ = mean(Ystar_EOQ_mat[, t], na.rm = TRUE),
Mean_Cost_EOQ = mean(Cost_EOQ_mat[, t], na.rm = TRUE)
)
all_results <- rbind(all_results, row_out)
}
}
# Write to CSV
write.csv(all_results, file = "newsvendor_with_all_policies.csv", row.names = FALSE)
###############################################################################
# END
###############################################################################