On AI and Investment Management

Index funds are the most highly traded equity investment vehicles, with some funds like ones created by Vanguard Group cumulatively being valued at over $4 Trillion USD. Index funds have democratized investing by allowing access to passive investments for millions of people. But what are they?

An index fund is a market-capitalization weighted basket of securities. Index funds allow retail investors to invest in a portfolio made up of companies representative of the entire market without having to create that portfolio themselves. Compared to actively managed funds like mutual funds and hedge funds, index funds tend to have much lower fees because the only balancing that happens occurs based on an algorithm to keep the securities in the fund proportional to their market cap (market capitalization, or market cap, is the number of shares that a company has on the market multiplied by the share price).

Starting in the 1970s, the first ‘index funds’ were created by companies that tried to create equally weighted portfolios of stocks. This early form of the index fund was abandoned after a few months. It quickly became apparent that it would be an operational nightmare to be constantly rebalancing these portfolios to keep them equally weighted. Soon companies settled on the market capitalization weighting because a portfolio weighted by market cap will remain that way without constant rebalancing.

With the incredible advancement of AI and extraordinarily powerful computers, shouldn’t it be possible to create new types of ‘passively managed’ funds that rely on an algorithm to trade? What that could mean is that index funds might not have to be market cap weighted any longer. This push is actually happening right now and the first non-market cap weighted index funds to appear in over 40 years could be available to retail investors soon.

But this means that we need to redefine the index fund. The new definition has three criteria that must be met for a fund to meet:

  1. It must be transparent – Anyone should be able to know exactly how it is constructed and be able to replicate it themselves by buying on the open market.
  2. It must be investable – If you put a certain amount of money in the fund, you will get EXACTLY the return that the investment shows in the newspapers (or more likely your iPhone’s Stocks app).
  3. It must be systematic – The vehicle must be entirely algorithmic, meaning it doesn’t require any human intervention to rebalance or create.

So, what can we do with this new type of index fund?

“Sound Mixer” board for investments with a high-risk, actively traded fund (hedge fund) on the top and lower risk, passively traded fund (index fund) on the bottom.

We can think of investing like a spectrum, with actively managed funds like hedge funds on one side and passively managed index funds on the other and all the different parameters like alpha, risk control and liquidity as sliders on a ‘mixing board’ like the one in the image above. Currently, if we wanted to control this board, we would have to invest in expensive actively managed funds and we wouldn’t be able to get much granular control over each factor. With an AI-powered index fund, the possibilities of how the board could be arranged are endless. Retail investors could engage in all sorts of investment opportunities in the middle, instead of being forced into one category or another.

An AI-powered index fund could allow an investor to dial in the exact parameters that they desire for their investment. Risk, alpha, turnover, Sharpe ratio, or a myriad of other factors could easily be tuned for by applying these powerful algorithms. 

The implications of a full-spectrum investment fund are incredible. Personalized medicine is a concept that is taking the industry by surprise and could change the way that doctors interact with patients. Companies like Apple are taking advantage of this trend by incorporating new medical devices into consumer products, like with the EKG embedded into the new Apple Watch Series 4.

Personalized investing could be just as powerful. Automated portfolios could take into account factors like age, income level, expenses, and even lifestyle to create a portfolio that is specifically tailored to the individual investor’s circumstances.

So why can’t you go out and purchase one of these new AI managed, customizable index funds?

Well, unfortunately, the algorithms do not exist, yet. The hardware and software exists today to do this but we’re still missing the ability to accurately model actual human behaviour. Economists still rely on some pretty terrible assumptions about people that they then use to build the foundations of entire economic theories. One of these weak assumptions is that humans act rationally. Now, there is a lot of evidence to suggest that many people act in the way that we are programmed to by evolution. The problem is, a lot of what allowed us to evolve over the last 4 billion years of life on earth, is pretty useless for success in 2018-era financial planning and investment.

