使用R和神经网络(neuralnet)使用先前的价格预测价格 [英] Predicting price using previous prices with R and Neural Networks (neuralnet)

查看:262
本文介绍了使用R和神经网络(neuralnet)使用先前的价格预测价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在"R神经网络"页面中,我正在使用神经网络功能来尝试预测股票价格.

训练数据包含高",低",打开",关闭"列.

myformula <- close ~ High+Low+Open
neuralnet(myformula,data=train_,hidden=c(5,3),linear.output=T)

我的问题,考虑到以下数据示例,您能否告诉我该公式的外观.

我有一个表,该表的列为高",低",打开",关闭",它具有两行值,每一行代表当天的烛台.因此,数据中的两行是前两天的烛台.我的目标是根据给定的前两根烛台,预测下一个烛台是什么,即打开",高",低",收盘". /p>

我的神经网络将一次显示之前的dtata 1烛台.我想知道下一个烛台是什么,那么我的R公式会是什么样子.

谢谢 让我知道

解决方案

我的神经网络将一次获得一根蜡烛棒的先前数据.我想知道下一个烛台是什么,那么我的R公式会是什么样子.

在前馈神经网络*中,您必须指定要用于预测的功能以及要预测的目标.在上面的示例中,该功能例如prev_close,目标是close.如您在训练数据中所见,您还没有prev_close,这就是我的回答的全部重点,您需要首先正确地解决问题.

如果您只有close,则没有公式可以为此训练FF NN.您需要创建prev_close,然后公式将为close ~ prev_close.

*可以在序列上训练递归神经网络(RNN),并根据输入序列输出预测,但这就是整个蠕虫的另一罐

简单的例子:根据最近的两个关闭值预测关闭

