Net Present Value PROFILE & Internal Rate of Return (IRR) Basic Python/Matplotlib Visualization!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will explain and use the concept of net present value profile and internal rate of return to practice our python plotting skills while learning finance please don't forget to like this video and subscribe to my youtube channel so i can keep adding more content on python and finance i would really appreciate your support to follow the topics of this video it is essential that you watch my previous video net present value with python so if you haven't done so please check the link in the description okay let's do this first we set the cash flows from a project starting from the initial cash outlay or the initial investment with a negative sign followed by the cash flows generated by the project with a life of five years from t1 to t5 we save these variables in our python environment as well as the discount rate that we'll we're going to be using in order to calculate the net present value next we're going to be saving each one of these variables into a python list to be able to use them inside a for loop to set the net present value function we will use the numeran function this time what does the enumerate function do let's take a look at it so if we say enumerate and include the cash flows that we have saved in memory the only thing that we will receiving is the location where the information is going to be stored so we need to force that function in order to display the output by using for example the function list so in this case we see that we receive a list of tuples that include every single one of the cash flows of the of the project and an index that corresponds to them so for example for the first cash flow 4000 we have the index zero for the second cash flow we have the index one for the third cash flow the index two and so on okay so we can use this function in order to set our own user created function to calculate the net present value so we're going to be defining it by naming def a calc net present value in this case npv and is going to be taking two parameters the discount rate on the one hand and the cash flows on the other we set the net present value equals to zero in order to start adding the discounted values of every single one of the cash flows and we say that for i in val in the enumerate function that includes the cash flows that we defined in the calc npv function we want to add them every single one and discount them so we will divide eval every single one of the cash flows by one plus the discount rate raised to the power of i finally we're going to be returning the return the net present value so the function that we need to use is ready so let's just take a look let's just verify if this is working so round calculate the net present value with the discount rate that we set equals to r or 12 like in this case and the cash flows equals to the list of cash flows so let's take a look at the three decimal digits to see if our function works so it is telling us that the net present value of the project with a discount rate of 12 percent is going to be equal to 7 674 okay good so let's start plotting the net present value profile first of all we need to define the net present value profile so this is simply a graph showing how a project's net present value changes as the discount rate changes in this case we calculate the corresponding net present value of a project with a specific range of discount rates so we're going to be first of all defining the rates or the set the range of rates that we want to use as discount rates by creating an empty list in addition we're going to be creating another empty list that will take the all of the net present values calculating at every single one of the rates that we will be defining now so we will be saying we will be using a for loop and saying that for i in a range that will go from one percent to 40 percent we want to populate the rate the empty list call rate with the following information so the rate we're going to be f to the rate we're going to be appending 0.01 multiplied by i meaning by every single one of the values of the range from 1 to 41 in order to get the percentage in decimal points so we can use them now in order to calculate the net present value so we're going to be populating now the empty list that is called npv so we set mpv npv append and we will include the rounded value of the results when we use our function to calculate the net present value that uses a discount rate of 0.01 multiplied by every single one of the values in this range okay so in addition to that we know that our function takes in addition to the discount rate it takes the parameter cash flows that is saved in the list so we're going to be setting three decimal digits so we run this cell and we have very easily populated the raid list as well as the npv list so let's take a look for example at the first five 15 elements for example over the rate uh variable and we see that it contains the interest rate uh one percent in decimal digits from one percent up to 15 so this is these are the 15 the first 15 elements we can take a look also at the net present value variable to the first 15 elements and we can see that it contains the net present values calculated with every single one of the interest rates that we have defined in this variable okay it is a good idea now to convert or to include these two variables into a single data frame so we can say we can do this by using the pan a pandas data frame importing pandas as pd and then defining the data frame called df as pd data frame and we will include the data in the form of a dictionary so we will say that the column that the first column is going to be called rate and as the values it is going to be including all the values in the variable rate okay so this is going to be the first column in addition we will assign a second column that we will call the net present value and that will take the values included in the npv variable okay so we said npv we run this line of code and we can inspect the first 23 elements of that data frame so we see that there is there are two columns the column rate and the npv of which every single one npv corresponds to the interest rate that we have used in order to calculate it so we can verify this by taking a look at the net present value that we have calculated with 12 so as you can see this value is exactly the same one as we have calculated at the beginning once that we have our pandas data frame we are ready to start creating our plot so i'm going to importing the matplot live library dot pipelot as plt as his convention and before starting plotting let's just remember that we can set we can verify that the um we can filter the net present value that corresponds to the uh to 12 so we will see a little bit later why this is going to be useful so we're going to be saying we're going to be filtering df with the with log and we're going to be saying that we want in the column rate the rate that is going to be equal to 12 so from that row we want to get the information for the column of the net present value so if we run this line of code we will be receiving the net present value that corresponds to a discount rate of 12 and that is included in index 11. we will see now why this is going to be very useful so let's start by let's start plotting the first of all the net present value profile so we will be creating we'll we're going to be using the plt subplots function so we're going to be defining two objects the object fig and the object x i will be explaining now what does this mean but i'm going to be writing first the code so we can assign a figure size over for that lot that is going to be equal to 7 per 5 inches for example and i'm going to be assigning the parameter to the parameter dpi the value of 150 dpi is going to be giving the resolution of the image so the dots per inch we know that the higher the dots per inch the higher the resolution of our image okay so the plt subplots um function creates two different objects the object fig and the object ax this is the object figure and the axis the figure object is a sort of container because it holds everything you see on the space whereas the axis is the part of the space that holds the data that we want to plot so that is why we're going to be using the next the axe in order to plot the net present value profile what is the definition the net present value profile well it is the rate that is in the df the column rate that is included in the df pandas data frame against the net present value so that is as simple as that it is the net present value profile we're going to be setting the z order as one i'm gonna be explaining what is uh what does this mean in a second so we then are going to be set the caller equals to red and a label that explains or that defines what this line is going to be referring to and as we have said this is going to be the net present value profile okay so we're going to be setting the x labels of that lot with the function set for example first x label and this is going to be the rate in addition we can set the y label that is going to be the net present value it is as simple as that so in order for these um for this image for this plot to be displayed we want to make sure that the labels are going to be correctly shown by using the plt legend and if you're using for example and a dot pi script you can you say in order for the image to be displayed the plt show command so let's take a look at what this is returning this is returning on the net present value profile this is the line that plots the rate against the net present value okay so what can we do next so next we can say um we can do we can plot the line in a um a point let's plot a the point of the corresponds to the net present value calculated with a 12 discount rate so um the rate is going to be on the x axis and the net present value on the y axis so in this case we are going to be using to make the things easier because we are going to be plotting a point we can use the scatter type of matlab life so we're going to be saying scatter and we're going to be defining the x-axis and the y-axis in the x-axis we want to include the rate that is going to is saved in the variable r and that is equal to 12 in addition we are going to be plotting the other the next coordinate the um y coordinate is going to be given by the net present value so we can copy this value as is but we know that this is um this is obtained with this line of code so let's just copy this line of code to plot the second coordinate that we need in order to plot that point in our in our plot um we need to define for example the marker or what is going to be the marker it's going to be a dot with an o then just with the letter o we can say that we want it to be to have a color blue um that the size is going to be 75 um the z order is going to be the second one i'm going to be explaining in a second what this means then the label and the label is going to be the uh it's going to be called the cost of capital okay so the cost of capital as we know is r and in this case is going to be 12 which corresponds to a net present value equals to this number okay so let's run this line of code hopefully we haven't made any any mistakes and it is not the case so in this case you see that on the one hand you have the net present value profile and on the other hand you have the dot that corresponds to the net present value calculated with the cost of capital equals to 12 so why am i using then this z order so assume that i am not using i am not plotting or using this parameter let's get rid of the parameter z order and let's see what the result is so let's run this line of code and as we see the dot is behind the line so we want to the to the line to be displayed first and then above the line we want to plot the cost of capital that is why we are adding the z order parameter okay so the z order parameter first of all um let's put it in here the order if we want if we assign the value 1 that means that the first thing that we want to plot is going to be the net present value profile or the line so that is the order one the z order equals to one if we set on the point corresponding to the net present value with an interest rate of 12 percent the z order equals to 2 then in this case what this is going to be doing is that it is going to be plotting first of all the line corresponding to the npv profile and secondly above that line it is going to be plotting the dot the point corresponding to that specific net present value okay so that is the use of the z order parameter okay so next we can show a horizontal line that is going to be dividing the graph into positive net present value and the negative present value so in order to do so we are going to be defining two variables in one single line of code we're going to be defining the coordinates the variables are x and y and the values that corresponds to x let's set it to 0 and 0.40 so this tuple is going to be equals to x so then we are going to be defining the value of y which is going to be equals to zero comma zero what is this so in this case these th the first zero that corresponds to the variable x is going to be x one and zero point zero point forty is going to be x two while the first zero of the variable y is going to be y one and the second zero is going to be equals to y2 so we have defined these two variables and this time we want to plot a line so that is why we'll use the plot function of matlab live so we are going to be plotting this x and y line so let's run this line of code and we see that we have a line that goes from this point to this one set by this coordinates and that corresponds to a net present value exactly equals to zero okay so these are the basics of plotting suppose that we wanted to um add some other elements to these uh to this plot so we are going to be using we're going to be adding the internal rate of return what is the internal rate of return well it is just the discount rate that makes the net present value of the project equal to zero in other words the internal rate of return can be defined as a discount rate that equates the present value of the project's cash flows with the project initial cash outlay or initial investment such as in in this equation so put it simply the internal rate of return attempts to answer the question what rate of return does this project earn okay so as you um can imagine now what we need to do to calculate is the internal rate of return to do so we're going to be importing the numpy financial library as npf and then we're going to be calling from that library we're going to be calling the function internal rate of return that take only one argument which are the cash flows if we run this line of code then we can verify that the discount rate that makes the net present value of the project equals to zero is going to be approximately 19.94 percent okay so this is the net present uh part of the internal rate of return okay how can we demonstrate that this is the case that the net present value when we use it as the discount rate in this formula is going to be returning a net present value equals to zero well we can use the function that we have defined as calculate net present value and as the discount rate we're going to be including the internal rate of return this is the discount rate with the cash rules that we defined and the second element are going to be the kind of the cash flow the second parameter so what is the net present value that this is going to be returning well as we can see it is in scientific notation but this is extremely close to zero okay we can round for example to um to use normal the normal notation to that we are all we're all used to um we can round for example this number with a round function up to five digits or ten whichever you like because in any case this is not going to be returned this is going to be returning only the first one given that all of the others all of the other five are zeros in addition we can also use a formatted string letter or the f string with a replacement field denoted by curly brackets in order to again transform the scientific notation into the classic display of numbers so in order to use the f string we set the f open quote and inside the curly bracket this is going to be the replacement field we're going to be including the information that we want to be formatted as we want so the information is going to be the function net present value that includes the internal rate of return as the discount rate with the cash flows that we have defined and uh the cash flows as the second the second argument okay so once that we have defined the number that we want to format we need to specify the exact format that we want supposing that we want to display from the scientific notation we want to display only the 15 decimal digits so we're going to be saying that we're going to be saying then point 15 f and this is going to be returning the number this number in scientific notation the um sorry in the in the um classic notation but with 15 digits and we can verify that this is practically zero so we can confirm that this internal rate of return equals to approximately 19.94 percent is the discount rate that equates the net present value to zero okay good so this is the this is what we need in order to plot the internal rate of return this is the information that we need so in order to plot it it is going to be very very easy let's just copy the the code that we have defined because the only thing that we need to do is to add just so a few lines of code so let's make some space in this case to include the lines of code that we need which correspond to the point or the intersection between the net present value line and the horizontal line that represents the net present value equals to zero so this intersection is going to be the internal rate of return so as we can see just visually it is going to be around 20 which is the number approximately that the function internal rate of return um returned okay good so because we said that the internal rate of return is going to be a point it is very easy to plot it by using the scatter function of a matplotlibe that is going to be taking as the x axis the internal rate of return with the cash flows that we set this is the first axis and in the y y-axis we're going to be setting it to zero because remember that we want it to be exactly in this line okay good what is next and what do we do next then we set a marker for example say that we want the marker to be equal to a star we uh set uh set it as an asterisk and this is going to be marker not marker marker equals to a star we set the color equals to black just for example the size equals to 175 the z order is going to be logically three and we're going to be assigning a label by using by practicing using the f string so the f string is going to be taking the um it's going to be set by opening quotes so we're going to be saying that this is the internal rate of return so this is text and this is going to be equal to what it is going to be equal to a number that we want to replace in the space that is given by the curly brackets so in that space what do we want to do we want the rounded value of the internal rate of return so npf irr with the argument cash flows and rounded to um for example five digits to five digits okay so in addition we're going to be specifying again as text this is the only um inside the curly brackets is the numerical information in that we need to add or that we want to replace and we can we can continue with text by specifying that this corresponds to a net present value equals to zero okay so let's check if we haven't made any mistake and apparently the only thing that to to know that is by running this uh this code okay so this is not matching the parentheses what is the mistake so cash flows five so in this case the round we are missing here a a parenthesis in this case the closing brackets closing parenthesis now that we have added we have exactly what we wanted okay so this star is exactly the internal rate of return that corresponds to a rate a discount rate of 19.94 and the net present value equals to zero okay so this is the way that we plot the net present value profile as well as the internal rate of return by using matplotlibe in python so what we can conclude of all of this over this exercise is that the accept reject criterion of the internal rate of return method is going to be the following if the internal rate of return is greater than or equal to the firm's required rate of return of all cost of capital we need to accept that project because in this case the net present value is positive if the internal rate of return is lower than the firm's required rate of return or cost of capital we reject it because in this case the net present value is negative okay good so i hope this was useful thank you for watching this video and hope to see you on the next one thank you very much
Info
Channel: pythonbox
Views: 62
Rating: undefined out of 5
Keywords:
Id: mkaprRCJiJ4
Channel Id: undefined
Length: 31min 5sec (1865 seconds)
Published: Wed Nov 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.