 Welcome to another Optaplanner example. This time we'll be looking at investment portfolio optimization. This is a form of financial optimization. So in this case, we are going to the stock market and the other markets out there in the financial setting, of course. And we are going to buy certain assets. So for example, we'll be buying stocks from Red Hat and Google and Oracle, as you can see here, or other companies like Tesla, Ford, and so forth, which is very typical in financial optimization. We'll be trying to maximize our expected return. We'll be trying to make as much profit as possible. So let's take a look at what happens here. So we have all of these different assets classes. This is just one data set. And you can see that, for example, for Red Hat here, we have an expected return of 13.6%. And we have a standard deviation risk of 29%. This was taken from quite some time ago. So these numbers, of course, change every day. And this is based on historical data, of course. Now let's take a look at what happens if we want to maximize our expected return. If we can invest 100% of our money on these different assets and what the planner will recommend us to do. Now let's take a look at what happens if we optimize this. It's quite clear that it's telling us to invest 100% in Tesla Motors. Why? Very simple. Tesla Motors has the highest expected return. It has a 54.7% return, which is, of course, higher than everything else. So it's very clear. We should invest everything in Tesla. That's a good thing. It's a green company. But you might also notice there's a downside on investing in Tesla Motors. The downside is that there is a big risk when you invest in them because they have a standard deviation risk of 53.9%. That's very high. It basically means that, although in general you'll get a lot of money back, there's a good chance that you will actually lose all your money. Not necessarily all your money, but a large portion of your money. So you might want to invest in other things too. More importantly, every one of us has a certain amount of risk they want to take with the assets that they buy, of course. So instead of going for a standard deviation maximum of 100% here, let's see what happens if we go for something like, okay, I don't want to take more than 15% risk basically. The standard deviation is a measure of risk. So let's see how that happens. We solve this. And what you now see is that on the bottom you can see the expected return. It's much less now. It's only 30% instead of 54%. But you can also see that our standard deviation risk is no longer 53%, but instead it's 15%. So now it's telling us that we should apparently buy Apple, not my favorite thing to buy, but we should buy 24% of them. A little bit of Google, again, 10% of Tesla Motors, they are good to invest some of your money in and a large amount of Starbucks apparently in this case. So how do we calculate that standard deviation risk? First of all, this is the input given from historical data from the number of websites you get out there. And then we have a formula to calculate the average standard deviation risk. Now this is not actually the average because you cannot just take the average on these numbers. There's something else that comes into play, which is called correlation. So here's the correlation. So let's take a look. For example, between the first two stocks, Red Hat and Google, there is a correlation of 0.05. So that means that when the Red Hat stock becomes more valuable, it's likely that the Google stock also becomes more valuable for about 5% at least of what the Red Hat gained. This also means that if one of them drops, that the other will likely be affected too. So this is quite interesting. So you can, for example, see that apparently Red Hat and Oracle are quite tied together. They have a 0.6 correlation. And you can see, of course, that everything has with itself, every asset has with itself or every asset class has with itself a zero correlation, of course. And of course, you can also have negative correlation. So for example, here's an example of a negative correlation where the Oracle and the McDonald's company are, for whatever reason, they are negatively inclined. This means that once Oracle starts making more profit, when their stock value increases, it's likely that McDonald's will decrease with at least 1%, which is probably more than a rounding error than anything else, I think. But anyway, that's what the historical data is telling us. You can see sometimes they're higher, right? And once you would mix in other types of assets, like, for example, gold or silver, you would actually see a higher, more correlations that are negative. Because when the stock market crashes, then gold rises, and vice versa, of course. So we need to take those correlations into account. So how do we take those into account? Well, again, Optoplanar just applies those in its calculation to calculate this total standard deviation risk. So you're probably wondering, what's the formula to do that? Well, this is the formula to do that. It's a very heavy formula. Apparently, it's called the Markowitz portfolio theory or something like that, whatever. As you can see, it's not a simple formula. We take in the weights of how much we're investing in that stock. We take in, of course, the standard deviation of the stock itself, of the class itself. And then we also add in the correlations, which are over here. Those, no, the R is the correlation between those two assets and so forth. And we do lots and lots of sums over those. And then we do some squaring over those. So this is actually a formula which, as you can easily see, that goes up to... Here we have to the power 2 and to the power 2. So that's up to the power 4 in terms here. And here we have terms up into the power 5. Which is quite a heavy formula, as you can imagine. Of course, this is no problem for OptoPlanar, because it can handle basically any type of constraint. So, yeah, and for example, if you have two assets, this is the way to calculate that formula. But if you have three assets, it becomes actually heavier already. This is it. And if you have, like in the example I have here, 12 assets, it comes much, much more. Despite that, we can actually calculate it quite quickly. And with OptoPlanar, we can do it even with incremental score calculation, which means that we don't recalculate the entire formula, but only incrementally the part of it, which has changed when OptoPlanar starts changing things. Okay, great. We now know that apparently we have to invest in these combinations to maximize our return and to stable, while we stay below our desired risk ratio. But we might have extra constraints. For example, in this case, we are investing in there are three sectors, namely the tech, the cars and the food sector. You can easily see that right now, I would be investing 48, actually more, more than 50% into the food sector, because I'm investing in Starbucks and McDonald's, right? So actually 56%, we are currently investing in food. So let's say I don't like food that much, or I fear that I expect that there will be a crisis in food very soon. So I don't want to invest more than 20% in food, right? So now I've changed it. As you can see, the direct result is that it tells me, okay, 65% is too much. We can actually see the hard score breaking here. So let's see what happens if we solve this. Let me just go back here and see what happens if we solve this. And you can see right now that as a result of this, now we are only, we are buying 20% of Starbucks stocks, and we are buying no McDonald's corporations. So if you actually look here, we can see that the food, we are now investing only 20% of our budget into the food thing, right? Similarly, it would be easy to add a constraint which says we want a minimum amount to invest in tech or something like that. That's all possible, of course, right? And Sectors is just one axis, right? So right here, you can see the sector, they're in the Starbucks sector, and you have the McDonald's are also in the, sorry, they're both in the food sector. In this case, all of them are in the same region. They're all in the global region. But let me load a different data set that's created by Satishi Yurinki, who helped me create this example. And you can see here in this data set that we have a number of different types of asset classes. These are real asset classes, they're not individual stocks anymore. And we see that we have UK equities, which is in the region UK, and which is in the sector equities. And then we have ex-UK equities, which I put in the region global and in the sector equities. Again, I'm not a financial expert. Might be big there in my data set. But of course, this is just an example. And then of course, we have a couple of others like UK bonds, which are in the UK, but long-term bonds are just global, right? So when we optimize this, we can see we get these numbers. Now, let me just show you on regions, I've actually put a limit on no more than 18% in the UK. So let's say we raise that to maybe 30%, 30%. And we calculate this, then you can see that it will be... Actually, it chooses to invest more in the UK right now. Actually, apparently it really likes the UK. So let's see what happens if I actually put this on 100%. You can see that it goes up to 36% no more. Let's give it some time to find a good solution here. Up to 36% in the UK, right? So the combination of these two is 37 already. Okay, now, if instead we don't like the UK, let's say I don't want to invest more than 10% in the UK, you will see of course that the result of that, we don't invest that much in the UK anymore. And of course the UK is this line and it's this line, UK bonds line. You can see that we have those effects immediately. Similarly, I've put limits on the sectors, as you can see. For example, currently we're putting 40% in equities. There's a limit of 40% there. It probably wants to invest even more in equities if it didn't have this constraint. But for example, for bonds, even if you put a 20% maximum limit there, it will not affect it of course because it only wants to invest 9.7% in bonds at this point in time to maximize that return. So I hope you find this example interesting. And if you want to know more about this, try it yourself in the Optoplanar examples. Thanks for watching. Thanks for watching this demonstration. If you want to know more about Optoplanar, just go to the website optoplanar.org. And if you want to try this example yourself, just download the zip and zip it and run the examples. Thanks for watching.