All hope is not lost, however. New research into the concept of bounded rationality, the idea that rational decision making is limited by the extent of human knowledge and capabilities, could help move this idea forward. One of the founding fathers of artificial intelligence, Herbert Simon,  postulated that AI could be used to help us understand human cognition and better predict the kinds of human behaviours that helped keep us alive 8,000 years ago, but are detrimental for wealth accumulation today. 

By creating heuristic algorithms that can capture these behaviours and learning from big data to understand what actions are occurring, we may soon be able to create software that is able to accentuate the best human behaviours and help us deal with the worst ones. Perhaps the algorithm that describes humanity has already been discovered.

I Built a Neural Net That Knows What Clothes You’re Wearing

Okay, maybe that is a bit of a click-baitey headline. What I really did was program a neural network with Pytorch that is able to distinguish between ten different clothing items that could present in a 28×28 image. To me, that’s still pretty cool.

Here’s an example of one of the images that gets fed into the program:

Yes, this is an image of a shirt.

Can you tell what this is? Looks kind of like a long-sleeve t-shirt to me, but it is so pixelated that I can’t really tell. But that doesn’t matter. What matters is what my trained neural-net thinks it is and if that’s what it actually is.

After training on a subset of images like this (the training set is about 750 images) for about 2 minutes, my model was able to choose the correct classification for any image that I fed in about 84.3% of the time. Not bad for a first go at building a clothing classifying deep neural net.

Below I have included the code that actually generates the network and runs a forward-pass through it:


class Network(nn.Module):
    def __init__(self, input_size, output_size, hidden_layers, drop_p=0.5):
        ''' Builds a feedforward network with arbitrary hidden layers.
       
            Arguments
            ---------
            input_size: integer, size of the input
            output_size: integer, size of the output layer
            hidden_layers: list of integers, the sizes of the hidden layers
            drop_p: float between 0 and 1, dropout probability
        '''
        super().__init__()
        # Add the first layer, input to a hidden layer
        self.hidden_layers = nn.ModuleList([nn.Linear(input_size, hidden_layers[0])])
       
        # Add a variable number of more hidden layers
        layer_sizes = zip(hidden_layers[:-1], hidden_layers[1:])
        self.hidden_layers.extend([nn.Linear(h1, h2) for h1, h2 in layer_sizes])
       
        self.output = nn.Linear(hidden_layers[-1], output_size)
       
        self.dropout = nn.Dropout(p=drop_p)
       
    def forward(self, x):
        ''' Forward pass through the network, returns the output logits '''
       
        # Forward through each layer in `hidden_layers`, with ReLU activation and dropout
        for linear in self.hidden_layers:
            x = F.relu(linear(x))
            x = self.dropout(x)
       
        x = self.output(x)
       
        return F.log_softmax(x, dim=1)

After training the network using a method called backpropagation and gradient descent (code below), the network successfully classified the vast majority of the images that I fed in, in less than half a second. Mind you, these were grayscale images, formatted in a simple way and trained with a large enough dataset to ensure reliability.

If you want a good resource to explain what backpropagation actually does, check out another great video by 3 Blue 1 Brown below:

So, what does this all look like? Is it all sci-fi futuristic and with lots of beeps and boops? Well… not exactly. Here’s the output of the program:

Output of my clothing-classifier neural net. Provides a probability that the photo is one of the 10 items listed.

The software grabs each image in the test set, runs it through a forward pass of the network and ends up spitting out a probability for each image. Above, you can see that the network thinks that this image is likely a coat. I personally can’t distinguish if it is a coat, a pullover or just a long-sleeve shirt, but the software seems about 85% confident that it is, in fact, a coat.

Overall, it’s pretty awesome that after only a few weeks of practice (with most of that time spent learning how to program in python) I can code my very own neural networks and they actually work!

If you’re interested, here’s a video of the neural network training itself and running through a few test images:

If you’d like to test out the code for yourself, here’s a link to my GitHub page where you can download all the files you need to get it running. Search Google if you can’t figure out how to install Python and run a Jupyter Notebook.

