The Depth of a Financial Model, Continued¶
Explores building a more complex and realistic model with Python. Here we focus on extending the simple retirement model to have dynamic salary growth.
Resources¶
Using Jupyter to Structure a Python Model¶
Notes¶
It can be a bit tricky in the beginning to structure Python models in Jupyter as you are dealing with two different layers of organization
Jupyter gives us nicely formatted markdown cells which make it easy to organize sections of the model
Markdown is actually a general markup language, not anything specific to Jupyter, and it supports a lot of features. Jupyter has their own extension to markdown which also adds LaTeX equation support
Most often, you will just need section headers, bullets, and equations, and anything else you can look at a Markdown reference
It is easy to add a table of contents for a Jupyter notebook and you should do this to increase the readability of your model
When adding a TOC item, spaces get converted to dashes for the reference
Transcript¶
- hey everyone
- nick dear bertis here teaching you
- financial modeling today we're going to
- be talking about
- how to structure a python model inside a
- jupiter notebook
- and this is the first video of our
- lecture series on
- building a dynamic salary retirement
- model in python
- so it can be a little bit
- difficult for you know new people in
- financial modeling in python to figure
- out how to structure their
- models um but certainly the organization
- is important uh because it's going to
- make it so much easier for other people
- to understand
- what is going on in your model and to be
- able to add to it
- so we've already seen how in excel
- models
- uh you know clearly defining input and
- output areas
- uh having different sheets for different
- parts of the model
- you know clear clear table headers and
- things like that are very important
- for making it a readable model and we
- can bring a similar kind of organization
- to our python models as well
- so we've already talked in the course
- about how
- it makes sense to use functions to
- organize your logic within a python
- model and in order to wrap all the
- model inputs up into a single data
- structure a data class
- is very helpful for that
- and the way that you would work with
- these functions in the model
- is basically any discrete step any
- logical step
- in the model becomes a function
- and then functions can call other
- functions so you kind of
- build up your model in layers uh going
- from
- very individual steps all the way up to
- you know run the entire model
- as a single function so you'll have you
- know your model function and that model
- function
- calls each of the sub model functions
- and then each of those
- sub model functions have their own uh
- functions for the individual steps you
- know something like that you can have
- any number of layers in here
- however you know complex your model is
- is going to determine how many layers
- you need
- um but you know just talking about all
- of that that's just
- you know playing python organization but
- then we're also working in
- jupiter to build our financial models
- so jupiter gives us a lot of additional
- tools that we can use to give even
- better structure
- even more organization to our python
- models
- so with jupiter you know we don't just
- have
- plain text code anymore
- uh not only can we have that code but we
- can have this you know nicely formatted
- text uh with headers and
- bold italics you know whatever you know
- kind of standard things you would want
- to see with
- nicely formatted text you can have
- equations in there
- you can have sections you can have links
- you can have graphics
- and everything is all in there right
- with the code
- so it's a really interesting way to
- present
- your results of a model
- uh this nicely formatted text jupiter
- uses something called
- markdown markdown is actually its own
- mini markup language it's not a jupiter
- specific thing
- it's there in a lot of different
- platforms
- and so you can look up just general
- resources on markdown
- and i've given a couple of those for you
- here and those will let you know
- all the different things that you can do
- in these nicely formatted
- cells uh but for the most part as far as
- what you're going to need
- you know on a daily basis it would be
- you know headers
- for sections uh bullet points are
- another good one to know offhand
- and you want to know also equations so
- you can
- speak about what you're doing in math
- terms
- and then the equations are actually not
- built into regular
- markdown jupiter actually has extended
- markdown to add
- equation support by using latex
- equations um so
- uh you know you can google about how to
- write latex equations uh
- in order to be able to add them to
- jupiter but we'll also look at an
- example
- as well um
- so with our jupiter organization we can
- think about
- you know how in excel we had worksheets
- within the workbook to represent
- different sections or sub models within
- the model
- here we can use you know the headers in
- jupiter
- to separate these sections and think of
- these sections as analogous to those
- excel
- worksheets
- um and it's always good with these
- sections to give some kind of overview
- uh you know what are we doing in this
- section
- and this really goes a long way to
- making your model a lot more readable
- especially for non-technical people who
- aren't necessarily going to be able to
- read the code very easily
- having this nicely formatted text in
- there
- then makes it so much easier to read
- without having to go and understand all
- of the code
- let's look at some examples of the
- things that we can do with this
- jupiter markdown
- so moving over to the jupiter notebook
- uh so the first thing
- that uh you'll see you know when i start
- uh typing i'm in edit mode
- um and when i hit escape i'm in command
- mode so you can see that
- here uh when i click in it switches to
- edit
- and the reason i'm talking about that is
- because when you're in command mode
- then you can switch the type of the cell
- with the keyboard so
- m is going to make it into a markdown
- cell and y
- is going to make it into a code cell
- now you can always just use this drop
- down to do the same thing
- uh but it's nice to know the keyboard
- shortcuts to make your life easier
- um so you know if we have a code cell
- then we can
- you know run whatever python code in it
- and then if we have a markdown cell uh
- you know you try to put code in it it's
- not going to run the code it's going to
- display that now as text because it is a
- markdown cell it does not run code it
- displays
- uh formatted text so what can we do
- in these markdown cells so uh you know
- most
- often the thing that you're gonna do is
- create headers
- so to create a header is with the hash
- sign
- so this would be a level one header
- and then as you add additional hash
- signs
- it makes additional smaller levels of
- headers
- so i think you have up to six um
- but you can see uh you notice the more
- hash
- signs that you have the smaller the uh
- header for the section is and so this is
- how you can
- represent subsections within the section
- uh you know you can have
- you know some things in between the
- sections
- uh and you know this would be like a sub
- section
- and then this would be like a sub sub
- section
- um so that you can you know have one
- overall
- header for your model describing what
- your model is and then you can have each
- you know sub model as the level two
- header
- and then you can even break that further
- up into parts and and
- keep going further down as you need to
- to really
- uh give a good structure to something
- so these headers are are going to be the
- biggest thing that we're going to use
- and then another one which is
- uh useful is uh to use
- uh bullet points so
- bullet points uh all it is is you just
- do
- a dash and then a space and then
- whatever you want to have in the bullet
- and then you know enter to go to the
- next line and repeat
- that way you're able to get bullets
- and then we can do equations as well so
- equations
- uh in order to do equation
- you're going to wrap the actual equation
- part of it
- in dollar signs so it could be that you
- have
- a sentence uh
- with an equation uh
- in the middle a equals b plus c
- uh and it would render
- like this so we see you know the nicely
- formatted equation coming here in the
- middle of the text
- um and it rendered that way because we
- added the dollar signs if we don't add
- the dollar signs
- then it doesn't know as an equation and
- you don't see like the
- uh you know nicer equation format there
- it doesn't look a lot different with
- you know this very simple equation but
- if we want to do something like
- say add a subscript let's say this is
- you know b1
- and this is c2 um then you can see that
- you know it looks very nice with the
- subscripts that way and if we didn't
- include the dollar signs then it's not
- rendering as an equation so it's just
- going to be
- you know raw with those uh underscores
- so there's there's a lot that you can do
- with this
- equation syntax and we're not really
- going to
- dig into it a whole lot in this course
- you can definitely take a look at
- latex references and i gave a reference
- along with the lecture
- you can look at but just know that
- you know kind of the basic math symbols
- work fine
- underscores uh do subscripts
- and then the uh carrot sign does a
- superscript
- and that's mostly what you're going to
- need to know to be able to write
- simple equations uh one other thing
- to note is you know this equation came
- in the middle of the text
- if we wanted it to be offset then you do
- two dollar signs
- on each side and then you can see it
- actually broke the sentence into two
- and it has now put the equation centered
- and on its own line
- so if you want your equation to really
- stand out
- then it makes sense to do the double
- dollar signs and if you want your
- equation to just
- flow in line with the text then do
- single dollar signs
- so these are going to be the main things
- that you would want to use
- and one other is
- we'll look at building a table of
- contents in jupiter
- and so a way we can do that um
- is we put um
- we put what we want to show
- as the um
- as the uh text which is going to display
- and then uh we're going to put
- the header um so let's go to sub sub
- section so if you sub
- sub section so when we do that
- then we click this link we can see it
- takes us right to that
- section so this will be really useful
- for building out a table of contents
- which we'll look at
- in the full uh python model example
- but what's going on here is to do this
- link and you'll see this in the markdown
- references as well but it's it's
- brackets
- square brackets and then you put
- whatever
- text you want to display and then it's
- parentheses
- and then we have the hash sign to say
- that this is going to be a header in the
- page
- and then it's the header text but
- wherever there are spaces
- you replace those spaces with dashes to
- get that to work
- so those are all the main jupiter
- features
- that we'll use and you can take a look
- at the references to see the other
- features
- which there are plenty of them
- and then you know thinking about how
- this all
- fits in with what we've talked about
- already about structuring a python model
- you know where the overall model is
- going to be a function
- uh each you know individual kind of main
- step in the model we'll call that a sub
- model
- is also going to be a function and then
- any
- steps involved in those sub models are
- going to be individual functions as well
- and this can keep going down as many
- layers as you need it to go
- for your organization
- but we want to work this into the
- jupiter structure as well
- so what i would generally recommend is
- you know follow a similar structure with
- your jupiter sections
- as you are with your functions so we'll
- have
- you know an overview header which talks
- about the main model
- and what the main it will have some
- description of the main model
- and then we'll have the table of
- contents and then each section
- will be one of the sub models and in
- each of those sections
- uh we define that function for the sub
- model
- and explain what it does and what we're
- doing
- um and you know any individual step
- functions and everything
- and then uh you know go on to the next
- jupiter section
- it has another uh function for the next
- sub model
- and if you know any of these areas got
- really involved
- uh you know you could further break them
- down you know with additional layers of
- functions or additional jupiter sections
- so that everything is very clear
- and one other thing to note about just
- the workflow
- in jupiter is you know when you're first
- filling things out in jupiter
- i've always found that i just have cells
- going everywhere
- with all these random bits of testing
- code just trying random little things
- uh which is totally fine but then once
- you get to the end of the project you
- want to make sure that you clean up
- all these little cells so that you know
- everything just goes in order from
- beginning to end
- and there's a logical flow all the way
- through
- and you can ensure that this is
- there by going into the kernel menu
- and hitting restart and run all cells
- what that's going to do
- is it's going to give you a totally new
- session in the notebook so nothing is
- defined at all
- and it's going to just start from the
- top and it's going to run everything in
- order
- and that way you know for sure that the
- thing runs from
- end to end appropriately and you get the
- results that you're expecting
- and you know once you're at this stage
- of everything seems to be working
- let me you know make sure you've
- restarted it run all the cells you got
- the
- result that you expected you want to
- keep going a step further
- and change around the inputs in your
- model and make sure that the outputs are
- changing
- as you would expect to for whatever
- change you made in the inputs
- so that way you can make sure that all
- of your inputs are being properly
- incorporated
- into the model um and
- you should have your your final output
- at the end of the notebook whatever your
- final answer from the model is so that
- you know someone who doesn't care so
- much about the details of the model can
- just scroll through and
- see the answer right at the end uh but
- it's also good to have
- outputs in each section if possible or
- just you know whatever the most
- uh useful tangible uh you know
- intermediate outputs to show you should
- show them
- you know unlike in excel where you know
- all your calculations are kind of shown
- automatically
- in python you have to be very explicit
- about what you want to show and not show
- and so you should show everything which
- is
- you know interesting or useful to
- understand the whole context
- of your model not just going all the way
- doing a bunch of calculations and
- spitting out one number at the end
- then it becomes way less clear how we
- actually
- got to that number versus
- actually having a full um
- you know step-by-step you know with the
- retirement model
- you know showing the salaries showing
- the wealth before
- ultimately getting to just a single
- years to retirement number
- so that's an overview of how to
- structure python models
- in jupiter notebooks thanks for
- listening and we'll see you next time
Salaries in the Python Dynamic Salary Retirement Model¶
Notes¶
For development purposes, create a new variable data which is set equal to model_data. When you are done with the model, you will remove this.
Write the logic for a function in a cell and run it to ensure it works, then move it into a function
Using data in the functions while the original variable is model_data ensures that you are not accidentally accessing the overall (global) model_data when it should be the specific instance of ModelInputs being passed
This may be confusing and sound like extra unnecessary steps, but setting things up this way will enable your model to be easily extended
Here we will create a function which can get the salary in any given year
We will write also some example code to test the function and show its results
Later we will use this function in the overall calculation
Transcript¶
- hey everyone
- nick dear bird is here teaching you
- financial modeling today
- we're going to build out the salary
- portion of the dynamic salary retirement
- model
- in python so
- we've been talking about this model uh a
- fair amount already
- we built out the whole model in excel
- and now it's time to go do that in
- python
- so just as a quick review of what's
- involved in this model
- so we're looking at a model where
- the salaries are uh determined by
- a cost of living raise that occurs every
- year uh promotion that
- happens every number of years with a
- certain percentage raise
- and of course an initial starting salary
- and it grows through time
- and then the individual saves a certain
- portion of that
- each year and that is going to
- be invested at a certain interest rate
- to
- build up the individual's wealth over
- time
- and then we check
- the wealth year by year to determine
- once the individual has hit
- a certain goal of how much cash they
- want to retire
- and that's how we determine how long it
- takes for them to retire
- so as more quick recap this is what the
- salary equation
- looks like we start from an initial
- salary and then we grow that
- at the cost of living raises and we also
- grow that
- for the number of promotions at the
- promotion raise rate
- and for the wealth it is
- taking always whatever the prior wealth
- was
- and adding the investment return to it
- and then
- taking the cash saved in that year
- the salary times the savings rate and to
- get the wealth for that given
- year
- so let's go ahead and see what this
- looks like in python
- so uh the full completed example of this
- uh is there on the course site but i'm
- going to go ahead and build it out
- in the this and the following videos
- so i strip this down to
- just an initial uh basically template
- this
- is you know basically what you would get
- on
- any given project in the course as far
- as the python side
- where you know you have the basic inputs
- of the model
- already set up in a data class for you
- and you have you know the very start of
- a table of contents
- and a placeholder for a description and
- things like that
- so you know certainly you would want to
- fill out a full description
- of what your model is doing um
- but we'll leave that for now uh for the
- sake of time
- um so i'm just gonna go ahead
- and just run run everything in order
- just to start with so i have everything
- defined
- so now i have this model data
- which has all of my different
- variables attached to it
- now there's a couple um things that you
- can do
- which are going to make your experience
- of building the model a little bit
- smoother and help you avoid some
- pitfalls
- so one trick that i like
- is i'm going to go ahead and
- assign data to be model data
- and now we can use data instead of model
- data so why are we doing that
- uh so it goes along with another pattern
- that i'm about to show you
- where uh you know ultimately we're
- trying to write functions
- but it's not uh necessarily as easy to
- go straight
- to writing functions it adds additional
- structure
- and so it can be just a little bit more
- challenging to work with especially as a
- beginner
- and so what we can do is we can write
- out all the logic of the function
- just straight in the cell like this
- and then we can take that and we can
- just put it into a function
- once it works and
- the way that we'll define our functions
- we'll define them all to take data
- you know data being an instance of model
- inputs
- and it'll work with data not model data
- and the reason we're doing that is so
- that we don't accidentally use
- this model data
- without passing it to the function if
- you use it
- in the function without passing it to
- the function then it's going to be tied
- to this
- specific instance of the data and not
- any arbitrary data that we
- could pass to it so we want our
- functions uh
- to be able to just take an input and
- give an output
- we don't want them to be you know in the
- background
- using some different inputs that we
- didn't expect it to be using
- so this you know pattern of of going to
- using data
- and using data everywhere basically at
- the end you'll remove this
- data equals mod of data cell and so then
- you'll just be left with functions that
- take data
- and you'll be passing model data to them
- um
- and so they should still work and they
- should not be accessing the
- overall model data so
- that should become more clear as we go
- to build all this out but that's a quick
- overview of why i'm setting data equal
- to model data here
- so the first portion of the model that
- we want to build
- out is the salaries portion
- uh so i'm going to create a salaries
- header and we already use
- you know the level 1 header for the
- overall model title so i'm going to use
- the level 2 header
- for each of the sub models so you know
- the sub models are going to be salaries
- wealth and retirement
- um so we can go ahead and add salaries
- to
- our uh table of contents so you can just
- you know copy the previous
- so that you're able to work off that
- and here we can say determines the
- annual salary based on
- cost of living raises and promotions
- and now when we click that it takes us
- right to the salaries section
- so you know you'll see in the completed
- uh model that i have a full description
- written out here of what's going on and
- it has the equation
- and everything um but we'll just leave
- that off
- for now just for the sake of time just
- go and reference the completed example
- to see a good example of the kind of
- description that we should see
- for one of these sections but let's just
- go ahead and go right to the code
- so we know that we want to be taking
- the salary the initial salary
- times one plus the cost of living return
- to the number of years
- times one plus the promotion return to
- the number of promotions
- so how do we figure out the pieces
- for this so we don't know
- off the bat how many promotions there
- have been at a given year
- we know how often those promotions are
- occurring here by default every five
- years
- uh but we you know if the year is three
- if the year is seven
- ten whatever we don't immediately know
- how many promotions have occurred up
- until that point
- so we have to calculate that so i'm
- going to calculate that
- um so you know first
- let me just say you know let's work with
- year seven
- year equals seven um so let's
- say um you know year
- divided by uh the promotions every
- n years uh what do we get from that
- so uh year seven we've had 1.4
- promotions that doesn't we're not quite
- the way we're there would be one
- promotion right
- we get 10 that goes to 2 we hit 11. it's
- 2.2
- up to 14 we're at 2.8 so really we want
- just the integer portion of this right
- we don't care about the decimal
- as soon as it you know goes up to the
- next integer that's when we
- recognize that there's been another
- promotion so if we just take the integer
- portion of that then we will be left
- with what we want uh you know
- years 11 through 12 or 11 through 14.
- it's two promotions as soon as we have
- 15
- we get to three promotions so this is
- what we want
- and then we can go ahead and calculate
- calculate the salary at the given year
- so salary at year t
- is going to be the starting salary
- which is in the inputs times 1
- plus the cost of living raise
- and we're going to take that to the
- power
- of the year
- and then that's going to be times one
- plus
- the promotional raise uh
- to the power of the number of promotions
- um so then we see by year 15
- we have 120 000 roughly a year one
- sixty one thousand two hundred year two
- sixty four
- sixty two thousand uh you know starting
- off in a starting salary of
- six thousand two percent cost of living
- raise so this all sounds correct
- uh and year four uh just under 65 and
- then we hit year five and we get a big
- jump
- in the salary as we would expect coming
- along with the promotion
- so it looks like everything is working
- correctly
- so now we have a piece of the model
- which
- works appropriately let's go ahead and
- make this logical step
- into a function so
- uh what we can do you know this year uh
- is going to be an input to the function
- now and
- also the data is going to be the other
- input to the function so def
- uh you know salary at year and it's
- going to take the data and it's going to
- take the year
- and then we just uh
- you know highlight all that and press
- tab to shift it over
- and then we're just going to add a
- return on to the
- and now we have a function which does
- what we were trying to do
- so now we can get rid of this year
- um so at year four we see the same
- almost 65
- 000 year five we see the 76 000.
- and since we
- used just whatever data is being passed
- to it and not the overall model data
- that means that you know you could
- potentially
- uh you know construct you know other uh
- data you know say uh you know we wanted
- the salary to start at 40 000 instead
- and pass that data into there
- um then we can see that it's going to be
- based off of that
- new inputs whereas if you had to use
- model data
- in all of these and you may or may not
- have put an argument here for that
- then it's going to always use these
- values which are defined up here which
- makes your model significantly less
- flexible
- so we have our portion of the model
- that works getting the salary at a given
- year
- and you know definitely take a look at
- the full example
- you would want to put a description of
- what it does in the docs string and you
- want to describe what's going on in this
- section
- uh but the other thing that i think is
- appropriate to complete out this section
- is uh just showing how this works year
- over year
- so we define the function now let's look
- at what it looks like
- you know over a range of years so
- we're just going to go through six years
- and with the
- zero based indexing and python you know
- the the
- i is always starting at zero but we want
- our year to start from one so we're just
- going to add one
- to the i to get the actual year
- and then we're going to calculate the
- salary at that year
- and here we're going to pass it the
- model data because all this is happening
- outside of function and we want this to
- still work after we
- remove this when we're done with the
- model
- and then we want to go ahead and print
- a nice formatted string the salary
- at year
- and then the year is
- uh and then
- let's give it you know some commas in
- there uh and
- zero decimal places so then we can
- run that and we see that we get the
- salary year every year and indeed see
- you know cost of living raises going up
- through year four and then we hit year
- five a promotion occurs and we see a
- larger jump in salary so it looks like
- everything is working appropriately
- so definitely look at the full completed
- example for
- all the description of everything but
- that's the basic logic
- so next time we will come back to build
- the wealth section
- of the model and then the following
- video build the retirement section to
- finish it up
- so thanks for listening and see you next
- time
Wealth in the Python Dynamic Salary Retirement Model¶
Notes¶
Here we will develop two functions which comprise the wealth sub-model
First create a function which determines the amount of cash saved in a given year
Then create a function which determines the amount of wealth in a given year
We create some example code to show how the function works, but it will actually be applied in the Retirement sub-model
Transcript¶
- hey everyone
- nick duraburtis here teaching you
- financial modeling today we're going to
- be
- building out the wealth portion of the
- dynamic salary retirement model
- in python
- we left off last time and that we were
- uh you know building out the full python
- model and we had already done the salary
- portion of the model
- so in this video we're working on the
- welsh portion
- and we'll come back in the following
- video to finish up the model with the
- retirement portion so let's go ahead and
- go over to the jupiter notebook
- so we already have you know this setup
- stuff at the beginning
- and then we have the salary section
- built out already
- now we're getting to the wealth portion
- of the model
- so let's uh you know just like we did
- for salary make a section
- and clearly differentiate this from the
- salary related code
- and you would definitely want to have uh
- you know a description of what this
- section is doing as well as the
- equations
- involved but for the interest of time
- i'm gonna leave those out here
- um just take a look at the completed
- example to see
- what that should look like
- so to get the wealth we can go back to
- what the wealth equation
- was it is just taking whatever the prior
- wealth was
- and multiplying it by one plus the
- investment return
- and then we're going to add the cash
- save to that and the cash saved is the
- salary
- in that given year times the savings
- rate
- so we can think of this
- equation in two parts one is uh
- you know calculating the overall wealth
- equation and the other is
- figuring out this cash saved so let's
- first create a function which will get
- us the cash saved
- and then we can plug that into another
- function which gets us the overall
- wealth at a given year
- so we want to figure out the cash saved
- in any given year
- and you know obviously as we just looked
- at the equation it's just going to be
- salary times the savings rate right
- um but what is salary where do we get
- salary from
- well we have this whole other section of
- the model that
- was responsible for being able to
- calculate a salary at any given year
- so you know if we just you know say it's
- a certain year let's say a zero three
- um before we can do this calculation to
- get the
- cash saved we need to figure out the
- salary for that year
- so to get the salary of that year we had
- this nice
- convenient function where we just pass
- it our data
- and we pass it the year
- and it's going to give us the salary for
- that year year three in this case
- and then we can calculate the cash saved
- based off of that salary
- so let's see what that looks like
- so in year three we're gonna be saving
- almost sixteen thousand dollars
- which uh if our uh salary here is sixty
- three thousand and our savings rate
- is twenty five percent then that sounds
- correct
- and we can try it with some other years
- year 10
- year it's going up year one it's it's
- lower
- so this all seems appropriate for what
- we would expect to see
- for the cash saved in a given year
- so now that we know that it works
- appropriately we can go ahead and wrap
- it up into a function
- so it can be easily reused
- so year is going to become an input
- and the data is our other input so let's
- go ahead and write
- our function definition so cache
- saved during year
- and it's going to take the data and the
- year and then i'm going to highlight all
- that hit tab to shift it over
- and then return the cache saved and then
- we'll see when we call
- this function uh with the data uh
- and some year that will get the same
- kind of results that we were getting
- just with the planes code in the cell
- so now we have a function which can give
- us the
- cash saved at any given year
- now we need to figure out what the
- wealth is for that
- year so we know that
- we're just going to take this cash save
- portion and add it on
- so then the other portion is taking
- whatever the prior wealth was
- and then multiplying it by
- one plus the investment rate
- so
- we're going to figure out the wealth in
- any given year now
- and the first step that is calculating
- the cash saved which now we have
- a nice convenient function to do that
- cash saved during year
- let's again set up you know year equals
- five
- um and that's going to take the data and
- it's going to take the year
- as we have defined that and i
- you know as we just saw from the prior
- cell will give us the cash saved in that
- year
- so then we need to go and calculate the
- wealth
- so the wealth we know is going to be
- equal to the prior wealth
- times 1 plus data
- the interest rate and we're going to add
- the cash saved
- onto that now of course when we run this
- we're going to get an
- error because prior wealth is not
- defined we don't know what prior wealth
- is yet so let's just
- uh you know think of that as an input to
- this function that we're creating as
- well
- and say that we start with no well um
- and then the function is able to run um
- and it says we have ninety thousand
- dollars saved at year five which is
- not correct that's just how much we're
- saving in year five
- but it's because we have no wealth to
- begin with
- um so clearly we're gonna need to use
- this in a way that we're able to take
- whatever the wealth was from
- last year and pass it in next time
- for the wealth the prior wealth
- so let's go ahead and create this
- function as is
- and then we can look at how we can use
- this in a structure of
- taking the wealth from each year and
- saving it and then using it as the prior
- wealth
- for the next year so
- we can create our function wealth that
- year
- which is going to take the data and it's
- going to take the year
- and it's also going to take the prior
- wealth
- and we can tab that all over and we want
- to return the wealth
- at the end and just double check that we
- get the same thing when we passed the
- data we passed it the year and we passed
- the prior wealth
- just a quick sandy check to make sure
- you didn't break anything while
- transferring it over to a function
- we're doing d d get the same number here
- so we don't need these anymore
- and now we can go and look at how we
- would use this
- so this will be good just you know for
- two things one
- to to get a better understanding of how
- this will be plugged into the overall
- function which determines the years to
- retirement uh but also
- just as a quick check of showing how
- this section
- works standalone you know what happens
- to the wealth
- year by year so the way that we can do
- this
- is a fairly common pattern in
- programming
- where you initialize some variable
- and then you go throughout a loop and
- then at the end of the loop
- you're going to uh set that variable to
- be
- uh you know the main variable's value
- and it's going to be like the prior
- value
- and you you know you keep going through
- and at the end of the loop it always
- gets
- reassigned to whatever last uh the last
- value was
- so let's look at how that would work so
- first we set our prior wealth to zero
- we aren't starting with any money and so
- that makes sense
- if you had some other input in the model
- of you know starting cash
- then you would want to use that instead
- of zero
- and then we want to have some kind of
- loop and let's just
- look you know over six years so we can
- make sure to get that promotion
- in there and again just like we did for
- salaries
- uh we want to think about the year
- starting from one
- not starting from zero so we're going to
- take year as i plus one
- then we can get the wealth as the using
- our wealth at year function
- passing it uh this time model data
- because this
- is going to be permanently outside of a
- function uh
- the year and the prior wealth
- and then we can show that result so
- the wealth at year year
- is and then we're gonna put the wealth
- and let's put commas and zero decimal
- places
- and then at the end of the loop we're
- going to set prior wealth equal to
- wealth um so then when we run this
- we can see that indeed does go through
- each of the years
- and the money is accumulating like you
- might think it would
- uh you know we have after year two we
- have more than double of what we were
- able to save
- after the first year because we got two
- years of savings
- second year had the cost of living raise
- on the salary and then there's also an
- investment return
- baked in there as well so this all looks
- like
- what we would expect to see and then
- uh you know you can see that uh here
- from year four to five
- it's jumping by about 23 000
- uh and then going to year six
- it's an even um greater or sorry year
- four
- year three to four we have uh only a 16
- 000 increase
- and then year four to five we have a uh
- 23 000 increase so it's quite a bit
- bigger going to year five when we get
- that promotion
- and so this uh prior wealth pattern
- you can see that every time at the end
- of the loop it's setting prior wealth
- equal to wealth
- so that means that the next time that
- this loop comes around
- it's going to be using the wealth from
- the last calculation
- as the prior wealth and keep going
- through always using the last value
- of wealth as prior wealth
- and we'll apply the same exact pattern
- when we go to
- build out the retirement portion of the
- model in the next
- video so thanks for listening and i'll
- see you next time
Retirement in the Python Dynamic Salary Retirement Model¶
Notes¶
Now we will bring everything together to calculate the years to retirement
The salary and cash saved functions are already getting called from within the wealth function, so we only need to call the wealth function in the final loop
Here we are making use of a while loop to stop the loop once a certain condition is met, in this case once wealth exceeds desired cash
We will use formatted strings and new lines to create a good display for the output
Transcript¶
- hey everyone
- nick dear burtis here teaching you
- financial modeling and today we're going
- to be building out the retirement
- portion
- of the dynamic salary retirement model
- in
- python and this retirement portion of
- the model is the final portion which is
- going to finish up
- the model and the lecture series
- so we've been working on this dynamic
- salary retirement model we built out the
- entire thing in excel
- and then in python we've already built
- out the salary and wealth portions
- so look at the prior videos if you
- haven't seen those
- and now we're just going to build out
- the retirement portion of the model
- and finish it out so let's jump back
- over to the jupiter notebook
- so we already have you know all this
- initial setup stuff and we have the
- salaries section and we have the wealth
- section
- and now we're going to build out the
- retirement section
- so here i'm going to have retirement and
- now we're going to
- write out logic which the goal here is
- as soon as this person hits the desired
- cash
- which is given in the inputs then
- uh whatever year it is that they've
- accumulated that much wealth
- that is the year that they're going to
- be able to retire
- so uh we don't have to completely start
- from scratch here we already have
- a way to get the wells going up over
- time
- so then we just need to take this and
- make it stop
- as soon as we hit the desired cache
- so i'm just going to go ahead and start
- from this code and just copy it
- and i'm going to switch data for model
- data since this is code which is going
- to go inside of a function so we want to
- use data not model data
- and i'll just give that another try
- there so it still works
- and gives us the salary or the wealth in
- each year
- and we could make this thing go out to
- you know 30 years or whatever
- and now we can see we do hit the point
- where
- we hit the desired cash we can see here
- that year 28 is going to be the year
- but we want the code to be able to stop
- specifically at that point and tell us
- that that was the year that they were
- able to retire
- so what we can do to accomplish that
- there are a few different ways that we
- can go about this
- um one way is with actually a different
- type of loop
- that we haven't covered uh called a
- while loop
- and a while loop uh you know whereas the
- for loop
- runs for whatever you're passing it you
- know range it will you know just run 30
- times
- if you have you know for uh something
- and a list then it's going to run for as
- many items as you have in that list
- etc whereas the while loop
- runs until some condition is
- met so it's kind of a combination
- between
- a loop and an if statement and it's
- going to stop the loop as soon as that
- if condition is true
- or sorry as soon as it is false it's
- going to stop the loop
- so um what we can do
- is we can also initialize
- wealth to zero before starting this
- and what we want to do is say while
- the wealth is less than
- the desired cash then we're going to do
- this
- but you'll notice as soon as i make that
- change and try to run this
- that it's not working anymore right
- because uh
- this i there's no i anymore we had i
- from my code that i deleted to where it
- was a string
- and so i'm getting an error but
- really we just need to have a new way of
- figuring out
- what the year is going to be so let's
- also
- start the year at zero
- and every time we're going to increment
- the year by one
- so that way the first loop is starting
- at zero then it has one so the first
- loop it's one and then it comes around
- to the next loop
- now it's going to add one again so the
- year is going to be two on the second
- loop as we would expect
- so now we run this and we can see it
- does indeed stop at year 28
- so this already has gotten us to
- what we wanted um only
- that we don't necessarily have it in a
- very usable format yet
- um you know it printed out what the year
- last year was but we don't have that
- saved in a variable that we could use
- in some other way uh it's just kind of
- you know the the print the prince of the
- wealth stop happening at that point
- so but let's look at the value of the
- year variable when this is all done
- oh that is 28. so
- the year variable now contains what we
- need as our final answer
- um so just putting year there at the end
- then we get the retirement year from
- that
- and the reason was because it kept going
- through the loops it kept incrementing
- the year
- and then as soon as we got enough cash
- uh this condition evaluated to false and
- then it stopped going through additional
- loops
- and so the last time that it incremented
- the year would be the last year that we
- added the wealth
- and that would be the same year where
- the wealth has now surpassed the desired
- cash
- so just the year variable is going to
- contain what we need
- so then we've gotten to the final answer
- but we want to clean up the output here
- a little bit
- um so you know we don't want to just
- uh you know print out a show a number at
- the end we want to say what this number
- means
- so we can say uh you know
- it will take you know year
- i need to put the f at this beginning of
- the string it will take year
- years to retire
- okay now this is a little bit better it
- will take 28 years to retire that's a
- lot
- more clear um but we do have like two
- different
- sections of the output here one is
- showing the wealth year over year
- and one is showing the final year's
- retirement
- so it makes sense to separate those so
- let's
- uh put like a header here
- in the output and we can put one here
- as well
- and now this is a little bit better we
- have kind of a header
- for each portion of the output but it's
- still all together
- there's a couple ways that we can add
- some separation here in between these
- lines
- one is just you know kind of simple
- intuitive you can just print
- an empty string and that's going to make
- an empty line there
- the other way to go about it is
- backslash
- n actually means make a new line so if
- we put
- backslash n retirement there then that's
- going to do the exact same thing
- as uh having the empty print so even if
- you wanted
- you know a few lines there of spacing
- then you can easily do that in one print
- statement there
- so now we have two clear sections in the
- output what are the wealths
- over time and what are the retirement
- details and
- we get our main answer here in the
- retirement details that it's going to
- take
- 28 years to retire
- so now this block
- does do what we want to do but we should
- go ahead and
- wrap this up into a function as well
- um so these uh or so the
- uh these are all initializing for the
- loop they aren't actually inputs this is
- all going to happen inside the function
- so years to retirement and the only
- input here is going to be the data and
- then we can take
- all of the remaining code and hit tab to
- indent it
- and we're going to switch this to
- returning the year
- at the end um so then when we call
- years to retirement on the model data
- then we see
- the whole summary of the output here
- that it's going to take 28 years to
- retire
- and we can also have that saved in a
- variable
- as well so that we could do additional
- things
- with that result
- and so now our model works we have it
- all together
- so now that you have all your sections
- working you've got your final result
- the next thing that you should do and
- building out your models is
- go to this kernel menu and then restart
- and run all cells
- because this is gonna say you know
- throughout your your session you've been
- jumping all over the place
- uh you know building things and then
- deleting them
- and so things are just in a weird state
- from you playing around with things
- so if you restart this kernel and run
- all cells
- that gives you a fresh session and it
- runs everything in order
- and that way we can be sure that the
- model does indeed work
- from end to end the next thing that i'm
- going to do
- is i'm going to go back and i'm going to
- remove this data equals model data cell
- and then i'm going to restart and run
- all cells again
- so there we want to see again that it
- goes all the way through without error
- and here actually it did have an error
- i accidentally left this one cache saved
- during year
- using the data not the model data
- so this one should be model data to show
- that
- because it's happening outside the
- function so now i'm going to restart and
- run all cells again
- and now we can see it ran all the way
- through just
- fine um and now
- because all the functions are set to
- take data
- and they're not directly using the model
- data
- that means that any possible data can be
- passed into this function
- um so you know you can change it so that
- uh you know you're passing the model
- inputs
- with you know let's do starting salary
- equals 30 000 and then you run it again
- and now we can see it takes 37 years to
- retire
- oh well what if instead we had uh
- the cost of living raised was higher at
- 4
- going back to the original starting
- salary then it would
- be three years quicker for us to retire
- from that additional return
- um and so you're able to pass whatever
- you want to it
- and as we look at other extensions to
- models
- as we go throughout the course you're
- going to be able to add whatever
- extensions you want very easily
- and it's not tied to this particular
- instance
- of the model inputs defined here at the
- top you can pass it whatever
- inputs you want and everything flows
- through the entire model
- so that wraps up this lecture series
- on going uh to build out the full
- dynamic salary retirement model
- in python the next lecture series is
- going to be focused
- on visualization so thanks for listening
- and see you next time
Lab Exercise¶
Notes¶
Feel free to work from the example model though I would recommend you build that out yourself following the prior videos
This exercise is exactly the same as the one we did for Excel to calculate the desired cash rather than taking it as an input
Hint: You should add the new inputs to the ModelInputs dataclass and remove the desired cash input. Then you can create a function which calculates the desired cash based on the model inputs, and use that in place of where the desired cash was being accessed directly before
Transcript¶
- hey everyone
- nick derbertus here teaching you
- financial modeling and today
- we're going to talk about the lab
- exercise for the
- section of the course on the depth of a
- financial model
- in python where we focused on building
- out the dynamic salary model
- in python so the lab exercise here
- is the same as the one that we did for
- excel
- in the section on building out the excel
- dynamic salary retirement model
- and here what we want to do is we want
- to
- relax this assumption that there's some
- fixed amount of desired cash
- that we need for retirement and the way
- that we talked about
- relaxing that was that we're going to
- think about instead of
- just having a certain number instead
- we're going to have
- an amount that we want to spend each
- year in retirement as an input
- as well as the length of time that this
- individual
- is in retirement until they die
- so then based off of those inputs
- we can then calculate the desired cash
- within the model and plug that through
- into the existing structure
- so that the rest of the model works the
- same way that it did
- before so that's the basic idea
- and to accomplish this um something
- a way you might want to go about it is
- go ahead
- and remove the desired cache from the
- model inputs in the model
- and add these two new inputs
- and then it makes sense to create a
- function which is able to
- calculate the desired cache from the
- model inputs
- and then you'll just take uh calling
- that function
- and put it in the place of where you
- were just directly
- using the desired cache before and then
- you really won't have to change much at
- all
- about the existing model and it will
- just start working with this new
- desired cache calculation
- so thanks for listening and see you next
- time