library(data.table)

print("Reading data")
train <- fread('../input/train.csv', 
              select = c('Agencia_ID', 'Ruta_SAK', 'Cliente_ID', 'Producto_ID', 'Demanda_uni_equil'))

train$log_demanda = log(train$Demanda_uni_equil+1) 

print("Computing means")
mean_PCRA <- train[, .(PCRA = mean(log_demanda)), by = .(Producto_ID, Cliente_ID, Ruta_SAK, Agencia_ID)]
mean_PCR <- train[, .(PCR = mean(log_demanda)), by = .(Producto_ID, Cliente_ID, Ruta_SAK)]
mean_PCA <- train[, .(PCA = mean(log_demanda)), by = .(Producto_ID, Cliente_ID, Agencia_ID)]
mean_PC <- train[, .(PC = mean(log_demanda)), by = .(Producto_ID, Cliente_ID)]
mean_PRA <- train[, .(PRA = mean(log_demanda)), by = .(Producto_ID, Ruta_SAK, Agencia_ID)]
mean_PA <- train[, .(PA = mean(log_demanda)), by = .(Producto_ID, Agencia_ID)]
mean_PR <- train[, .(PR = mean(log_demanda)), by = .(Producto_ID, Ruta_SAK)]
mean_P <- train[, .(P = mean(log_demanda)), by = .(Producto_ID)]
mean_total <- mean(train$log_demand)

print("Merging means with test set")
submit <- merge(train[1:15000000], mean_PCRA, all.x = TRUE, by = c('Producto_ID', 'Cliente_ID', 'Ruta_SAK', 'Agencia_ID'))
submit <- merge(submit, mean_PCR, all.x = TRUE, by = c('Producto_ID', 'Cliente_ID', 'Ruta_SAK'))
submit <- merge(submit, mean_PCA, all.x = TRUE, by = c('Producto_ID', 'Cliente_ID', 'Agencia_ID'))
submit <- merge(submit, mean_PC, all.x = TRUE, by = c('Producto_ID', 'Cliente_ID'))
submit <- merge(submit, mean_PRA, all.x = TRUE, by = c('Producto_ID', 'Ruta_SAK', 'Agencia_ID'))
submit <- merge(submit, mean_PA, all.x = TRUE, by = c('Producto_ID', 'Agencia_ID'))
submit <- merge(submit, mean_PR, all.x = TRUE, by = c('Producto_ID', 'Ruta_SAK'))
submit <- merge(submit, mean_P, all.x = TRUE, by = c('Producto_ID'))

submit$Pred <- (exp(submit$PCRA) - 1)
submit[is.na(Pred)]$Pred <- (exp(submit[is.na(Pred)]$PCR) - 1)
submit[is.na(Pred)]$Pred <- (exp(submit[is.na(Pred)]$PCA) - 1)
submit[is.na(Pred)]$Pred <- (exp(submit[is.na(Pred)]$PC) - 1)
submit[is.na(Pred)]$Pred <- (exp(submit[is.na(Pred)]$PRA) - 1)
submit[is.na(Pred)]$Pred <- (exp(submit[is.na(Pred)]$PA) - 1)
submit[is.na(Pred)]$Pred <- (exp(submit[is.na(Pred)]$PR) - 1)
submit[is.na(Pred)]$Pred <- (exp(submit[is.na(Pred)]$P) - 1)
submit[is.na(Pred)]$Pred <- (exp(mean_total) - 1)

print("Write out submission file")
preds = submit$Pred
trains = train$Demanda_uni_equil[1:5]
A = sum((log((preds+1)/(trains+1))^2))/nrow(1500000)
print(A)
