---
title: "Grid size"
author: "Picarus"
date: "June 13, 2016"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Grid size

Just a quick script to see how the size of the grid affects the maximum score that can be achieved.


Read the data: 

```{r}

library(data.table)
library(ggplot2)

#DATA_PATH <- "./data/"
DATA_PATH <- "../input/"

TRAIN_FILE <- paste0(DATA_PATH,"train.csv")

train <- fread( TRAIN_FILE, header = T, sep = ",", nrows = 2911802, showProgress = F, # read 10% of the file
              colClasses = c("integer","numeric","numeric","integer","integer","factor")) 
 
```

Calculate distance to the mean to each location and for different number of cells calculate how many points have its mean inside its own cell, if the possible results are restricted to the locations whose mean is on the same cell. The cell size on Y can be smaller than on X.


```{r}

varianceXY <- function(data){
  
  data[,':='(sdX = sd(x), sdY = sd(y), muX = mean(x), muY = mean(y), N = .N), by = c("place_id")]
  data[,':='(dX = muX - x, dY = muY - y)]
  
  data
  
}


gridSize <- function(data){
  
  data$xB <- cut(data$x, NBREAKS_X)
  data$yB <- cut(data$y, NBREAKS_Y)
  
  sizeX <- 10 / NBREAKS_X
  sizeY <- 10 / NBREAKS_Y

  data[, ':='( inX = (abs(dX)<sizeX), inY = (abs(dY)<sizeY)) ]
  data[, ':='( inside = inX & inY )]
  data
}

plotInside <- function(data){
  inside <- data[, .( pct = sum(inside)/.N ), by = c("xB","yB")]
  ggplot(inside, aes( x = xB, y = yB ) ) + geom_tile( aes( fill = pct ) ) + 
    scale_fill_gradient( low = "red", high = "green") + 
    theme( axis.text.x = element_text(angle = 90, hjust = 1), axis.text.y = element_blank()) + 
    ggtitle("Percentage of points inside its area")
}

train <- varianceXY(train)

pcts <- list()

for ( NBREAKS_X in seq(10,100,10) ) {
  for ( NBREAKS_Y in seq(100,1000,100) ) {
    train <- gridSize(train)
    pct <- sum(train$inside) / nrow(train)
    #cat(NBREAKS_X, NBREAKS_Y, pct,"\n" ) 
    pcts[[length(pcts)+1]] <- list(NBREAKS_X, NBREAKS_Y, pct)
  }
}

pcts <- data.frame(matrix(unlist(pcts), ncol=3, byrow=T))

names(pcts) <- c("x","y","pct")

ggplot(pcts, aes(x = factor(x), y = factor(y))) + geom_tile(aes( fill = pct)) +
  geom_text( aes(label = format(pct*100, digits = 4))) + ggtitle("Scoring percentage")

ggplot(pcts, aes(x = factor(x), y = factor(y))) + geom_tile(aes( fill = x*y*pct)) + 
  geom_text( aes(label = format(x*y*pct/1000, digits = 2))) + ggtitle("Scoring number of regions and percentage")

```

Plot one of the cases:

```{r}
NBREAKS_X <- 20
NBREAKS_Y <- 500

train <- gridSize(train)
plotInside(train)
```
