Giter VIP home page Giter VIP logo

Comments (7)

tomtom94 avatar tomtom94 commented on May 24, 2024 1

You may need to have a look to this one https://github.com/tomtom94/stockmarketpredictions

from time-series-forecasting-tensorflowjs.

jinglescode avatar jinglescode commented on May 24, 2024

Hi, thanks for checking out the project. Let's discuss each of these points.

Improvements: Data selection

Great suggestion, I have changed it to pull from these APIs:

Looking good!
image

Maybe not correct: Calculation of the SMA + Maybe issue: Where do you offset your Y results?

I just did some checks by putting console.log to check the data in and out of the ComputeSMA function. It seems correct. Correct me if I'm wrong, you maybe have been confused by the window_size parameter. window_size is the size of the sliding window, not the size of the data. I also changed the window size on the web UI to 5, for easy calculation.

  console.log(11, data_raw, window_size);
  sma_vec = ComputeSMA(data_raw, window_size);
  console.log(22, sma_vec)

For console.log(11, data_raw, window_size), data_raw is:

0: {timestamp: "1999-11-12", price: 89.19}
1: {timestamp: "1999-11-19", price: 86}
2: {timestamp: "1999-11-26", price: 91.12}
3: {timestamp: "1999-12-03", price: 96.12}
4: {timestamp: "1999-12-10", price: 93.87}
5: {timestamp: "1999-12-17", price: 115.25}
6: {timestamp: "1999-12-23", price: 117.44}
7: {timestamp: "1999-12-31", price: 116.75}

For console.log(22, sma_vec), sma_vec is:

0:
avg: 91.26
set: Array(5)
0: {timestamp: "1999-11-12", price: 89.19}
1: {timestamp: "1999-11-19", price: 86}
2: {timestamp: "1999-11-26", price: 91.12}
3: {timestamp: "1999-12-03", price: 96.12}
4: {timestamp: "1999-12-10", price: 93.87}
length: 5

1:
avg: 96.472
set: Array(5)
0: {timestamp: "1999-11-19", price: 86}
1: {timestamp: "1999-11-26", price: 91.12}
2: {timestamp: "1999-12-03", price: 96.12}
3: {timestamp: "1999-12-10", price: 93.87}
4: {timestamp: "1999-12-17", price: 115.25}
length: 5

So what is happening was, it averages the 5 values, (89.19 + 86 + 91.12 + 96.12 + 93.87) / 5 = 91.26. Then, it slide one step, and average the next 5 values, (86 + 91.12 + 96.12 + 93.87 + 115.25) / 5 = 96.472.

Am I correct? Did I answer your question? Or did I make a mistake?

Question: Model

explain in more details how you build your model?, I would need to expand on it on the article. But here are some of the other pointers:

  • input_layer_neurons = 100: the 100, I simply pluck from thin air, no scientific reasons why it is 100. It is a model parameter which you can tune. This is the parameter for the linear (or dense) layer. Generally, the higher this is, the model can memorize better. Overfitting can be an issue if this is too much though. Maybe 32 is good? maybe 128 can give you a good result for a particular stock.
  • rnn_input_layer_features = 10: same as input_layer_neurons, but this is the parameter for the RNN
  • .div(tf.scalar(10)): honestly I cant quite remember what is this for, but it is for to make the tensor size correct

Hope these are useful, we can discuss more.

from time-series-forecasting-tensorflowjs.

desduvauchelle avatar desduvauchelle commented on May 24, 2024

Hi Thanks for answering!

Data

Awesome. Glad it worked. I hope it helps.

Average

I just did a test and it does give the average. Apologies for that. For some reason I can't wrap my head around why it's working ha :) I usually use reduce functions for this.

const calculateAverage = (quotes = [89.19,86,91.12]) => {
     return quotes.reduce((total, num) => total + sum) / quotes.length
}

Shifting the Ys

Maybe I need clarify this question. Using your example above for the data:

avg: 91.26
set: Array(5)
0: {timestamp: "1999-11-12", price: 89.19}
1: {timestamp: "1999-11-19", price: 86}
2: {timestamp: "1999-11-26", price: 91.12}
3: {timestamp: "1999-12-03", price: 96.12}
4: {timestamp: "1999-12-10", price: 93.87}

I'm not seeing where you set the future value, right now I think you are saying

const X = [
   {timestamp: "1999-11-12", price: 89.19},
   {timestamp: "1999-11-19", price: 86},
   {timestamp: "1999-11-26", price: 91.12},
   {timestamp: "1999-12-03", price: 96.12},
   {timestamp: "1999-12-10", price: 93.87}
]

const Y = 91.26. // <= The actual average for that period X

If that is correct, it would mean that your model is learning how to calculate an average, not forecast in the future. But I'm probably not seeing/understanding something.

Model

Yes, I think adding it to your article would be great!
A whole breakdown of your model.js file would be amazing.

Thanks again.

from time-series-forecasting-tensorflowjs.

desduvauchelle avatar desduvauchelle commented on May 24, 2024

oh also, relating to the model, I was thinking the .div(tf.scalar(10)) is to "normalize" the data. If it is, wouldn't it be better to do a soft min/max ?
So something along the lines of:

const normalizedInputs = xs.sub(inputMin).div(inputMax.sub(inputMin))
const normalizedOutputs = ys.sub(outputMin).div(outputMax.sub(outputMin))

In plain normal JS, would look like this:

const normalize = (value, min, max) => { 
	if (min === undefined || max === undefined) {
		return value
	}
	return (value - min) / (max - min)
}

from time-series-forecasting-tensorflowjs.

jinglescode avatar jinglescode commented on May 24, 2024

Oh yes, I got your question on the Shifting the Ys now. I just checked these:

By logging line 97:

sma_vec = ComputeSMA(data_raw, window_size);
console.log(sma_vec)

And looking at line 190:

  console.log('train X', inputs)
  console.log('train Y', outputs)

So yes, you are right that the model is calculating the average, and the aim is to predict the future SMA. So means that predicting the next point is "pointless", but predicting the next 10 points (or how far you wanna go) will be more helpful so you are predicting if it's going up and down next (and by how much). Also using SMA is possibly the easiest one to understand (for learning), that's why it was chosen in the tutorial. Alternatively, we could also, as you have suggested, shift the Y, predict the next SMA instead of the current, so it makes a bit more sense, and not just computing the average.

In short, there are a few better solutions:

  • predicting the next n points, this will have to change the model to be a sequence to sequence model
  • shift the y, so the model is predicting the technical analysis indicator future values (can be another indicator), (e.g. using day 1 to 5, to predict SMA day 10 SMA value)

What do you think? Make sense? I would love to see what you have done and hope you can do a PR on the cool things you've done.

from time-series-forecasting-tensorflowjs.

brandonculver avatar brandonculver commented on May 24, 2024
function ComputeSMA(data, window_size)
{
  let r_avgs = [], avg_prev = 0;
  for (let i = 0; i <= data.length - window_size; i++){
    let curr_avg = 0.00, t = i + window_size;
    for (let k = i; k < t && k <= data.length; k++){
      curr_avg += data[k]['price'] / window_size;
    }
    r_avgs.push({ set: data.slice(i, i + window_size), avg: curr_avg });
    avg_prev = curr_avg;
  }
  return r_avgs;
}

You never actually use avg_prev for anything here.

from time-series-forecasting-tensorflowjs.

jinglescode avatar jinglescode commented on May 24, 2024

Thanks @brandonculver for highlighting that. That must be a bug. Feel free to reply here if you have fixed it or do a PR.

from time-series-forecasting-tensorflowjs.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.