我目前正在 R 中建模定价和折扣系统。
我的数据框如下所示:
df = structure(
list(
Customers = structure(
c(1L, 1L, 1L, 2L, 2L, 2L),
.Label = c("A", "B"),
class = "factor"
),
Products = structure(
c(1L,
2L, 3L, 1L, 2L, 3L),
.Label = c("P1", "P2", "P3"),
class = "factor"
),
Old_Price = c(5, 2, 10, 7, 4, 8),
New_Price = c(6, 3, 9,
6, 3, 9)
),
class = "data.frame",
row.names = c(NA,-6L)
)
有几个客户使用“旧价格”和“新价格”购买不同的产品。我现在想为每个客户确定一个折扣参数(从 -1.0 到 1.0 的实数),以最小化旧价格和新价格之间的差异。
因为我对优化等方面了解不多,所以我目前的方法是执行以下操作,这似乎非常低效,并且无论如何可能不会导致最佳解决方案:
df %>%
mutate(Individual_Discount = (Old_Price-New_Price)/New_Price) %>% # Identify optimal discount individually
group_by(Customers) %>%
mutate(Optimal_Discount = mean(Individual_Discount)) # Average individual discount to get approximate discount for customer
解决这种情况的最佳方法是什么?如何在 R 中实现它?
更新:
更清楚地说明问题。有一个如下所示的数据框:
Customers | Product | Old Price | New Price | Delta | Discount | Discounted New Price
CustA | ProdA | 10.00 | 12.00 | 2.00 | -0.167 | 10.00
CustA | ProdB | 30.00 | 25.00 | -5.00 | 0.2 | 30.00
CustB | ProdA | 15.00 | 12.00 | -3.00 | 0.25 | 15.00
CustB | ProdB | 20.00 | 25.00 | 5.00 | -0.2 | 20.00
折扣表示将旧价格和新价格之间的差异减少到零的最佳折扣(因此新价格 2 将计算为新价格 + 新价格 * 折扣)。但是每个客户只能获得一个折扣,那么我应该为每个客户选择哪个折扣以最小化剩余的增量(折扣新价格和旧价格之间的增量)?
更新2:数学关系
Delta = New_Price - Old_Price
折扣 = Delta / -New_Price
Discounted_New_Price = New_Price+New_Price*Discount
更新3:
我已经根据评论拟合了一个线性模型,但是基于分组线性模型的梯度的“线性折扣”产生的结果比我的“平均黑客”更差:
df %>%
group_by(Customers) %>%
do({ co <- coef(lm(Old_Price ~ New_Price, .))
mutate(., linear_discount = co[2])
}) %>%
ungroup %>%
mutate(linear_discount = 1/linear_discount-1) %>%
mutate(linear_price = New_Price+New_Price*linear_dis
结果是
Customers | Product | Old Price | New Price | Linear Discount | Linear Price | Discounted New Price
CustA | Prod1 | 05.00 | 06.00 | -0.25 | 4.50
CustA | Prod2 | 02.00 | 03.00 | -0.25 | 2.25
CustA | Prod3 | 10.00 | 09.00 | -0.25 | 6.75
CustB | Prod1 | 07.00 | 06.00 | 0.50 | 9.00
...