Hello everyone and welcome to part two of the finance project solutions lecture.
Let's go ahead and jump right back to the Jupiter note book where we left off last time.
OK.
Here I am at the Jupiter notebook.
Last time we left off we had just created this page plot.
Notice a little bit of strange behavior on Citigroup's return.
And if that stood out to you you can then check out the Citigroup Wikipedia page and you'll notice that
in November 2008 they kind of had a crash.
And we'll actually explore that a little later on.
But coming up next what we want it to do is using this returns data frame figure out on what dates each
bank stock had the best and worst single day returns and you should notice that four of the banks share
the same day for the worse drop and that anything significant happened on that day.
Let's go ahead and get started something we can do just check out the returns data frame and notice
we have these columns here that B A C returned C return etc..
I want to get the minimum values for these returns and they're all SINGLE DAY returns.
I can just say that men will return the single day return value minimums.
Now what we actually want are the dates when this happened and there's couple of ways to do this.
One is you can iterate through the column names and then if you remember your name PI instead of calling
for example men off of that column you can call arguments an argument will return that time stamp and
this case it's the 20th of January 2009.
Now you could do that for every single column or Panthers also allows you to do this across the entire
data frame by just calling id x men and that returns a that basically a series that looks like this
with the dates or indexes of the minimum values for the returns for each bank and you can do the same
thing by saying returns id x max to get the biggest single day gains.
Now as far as the worst strops you'll notice a lot of them happened the 20th of January 2009.
And if you're curious what happened that day you can go ahead and just search google and you'll notice
that this was actually Barack Obama's inauguration date which is kind of interesting.
Something to note here whether or not that's correlated or just a coincidence or basically some some
of the market investors get panicky when some big political change happens.
You can actually check out an idea Max that for instance JP Morgan's biggest return day is the next
very next day.
So don't take this as any sort of political comments or even financial advice.
Stock markets can be kind of weird sometimes.
Now if we check out the next question it says you should have noticed that Citigroup's largest drop
and biggest gain were very close to one another.
So we noticed that JP Morgan's gain happens the same day after the inauguration day right after their
biggest loss.
But if we look at Citigroup mageddon clear that's sell if we look at Citigroup they have 2011 6th of
May and 2000 11 ninth of me as very close to each other is their worst return and their best return
and it's something significant happened at that time.
Actually yes.
If you end up Googling something like Citigroup may 2011 you should get some news that looks like this
where Citigroup announces a reverse stock split and that's essentially what happened around that time
.
And you can notice that it was effective on May 6 2011 which kind of lines up with our data here.
All right.
So that's pretty cool.
We can actually correlate or see real life events affecting our stock prices in our data that we have
in our data frame which is kind of interesting.
Now if we continue on where we want to do is take a look at the standard deviation of the returns and
which stock would you classify as the riskiest over the entire time period and which would you classify
as the riskiest for the year 2015.
So sometimes a basic measurement of risk need riskiness of a stock is the standard deviation of the
returns of the stock.
So if you have a very large standard deviation of the returns it means your stock is going up and down
a lot versus a small Steria deviation.
It means your price is pretty steady.
So if you have a high standard deviation your stock is probably pretty risky.
Let's go ahead and see if we can do this.
It's actually quite simple.
You can just say returns and I'm actually going to put this in the cell right here.
Returns da SDD for standard deviation and we get something that looks like this.
Basically the standard deviation for each stock and it looks like the riskiest overall by quite a bit
is actually Citigroup.
And later on we'll see why.
Because it had a huge crash and as far as for 2015 we can go ahead and say returns in what we want to
do is find out if we take a look at the head.
We want to only get values for 2015 and that's based off of the index.
Meaning I want to actually grab a section or a slice of the index and I can use that using I X and I'll
passen from 2015 0 1 0 1 so from January 1st 2015 as a slice all the way up to 2015.
Twelve thirty one is a slice and then call standard deviation off of that.
And you'll notice that in 2015 which is way outside of the crash of Citigroup they actually all have
a very similar risk profile with false Fargo being the lowest risk by not that much.
And it looks like Morgan Stanley or Bank of America are the highest risk but essentially the standard
deviations are extremely close to one another.
OK next we wanted to say create a plot using seaborne of the 2015 returns for Morgan Stanley.
Let's go ahead and do that.
We can go ahead and say as soon as I thought this plot in I want the 2015 returns of Morgan Stanley
when we go out and copy this code here and this is my 2015 returns.
And then I just want Morgan Stanley meaning I just want the M-S return column and we can go ahead and
color this green just so it matches the solutions lecture.
And let's go ahead and increase the bin size or the number of bins excuse me since we actually have
quite a bit of data.
So I'm going to go ahead and say bins is equal to something like let's just say 50.
All right.
And now we have a distribution of the returns for Morgan Stanley in the year 2015.
All right so here we can see as far as the M-S return and the distribution as far as what we're looking
at is that it's pretty stable.
You're looking at percent returns that only deviate about 0 6 from the average but we're going to go
ahead and do now is check out a distribution plot using seaborne of the 2008 returns for Citigroup which
was quite a volatile year for them north to do this will go ahead and say seaborne this plot or Sonesta
this plot returns I X and I will go ahead and slice it and copy this but instead of 2015 we'll go ahead
and put in 2008
we'll do the same thing over here 2008s and then we want the C return column.
Don't go ahead and color this red so we can tell a difference here and I'll go ahead and set the bin's
equal to.
Let's go ahead and say 50 again.
And if I run this if we go in and compare this you'll notice that the standard deviation is actually
quite a bit more stretched out especially here you're almost that points 6.
And if we look back for kind of a normal year for a bank 2015 Europe point 0 6 is actually 10 times
as much.
As far as this distribution shows the extremes out here is quite an outlier and we'll go ahead and explore
what actually happened to Citigroup during this year.
But this is kind of an indication here of the financial crisis that the stock went through.
All right.
So we're gonna do some more visualizations and a lot of this project projects actually focused on visualisations
and you can feel free to use any of your preferred visualization libraries in order to recreate the
plots described below.
You're going to be able to do them actually with either see more lib plotline and cufflinks or even
just panderers so you can go ahead and choose whatever methods you like.
I'm going to go out and show you a couple of ways to do each plot and we'll actually show you how to
use Paulian cufflinks in order to make these plots well.
All right so after running those imports we want to go out and do is create a line plot shows in close
price for each bank or the entire index of time.
And there was a couple of hints here to either try using a for loop or use the cross section method
to get a cross-section of the data.
And here we can kind of see both methods at play.
Let's go ahead and show the for loop method first.
And hopefully here it's clear now using seeing Citigroup as seeing green this huge crash in 2008 has
had to do as far as their standard deviation being quite large for that year.
But anyway it's going go back to the for loop.
I'm going to go and say for a tick in ticker's which remember is that list of stock tickers call bank
stocks tick closing price and then just go ahead and plot this out and I'm going to plot this out I'm
going to specify that the label is equal to tick.
Now we know what line is which as far as colors go.
And then I also going to specify Fig's size to be stretched out a bit just so we can kind of get this
same visualization we have here.
And then finally I'll call t legend so we can see the legend and if I run that I get the same plot.
It looks something like this so we can clearly see that Citigroup just really crashed after 2008 the
great recession.
And what's interesting as well is this Goldman Sachs red line which kind of like dips and then bounces
back up quite quickly after the recession.
And you have to zoom in further on to see the other banks and see how they perform.
But relatively speaking it wasn't nearly as much the same effect as the other stocks let's go ahead
and show you how to do this we have cross section could have also said bank stocks stop access with
key equal to close access equal to ones and we're looking at the columns say level is stock info.
And if we just take a look at that.
This is basically all the closing prices for the stocks.
And then what we have to do there is just call plot and you get the same plot.
So instead of doing a for loop you can just do this all in one line using a cross section.
And again you can change the figure size and do any other map plot loop commands you want on that plot
.
Finally let's go ahead and show the plot the way to do this which is essentially just this same line
which creates the data frame.
But then you call I plot off of it and cufflinks will now allow you to create this very nice looking
plot where you can visualize things and actually see the values and kind of play around with it.
So if you actually want to kind of zoom in on the non Citigroup or Goldman Sachs stocks you can go out
and do that by just grabbing a cross section and then checking out the dips so you can see the dip here
during the financial crisis as well as some weird behavior during things like stock splits such as this
one in May.
All right.
So this makes it a lot of fun to play around of the data and kind of explore the stock prices during
the financial crisis.
We're going to go ahead and do is move along to moving averages.
And we're going to analyze the moving averages for the stocks during the year 2008.
So the first thing we need to do is plot the rolling 30 day average against the closing price for Bank
of America stock for the year 2008.
Let's go ahead and show how we can do this.
I'm going to say B A C and check out the head of B A C number.
That was my very first original data frame for Bank of America.
Before we actually put it into bank underscore stocks and then going to grab the close call them off
of that and I'm going to use the I-X method to go ahead and slice the dates I want and I want 2008 January
1st all the way to 2009
January 1st.
So a whole year.
And then what we want to do is use the rolling method.
So Panas has the ability to give you a rolling method for a moving average and then we can say mean
as our method.
And then we can just go ahead and plot this and we can actually plot this with a label so we can show
what we're looking at.
So we'll go in and say label this is a 30 day moving average as a label and the next thing we're gonna
do is we're just going to plot the same thing see close but in this case all plotted against the stock
price.
So say that plot and this label will be BAMC close and then we'll call PLDT that legend.
So if we just run that we get a plot that looks like this and we can go ahead and stretch this out if
we want to by saying Piazzi figure big size and we can state this to be something a little nicer like
I told before it's kind of a stretch it out and there is your 30 day moving average against the actual
closing price.
This allows you to kind of see the general trend of the stock as it goes along.
And you can change this window to be the weekly moving average or 60 day moving average whatever you
want.
All right the next thing you want to do is create a heat map of the correlation between these stocks
closing prices.
Let's go ahead and show you how we can do that.
You can call Asinus the heat map and now we want to get a correlation of the stock Issing closing price
.
Let's go ahead and just hash tag that out and actually build out that data frame first.
It's going to be very similar to what you've been doing before.
We'll go out and grab the cross section of the closing prices by saying he close on X equals 1.
The level is stock info
and if we just check that out.
That's this just the closing prices for the stocks.
And if I say c o r r that gives me the correlation.
And now I have something in a matrix form that I can call into heat map.
So I'm going to go ahead and unhatched out close that and say heat map here and check out my heat map
.
And now I have my heat map and if I want the actual annotations I can just say and it equals true and
then we'll go ahead and put in the annotations here and here we can see the correlation between the
closing prices of the stocks in something we can optionally do as well as use cluster maps to actually
cluster these correlations together maybe helpful make it a little more clear as to what's going on
.
So in sort of heat map all you have to do is type in cluster map and you run this you should get a lot
that looks something like this.
So it looks like seaborne tried to group together Citigroup Morgan Stanley and Bank of America and their
correlation together into kind of one group as well as gold.
Goldman Sachs J.P. Morgan and Wells Fargo kind of into another group is having some sort of correlation
to each other.
All right.
You can kind of check out which plot are which stocks are the most correlate or if each other.
Looks like it's JPMorgan and Wells Fargo as well as Morgan Stanley and Bank of America.
OK.
So that was it for the financial project.
There's a part two optional that will show in part three of the solutions lecture and that has more
to do if I plot and some of the special capabilities of cufflinks but due to its heavy reliance on cufflinks
it's kind of an optional thing that you can kind of explore on your own in case some of that functionality
is broken in the future.
But I hope you enjoy this project.
It was meant to be extremely challenging and difficult but that's kind of by nature of stock and financial
analysis.
The kind of thing you'll encounter if this is something you're interested in.
So a lot of these things are quite tricky and hard but hopefully you can see that what you feel really
really comfortable of pandas and seaborne is these things become a lot easier than if you had to do
them for instance in Excel or in another programming language for a lot of these answers with just a
few lines of code or even just one line of code.
And what's really cool is the fact you can make something like this in one line of code so you can definitely
impress your colleagues or your friends by showing them your own financial projects analysis.
OK.
Hope you enjoyed the project.
Coming up in part 3 we'll just go over the optional section of these solutions.
Thanks everyone and I'll see you at the next lecture