<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by AlphaFitness on Medium]]></title>
        <description><![CDATA[Stories by AlphaFitness on Medium]]></description>
        <link>https://medium.com/@davidjanosky?source=rss-12e5f2214209------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*PaTMFR820Y6NEOby</url>
            <title>Stories by AlphaFitness on Medium</title>
            <link>https://medium.com/@davidjanosky?source=rss-12e5f2214209------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 09:03:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@davidjanosky/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Stock Prediction Using GANs]]></title>
            <link>https://medium.com/@davidjanosky/stock-prediction-using-gans-networks-ee97f830ad82?source=rss-12e5f2214209------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee97f830ad82</guid>
            <category><![CDATA[stock-market]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[gans]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[AlphaFitness]]></dc:creator>
            <pubDate>Sun, 08 May 2022 00:26:30 GMT</pubDate>
            <atom:updated>2022-05-08T00:28:29.515Z</atom:updated>
            <content:encoded><![CDATA[<h3>Reason for this Project</h3><p>Machine learning is an interesting field of work and the full range of applications has yet to be researched. A commonly researched problem is that of stock prediction as it would potentially be one of the most profitable areas of research. Investors would be able to comfortably place their money in high performing companies and advancement would skyrocket as investment rose and companies detecting a decrease in productivity would be able to avoid crisis and get back on track. This problem is still very much open and much of the results so far are still in the range of comparable with random investment strategies and established stock brokerage firms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*Ktpgh4XIcwgOB5_c" /></figure><h3>Previous Works</h3><p>Reviewing last year’s projects we saw a project exploring many of the topics we learned in class and applying those to the problem of stock prediction. We wanted to validate the results of this team and optimize where we could as well as extend the project to the emerging field of GANs networks. We chose to explore GANs over the more common research of LSTMs as the next level of predictors after regressors, mostly because of experience overlap and secondly as for the challenge of exploring less researched areas on the fringe of emerging works.</p><h3>The Dataset</h3><p>In formulating our project we explored the kaggle competitions and saw as the head of hot new competitions with the largest prize pool, the task of predicting on the JPX market. Yet we avoided participating in this competition as we felt the competition was poorly managed and the dataset was less explored, meaning we would be on our own if we encountered problems. The competition would be open for a matter of months and the leaderboard is populated with people that managed to cheese their scores by finding and fitting on the test dataset, using account submission limit subversion, and generally overfitting their models. With this in mind we decided to score ourselves using a common metric and instead predict on a limited subset of high performing NASDAQ stocks. This would allow us to really tune our results and achieve higher potential performance without worrying about being overly general. This is a common practice at brokerage firms everywhere, where a limited set of stocks are utilized. If the stock is unmentioned in results, the target testing stock was Apple stock.</p><h3>What Frameworks are to be Explored</h3><p>We wanted to explore some of the successes of the previous year so that we could have some context to our final results. We will be implementing the same preprocessing methods and scoring metrics for all models to maintain consistency. We will start with a baseline model of a linear regression expected to have poor performance. With this as a minimum viable baseline we then want to replicate the process the group describes for their best model. This means optimizing an XGBoost model and a CAT boost model, then making a stacked ensemble of these two. Our goal is to achieve a higher performance than the best model, and if the work is extremely successful to match or beat real investment strategies. A successful project focuses on exploring the GANs solution so a high performance isn’t a strict requirement of the success of our project, but it will give meaning and weight to our results.</p><h3>Evaluation Metric</h3><p>The dataset will be split into past training data and recent testing data, where all extracted and engineered features maintain this split. This will ensure we can validate our initial training results on unseen data. We will be using the Sharpe Ratio which is almost universally the scoring metric used in ML and human investment strategy scoring. The Sharpe ratio was developed by Nobel laureate William F. Sharpe and is used to help investors understand the return of an investment compared to its risk. The ratio is the average return earned in excess of the risk-free rate per unit of volatility or total risk. Volatility is a measure of the price fluctuations of an asset or portfolio. The Sharpe ratio adjusts a portfolio’s past performance, or expected future performance, for the excess risk that was taken by the investor. A high Sharpe ratio is good when compared to similar portfolios or funds with lower returns. The Sharpe ratio has several weaknesses, including an assumption that investment returns are normally distributed.</p><h3>Data Preprocessing and Feature Engineering</h3><p>In the realm of technical analysis in finance, a technical indicator refers to a mathematical calculation or pattern signal based on the historic price, volume, or open interest of a security or contract that attempts to forecast financial market direction. For a data scientist this is known as feature engineering. For this project, the top technical indicators as proposed by a field of experts, were taken as features for our networks. Seven day and twenty one day moving averages, exponential moving average, log momentum, Bollinger bands and MACD were all calculated and extracted as features using python libraries. A Fourier transform is a common methodology of reforming your data in a way such that statistical anomalies in your data can be suppressed and has been shown to allow for GRUs (utilized in our GANs model) to learn more robust patterns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*X8yxKyGG0nfOPpBq" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*918vpbtYBOvaXwG_" /></figure><p>Our target prediction is the closing price of the stock and graphically we seem to suspect that past data is a good predictor of future data but we wanted to do a statistical analysis with autocorrelation packages. Autocorrelation refers to the degree of correlation of the same variables between two successive time intervals. It measures how the lagged version of the value of a variable is related to the original version of it in a time series. We used a lag parameter of 100 and obtained the following strong positive correlation implying we expect good success of our predictive models:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/0*md2qApBo07ltMs_I" /></figure><p>After all this preprocessing, we are ready to train the different models and get our results.</p><h3>Linear Regression as Benchmark</h3><p>For the linear regression we had a validation sharpe of 0.44, which is close to what the numerai article was able to achieve, getting a value 0.49. For all of these models we split the data into a training and testing set and used the technical indicators of the stock market on a given day in order to determine the closing price of the stock market the following day.</p><h3>Hyperparameter Tuning Algorithm</h3><p>For our searching algorithm, we opted to use a custom binary searching algorithm that rapidly searches a space of possible hyperparameter values. This combined with our knowledge of model tuning by hand, we felt to have better results than a brute force search of all combinations of hyperparameters such as in a typical gridsearch. This allowed for us to make improvements to our design and have rapid turn around in testing.</p><h3>XGBoost</h3><p>With XGBoost we saw that adding hyperparameters to the model gave us the best performance. Our vanilla XGBoost only had a sharpe score 0.71 but our hyperparamterized XGBoost model gave us a sharpe score of 0.78, a significant improvement over the vanilla model.</p><h3>CAT Boost</h3><p>For the CAT Boost model we saw that the vanilla CAT boost had the better performance of a parameterized CAT boost model and the highest sharpe score, giving us a sharpe of .90 compared to the value in the numerai article of .87.</p><h3>Stacking of Our Best Regressions</h3><p>Stacking our hyper parameterized XGBoost model and our vanilla CAT boost gave us our best performance. Combining both of these models gave us a sharpe score of .946, close to the value of .934 in the article. This model worked by taking the average of the two predictions between the two models.</p><h3>GAN (Generating Adversarial Network)</h3><h3>What is a GANs</h3><p>Generative Adversarial Networks, or GANs for short, are an approach to generative modeling using deep learning methods, such as convolutional neural networks. This is a method of turning an unsupervised problem of generating new data into a supervised problem where the model is scored on the believability of its productions. A GANs consists of two sub modules, a Discriminator and a Generator. The Discriminator is fed in its learning process, two classes of images, real and fake. Its job is to learn to distinguish between the two and to then give the Generator relevant information in generating more realistic outputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/0*pNgdf3rZUgnvkiUD" /></figure><p>You want the Discriminator to be good enough at its job that it does not learn simple functions but not so good that the Generator cannot fool it no matter what it learns as then it will likely not converge to good output. To accomplish this we must design a robust network with good tuning.</p><h3>WGAN-GP as an Improved Model</h3><p>Wasserstein GAN + Gradient Penalty, or WGAN-GP, is a generative adversarial network that uses the Wasserstein loss formulation plus a gradient norm penalty to achieve Lipschitz continuity [ <a href="https://arxiv.org/abs/1904.01184">https://arxiv.org/abs/1904.01184</a> ]. This union of two results in an attempt to address weaknesses in older models.</p><p>The Wasserstein distance (Earth Mover’s distance) is a distance metric between two probability distributions on a given metric space. Intuitively, it can be seen as the minimum work needed to transform one distribution to another. It is differentiable everywhere, which allows us to train the model to optimality and is stable enough to mostly avoid training collapse because unlike previous models where the gradient vanishes to zero if the discriminator saturates and becomes too good, this only converges as the distributions get closer, which is a meaningful result. The recently proposed Wasserstein GAN (WGAN) makes progress toward stable training of GANs, but sometimes can still generate only low-quality samples or fail to converge. Without gradient penalty this loss function utilizes weight clipping which can lead to vanishing or exploding gradients.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/0*AI3xhnJj6TzLY9p6" /></figure><p>The introduction of GP is an alternative to this harsh method of clipping that instead penalizes the norm of gradient of the critic with respect to its input. This leads to a model that converges with almost all architectures and little hyperparameter tuning (though obviously better tuning leads to more stable results with less training).</p><h3>The Addition of GRUs</h3><p>In order to improve our model further, we want to learn from the success of LSTMs and somehow capture temporal features and locality in our learning. This is where GRUs as a replacement of normal convolutions achieve much of the gains of LSTMs with a simpler design. The GRU, known as the Gated Recurrent Unit is an RNN architecture, which is similar to LSTM units. The GRU consists of a reset gate and the update gate instead of the input, output and forget gate of the LSTM. In experimentation with convolutions, the problem of selecting good filter parameters is not an easy one. Take for example an image of a midi musical file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*LNaLqYuvn99oxUfK" /></figure><p>We can see that notes have some sort of relation to the immediate surroundings but the sparseness leads to a lack of critical information at times and in that temporally a lot is encoded in the vertical dimensions. A chord of notes is as temporally close as possible but spatially on the image may be very far apart, which leads to a mismatch in this problem. Traditionally convolutions on images work quite well as you can detect the boundaries of image features easily by nearby information, but it is not generally suited to all problems.</p><h3>How does WGAN-GP perform on Stock Predictions</h3><p>Now that we know what WGAN-GP is designed to do, we can address the problem at hand. We implemented the model described and trained it on preprocessed stock data and got the below results for prediction on training data:</p><p>Gains on 1000.00USD = (End Portfolio Value : 5327.83USD, Sharpe Ratio : 0.819)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/0*0q5sJPvr2aIUvVLA" /></figure><p>It looks pretty good after 1000 training epochs. This model is not a generalist yet, but it could be with some tweaking. This is a training on one stock, the Apple stock. This is fine however as many brokerage firms would select a subset of high performing stocks to invest in and our model can easily be trained on multiple stocks, benefitting from the effects of transfer learning.</p><p>However, when you attempt to validate on a future set of completely unseen data you get the following results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/0*jPxFChbX_yed9m8b" /><figcaption>*Note that this is a testing set, mislabeled as training. The original result was lost and could not be corrected.*</figcaption></figure><p>Now does this mean that the model does not work? Not exactly, and here is potentially why. In a review of other similar works, normally using regressors or LSTMs, a few papers mention the COVID period as an anomaly in the data that is reasonably unprecedented. This is a simplistic explanation with a similarly simple solution of excluding the anomalous period that I believe to just be masking an issue, so I will not be taking this approach.</p><p>What if instead what is happening here is that the future data is unbelievable to the current model as it exceeds the bounds of anything it has ever seen before. If you observe the prices of training and then of testing what you see is that the price had never before exceeded around 40USD. With this context and then perhaps the tail end of training where high price is followed by a rebounding price drop, maybe the results have some measure of believability. Yet the fact remains that this is a negative result. You could train the model everytime new data becomes available but that is undesirable. Instead we consider if we can change the data format such that old and new data are much the same. This could be achieved if we transform the data from USD to % gains or loss as scaled between [-1, 1].</p><p>So we set out to see if this <strong>hypothesis </strong>and correction yielded a better result and achieved the following result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/0*IKliAYGlQpoVRbXs" /></figure><p>For our training set on the method of using multiplying factors over the raw price we get very poor fitting changing our features into percentages of previous outputs. To clarify, if the previous price was 1USD and that entry has a multiplying factor of 1.0, the next day is 1USD. For 0.5 MF the next day is 0.5USD. For 1.5 MF the next day is 1.5USD. We transformed all features like this. In this way, we abstract away all the price specific data. This is obviously a poor result and our model manages to turn 1000.00USD into 657.90USD. <strong>I include this here, simply as a report of a failed test</strong>.</p><h3>Implementing A Viewing Window</h3><p>It is at this point that I reexamine my core assumptions and beliefs here and I realize that the way we are training the model is different from the way we are testing the model. The inputs under training have a certain viewing window, while for testing I take this viewing window and learned geometrical attributes and throw it away. Accounting for this and instead producing a testing widow padded with historic data we end up with the following results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/0*RGmm56x8EfuJQhq_" /></figure><p>We can see here that the predictions roughly follow trends now but have significantly weaker granularity in its predictions. This is okay however, as this is a result of undertraining while identifying a working strategy. Going from 500 to 5000 iterations produces the following results on the testing set:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/0*_f9gLvHDubWsM5q7" /></figure><p>We can see here that our model is performing almost perfectly for quite a long time on completely unseen data. <strong>Our model is able to turn 1000.00USD into 1181.15USD with a sharpe ratio of 1.52 from the dates 2017–07–12 to 2018–02–08 (146 days) while just simply holding your money only increased your value to 1064.57.</strong> It cannot be understated how spectacular this result is, and what is even more impressive is that if I search for the best result on this range, the results are even better, whether by measure of maximal gains, lowest risks, or difference from the result of holding long term. The fact that the model diverges over time in places is mitigated by the fact that the model can be continually retrained every month or so with emerging data and new trends, even benefiting from the effects of transfer learning. Data can be gathered nightly and predictions can be ready before opening the next day.</p><h3>Closing Thoughts and Observations</h3><p>GANs networks show promise not only in image processing, but even in the field of finances and stock predictions. These networks can be applied to even more applications than listed here. Work has been done to detect fraudulent earnings reports by detaching the discriminating portion of the network from the wider setup and using learned features to find human generated fake data as opposed to the network’s generated data. With more tuning and a bit of work in properly formatting the predictions, the results of these GANs could be stacked with a well performing regressor for even better, more resilient predictions. It is also possible to extract the entire learned latent space of the network and use this as feature inputs to a regression based model. Overall, the results of these experiments were extremely promising and warrant a more involved refinement of this application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee97f830ad82" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>