layout | title | date | author | website | category | tags | categories | ||||
---|---|---|---|---|---|---|---|---|---|---|---|
post |
Stock Prediction |
2017-01-22 16:00:00 +0530 |
saketharsh |
Project |
|
|
This Project predicts closing value of a stock of a particular day in a market, given its value of opening, maximum, minimum and volume of a stock of that day.
- The entire code has been written in Torch and Lua
- This project implements the basic algorithms of Machine Learning.
This project uses Torch , a language particularly made for Machine Learning. Torch can be installed easily on linux from its terminal. I
- Link for installation guide:- http://torch.ch/docs/getting-started.html
Other module needed can be installed by using below commands.
$ luarocks install nn
$ luarocks install itorch
$ luarocks install image
$ luarocks install optim
The Data Set used for training the Neural Network can be found here. here.
Here is the name and links of important persons and important stuffs.
- Andrew NG course on Machine Learning present at Coursera
- Book for Neural Networks written by Michael Nelson
- Python from http://learnpython.org/
- Git from https://try.github.io/levels/1/challenges/1
- Torch from https://github.com/torch/torch7/wiki/Cheatsheet
- Lua from http://tylerneylon.com/a/learn-lua/
- Machine learning In Torch from rnduja blog
In total we have five main files:
- core_function.lua
- stock_function.lua
- Dataset.txt which contains 20,232 data of stock details of various MNC's
- final_output.lua
- graph_output.lua
The Dataset has been taken from other data repository ,which contains more than one lakh stocks of one year. Our dataset just contains 20,232 stocks of which 10,232 has been used as Training Set and remaining as Validation Set. We have ignored dates and tickers as they are not useful for us to calculate our desired output. One can see how we have done it in stock_function.lua.
fullset = database("Dataset.txt") .
trainset = {
size = 10232,
data = fullset['datainput'][{{1,10232}}],
label = fullset['dataoutput'][{{1,10232}}]
}
validationset = {
size = 10000,
data = fullset['datainput'][{{10233,20232}}],
label = fullset['dataoutput'][{{10233,20232}}]
}
Here 'database' is an user defined function in file stock_function.lua which helps us to convert Dataset.txt to convert into a Double Tensor so that we can apply arithmetic operations on it.
Our model contains two hidden layer.One has 70 neurons and other one has 50 neurons.
model = nn.Sequential()
model:add(nn.Linear(4,70))
model:add(nn.Tanh())
model:add(nn.Linear(70,50))
model:add(nn.Tanh())
model:add(nn.Linear(50,1))
model:add(nn.Tanh())
This model applies Tanh function. Tanh is defined as f(x) = (exp(x)-exp(-x))/(exp(x)+exp(-x))
We have used MSECriterion(Mean Squared Error) for our loss function. Since we are not intersted in backpropagation. One can find about this criterion here.
criterion = nn.MSECriterion()
Since we are optimizing our model through Stochastic Gradient we have used module "optim.sgd". The tutorial link is here
_, fs = optim.sgd(feval,x,sgd_params)
The training set has been trained by "step" function.The code for it is simple and can be understood after going through the Turorials given above. We have used batches of batch_size = 200 .
eval function gives us accuracy calculated after testing validation set with the parameters we got after training trainingset in "step" function.
After iterating the same datasets for 200 times or (200 epochs) we have saved the final parameters of model in other file model.net. This helps us to use these parameters any other time without iterating over 200 times. This file has been used in files like final_output.lua and graph_output.lua.
Description of final_output.lua and graph_output.lua
The final_ouput.lua , as the name itself says, gives us the closing stock value when given value of opening , highest , lowest and overall volume of a stock of a day as an input.
The graph_ouput.lua gives us the graph between the predicted closing value and actual closing value .It also save the graph in home directory as a .png file.
Team Name = ML-Coderss
Team Membres :
- IIT Kanpur
- Pclub
- P Club Co-ordinator - [Vinayak Tantia]
- Coursera Co-founder - Andrew Ng
Our website is : http://mlcoderss.github.io/ML-project/