Last active
September 5, 2015 12:55
-
-
Save chiral/11508bfd178c8c2fc12b to your computer and use it in GitHub Desktop.
marketing action optimization with binomial test and frontier curve
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ad | Imp | Clk | |
---|---|---|---|
A | 300 | 6 | |
B | 500 | 14 | |
C | 150 | 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
input <- read.csv("input.csv",header=T) | |
ctr <- data.frame() | |
for (i in 1:nrow(input)) { | |
r<-input[i,] | |
bt1 <- binom.test(r$Clk,r$Imp,conf.level=0.80) | |
bt2 <- binom.test(r$Clk,r$Imp,conf.level=0.95) | |
p <- bt1$estimate | |
conf1 <- bt1$conf.int | |
conf2 <- bt2$conf.int | |
p_var <- p*(1-p)/r$Imp | |
tmp <- data.frame(CTR=p,CTRvar=p_var, | |
CTR11=conf1[1],CTR12=conf1[2], | |
CTR21=conf2[1],CTR22=conf2[2]) | |
ctr <- rbind(ctr,tmp) | |
} | |
input <- cbind(input,ctr) | |
g <- ggplot( | |
input, | |
aes ( | |
x = Ad, | |
middle = CTR, | |
ymin = CTR21, | |
ymax = CTR22, | |
lower = CTR11, | |
upper = CTR12, | |
color = Ad | |
) | |
) | |
ymin <- min(input$CTR21) | |
ymax <- max(input$CTR22) | |
# relaxing ylim | |
margin <- (ymax-ymin)/5 | |
ymin <- max(0,ymin-margin) | |
ymax <- min(1,ymax+margin) | |
g <- g+geom_boxplot(stat="identity") | |
g <- g+coord_cartesian(ylim=c(ymin,ymax)) | |
png("bar.png") | |
plot(g) | |
dev.off() | |
V <- diag(input$CTRvar) | |
V1 <- V | |
V1 <- rbind(V1,0) | |
V1 <- rbind(V1,0) | |
V1 <- cbind(V1,0) | |
V1 <- cbind(V1,0) | |
N <- nrow(input) | |
V1[N+1,1:N] <- input$CTR | |
V1[N+2,1:N] <- 1 | |
V1[1:N,N+1] <- -input$CTR | |
V1[1:N,N+2] <- -1 | |
x <- c() | |
y <- c() | |
for (r in seq(ymin,ymax,length.out=100)) { | |
bvec <- c(rep(0,N),r,1) | |
wvec <- solve(V1,bvec) | |
w <- wvec[1:N] | |
if (all(w>=0)) { | |
x1 <- sqrt(t(w) %*% V %*% w) | |
x <- c(x,x1) | |
y <- c(y,r) | |
out <- c(r,x1,w*100) | |
names(out) <- c('CTR','CTR_sd',paste(input$Ad,'_weight(%)',sep='')) | |
print(out) | |
} | |
} | |
png("frontier.png") | |
plot(x,y,type="p",xlab="CTR_sd",ylab="CTR") | |
lines(x,y) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment