15.5 Jupyter Notebook Sharpe Ratio Calculations - YouTube

Channel: unknown

[0]
[Music]
[4]
so
[5]
we will start by the sharp ratio and
[8]
figure out what it is by calculating
[10]
inside jupyter notebook so let's get
[12]
started so we are here inside jupiter
[14]
notebook and we have something about the
[15]
sharp ratio we remember it represent
[18]
both the risk and return simultaneously
[21]
if you want to read more about it you
[22]
can actually follow the link here to
[24]
investopedia and i actually think also
[27]
to have a video this
[29]
telling a bit more about it if you're
[31]
interested otherwise we're just going to
[32]
use it as it is and so on and the
[35]
formula here and this is the formula
[37]
we're going to use to calculate it and
[39]
again here in this tutorial here not
[41]
tutorial this is a lecture here we're
[43]
going to use zero free
[45]
zero as the risk-free return because the
[47]
risk-free return is so low and many
[49]
people when you look
[51]
currently are actually using that value
[54]
and for instance for me in denmark the
[56]
risk return is zero and sometimes it's
[58]
even negative
[60]
interest rate in denmark it's crazy so
[62]
you actually pay money to
[66]
it doesn't make sense to me either but
[68]
actually that's how it is okay and then
[70]
we're gonna use the standard deviation
[72]
of the portfolio
[74]
and again here the goal is to get a high
[76]
sr ratio as sharp ratio here right
[80]
so
[80]
i did what we usually did here i
[83]
imported pandas i imported numpy i
[85]
imported matplotlib that means i didn't
[87]
do that yet
[88]
now i did here we are and you did it too
[91]
right yes good
[93]
and then we do the same thing here we
[95]
read the data here
[97]
for the tiggers facebook nvidia amazon
[99]
hp and apple and we do the magic here
[102]
i've gone through this so many times now
[104]
so i think you feel comfortable about
[106]
that so let's just take a look at the
[108]
data here
[109]
head it is as we are used to
[112]
so
[113]
what is the first thing we need to do
[115]
well let's look at the formula again
[117]
right we need to find
[119]
the return of the portfolio right the
[122]
return of the portfolio well i actually
[125]
already said that we actually did that
[127]
already in the previous lecture so let's
[130]
do that so let's make it a portfolio
[133]
portfolio
[136]
later we're going to call these
[137]
portfolio weights but that's when we
[139]
calculate a lot of them right now we
[140]
just keep the name portfolio
[142]
and we use an array numpy array nd array
[146]
and
[148]
let's try to make a different allocation
[149]
than last time because i found that
[152]
there were too many 20
[154]
huh don't you think i think so
[156]
okay so let's try to do 20
[159]
15
[160]
oops i was not 15 15
[164]
35
[166]
[Music]
[168]
what 10 and
[171]
25 right we do this one is that one even
[176]
actually i don't know i'm so good at
[178]
math when i'm sitting in front of a
[179]
computer so let's just verify this
[184]
no this is not one so what what did i
[186]
mess up so let's
[187]
say this one is 30 right
[190]
here we have it one
[192]
good and for some reason inside when you
[195]
use jupiter notebook it represent the
[198]
numbers quite annoying but this is the
[200]
same as one 0.9999 infinitely is the
[203]
same as one
[204]
luckily when you do the calculations
[205]
within
[207]
numpy's and the
[209]
pandas it never does represent the
[211]
numbers in this awkward way
[213]
but
[214]
the numbers are identical
[217]
good now we have the portfolio so we
[220]
also wanted to normalize the data right
[222]
so data
[223]
equals to data
[227]
divided by data
[228]
i log
[230]
zero right that was how we
[232]
normalized it maybe just do it in a
[235]
different cell actually
[237]
not that it matters that much but i
[239]
actually just
[240]
want to keep it separated here data hit
[243]
and here we have it normalized right
[245]
so what we're looking for in the first
[248]
place was actually
[250]
figuring out what is the return of our
[252]
investment right so that's what we are
[255]
aiming to figure out so how do you get
[257]
that
[258]
do you have a guess
[259]
yeah
[260]
good
[262]
so what we're actually going to do in
[263]
this one here is actually put data and
[265]
we put
[268]
log returns because that's we need to
[270]
calculate uh the
[272]
we need to calculate the standard
[275]
deviation right so how do you do that
[277]
the log return of the
[279]
data
[284]
um
[285]
we need actually to take the sum of the
[287]
data and then we take the data here
[290]
and uh
[292]
yeah we need to take the log
[296]
log of data
[298]
divided by data shift
[300]
correct me if i am wrong so what we're
[303]
doing here in this one here this is
[305]
maybe a many step at the time right we
[307]
do the
[308]
log returns so the log returns of the
[312]
normalized data is the same as log
[314]
returns on non-normalized data because
[317]
it's percentages right so that's a log
[319]
return and then we actually sum all
[321]
these up to find the log return of the
[325]
full investment but we also
[329]
need to actually calculate the
[332]
port
[333]
folio
[336]
how it's weighted portfolio here
[339]
and then finally we need to sum it along
[341]
axis one here right this is it data head
[345]
here we have it right so what we have
[347]
out here is a log return of the
[350]
portfolio and not the individual
[355]
stocks here right so here we have it
[357]
right so this is how you can see it so
[359]
these are the daily log returns as we
[362]
know them
[364]
awesome so far so good are you
[366]
comfortable i am so let's continue so
[369]
let's actually try to visualize this
[370]
this could be funny fig axis
[373]
plc sub
[375]
plots
[377]
perfect and what is it we want to
[379]
visualize is actually this data log
[381]
returns
[382]
log return
[385]
and then we want to
[386]
we want to plot it
[390]
we want to plot it as
[393]
kind hist
[395]
histograms and how many bins do we want
[398]
50
[399]
i don't know
[402]
and then axis axis let's try to do this
[406]
it was not entirely happy here i can see
[409]
so what am i missing here
[411]
so what i'm missing here is actually
[413]
it's not called bin it's called bins
[414]
right here we have it okay good uh so
[417]
here we have it
[418]
that's actually nice right so what you
[420]
can see here is actually let's just
[424]
do this
[425]
what we can see here is actually that we
[427]
have most of them around zero here
[430]
obviously and the outliers are often
[432]
down in the negative side here that's
[435]
how it is because when people panic they
[437]
panic right but actually not that bad we
[439]
actually have some really positives here
[440]
as well
[441]
so it's quite nice
[444]
so let's calculate this magic sharp
[447]
ratio after we've seen this picture well
[450]
how do we do that
[452]
well let's look at the formula right so
[454]
now we actually should be able to
[455]
calculate the risk-free return
[459]
the risk-free return here or the return
[461]
of the portfolio apologies not risk
[464]
return and the standard deviation of the
[465]
portfolio so let's do that
[468]
so the sharp ratio
[469]
is equal to
[471]
two
[473]
so we take the data
[475]
and then we have the
[478]
log return
[480]
and then we actually take the mean value
[482]
of that
[483]
and
[484]
let's do the daily first and then we
[486]
take data
[488]
and
[490]
then we do log return
[492]
and then we do the standard deviation
[495]
here so this is also the daily standard
[497]
deviation and so we don't do the
[499]
normalized first so sr so then we have a
[502]
value like this so this is a daily so
[504]
get the annualized sharp ratio
[508]
then you actually take the sharp ratio
[510]
and
[512]
multiply
[514]
25
[515]
2
[516]
and the square root of that
[519]
so here we have a annualized sharpe
[521]
ratio which is 0.87 or 7.8 almost right
[526]
so
[527]
a bit note on the calculations here
[529]
because this might confuse you a bit but
[532]
actually what we are looking let's try
[533]
to do the same calculation here again
[537]
because if you take the mean here what
[538]
you do is actually calculate a yearly
[542]
return mean here
[543]
i just add parenthesis
[545]
here this is a yearly return and then we
[548]
actually take the square root of this
[549]
one down here
[551]
or 252
[553]
square root of that
[558]
i put two
[559]
commas here and then we actually get the
[561]
same value here so
[563]
the key thing is that actually
[564]
multiplying by this and
[567]
multiplying by this below is actually
[569]
the same as doing this one here this is
[571]
just mathematics
[573]
but it can be a bit confusing why i'm
[574]
multiplying by this value here but this
[576]
is exactly what we do here in
[579]
we do here we calculate the mean return
[582]
we take the mean return so this is the
[584]
average return and multiply that by 252
[586]
trading days and then we take the daily
[589]
standard deviation and multiply that out
[591]
by the square root of 252. that was a
[593]
formula remember okay so here we
[596]
actually have the sharp the annualized
[599]
sharp ratio and this calculation we're
[601]
going to use later
[603]
to find the best portfolio because right
[606]
now it's just one and is this value good
[608]
well we are trying to get at least above
[610]
one of this but in theory we just need
[613]
to compare each of the sharp ratio with
[615]
each other and to see what is best and
[617]
the higher the better so this was one
[619]
portfolio we could see
[620]
if you needed to do this manually trying
[623]
each one of them to find the best one
[624]
that will take a long time so this is
[626]
where monte carlo simulation comes into
[628]
the picture so this is exciting right
[631]
okay
[632]
so what did we do here well we
[634]
calculated the sharp ratio with a
[636]
risk-free return of zero you can add any
[639]
value but you see it really doesn't make
[640]
a diff big difference it just
[643]
changes the value of the sharper ratio
[645]
obviously so
[647]
we did that and we did
[649]
do that by making some random portfolio
[651]
here and then
[654]
we just plotted it for fun and then we
[657]
calculate the sharpe ratio and the
[658]
annualized shop ratio so that's
[661]
basically it straightforward so let's
[663]
continue to the next one see you