That’s all for now! See you soon 🙂

Facebook Made The Best Tool For Creating Neural Networks

It’s called PyTorch. And it’s a tool designed to work perfectly with Python libraries like NumPy and Jupyter Notebooks to create deep neural networks. As it turns out, it is much easier to use and more intuitive than Google’s TensorFlow packages. In fact, I have been trying to get TensorFlow working on my Mac laptop for about a month, each time I run it, I get a new error, and when I fix that error, I encounter another, and another, until I eventually resign myself to never being able to train a neural network on my laptop.

Fortunately, compatibility is not the only thing that PyTorch has going for it. After its release in 2017, it has been adopted by teams around the world in research and in business. It is extremely intuitive to use (for a high-level programming language targeted mostly at people with PhDs in Computer Science and Mathematics, admittedly). But seriously, it is designed with the structure of neural networks in mind, so the syntax and structure of your code can match the logical flow and linear algebra model that a neural network has conceptually.

A neural network with two hidden layers, as shown above, can be coded in PyTorch with less than 10 lines of code. Quite impressive.

All the squishification functions are built in to the PyTorch library, like

  • Sigmoid: S(x) = 1/(1+e^{-x}) ,
  • ReLU: f(x) = max(x,0) , and
  • Softmax: \sigma(z)_j = {e^{Z_j}}/{\sum_{k=1}^K*e^{Z_k}} .

On top of that, you can define a 756 bit input multi-dimensional matrix (AKA ‘Tensor‘) with one line of code.

Here’s the code for the above neural network that I created. It takes a 784-bit image file, pumps it through two hidden layers and then to a 10-bit output where each one of the output nodes represents a digit (0-9). The images that are in the training set are all images of handwritten numbers between zero and nine, so, when trained, this neural network should be able to identify the number that was written automatically.

Jupyter notebook code for a neural network with 784 input bits, two hidden layers and a 10 bit output

This few lines of code, executed on a server (or local host) produces the following output:

The neural network trying to classify a handwritten 5, notice that the probability distribution is fairly even, that’s because we haven’t trained the network yet.

See how cool that is!? Oh… right. The network seems to have no clue which number it is. That’s because all we’ve done so far is a feedforward operation on an untrained neural network with a bunch of random numbers as the weights and zeroes as the biases.

In order to make this neural network do anything useful, we have to train it, and that involves another step, back-propagation. I’ll cover that in my next blog. For now, we’re left with a useless random distribution of numbers and weights and no idea what our output should be, enjoy!

How Much Is Your Idea Worth?

Nothing.

Zero, nada, zilch, bupkiss. That’s how much your idea is worth.

But…but, my idea is brilliant! It will change the world! My new plan for how to solve snow-covered streets is worth billions!

Really? Who is willing to pay you a billion dollars for your idea? Anyone?…Anyone? Bueller?

I’m sorry to burst your bubble, but the likelihood is that any idea that you’ve had, someone smarter than you has already had. Your idea is worthless. So what? It doesn’t matter that it’s not worth anything now. What matters is what you do with your idea.

Take your idea for a product or service, and sell it to someone. See if there are people willing to put down actual money for what you’ve thought of. And don’t be afraid to tell people what your idea is. If it’s so easy to replicate that just by telling someone, they could take it and turn it into a business, then your idea wasn’t really worth anything, to begin with. How do you sell your idea? Take it to market! Start by defining the problem that you’re trying to solve. Research the hell out of it, what the pain points are that your idea addresses, who has those pain points, and how you can reach those customers. See if you can interview people with the pain. Ask them to tell you a story about the pain and see if it really bothers them enough to change what they’re already doing. This type of research costs nothing but your time and will provide valuable insight into the minds of your target audience.

Image result for lean startup
Has the Lean Startup flopped?

Steve Blank, the entrepreneur responsible for customer development methodology says The Lean Startup is dead. What does that mean? Basically, there’s so much money available through angels and VCs that a young company’s success depends almost exclusively on their ability to raise huge sums of money and not on their ability to bootstrap a startup.

