1
$\begingroup$

This post is aptly titled: my stock prediction model's accuracy just won't go past 0.5088282227516174 despite loss decreasing. I have tried so many different things, such as:

  • Increasing batch size

  • Decreasing batch size

  • Decreasing learning rate

  • Changing optimizer back and forth between adam, rmsprop, and sgd.

  • Using literally the same exact code that worked very well for one of my other projects (iris dataset), except with the changed variables and names.

  • Changing units

  • Adding layers

  • Switching between ReLu, Sigmoid, Tanh and Softmax for the layers

For every one of these, I either get no results, as in "NaN" for the loss or 0 for the loss and accuracy; or if it does work, the loss goes down gradually, but the accuracy stays the same. Even though I used the same code format and outline for the Iris project--to classify between setosa, versicolor, and virginica--which worked flawlessly, this is not the case despite datasets being of relatively similar format.

The main premise of this is to predict if the stock price, when given the inputs of: opening and closing price, high and low price of the day, volume, and the percent change increase of SPY. The label is a classifier of whether or not it gained or lost value the next day from that date, with the classifiers being "profit," or "loss." A sample of the data CSV is shown below:

next_day_inc,Open,High,Low,Close,Volume,spy_inc
profit,1.761,1.761,1.674,1.707,4137000.000,-0.258

Here is a link to that dataset: https://drive.google.com/file/d/1FV4zxVejTZz9GcjnG6WjIhuXShG3q93_/view?usp=sharing.

I even tried another dataset, where I change it to a more relative format (percentage change rather than opening/closing price) and scale down the numbers a bit: https://drive.google.com/file/d/1slNCZItbRvAXVVjLixSMeu31z2PDTIeG/view?usp=sharing. Still didn't work.

Here is the complete program code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iris</title>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>

</head>
<script>
    async function run() {
        const csvUrl = 'SOXLcateg.csv';
        const trainingData = tf.data.csv(csvUrl, {
            columnConfigs: {
                next_day_inc: {
                    isLabel: true
                }
            }
        });
        const convertedData = trainingData.map(({ xs, ys }) => {
            const labels = [
                ys.next_day_inc == "profit" ? 1 : 0,
                ys.next_day_inc == "loss" ? 1 : 0]
            return { xs: Object.values(xs), ys: Object.values(labels) };
        }).batch(300);
        const numOfFeatures = (await trainingData.columnNames()).length - 1;

        const model = tf.sequential();
        model.add(tf.layers.dense({
            inputShape: [numOfFeatures],
            activation: "sigmoid", units: 5
        }));
        model.add(tf.layers.dense({ activation: "softmax", units: 2 }));
        model.compile({
            loss: "binaryCrossentropy",
            optimizer: tf.train.adam(0.0006),
            metrics: "accuracy"
        })
        await model.fitDataset(
            convertedData,
            {
                shuffle: true,
                epochs: 50,
                callbacks: {
                    onEpochEnd: async (epoch, logs) => {
                        console.log("E: " + epoch + " Loss: " + logs.loss + " Accuracy: " + logs.acc);
                    }
                }
            }
        )
        const testVal = tf.tensor2d([2.383,2.619,2.383,2.599,15642000.000,0.102], [1, 6]);
        const prediction = model.predict(testVal);
        const pIndex = tf.argMax(prediction, axis = 1).dataSync();
        const classNames = ["Profit", "Loss"];
        alert(classNames[pIndex]);
        // await model.save("downloads://model");
    }
    run();
</script>
<body>
</body>
</html>

Anytime this does work, the accuracy spikes from whatever it originally was, for example 0.5703322324423 or .49123242379423, to 0.5088282227516174 and just refuses to move beyond that for the rest of the training, despite loss decreasing still (so probably not a case of overfitting; I may be wrong though).

If anyone can help me with this, I would greatly appreciate it. Thanks!

$\endgroup$
2
  • $\begingroup$ your data probably just isn't especially predictive of thing you're trying to predict. $\endgroup$
    – David Marx
    Jul 6 at 19:39
  • $\begingroup$ Since your data in different columns has very different scales, consider to normalize your data (scale them to range $[0,1)$). Since your Volume column data has numbers in million and spy_inc has numbers in -1,1, the learning rate will be very slow. $\endgroup$
    – Kaveh
    Jul 7 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.