我制作了这个荒谬而简单的示例,只是为了说明问题的表述,它基于最后两个close值之一来预测close.我选择了一个带有1个神经元的隐藏层.我设置了linear.output=TRUE,因为我们正在预测连续值(如前所述,回归问题之前,以及 neuralnet文档声明如果该值为TRUE,将没有激活功能act.fct

*如果您对此进行交易,您肯定会丢失您的衬衫.这只是为了展示如何在神经网络中构建这样的预测问题.

问题表述

我要说明的一点是,如果您在某列中有价格,则必须创建该预测的功能

prev_close_1 | prev_close_2 | close

NN面临的问题是基于prev_close_1prev_close_2预测close,因此公式close ~ prev_close_1 + prev_close_2

这是网络体系结构

请注意,输入是先前的收盘价,而输出是预测的收盘价.

library(neuralnet)

N = 10
prices <- data.frame(close=1:N) # Dummy straight line uptrend for N periods 

print(prices)

shift <- function(x, n){
  c(x[-(seq(n))], rep(NA, n))
}

# Form the training dataframe
train <- data.frame(
  prev_close_1=prices$close,
  prev_close_2=shift(prices$close, 1),
  close=shift(prices$close, 2)
)

# When shifting the columns for time lag effect, some rows will have NAs
# Let's remove NAs
train <- na.omit(train)

print(train)

nn <- neuralnet(
  formula=close ~ prev_close_1 + prev_close_2,
  data=train,
  hidden=c(1), # 1 neuron in a single hidden layer
  linear.output=TRUE # we want regression not classification
)

print(prediction(nn))
plot(nn)

虚拟价格是什么样的

这就是您所拥有的,仅是历史股价

   close
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
10    10

NN的训练内容

这就是您所需要的,功能和目标,请尝试在下面的训练数据框中形成行以了解移位/滞后.

prev_close_1 prev_close_2 close
1            1            2     3
2            2            3     4
3            3            4     5
4            4            5     6
5            5            6     7
6            6            7     8
7            7            8     9
8            8            9    10

神经网络的预测

  prev_close_1 prev_close_2       close
1            1            2 2.994291864
2            2            3 4.017828301
3            3            4 5.002914789
4            4            5 5.968855729
5            5            6 6.978644849
6            6            7 8.030810042
7            7            8 9.051063456
8            8            9 9.945595495

Within the R Neural Network page, I am using the neural network function to attempt to predict stock price.

Training data contains columns High,Low,Open,Close.

myformula <- close ~ High+Low+Open
neuralnet(myformula,data=train_,hidden=c(5,3),linear.output=T)

My question, Given the below data example could you tell me what the formula would look like.

I have a table with columns "High","Low","Open","Close" it has two rows of values, each row represents a candle stick for the day. So the two rows in the data are candle sticks for the previous two days.My aim is tp predict what the next candle stick is i.e. "Open","High","Low","Close" given the previous two candlesticks.

My neural network will be presented with the previous dtata 1 candle stick at a time. I want to know what the next candlestick is, so what would my R formula look like.

Thanks Let me know

解决方案

My neural network will be presented with the previous data one candle stick at a time. I want to know what the next candlestick is, so what would my R formula look like.

In a Feed-Forward Neural Network*, you have to specify the features you want to use for the prediction and the targets to predict. In your example above the feature is e.g. prev_close, and the target is close. As you can see in your training data you don't have prev_close yet, that was the whole point of my answer, you need to formulate the problem correctly first.

If all you have is close, there cannot be a formula to train a FF NN for this. You need to create prev_close then the formula would be close ~ prev_close.

*A Recurrent Neural Network (RNN) can be trained on sequences, and output a prediction based on an input sequence, but that's a whole 'nother can of worms

Simple example: predict close based on last 2 close values

I cooked up this ridiculously-simplistic* example just to illustrate the problem formulation, that predicts the close, based one the last two close values. I chose a single hidden layer with 1 neuron in it. I've set linear.output=TRUE since we are predicting continuous values (regression problem as discussed before, and in the neuralnet documentation it is stated that there will be no activation function act.fct if that value is TRUE)

*If you trade with this you will certainly lose your shirt. This is just to show how to structure such a prediction problem in a Neural Network. Don't use this for real.

Problem formulation

The point I wanted to make clear is this, if you have prices in a column you have to create the features for the prediction

prev_close_1 | prev_close_2 | close

The problem posed to the NN is to predict close based on prev_close_1 and prev_close_2, hence the formula close ~ prev_close_1 + prev_close_2

Here's the network architecture

Notice the inputs that are the previous close values, and the output: the predicted close value.

library(neuralnet)

N = 10
prices <- data.frame(close=1:N) # Dummy straight line uptrend for N periods 

print(prices)

shift <- function(x, n){
  c(x[-(seq(n))], rep(NA, n))
}

# Form the training dataframe
train <- data.frame(
  prev_close_1=prices$close,
  prev_close_2=shift(prices$close, 1),
  close=shift(prices$close, 2)
)

# When shifting the columns for time lag effect, some rows will have NAs
# Let's remove NAs
train <- na.omit(train)

print(train)

nn <- neuralnet(
  formula=close ~ prev_close_1 + prev_close_2,
  data=train,
  hidden=c(1), # 1 neuron in a single hidden layer
  linear.output=TRUE # we want regression not classification
)

print(prediction(nn))
plot(nn)

What the dummy prices look like

This is what you have, it's just the historical stock prices

   close
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
10    10

What the NN was trained on

This is what you need, the features and the target, try to form the rows in the training dataframe below to understand the shift/lag.

prev_close_1 prev_close_2 close
1            1            2     3
2            2            3     4
3            3            4     5
4            4            5     6
5            5            6     7
6            6            7     8
7            7            8     9
8            8            9    10

What the NN predicts

  prev_close_1 prev_close_2       close
1            1            2 2.994291864
2            2            3 4.017828301
3            3            4 5.002914789
4            4            5 5.968855729
5            5            6 6.978644849
6            6            7 8.030810042
7            7            8 9.051063456
8            8            9 9.945595495

这篇关于使用R和神经网络(neuralnet)使用先前的价格预测价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