Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### title: MPQ 4* pulling v2
- ### author: justsing
- ### date: June 7, 2018
- ## Initial Parameters
- library("data.table")
- library("ggplot2")
- library("plyr")
- set.seed(17)
- nsims = 10000
- options(digits = 3)
- n4 = 65
- nlatest = 12
- n5 = 3
- p4 = 0.85
- p_old_latest = p4/(2*nlatest)
- p_old_vintage = p4/(2*(n4-nlatest))
- p_new = p4/n4
- p_BH = 0.05
- plist_old = c(rep(p_old_latest, nlatest), rep(p_old_vintage, (n4-nlatest)))
- plist_new = rep(p_new, n4)
- plist_five = rep((1-p4)/n5, n5)
- ## Function for finding number of pulls needed
- pull_lt <- function(plist4, plist5, specific, BH, old, chosen){
- totalpulls = 0
- fours = matrix(0, nrow=n4, ncol=3)
- four_numcovers = rep(0, n4)
- four_maxcovers = 0
- while (four_maxcovers < 13){
- totalpulls = totalpulls + 1
- pulled_four = rbinom(1, 1, p4)
- pulled_BH = rbinom(1, 1, p_BH)
- if (pulled_four >= 1){
- char = sample(1:n4, 1, prob=plist4)
- cover = sample(1:3, 1, prob=c(1, 1, 1))
- fours[char, cover] = min((fours[char, cover]+1*(four_numcovers[char] < 13)), 5)
- four_numcovers = rowSums(fours)
- if (BH >= 1 & pulled_BH >= 1){
- cover = sample(1:3, 1, prob=c(1, 1, 1))
- fours[chosen, cover] = min((fours[chosen, cover]+1*(four_numcovers[chosen] < 13)), 5)
- four_numcovers = rowSums(fours)
- }
- }
- four_maxcovers = ifelse(specific >= 1, four_numcovers[chosen], max(four_numcovers))
- }
- four_dist = rep(0, 14)
- four_dist[2:14] = tabulate(four_numcovers, 13)
- four_dist[1] = n4 - sum(four_dist[2:14])
- c(old, specific, BH, chosen, totalpulls, four_dist)
- }
- ## Run simulations for pulls needed
- ptm <- proc.time()
- old_latest_noBH = t(replicate(nsims, pull_lt(plist4=plist_old, plist5=plist_five, specific=1, BH=0, old=1, chosen=1)))
- old_latest_BH = t(replicate(nsims, pull_lt(plist4=plist_old, plist5=plist_five, specific=1, BH=1, old=1, chosen=1)))
- old_vintage_noBH = t(replicate(nsims, pull_lt(plist4=plist_old, plist5=plist_five, specific=1, BH=0, old=1, chosen=n4)))
- old_vintage_BH = t(replicate(nsims, pull_lt(plist4=plist_old, plist5=plist_five, specific=1, BH=1, old=1, chosen=n4)))
- old_any_noBH = t(replicate(nsims, pull_lt(plist4=plist_old, plist5=plist_five, specific=0, BH=0, old=1, chosen=1)))
- old_any_BH = t(replicate(nsims, pull_lt(plist4=plist_old, plist5=plist_five, specific=0, BH=1, old=1, chosen=1)))
- old_any_BH_vint = t(replicate(nsims, pull_lt(plist4=plist_old, plist5=plist_five, specific=0, BH=1, old=1, chosen=n4)))
- new_latest_noBH = t(replicate(nsims, pull_lt(plist4=plist_new, plist5=plist_five, specific=1, BH=0, old=0, chosen=1)))
- new_latest_BH = t(replicate(nsims, pull_lt(plist4=plist_new, plist5=plist_five, specific=1, BH=1, old=0, chosen=1)))
- new_vintage_noBH = t(replicate(nsims, pull_lt(plist4=plist_new, plist5=plist_five, specific=1, BH=0, old=0, chosen=n4)))
- new_vintage_BH = t(replicate(nsims, pull_lt(plist4=plist_new, plist5=plist_five, specific=1, BH=1, old=0, chosen=n4)))
- new_any_noBH = t(replicate(nsims, pull_lt(plist4=plist_new, plist5=plist_five, specific=0, BH=0, old=0, chosen=1)))
- new_any_BH = t(replicate(nsims, pull_lt(plist4=plist_new, plist5=plist_five, specific=0, BH=1, old=0, chosen=1)))
- new_any_BH_vint = t(replicate(nsims, pull_lt(plist4=plist_new, plist5=plist_five, specific=0, BH=1, old=0, chosen=n4)))
- pull_results = as.data.frame(rbind(old_latest_noBH, old_latest_BH, old_vintage_noBH, old_vintage_BH, old_any_noBH, old_any_BH, old_any_BH_vint, new_latest_noBH, new_latest_BH, new_vintage_noBH, new_vintage_BH, new_any_noBH, new_any_BH, new_any_BH_vint))
- colnames(pull_results) = c("old", "specific", "BH", "chosen", "totalpulls", "covers00", "covers01", "covers02", "covers03", "covers04", "covers05", "covers06", "covers07", "covers08", "covers09", "covers10", "covers11", "covers12", "covers13")
- proc.time() - ptm
- ## Results for pulls needed
- # Results for Total Pulls Needed
- melt(setDT(pull_results[, c("old", "specific", "BH", "chosen", "totalpulls")]), id=c("specific", "chosen", "old", "BH"))[, .(mean = mean(value),
- sd = sd(value),
- median = median(value),
- p25 = quantile(value, probs=0.25),
- p75 = quantile(value, probs=0.75),
- p5 = quantile(value, probs=0.05),
- p95 = quantile(value, probs=0.95),
- min = min(value),
- max = max(value)),
- .(specific, chosen, old, BH, variable)]
- # Boxplot of Total Pulls Needed
- colorgroups = factor(pull_results$specific*pull_results$chosen,
- levels = c(0, 1, n4),
- labels = c("cover any 4*", "cover specific latest", "cover specific vintage"))
- ggplot(pull_results, aes(x=interaction(specific, chosen, old, BH), y=totalpulls, color=colorgroups)) +
- geom_boxplot() +
- coord_trans(y = "log10") +
- scale_x_discrete(limits=c("0.1.1.0", "0.1.0.0", "0.1.1.1", "0.1.0.1", "0.65.1.1", "0.65.0.1", "1.1.1.0", "1.1.0.0", "1.1.1.1", "1.1.0.1", "1.65.1.0", "1.65.0.0", "1.65.1.1", "1.65.0.1"), labels=c("0.1.1.0"="old", "0.1.0.0"="new", "0.1.1.1"="old + latest BH", "0.1.0.1"="new + latest BH", "0.65.1.1"="old + vintage BH", "0.65.0.1"="new + vintage BH", "1.1.1.0" = "old", "1.1.0.0"="new", "1.1.1.1" = "old + latest BH", "1.1.0.1" = "new + latest BH", "1.65.1.0" = "old", "1.65.0.0" = "new", "1.65.1.1" = "old + vintage BH", "1.65.0.1" = "new + vintage BH")) +
- xlab("4* Odds in LTs") +
- ylab("Number of Pulls Needed to Cover a 4*") +
- ggtitle("Number of Pulls Needed to Cover Any 4* vs. a Specific Latest or Vintage 4*") +
- theme(legend.position=c(0.15, 0.82), legend.title=element_blank(), legend.background = element_rect(fill=alpha('white', 0.6)), axis.text.x=element_text(angle=50, hjust=1, size=rel(1.1)))
- # Results on 4* coverage
- setDT(pull_results)[, as.list(unlist(lapply(.SD, function(x) list(mean = mean(x),
- sd = sd(x))))),
- by = .(specific, chosen, old, BH), .SDcols=c("covers13","covers12")]
- ## Function for covered 4*s based on set number of pulls
- set_pull_lt <- function(plist4, plist5, BH, old, chosen, num_pulls){
- chosen0 = chosen
- fours = matrix(0, nrow=n4, ncol=3)
- four_numcovers = rep(0, n4)
- for (totalpulls in 1:num_pulls){
- pulled_four = rbinom(1, 1, p4)
- pulled_BH = rbinom(1, 1, p_BH)
- if (pulled_four >= 1){
- char = sample(1:n4, 1, prob=plist4)
- cover = sample(1:3, 1, prob=c(1, 1, 1))
- fours[char, cover] = min((fours[char, cover]+1*(four_numcovers[char] < 13)), 5)
- four_numcovers = rowSums(fours)
- if (BH >= 1 & pulled_BH >= 1){
- cover = sample(1:3, 1, prob=c(1, 1, 1))
- fours[chosen, cover] = min((fours[chosen, cover]+1*(four_numcovers[chosen] < 13)), 5)
- four_numcovers = rowSums(fours)
- }
- }
- if (four_numcovers[chosen] >= 13){chosen = chosen + 1}
- }
- four_dist = rep(0, 14)
- four_dist[2:14] = tabulate(four_numcovers, 13)
- four_dist[1] = n4 - sum(four_dist[2:14])
- num_covers_specific = c(four_numcovers[1], four_numcovers[n4])
- covered_latest = sum(four_numcovers[1:nlatest] >= 13)
- covered_vintage = sum(four_numcovers[(nlatest+1):n4] >= 13)
- c(old, BH, chosen0, totalpulls, num_covers_specific, covered_latest, covered_vintage, four_dist)
- }
- ## Run simulations for set number of pulls
- ptm <- proc.time()
- set_old_noBH_300 = t(replicate(nsims, set_pull_lt(plist4=plist_old, plist5=plist_five, BH=0, old=1, chosen=1, num_pulls=300)))
- set_old_noBH_600 = t(replicate(nsims, set_pull_lt(plist4=plist_old, plist5=plist_five, BH=0, old=1, chosen=1, num_pulls=600)))
- set_old_latestBH_300 = t(replicate(nsims, set_pull_lt(plist4=plist_old, plist5=plist_five, BH=1, old=1, chosen=1, num_pulls=300)))
- set_old_vintageBH_300 = t(replicate(nsims, set_pull_lt(plist4=plist_old, plist5=plist_five, BH=1, old=1, chosen=(nlatest+1), num_pulls=300)))
- set_old_latestBH_600 = t(replicate(nsims, set_pull_lt(plist4=plist_old, plist5=plist_five, BH=1, old=1, chosen=1, num_pulls=600)))
- set_old_vintageBH_600 = t(replicate(nsims, set_pull_lt(plist4=plist_old, plist5=plist_five, BH=1, old=1, chosen=(nlatest+1), num_pulls=600)))
- set_new_noBH_300 = t(replicate(nsims, set_pull_lt(plist4=plist_new, plist5=plist_five, BH=0, old=0, chosen=1, num_pulls=300)))
- set_new_noBH_600 = t(replicate(nsims, set_pull_lt(plist4=plist_new, plist5=plist_five, BH=0, old=0, chosen=1, num_pulls=600)))
- set_new_latestBH_300 = t(replicate(nsims, set_pull_lt(plist4=plist_new, plist5=plist_five, BH=1, old=0, chosen=1, num_pulls=300)))
- set_new_vintageBH_300 = t(replicate(nsims, set_pull_lt(plist4=plist_new, plist5=plist_five, BH=1, old=0, chosen=(nlatest+1), num_pulls=300)))
- set_new_latestBH_600 = t(replicate(nsims, set_pull_lt(plist4=plist_new, plist5=plist_five, BH=1, old=0, chosen=1, num_pulls=600)))
- set_new_vintageBH_600 = t(replicate(nsims, set_pull_lt(plist4=plist_new, plist5=plist_five, BH=1, old=0, chosen=(nlatest+1), num_pulls=600)))
- set_pull_results = as.data.frame(rbind(set_old_noBH_300, set_old_latestBH_300, set_old_vintageBH_300, set_new_noBH_300, set_new_latestBH_300, set_new_vintageBH_300, set_old_noBH_600, set_old_latestBH_600, set_old_vintageBH_600, set_new_noBH_600, set_new_latestBH_600, set_new_vintageBH_600))
- colnames(set_pull_results) = c("old", "BH", "chosen", "totalpulls", "num_covers_specific_latest", "num_covers_specific_vintage", "covered_latest", "covered_vintage", "covers00", "covers01", "covers02", "covers03", "covers04", "covers05", "covers06", "covers07", "covers08", "covers09", "covers10", "covers11", "covers12", "covers13")
- set_pull_results$covered_specific_latest = 1*(set_pull_results$num_covers_specific_latest >= 13)
- set_pull_results$covered_specific_vintage = 1*(set_pull_results$num_covers_specific_vintage >= 13)
- set_pull_results$covers_ge10 = set_pull_results$covers10 + set_pull_results$covers11 + set_pull_results$covers12 + set_pull_results$covers13
- proc.time() - ptm
- ## Results for set pulls
- # Results on 4* coverage
- # Number of Specific Latest Covers
- melt(setDT(set_pull_results[, c("old", "BH", "chosen", "totalpulls", "num_covers_specific_latest")]), id=c("totalpulls", "BH", "old", "chosen"))[, .(mean = mean(value),
- sd = sd(value),
- median = median(value),
- p25 = quantile(value, probs=0.25),
- p75 = quantile(value, probs=0.75),
- p5 = quantile(value, probs=0.05),
- p95 = quantile(value, probs=0.95),
- min = min(value),
- max = max(value)),
- .(totalpulls, old, BH, chosen, variable)]
- # Number of Specific Vintage Covers
- melt(setDT(set_pull_results[, c("old", "BH", "chosen", "totalpulls", "num_covers_specific_vintage")]), id=c("totalpulls", "BH", "old", "chosen"))[, .(mean = mean(value),
- sd = sd(value),
- median = median(value),
- p25 = quantile(value, probs=0.25),
- p75 = quantile(value, probs=0.75),
- p5 = quantile(value, probs=0.05),
- p95 = quantile(value, probs=0.95),
- min = min(value),
- max = max(value)),
- .(totalpulls, old, BH, chosen, variable)]
- # Probability of covering specific latest or vintage
- setDT(set_pull_results)[, as.list(unlist(lapply(.SD, function(x) list(mean = mean(x),
- sd = sd(x)
- )))),
- by = .(totalpulls, old, BH, chosen), .SDcols=c("covered_specific_latest", "covered_specific_vintage")]
- # Number of covered 4*s
- melt(setDT(set_pull_results[, c("old", "BH", "chosen", "totalpulls", "covers13")]), id=c("totalpulls", "BH", "old", "chosen"))[, .(mean = mean(value),
- sd = sd(value),
- median = median(value),
- p25 = quantile(value, probs=0.25),
- p75 = quantile(value, probs=0.75),
- p5 = quantile(value, probs=0.05),
- p95 = quantile(value, probs=0.95),
- min = min(value),
- max = max(value)),
- .(totalpulls, old, BH, chosen, variable)]
- melt(setDT(set_pull_results[, c("old", "BH", "chosen", "totalpulls", "covered_latest")]), id=c("totalpulls", "BH", "old", "chosen"))[, .(mean = mean(value),
- sd = sd(value),
- median = median(value),
- p25 = quantile(value, probs=0.25),
- p75 = quantile(value, probs=0.75),
- p5 = quantile(value, probs=0.05),
- p95 = quantile(value, probs=0.95),
- min = min(value),
- max = max(value)),
- .(totalpulls, old, BH, chosen, variable)]
- melt(setDT(set_pull_results[, c("old", "BH", "chosen", "totalpulls", "covered_vintage")]), id=c("totalpulls", "BH", "old", "chosen"))[, .(mean = mean(value),
- sd = sd(value),
- median = median(value),
- p25 = quantile(value, probs=0.25),
- p75 = quantile(value, probs=0.75),
- p5 = quantile(value, probs=0.05),
- p95 = quantile(value, probs=0.95),
- min = min(value),
- max = max(value)),
- .(totalpulls, old, BH, chosen, variable)]
- # Number of 4*s with 10 or more covers
- melt(setDT(set_pull_results[, c("old", "BH", "chosen", "totalpulls", "covers_ge10")]), id=c("totalpulls", "BH", "old", "chosen"))[, .(mean = mean(value),
- sd = sd(value),
- median = median(value),
- p25 = quantile(value, probs=0.25),
- p75 = quantile(value, probs=0.75),
- p5 = quantile(value, probs=0.05),
- p95 = quantile(value, probs=0.95),
- min = min(value),
- max = max(value)),
- .(totalpulls, old, BH, chosen, variable)]
- # Box Plot of covered 4*s
- ggplot(set_pull_results, aes(x=interaction(old, BH, chosen), y=covers13, color=as.factor(interaction(old, BH, chosen)))) +
- geom_boxplot() +
- facet_wrap(~paste(totalpulls, "pulls")) +
- scale_x_discrete(limits=c("1.0.1", "0.0.1", "1.1.1", "0.1.1", "1.1.13", "0.1.13"),labels=c("1.0.1" = "old", "0.0.1" = "new", "1.1.1" = "old + latest BH", "0.1.1" = "new + latest BH", "1.1.13" = "old + vintage BH", "0.1.13" = "new + vintage BH")) +
- xlab("4* Odds in LTs") +
- ylab("Number of Covered 4*s") +
- ggtitle("Number of Covered 4*s in a Set Number of Pulls") +
- theme(legend.position="none", legend.title=element_blank(), axis.text.x=element_text(angle=50, hjust=1, size=rel(1.1)))
- # Box Plot of 4*s with at least 10 covers
- ggplot(set_pull_results, aes(x=interaction(old, BH, chosen), y=covers_ge10, color=as.factor(interaction(old, BH, chosen)))) +
- geom_boxplot() +
- facet_wrap(~paste(totalpulls, "pulls")) +
- scale_x_discrete(limits=c("1.0.1", "0.0.1", "1.1.1", "0.1.1", "1.1.13", "0.1.13"),labels=c("1.0.1" = "old", "0.0.1" = "new", "1.1.1" = "old + latest BH", "0.1.1" = "new + latest BH", "1.1.13" = "old + vintage BH", "0.1.13" = "new + vintage BH")) +
- xlab("4* Odds in LTs") +
- ylab("Number of 4*s with 10 or More Covers") +
- ggtitle("Number of 4*s with 10 or More Covers in a Set Number of Pulls") +
- theme(legend.position="none", legend.title=element_blank(), axis.text.x=element_text(angle=50, hjust=1, size=rel(1.1)))
- # Bar Chart of Mean Covered 4*s by Type
- data_summary <- function(data, varname, groupnames){
- require(plyr)
- summary_func <- function(x, col){
- c(varmean = mean(x[[col]], na.rm=TRUE),
- sd = sd(x[[col]], na.rm=TRUE))
- }
- data_sum<-ddply(data, groupnames, .fun=summary_func,
- varname)
- #data_sum <- rename(data_sum, c("mean" = varname))
- return(data_sum)
- }
- set_pull_summary_latest <- data_summary(set_pull_results, varname = "covered_latest", groupnames=c("totalpulls", "old", "BH", "chosen"))
- set_pull_summary_latest$fourtype = "latest"
- set_pull_summary_vintage <- data_summary(set_pull_results, varname = "covered_vintage", groupnames=c("totalpulls", "old", "BH", "chosen"))
- set_pull_summary_vintage$fourtype = "vintage"
- set_pull_summary = rbind(set_pull_summary_latest, set_pull_summary_vintage)
- ggplot(set_pull_summary, aes(x=interaction(old, BH, chosen), y=varmean, fill=fourtype)) +
- geom_bar(stat="identity") +
- facet_wrap(~paste(totalpulls, "pulls")) +
- scale_x_discrete(limits=c("1.0.1", "0.0.1", "1.1.1", "0.1.1", "1.1.13", "0.1.13"),labels=c("1.0.1" = "old", "0.0.1" = "new", "1.1.1" = "old + latest BH", "0.1.1" = "new + latest BH", "1.1.13" = "old + vintage BH", "0.1.13" = "new + vintage BH")) +
- xlab("Odds for 4* in LT") +
- ylab("Mean Number of Covered 4*s") +
- ggtitle("Mean Number of Covered 4*s by Type in a Set Number of Pulls") +
- theme(legend.title=element_blank(), axis.text.x=element_text(angle=50, hjust=1, size=rel(1.1)))
Advertisement
Add Comment
Please, Sign In to add comment