I am not confident that Steve is correct, especially if you live outside of the Silicon Valley bubble, or are creating a startup that doesn’t immediately scream ‘FUND ME’ to Angels and VCs. It’s still possible to build a company without raising a hundred million dollars, it’s just difficult. I’ve been building my company, InOrbis Intercity for over three and a half years now. It started off as a worthless idea, just like yours. But it has grown to be more than that. We’ve just had our first profitable quarter, and we’re still only in Alberta. The vision I have for the company is beyond large. It will be a billion dollar company. But it takes time for great things to happen.

In order to change the way that people travel, we have to reinvent the model of a transportation company. We can’t rely on what companies like Uber did for intra-city ridesharing, and we definitely can’t copy what the airline and bus industries have done (RIP Greyhound). Our vision involves fleets of autonomous vehicles bringing business travellers, vacationers and more between the hundreds of cities that are within a few hundred kilometres of each other. So far, we have connected 6 cities with a combined population of nearly 3 million people. If we provide to access 100 times that number in 5 years, then we’ll be well on our way.

If you have an idea, and you want to talk to someone who also had one, and has tried to turn their idea into a reality, I am always open. Send me a message, I’ll happily sit down with you for a coffee to tell you my story and ask you about yours. I want you to succeed just as much as I want to succeed.

Think your idea is worth it? Let’s make it happen.

Mountain Climbing & Machine Learning

The goal of a neural network is to achieve a certain output or outputs from a given set of inputs. It is relatively straightforward to create a software program that can provide a specified output based on some inputs. A more challenging task is to produce a meaningful result. Let’s say I feed a program an input, like a picture of a dog, and I want the computer to tell me whether the photo is of a dog or not. This is a very simple task for a human to complete, even a one-year-old human child could probably differentiate between a dog and something that is not a dog, like a cat. However, until recently, this was not a trivial task for computers.

IMG_3063.jpg
My good boy, Lawrence

Until programmers realized that learning, at least for computers, is a lot like finding the most efficient way down a mountain, there was little hope of developing software that could make these distinctions. So how is Machine Learning like Mountaineering? Essentially, what we’re trying to do is teach the computer to arrive at the correct answer to a question we already know the answer to. In order to do that, we have to train the program. Training, in this context, means feeding the computer thousands or millions of images of dogs and images of not dogs and having the software output results and check those results with the label on the images. We’re comparing the answers on the images we already know, to what the program thinks the answer should be.

If we start out with a program that outputs random answers based on the inputs, we would expect that those random answers would not correctly identify a large percentage of the dog images. This means the error is large and we’re high up on the mountain. Our goal is to descend ‘Mount Errorest‘, reducing the difference between the actual answer (dog or not dog) and the output of the program or algorithm.

Screenshot 2018-09-26 15.54.34.png

So what’s the quickest way down a mountain? Well, off the steepest slope, of course, a cliff! That’s basically what we’re doing here. Figuring out the fastest way to reduce the output errors to a minimum so that we can make the output of the function equal to the expected answer that a human would give.

Screenshot 2018-09-26 15.54.45.png

Think back to your high school or undergraduate math courses. How do we find the slope of something? By taking its derivative, of course! As it turns out, there is a formula for finding the steepest slope of a multivariable function, it’s called the gradient or the symbol \nabla for short. Since we want to find the quickest way down, we actually want the negative gradient or -\nabla .

If you need a refresher on how to calculate the gradient of a function, or what it is. Here’s a great video from Khan Academy on the subject:

Once we figure out the steepest way down, it’s just a matter of iterating our program through small steps over and over again until the program’s output matches the expected output. Once the training is complete, we can start to feed in the images we want to classify and see how well our model does. We can even use the results from these images to further test and improve the model. Pretty cool, huh?

Cloud Vision Classifier

Image Classifier – Try it out!


Results