Search Engine Optimization:效用最大化

函数准备

#目标函数depend
depend = function(data, x0, maxe_p, B0 = 2000){
             y = sum(data$utility * x0) + 0.5*maxe_p * (B0 / 2 - (sum(data$price * x0)^2 / (2*B0)))
             return(y)
}

#到二进制函数ten2two
ten2two = function(ten){
              x = rbinom(17, 0, 1)
              for(i in 17:1) {
                  a = ten %% 2
                  x[i] = a
                  ten = ten %/% 2
                  if(ten == 0)  break
              }
              return(x)
         }

#计算长尾比例
longtail = function(data, max_x){
               data$max_x = max_x
               temp = (data$volume[data&max_x == 1] >= quantile(x = data$volume, probs = 0.8, type = 3))
               longtail_prop = length(which(temp == FALSE)) / length(which(is.na(temp) == FALSE))
               return(longtail_prop)
}

主函数

#导入数据
Rdata <- read.csv("C:/Users/Administrator.ZE1XUQWPNXEUCFB/Desktop/clean/R_Room/4.10 seo/Rdata.csv")
library(ggplot2)

#定义相关变量
maxep = max(Rdata$utility/Rdata$price)
max_y = 0
max_x = 0
y0 = 0
x0 = 0
MAXY = 0
LongTail = 0
j = 1

#预算为2万到20万
budget_seris = seq(2, 20, 1)*10000

#最大化效用
for(budget in budget_seris){
    for(i in 0:(2^17-1)) {
        x0 = ten2two(i)
        if(sum(x0*Rdata$price) > budget)  next
        y0 = depend(data = Rdata, x0 = x0, maxe_p = maxep, B0 = budget)
        if(y0 > max_y) {
            max_y = y0 
            max_x = x0
        }    
     }
    
    prop = longtail(data = Rdata, max_x)
    MAXY[j] = max_y
    LongTail[j] = prop
    j = j+1

}

预算与效用的关系

qplot(budget_seris, MAXY) +  geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

预算与关键词长尾比例的关系

qplot(budget_seris, LongTail) +  geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.