#  acceleration = 
#  
#  distance /  time ^ 2
#  
#  A =  D(V) / D(T)
#  
#  
#  s = d / t
#  a = 
#  
#  D(V) = D(s) / D(t)
#  A =  D(V) / D(T)
#  A =  D(s) / D(t) / D(T) = D(s) / (D(t)^2)
#  
#  B - A
#  ------ 
#  49
#  
#  B - A
#  ------ 
#  14 ^ 2
#  
#  =============================================

a * (x ^3)
a * (3x^2)
a * (6x)


library(ggplot2)


N <- 80
b <- 5
days <- 7
aggFunc <- "min"

t <- seq.Date(from=as.Date("1970-01-01"), length.out=N, by=sprintf("%s day",b))

------------------------------------------------------

lib(reshape2)

acceleration_score_actual
acceleration_score_sign
velocity_score_actual
velocity_score_sign

{
tmp.DT <- {
data.table(
    time = t
  , rand_norm = rand_norm(t=t, N=N, b=b, seed=1)
  , rand_exp = rand_exp(t=t, N=N, b=b, seed=1)
  , cubed_a0.9_shift =   cubed(t=t, a=0.9, shit=TRUE)
  , cubed_a0.9_noshift = cubed(t=t, a=0.9, shit=FALSE) 
  , cubed_a1.5_shift =   cubed(t=t, a=1.5, shit=TRUE)
  , cubed_a1.5_noshift = cubed(t=t, a=1.5, shit=FALSE) 
  , squared_a0.9_shift =   squared(t=t, a=0.9, shit=TRUE)
  , squared_a0.9_noshift = squared(t=t, a=0.9, shit=FALSE) 
  , squared_a1.5_shift =   squared(t=t, a=1.5, shit=TRUE)
  , squared_a1.5_noshift = squared(t=t, a=1.5, shit=FALSE) 
  ) %>% melt(id.var="time", variable.name="upc")
}

DT1 <- copy(tmp.DT)[, group := "smooth"]
DT2 <- copy(tmp.DT)[, group := "noise"][, value := value + as.integer(runif(.N, minn(value), maxn(value)))]
DT <- rbind(DT1, DT2)
}

## NORMAL
  func <- "rand_norm"
  (y <- match.fun(func) (t=t, N=N, b=b, seed=1) )

## CUBED
  func <- "cubed"
  (y <- match.fun(func) (t=t, N=N, b=b, seed=1) )
  
## EXPONENT  
  func <- "rand_exp"
  (y <- match.fun(func) (t=t, N=N, b=b, seed=1) )

# ggplot() + aes(x=t, y=y) + geom_smooth(se=FALSE) + geom_point()


{
  dev.new()
  DT.melted <- get_melted_DT(y=y, t=t, days=days, aggFunc=aggFunc)
  title <- sprintf("%s - %s - %s", func, days, aggFunc)
  DT.plot <- DT.melted[!(method == "natural" & type == "streams")]  [!is.na(value)][order(value)][(type %ni% c("time_min", "time_max", "dd"))] [(method == "natural")]  %>% head(-1) %>% tail(-1)
  P1 <- ggplot(data=DT.plot, aes(x=time, y=value, color=type, shape=what)) + geom_smooth(se=FALSE) + geom_point() + ggtitle(title) + nolegend()
  P2 <- ggplot() + aes(x=as.Date(t, origin=.origin), y=y) + geom_line() + geom_point() + guides(alpha=FALSE) + ggtitle(func) + nolegend()
  plot(arrangeGrob(P2, P1, ncol=2))


  DT.melted[]
  w <- 0.9

}

DT.melted[method=="natural"]


-----------------------

first_deriv_cubed  <- function(x, a=1.15) { x <- seq_along(x); x <- x - median(x);  a * (3 * x^2) }
second_deriv_cubed <- function(x, a=1.15) { x <- seq_along(x); x <- x - median(x);  a * (6 * x) }

(y <- cubed(t=t, N=N, smooth=FALSE, b=b, seed=1) )

DT <- 
  get_melted_DT(t=t, y=y, melted=FALSE, days=7, agg="min")
DT[, first := first_deriv_cubed(streams)]
DT[, second := second_deriv_cubed(streams)]

include <- c("streams", "time", "linear_acceleration", "linear_velocity", "first", "second")
include <- c("streams", "time", "linear_acceleration", "linear_velocity","second")
DT.melted <- get_melted_DT(DT=DT[, include, with=FALSE])

  P1 <- ggplot(data=DT.melted[!(method == "natural" & type == "streams")], aes(x=time, y=value, color=type, shape=method)) + geom_smooth(se=FALSE) + geom_point() + ggtitle(func) + legendbottom()
  P2 <- ggplot() + aes(x=as.Date(t, origin=.origin), y=y) + geom_line() + geom_point() + guides(alpha=FALSE) + ggtitle(func)
  plot(arrangeGrob(P2, P1, ncol=2))

