Last active
August 29, 2015 13:58
-
-
Save ascv/10155130 to your computer and use it in GitHub Desktop.
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
cc_sim = function(seed, tran, debug=FALSE) { | |
# Does a simulation from a list of transition coefficients | |
# and initial state. | |
# | |
# Args: | |
# seed: the initial state | |
# tran: a list of transition coefficients | |
n = length(tran) | |
lst = rep(1,n) | |
if (debug) { | |
print(seed) | |
print(tran[1]) | |
} | |
for (i in 1:n) { | |
lst[i] = seed * tran[i] + seed | |
seed = lst[i] | |
} | |
lst | |
} | |
Bcc_sim = function(seed, prev, tran, debug=FALSE) { | |
# Does a simulation from a list of transition coefficients | |
# and initial state. | |
# | |
# Args: | |
# seed: the initial state | |
# tran: a list of transition coefficients | |
n = length(tran) | |
lst = rep(1,n) | |
twodaysample = .7*seed + .3*(prev*tran[n] + prev) | |
seed = twodaysample | |
if (debug) { | |
print(seed) | |
print(tran[1]) | |
} | |
for (i in 1:n) { | |
lst[i] = seed * tran[i] + seed | |
seed = lst[i] | |
} | |
lst | |
} | |
rotate = function(lst) { | |
# Rotates a list by one element e.g. [1, 2, 3] --> [2, 3, 1] | |
# | |
# Args: | |
# lst: the list to rotate | |
n = length(lst) | |
tmp = rep(0, n) | |
end = lst[1] | |
for (i in 1:n-1) { | |
tmp[i] = lst[i + 1] | |
} | |
tmp[n] = end | |
tmp | |
} | |
errors = function(y_hat, y) { | |
n = length(y) | |
errs = rep(0, n) | |
for (i in 1:n) { | |
errs[i] = abs(1 - y_hat[i]/y[i]) | |
} | |
mean(errs) | |
} | |
################################################### | |
setwd('~/threebuys/') | |
setwd('C:/Users/josh/Desktop/threebuys') | |
data = droplevels(read.csv('10755.csv', header=TRUE)) | |
tran10755 = rep(c(-.0025,-.0531,.0925,.0457,.0011,-.0626,.0145), 4) | |
tran10111 = rep(c(-.0025,-.0531,.0925,.0457,.0011,-.0626,.0145), 4) | |
################################################### | |
dist = seq(-.2,.2, by=.0001) # percent distance from true value | |
N = length(data$Cost.Clicks) - 7 # exclude the last week | |
m = length(dist) | |
n = N - 7 # number of days | |
Y = matrix(nrow=m, ncol=n) # err at dist by day matrix | |
# Y matrix example: | |
# 1/17 1/18 | |
# ----- ---- | |
# - 1| .5 .8 | |
# - .9| .3 .1 | |
# Check convergence | |
tmp = tran10755 | |
for (j in 1:length(dist)) { | |
tmp = tran10755 | |
for (i in 8:N) { # exclude the first week | |
q = i + 1 | |
p = i + 7 | |
a = cc_sim(data$Cost.Clicks[i+1] * dist[j] + data$Cost.Clicks[i+1], tmp) | |
Y[j, i - 7] = errors(a, data$Cost.Clicks[q:p]) | |
tmp = rotate(tmp) | |
} | |
} | |
daily_errs = rep(0, m) | |
for (i in 1:m) { | |
daily_errs[i]=mean(Y[i,]) | |
} | |
plot(dist,daily_errs*100, col='blue', type='l', ylab='mean 7 day error %', xlab='% from true value', | |
main='Mean 7 day error ) | |
best = which.min(daily_errs) | |
dist[best] | |
################################################### | |
# Original method | |
tmp = tran10755 | |
for (i in 8:N) { # exclude the first week | |
print(rep('=',10)) | |
print(droplevels(data$Date[i+1])) | |
print(cc_sim(data$Cost.Clicks[i], tmp)) | |
tmp = rotate(tmp) | |
} | |
# Bill's method | |
tmp = tran10755 | |
for (i in 8:N) { # exclude the first week | |
print(rep('=',10)) | |
print(droplevels(data$Date[i+1])) | |
print(Bcc_sim(data$Cost.Clicks[i], data$Cost.Clicks[i-1], tmp)) | |
tmp = rotate(tmp) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment