mardi 21 mars 2017

neural network predicting random numbers - something's wrong (with code)

i have just started using neural networks in R. i create a vector of random numbers, transform it into a vector with "up" or "down" depending on if the number was higher or lower than the previous number. i then train a neural network using the first 1000 data points and try to predict the next 1000 data points. at the end, i compare the predicted results (up/down) against the actual values -- i give a score of +1 if the prediction is correct and a score of -1 if wrong. at the end, i plot the cumsum of the score. however, every single time i run this, it manages to predict the directions of the random numbers incredibly well with incredible consistency. something must be wrong...

require(quantmod)
require(nnet)

upOrDown <- function(x) {
  if (is.na(x))
    return (0)
  else if (x > 0)
    return (1)
  else
    return (0)
}

data = data.frame(Close = rep(0, 3000))
data$Close = (sample.int(101,size=3000,replace=TRUE)-1)/100
returns = ClCl(data)
inputOrigTr = returns[1:1000]
inputRawTr = inputOrigTr
inputOrigTes = returns[1001:2000]
inputRawTes = inputOrigTes

for (i in 1:5) {
  inputRawTr = cbind(inputRawTr, Lag(inputOrigTr, i))
  inputRawTes = cbind(inputRawTes, Lag(inputOrigTes, i))
}

inputTr = apply(inputRawTr, 1:2, upOrDown)
inputTr[is.na(inputTr)] = 0
outputTr = Next(inputTr[, 1], 1)
matTr = cbind(inputTr, outputTr)[2:length(inputOrigTr)-1, ]

matFactorTr=apply(matTr[, 1:(ncol(matTr)-1)], 2, as.factor)
outputTr = class.ind(matTr[, ncol(matTr)])
nn = nnet(matFactorTr, outputTr, size=ncol(outputTr), softmax=TRUE)

inputTes = apply(inputRawTes, 1:2, upOrDown)
inputFactorTes = apply(inputTes, 2, as.factor)
predTes = as.numeric(predict(nn, inputFactorTes, type="class"))
actualTes = Next(inputRawTes[, 1], 1)

resultMat = as.data.frame(cbind(predTes, actualTes))
resultMat[is.na(resultMat)] = 0
resultMat = resultMat[5:length(actualTes)-1, ]
resultMat$isCorrect = sapply(resultMat$Next, upOrDown) == resultMat$predTes
resultMat$score = lapply(resultMat$isCorrect, function(x) { if (x == TRUE) return (1) else return (-1) })
resultMat = as.data.frame(resultMat)
plot(cumsum(resultMat$score), type='l')




Aucun commentaire:

Enregistrer un commentaire