Practical Portfolio Optimization with Python - YouTube

Channel: unknown

[0]
hello everyone and welcome to the pythoninvest.com聽 youtube channel today we will talk about the聽聽
[6]
practical portfolio optimization so let's assume聽 you have a sum sum of money and you want to invest聽聽
[14]
into several stocks you may want to get an聽 optimal split of weights for your investments聽聽
[25]
why are going to do this a smart portfolio聽 management can reduce the overall risk portfolio聽聽
[32]
can increase returns per units of risk and聽 can reduce losses in the worst case events聽聽
[44]
we will use this standard library by portfolio聽 opt you can read the user guide here or you聽聽
[53]
can check the detailed collab example i聽 used a lot of functions from this example聽聽
[63]
let's check the plan we'll start from the聽 preparation works of imports getting the聽聽
[68]
financial data and built-in pivot tables then聽 we will talk about the correlation metrics the聽聽
[77]
intuition behind the portfolio optimization聽 theory then we will see three optimal聽聽
[85]
portfolios with mean volatility max share聽 pressure and mean c var or conditional variance聽聽
[92]
portfolios then we will see an efficient聽 frontier and finally we will um find the the聽聽
[102]
best discrete allocation of stocks i also wrote聽 a detailed article on my website by invest.com聽聽
[110]
with a slightly different setup when you do a聽 repetitive short term trading for three days聽聽
[121]
i manually build all combinations of聽 portfolios and check the portfolio metrics聽聽
[134]
and build a scoring model for those metrics聽 so let's begin first we install y finance and聽聽
[146]
get usual imports for panda snappy and matplotlib聽 we will have 2000 us dollars for our experiment聽聽
[158]
here and these stickers to invest they are聽 actual tickers that i recently tried to invest in
[172]
here we we get the daily prices for the stocks for聽 example last day prices is actually two day and聽聽
[185]
for today we have eight different records for聽 um for the stock values we also generate pivot聽聽
[194]
table so that one they represents one line聽 and we store only close prices for the stocks聽聽
[206]
so now with this setup we have eight聽 different rows which represent time series
[215]
stats and we can find the correlation聽 correlations between those time series聽聽
[223]
correlations matrix is a matrix like like this聽 it is it has one on a diagonal so correlation聽聽
[232]
of a stock with itself is always one it is a聽 symmetrical matrix so correlation of ba with聽聽
[239]
with bodies the same is the same as but with with聽 ba so so we are interested in uh either bottom um聽聽
[251]
triangle or upper triangle above the main diagonal聽 here we have the bottom triangle which is colored聽聽
[260]
it is always good to have different colors聽 or more very variation of a correlations聽聽
[268]
so any correlation lies between -1 and 1 and聽 stocks that have a similar correlation say 0.96.87
[282]
is not very good for the purposes of a portfolio聽 diversification because they tend to move in聽聽
[292]
the same direction and the optimization聽 algorithm will probably try to mix in um聽聽
[302]
other stocks that are loosely correlated with聽 many um top performing stocks so for these set聽聽
[312]
of stocks probably ptr is the most important one聽 because it's not correlated with many other stocks
[322]
so now let's uh look at the pi portfolio opt聽 library we do install it check its version聽聽
[329]
and we import some functions and聽 first we find an expected returns聽聽
[336]
for individual stocks there聽 are a number of options to find聽聽
[341]
in the expected returns here i use cpm聽 underscore return function it can be聽聽
[350]
mean historical return or ema聽 historical return you can read about it聽聽
[357]
in the library itself then we we聽 find the covariance matrix so it's a
[368]
unconstrained parameter and constraint matrix for聽 covariance between each set of each pair of stocks聽聽
[382]
and when we have mu and s we can build聽 an efficient frontier and calculate聽聽
[392]
the best performance portfolios we also
[397]
show we also
[402]
specify weight bounds between 0 and 1 so that聽 we don't allow short trading with negative聽聽
[410]
weights we also buy stocks and go along and then聽 sell after some period of time um let's check聽聽
[419]
what is a minimum volatility portfolio what is聽 a max sharp ratio and what is a c bar optimal聽聽
[429]
portfolio um okay first as we remember its聽 average returns expected for each stock and聽聽
[438]
they can be slightly different uh depending on the聽 method of estimation but here we see that nvidia聽聽
[445]
is actually the top performance stock with 30 uh聽 39 and all expected returns um also shopify and
[459]
b a are top performing stocks聽 and if we want to optimize聽聽
[468]
portfolio and to reduce its minimum volatility聽 sometimes the most performance stocks like nvidia聽聽
[477]
um they will have weight zero probably because聽 they bring a lot of volatility with the returns so聽聽
[490]
the metrics for this portfolio will be expected聽 and i'll return that is 16.2 percent which is聽聽
[497]
uh lower than um many of the returns for the聽 individual stocks but it has a low volatility of聽聽
[504]
8.9 percent and sharp ratio that is expected and聽 i'll return divided on nl volatility which equals聽聽
[512]
to 1.67 next if we want to optimize this sharp聽 ratio which is expected return pure volatility聽聽
[521]
we will see slightly different weights now nvidia聽 has a weight and va has a weight and with this we聽聽
[530]
are adding the top performing stocks where you聽 are increasing the expected handle return from聽聽
[537]
16.2 to 22.4 percent um but the annual volatility聽 is increasing only from 8.9 to 10.7 so that sharp聽聽
[550]
ratio actually is increased from 1.67聽 to 1.91 and in many cases this portfolio聽聽
[562]
will be treated as the optimal portfolio聽 and last but not least portfolio is
[574]
the the one that minimizes uh the losses in聽 the worst cases so um let's start with the聽聽
[583]
um max server portfolio um weights we have these聽 these weights and calculate for this portfolio聽聽
[593]
the daily returns uh distribution so in in聽 many cases daily returns are lie between聽聽
[604]
minus five percent and plus five percent we and聽 it can be slightly shifted towards left or right聽聽
[613]
um but in most of the cases you will see聽 a bell shy distribution curve then we find聽聽
[623]
5 worst cases quantile so here it is somewhere聽 between minus percent and zero and we take聽聽
[634]
average um returns for the worst cases so we聽 see that um uh the the quantile is actually聽聽
[647]
minus 2.56 so it is here and as 5 5 of the聽 worst cases days they will bring losses that are聽聽
[663]
higher than minus 2.56 percent for an optimal聽 sharper portfolio and if you take average of聽聽
[670]
these losses it will be minus four percent you can聽 improve this uh average of losses and uh optimize聽聽
[680]
your portfolio uh for uh to reduce this um average聽 of uh bad losses from minus four percent to聽聽
[689]
um minus three percent but uh you will lose some聽 of the expected tunnel return and this will be聽聽
[698]
a big loss because you are losing from 22 percent聽 in case of a sharp ratio to 15.8 percent uh for a
[710]
optimal civil war portfolio but it's up to you聽 whether you want to risk more and choose max聽聽
[717]
sharper portfolio or you want to reduce volatility聽 and reduce conditional variance for a portfolio聽聽
[725]
to build a highly protective case um now we聽 will talk about the efficient frontier um you聽聽
[736]
you will see um these eight points on on the聽 chart each point represents one stock um and聽聽
[748]
you will see returns and volatility on on聽 access so if if you are a risky investor聽聽
[757]
you you don't need to to make an optimal portfolio聽 you can just simply choose maximum return stock聽聽
[768]
nvidia in this case or you can choose a maximum聽 return alternative investment as bitcoin and聽聽
[774]
um go all in but you know that over the long聽 term this is a very risky strategy and that's聽聽
[782]
why we want to see um some um hatching in in our聽 portfolio and we are trying to mix in other stocks聽聽
[794]
um so if you're adding other other stocks from聽 these eight we will see later that any combination聽聽
[806]
of stocks will deliver some points under this聽 line which is called an efficient frontier聽聽
[815]
it is a convex area and any any point specifically聽 on this line will be a point on an efficient聽聽
[825]
frontier so this optimal uh selection point is聽 a maximum sharp ratio point when the dungeon聽聽
[837]
curve is is optimal on on this line but you can聽 select any any other point depending on your risk聽聽
[846]
return profile so let's try to get this efficient聽 frontier uh we we are doing um simulations and we聽聽
[859]
are building 10 000 of portfolios with various聽 uh weight splits between stocks with dirichlet聽聽
[868]
random distribution um and if you put all lines聽 from um the simulation or all all points from聽聽
[879]
the simulation you will get this this area聽 so you can approximately understand that um聽聽
[888]
the there will be a frontier uh for for聽 this area and it's always better to select聽聽
[899]
higher return um stocks with the given high return聽 combinations or higher than portfolio with a given聽聽
[909]
volatility say you have 15 volatility in聽 your portfolio so you're ready to risk 15聽聽
[916]
of your money you will definitely want to聽 choose a portfolio that will deliver your聽聽
[924]
approximately 30 of of returns highest possible聽 points and that's why you want to select聽聽
[932]
a point on this efficient frontier so last聽 piece of analysis is a discrete allocation聽聽
[940]
let's check why do we need this um we聽 take last day 8 prices and we take the聽聽
[951]
maximum sharp ratio weights if you multiply聽 prices on on weights you will see that聽聽
[959]
you need to buy specifically this amount聽 of stocks if you have in total just聽聽
[967]
dollars and many numbers are not close聽 to the discrete number of stocks like聽聽
[974]
0.48 for a shopify stock um so shopify stock聽 stocks price now is almost 1500 dollars
[991]
so
[994]
if you can buy only discrete number of stocks you聽 can't buy 0.48 you will need to buy at least one聽聽
[1003]
one stock of shopify and you will spend 1500 out聽 of 2 000 that is almost all money that you have聽聽
[1011]
that's why there is another function which聽 is uh called discrete allocation method聽聽
[1019]
which will find your um an optimal portfolio聽 which is close on metrics to the um max sharp聽聽
[1031]
or another optimal portfolio line on the efficient聽 frontier but this portfolio will have uh only聽聽
[1042]
um discrete amount of stocks to buy and this聽 restriction is often imposed by many brokers聽聽
[1051]
and you can't buy fractional shares but sometimes聽 you can buy and it can be an important factor to
[1061]
to think about when you choose your own broker聽 thanks for watching and let me remind you once聽聽
[1068]
again that there is a very detailed article聽 um on on the portfolio optimization that i聽聽
[1076]
wrote by myself where you can try to get this聽 portfolio metrics do the simulation and come聽聽
[1086]
up with the best performing portfolio with聽 your own hands and not just call the methods聽聽
[1097]
from the standard library and not聽 understanding how they actually聽聽
[1105]
calculate all those numbers thanks聽 for watching and see you next time