Skip to content

Data source, SMA calculation and Y outputs offsets #4

@desduvauchelle

Description

@desduvauchelle

Hello,

First of all, thank you for great tutorial. And sorry for the long post.

Was reading through the code and there are a couple of things I can't understand. And maybe some potential improvement ideas.

Improvements: Data selection

You are using the "price" for your calculations which I believe isn't the best value, you usually want an "Adjusted" (aka Adj) value. When you look at the stock prices for Apple (AAPL) for example, they did a stock split about 1-2 months ago (and end of 2014) and you can see in the data that it has a huge drop in "price" because it was split (ex: 1 share now became 2 shares)...so the results won't be correct. I would advise to use Adj Close or similar. Tesla also recently did a split.

(FYI: I ended up using Yahoo finance to get data that was Adj in my model)

Maybe not correct: Calculation of the SMA

I was looking at your ComputeSMA

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;
}

I might be reading the code wrong but in curr_avg += data[k]['price'] / window_size you are dividing the price by the window_size before adding it to the average. Ex:

const data = [1,2,3,4,5]
const window_size = data.length // => 5
// What it seems you are doing:
const avg = 1/5 + 2/5 + 3/5 + 4/5 + 5/5. // => 2.83
// How average is calculated
const avg2 = (1+2+3+4+5) / 5 // => 3 

Maybe issue: Where do you offset your Y results?

As I was reading your ComputeSMA, it seems like the set is the avg. Once again I might be wrong, but here are my thoughts. In your onClickTrainModel, you have

 let inputs = sma_vec.map(function(inp_f){
    return inp_f['set'].map(function(val) { return val['price']; })
  });
  let outputs = sma_vec.map(function(outp_f) { return outp_f['avg']; });

So it is taking the direct output from your ComputeSMA I believe. Now, when we look at your ComputeSMA again, your average is calculated like so:

 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;
    }

So it's taking the values from i to t which is i+window_size (so: i to i+window_size) to calculate the avg, and your set is also data.slice(i, i + window_size). So I'm not sure where you are offsetting your Y values for your model.

Question: Model

Any chance you can explain in more details how you build your model?
Some examples, are:

  • How do you decide input_layer_neurons = 100? What does it change?
  • Same for const rnn_input_layer_features = 10
  • What does .div(tf.scalar(10)) do on your tensors? Does it normalize the data?

Any help much appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions