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.

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

  • 00:02: hey everyone
  • 00:03: nick dear bertis here teaching you
  • 00:05: financial modeling today we're going to
  • 00:07: be talking about
  • 00:08: how to structure a python model inside a
  • 00:11: jupiter notebook
  • 00:13: and this is the first video of our
  • 00:15: lecture series on
  • 00:16: building a dynamic salary retirement
  • 00:18: model in python
  • 00:21: so it can be a little bit
  • 00:25: difficult for you know new people in
  • 00:29: financial modeling in python to figure
  • 00:31: out how to structure their
  • 00:32: models um but certainly the organization
  • 00:36: is important uh because it's going to
  • 00:38: make it so much easier for other people
  • 00:40: to understand
  • 00:41: what is going on in your model and to be
  • 00:43: able to add to it
  • 00:45: so we've already seen how in excel
  • 00:48: models
  • 00:48: uh you know clearly defining input and
  • 00:51: output areas
  • 00:52: uh having different sheets for different
  • 00:55: parts of the model
  • 00:58: you know clear clear table headers and
  • 01:00: things like that are very important
  • 01:02: for making it a readable model and we
  • 01:05: can bring a similar kind of organization
  • 01:07: to our python models as well
  • 01:10: so we've already talked in the course
  • 01:12: about how
  • 01:13: it makes sense to use functions to
  • 01:15: organize your logic within a python
  • 01:18: model and in order to wrap all the
  • 01:21: model inputs up into a single data
  • 01:24: structure a data class
  • 01:25: is very helpful for that
  • 01:29: and the way that you would work with
  • 01:32: these functions in the model
  • 01:33: is basically any discrete step any
  • 01:36: logical step
  • 01:37: in the model becomes a function
  • 01:40: and then functions can call other
  • 01:42: functions so you kind of
  • 01:44: build up your model in layers uh going
  • 01:47: from
  • 01:48: very individual steps all the way up to
  • 01:50: you know run the entire model
  • 01:53: as a single function so you'll have you
  • 01:57: know your model function and that model
  • 01:58: function
  • 01:59: calls each of the sub model functions
  • 02:01: and then each of those
  • 02:02: sub model functions have their own uh
  • 02:05: functions for the individual steps you
  • 02:07: know something like that you can have
  • 02:09: any number of layers in here
  • 02:12: however you know complex your model is
  • 02:14: is going to determine how many layers
  • 02:15: you need
  • 02:18: um but you know just talking about all
  • 02:20: of that that's just
  • 02:22: you know playing python organization but
  • 02:24: then we're also working in
  • 02:26: jupiter to build our financial models
  • 02:29: so jupiter gives us a lot of additional
  • 02:31: tools that we can use to give even
  • 02:33: better structure
  • 02:34: even more organization to our python
  • 02:38: models
  • 02:40: so with jupiter you know we don't just
  • 02:42: have
  • 02:43: plain text code anymore
  • 02:46: uh not only can we have that code but we
  • 02:48: can have this you know nicely formatted
  • 02:51: text uh with headers and
  • 02:54: bold italics you know whatever you know
  • 02:56: kind of standard things you would want
  • 02:58: to see with
  • 02:59: nicely formatted text you can have
  • 03:01: equations in there
  • 03:03: you can have sections you can have links
  • 03:06: you can have graphics
  • 03:07: and everything is all in there right
  • 03:09: with the code
  • 03:11: so it's a really interesting way to
  • 03:13: present
  • 03:14: your results of a model
  • 03:19: uh this nicely formatted text jupiter
  • 03:22: uses something called
  • 03:23: markdown markdown is actually its own
  • 03:26: mini markup language it's not a jupiter
  • 03:29: specific thing
  • 03:30: it's there in a lot of different
  • 03:32: platforms
  • 03:34: and so you can look up just general
  • 03:36: resources on markdown
  • 03:38: and i've given a couple of those for you
  • 03:40: here and those will let you know
  • 03:42: all the different things that you can do
  • 03:43: in these nicely formatted
  • 03:45: cells uh but for the most part as far as
  • 03:49: what you're going to need
  • 03:50: you know on a daily basis it would be
  • 03:52: you know headers
  • 03:53: for sections uh bullet points are
  • 03:57: another good one to know offhand
  • 04:01: and you want to know also equations so
  • 04:04: you can
  • 04:05: speak about what you're doing in math
  • 04:07: terms
  • 04:08: and then the equations are actually not
  • 04:10: built into regular
  • 04:12: markdown jupiter actually has extended
  • 04:15: markdown to add
  • 04:16: equation support by using latex
  • 04:19: equations um so
  • 04:23: uh you know you can google about how to
  • 04:25: write latex equations uh
  • 04:28: in order to be able to add them to
  • 04:30: jupiter but we'll also look at an
  • 04:32: example
  • 04:33: as well um
  • 04:36: so with our jupiter organization we can
  • 04:38: think about
  • 04:39: you know how in excel we had worksheets
  • 04:42: within the workbook to represent
  • 04:44: different sections or sub models within
  • 04:47: the model
  • 04:48: here we can use you know the headers in
  • 04:50: jupiter
  • 04:51: to separate these sections and think of
  • 04:54: these sections as analogous to those
  • 04:56: excel
  • 04:57: worksheets
  • 05:00: um and it's always good with these
  • 05:02: sections to give some kind of overview
  • 05:05: uh you know what are we doing in this
  • 05:06: section
  • 05:08: and this really goes a long way to
  • 05:11: making your model a lot more readable
  • 05:13: especially for non-technical people who
  • 05:16: aren't necessarily going to be able to
  • 05:17: read the code very easily
  • 05:19: having this nicely formatted text in
  • 05:22: there
  • 05:22: then makes it so much easier to read
  • 05:26: without having to go and understand all
  • 05:28: of the code
  • 05:30: let's look at some examples of the
  • 05:33: things that we can do with this
  • 05:34: jupiter markdown
  • 05:38: so moving over to the jupiter notebook
  • 05:40: uh so the first thing
  • 05:42: that uh you'll see you know when i start
  • 05:45: uh typing i'm in edit mode
  • 05:49: um and when i hit escape i'm in command
  • 05:52: mode so you can see that
  • 05:53: here uh when i click in it switches to
  • 05:56: edit
  • 05:58: and the reason i'm talking about that is
  • 06:00: because when you're in command mode
  • 06:02: then you can switch the type of the cell
  • 06:06: with the keyboard so
  • 06:09: m is going to make it into a markdown
  • 06:12: cell and y
  • 06:13: is going to make it into a code cell
  • 06:17: now you can always just use this drop
  • 06:19: down to do the same thing
  • 06:21: uh but it's nice to know the keyboard
  • 06:23: shortcuts to make your life easier
  • 06:26: um so you know if we have a code cell
  • 06:29: then we can
  • 06:30: you know run whatever python code in it
  • 06:34: and then if we have a markdown cell uh
  • 06:38: you know you try to put code in it it's
  • 06:40: not going to run the code it's going to
  • 06:42: display that now as text because it is a
  • 06:44: markdown cell it does not run code it
  • 06:47: displays
  • 06:48: uh formatted text so what can we do
  • 06:52: in these markdown cells so uh you know
  • 06:55: most
  • 06:56: often the thing that you're gonna do is
  • 06:57: create headers
  • 06:59: so to create a header is with the hash
  • 07:00: sign
  • 07:02: so this would be a level one header
  • 07:07: and then as you add additional hash
  • 07:09: signs
  • 07:10: it makes additional smaller levels of
  • 07:14: headers
  • 07:15: so i think you have up to six um
  • 07:18: but you can see uh you notice the more
  • 07:21: hash
  • 07:22: signs that you have the smaller the uh
  • 07:25: header for the section is and so this is
  • 07:28: how you can
  • 07:28: represent subsections within the section
  • 07:31: uh you know you can have
  • 07:33: you know some things in between the
  • 07:35: sections
  • 07:36: uh and you know this would be like a sub
  • 07:39: section
  • 07:40: and then this would be like a sub sub
  • 07:43: section
  • 07:44: um so that you can you know have one
  • 07:47: overall
  • 07:48: header for your model describing what
  • 07:50: your model is and then you can have each
  • 07:51: you know sub model as the level two
  • 07:54: header
  • 07:55: and then you can even break that further
  • 07:56: up into parts and and
  • 07:58: keep going further down as you need to
  • 08:00: to really
  • 08:01: uh give a good structure to something
  • 08:05: so these headers are are going to be the
  • 08:07: biggest thing that we're going to use
  • 08:11: and then another one which is
  • 08:14: uh useful is uh to use
  • 08:18: uh bullet points so
  • 08:21: bullet points uh all it is is you just
  • 08:24: do
  • 08:25: a dash and then a space and then
  • 08:28: whatever you want to have in the bullet
  • 08:29: and then you know enter to go to the
  • 08:31: next line and repeat
  • 08:33: that way you're able to get bullets
  • 08:38: and then we can do equations as well so
  • 08:41: equations
  • 08:42: uh in order to do equation
  • 08:46: you're going to wrap the actual equation
  • 08:49: part of it
  • 08:50: in dollar signs so it could be that you
  • 08:53: have
  • 08:54: a sentence uh
  • 08:57: with an equation uh
  • 09:00: in the middle a equals b plus c
  • 09:03: uh and it would render
  • 09:07: like this so we see you know the nicely
  • 09:10: formatted equation coming here in the
  • 09:12: middle of the text
  • 09:14: um and it rendered that way because we
  • 09:16: added the dollar signs if we don't add
  • 09:18: the dollar signs
  • 09:19: then it doesn't know as an equation and
  • 09:21: you don't see like the
  • 09:23: uh you know nicer equation format there
  • 09:26: it doesn't look a lot different with
  • 09:27: you know this very simple equation but
  • 09:30: if we want to do something like
  • 09:32: say add a subscript let's say this is
  • 09:35: you know b1
  • 09:36: and this is c2 um then you can see that
  • 09:40: you know it looks very nice with the
  • 09:42: subscripts that way and if we didn't
  • 09:44: include the dollar signs then it's not
  • 09:46: rendering as an equation so it's just
  • 09:48: going to be
  • 09:49: you know raw with those uh underscores
  • 09:52: so there's there's a lot that you can do
  • 09:55: with this
  • 09:56: equation syntax and we're not really
  • 09:58: going to
  • 09:59: dig into it a whole lot in this course
  • 10:00: you can definitely take a look at
  • 10:02: latex references and i gave a reference
  • 10:05: along with the lecture
  • 10:06: you can look at but just know that
  • 10:10: you know kind of the basic math symbols
  • 10:12: work fine
  • 10:14: underscores uh do subscripts
  • 10:18: and then the uh carrot sign does a
  • 10:21: superscript
  • 10:22: and that's mostly what you're going to
  • 10:24: need to know to be able to write
  • 10:26: simple equations uh one other thing
  • 10:29: to note is you know this equation came
  • 10:32: in the middle of the text
  • 10:34: if we wanted it to be offset then you do
  • 10:37: two dollar signs
  • 10:38: on each side and then you can see it
  • 10:40: actually broke the sentence into two
  • 10:42: and it has now put the equation centered
  • 10:45: and on its own line
  • 10:46: so if you want your equation to really
  • 10:48: stand out
  • 10:49: then it makes sense to do the double
  • 10:51: dollar signs and if you want your
  • 10:53: equation to just
  • 10:54: flow in line with the text then do
  • 10:57: single dollar signs
  • 11:00: so these are going to be the main things
  • 11:02: that you would want to use
  • 11:04: and one other is
  • 11:08: we'll look at building a table of
  • 11:09: contents in jupiter
  • 11:13: and so a way we can do that um
  • 11:17: is we put um
  • 11:21: we put what we want to show
  • 11:24: as the um
  • 11:28: as the uh text which is going to display
  • 11:32: and then uh we're going to put
  • 11:35: the header um so let's go to sub sub
  • 11:39: section so if you sub
  • 11:41: sub section so when we do that
  • 11:45: then we click this link we can see it
  • 11:47: takes us right to that
  • 11:48: section so this will be really useful
  • 11:51: for building out a table of contents
  • 11:52: which we'll look at
  • 11:53: in the full uh python model example
  • 11:59: but what's going on here is to do this
  • 12:01: link and you'll see this in the markdown
  • 12:03: references as well but it's it's
  • 12:06: brackets
  • 12:06: square brackets and then you put
  • 12:09: whatever
  • 12:10: text you want to display and then it's
  • 12:12: parentheses
  • 12:13: and then we have the hash sign to say
  • 12:15: that this is going to be a header in the
  • 12:16: page
  • 12:17: and then it's the header text but
  • 12:20: wherever there are spaces
  • 12:21: you replace those spaces with dashes to
  • 12:24: get that to work
  • 12:27: so those are all the main jupiter
  • 12:30: features
  • 12:31: that we'll use and you can take a look
  • 12:33: at the references to see the other
  • 12:35: features
  • 12:36: which there are plenty of them
  • 12:42: and then you know thinking about how
  • 12:45: this all
  • 12:46: fits in with what we've talked about
  • 12:48: already about structuring a python model
  • 12:51: you know where the overall model is
  • 12:53: going to be a function
  • 12:54: uh each you know individual kind of main
  • 12:57: step in the model we'll call that a sub
  • 12:59: model
  • 13:00: is also going to be a function and then
  • 13:02: any
  • 13:03: steps involved in those sub models are
  • 13:06: going to be individual functions as well
  • 13:09: and this can keep going down as many
  • 13:11: layers as you need it to go
  • 13:12: for your organization
  • 13:15: but we want to work this into the
  • 13:17: jupiter structure as well
  • 13:18: so what i would generally recommend is
  • 13:22: you know follow a similar structure with
  • 13:24: your jupiter sections
  • 13:25: as you are with your functions so we'll
  • 13:28: have
  • 13:29: you know an overview header which talks
  • 13:32: about the main model
  • 13:34: and what the main it will have some
  • 13:35: description of the main model
  • 13:37: and then we'll have the table of
  • 13:38: contents and then each section
  • 13:41: will be one of the sub models and in
  • 13:43: each of those sections
  • 13:45: uh we define that function for the sub
  • 13:48: model
  • 13:48: and explain what it does and what we're
  • 13:50: doing
  • 13:52: um and you know any individual step
  • 13:54: functions and everything
  • 13:56: and then uh you know go on to the next
  • 13:59: jupiter section
  • 14:00: it has another uh function for the next
  • 14:03: sub model
  • 14:05: and if you know any of these areas got
  • 14:08: really involved
  • 14:09: uh you know you could further break them
  • 14:11: down you know with additional layers of
  • 14:13: functions or additional jupiter sections
  • 14:16: so that everything is very clear
  • 14:21: and one other thing to note about just
  • 14:25: the workflow
  • 14:26: in jupiter is you know when you're first
  • 14:29: filling things out in jupiter
  • 14:30: i've always found that i just have cells
  • 14:32: going everywhere
  • 14:33: with all these random bits of testing
  • 14:36: code just trying random little things
  • 14:38: uh which is totally fine but then once
  • 14:41: you get to the end of the project you
  • 14:43: want to make sure that you clean up
  • 14:45: all these little cells so that you know
  • 14:47: everything just goes in order from
  • 14:48: beginning to end
  • 14:49: and there's a logical flow all the way
  • 14:51: through
  • 14:53: and you can ensure that this is
  • 14:56: there by going into the kernel menu
  • 15:00: and hitting restart and run all cells
  • 15:03: what that's going to do
  • 15:04: is it's going to give you a totally new
  • 15:06: session in the notebook so nothing is
  • 15:08: defined at all
  • 15:09: and it's going to just start from the
  • 15:10: top and it's going to run everything in
  • 15:12: order
  • 15:13: and that way you know for sure that the
  • 15:15: thing runs from
  • 15:16: end to end appropriately and you get the
  • 15:18: results that you're expecting
  • 15:22: and you know once you're at this stage
  • 15:25: of everything seems to be working
  • 15:27: let me you know make sure you've
  • 15:29: restarted it run all the cells you got
  • 15:31: the
  • 15:31: result that you expected you want to
  • 15:33: keep going a step further
  • 15:35: and change around the inputs in your
  • 15:37: model and make sure that the outputs are
  • 15:39: changing
  • 15:40: as you would expect to for whatever
  • 15:42: change you made in the inputs
  • 15:44: so that way you can make sure that all
  • 15:47: of your inputs are being properly
  • 15:49: incorporated
  • 15:49: into the model um and
  • 15:54: you should have your your final output
  • 15:57: at the end of the notebook whatever your
  • 15:58: final answer from the model is so that
  • 16:01: you know someone who doesn't care so
  • 16:02: much about the details of the model can
  • 16:04: just scroll through and
  • 16:05: see the answer right at the end uh but
  • 16:08: it's also good to have
  • 16:10: outputs in each section if possible or
  • 16:13: just you know whatever the most
  • 16:15: uh useful tangible uh you know
  • 16:18: intermediate outputs to show you should
  • 16:20: show them
  • 16:21: you know unlike in excel where you know
  • 16:23: all your calculations are kind of shown
  • 16:25: automatically
  • 16:26: in python you have to be very explicit
  • 16:28: about what you want to show and not show
  • 16:30: and so you should show everything which
  • 16:34: is
  • 16:35: you know interesting or useful to
  • 16:36: understand the whole context
  • 16:38: of your model not just going all the way
  • 16:41: doing a bunch of calculations and
  • 16:42: spitting out one number at the end
  • 16:44: then it becomes way less clear how we
  • 16:47: actually
  • 16:47: got to that number versus
  • 16:51: actually having a full um
  • 16:54: you know step-by-step you know with the
  • 16:57: retirement model
  • 16:58: you know showing the salaries showing
  • 16:59: the wealth before
  • 17:01: ultimately getting to just a single
  • 17:02: years to retirement number
  • 17:06: so that's an overview of how to
  • 17:07: structure python models
  • 17:09: in jupiter notebooks thanks for
  • 17:11: 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

  • 00:04: hey everyone
  • 00:04: nick dear bird is here teaching you
  • 00:06: financial modeling today
  • 00:08: we're going to build out the salary
  • 00:10: portion of the dynamic salary retirement
  • 00:13: model
  • 00:13: in python so
  • 00:16: we've been talking about this model uh a
  • 00:19: fair amount already
  • 00:20: we built out the whole model in excel
  • 00:24: and now it's time to go do that in
  • 00:26: python
  • 00:27: so just as a quick review of what's
  • 00:31: involved in this model
  • 00:32: so we're looking at a model where
  • 00:36: the salaries are uh determined by
  • 00:39: a cost of living raise that occurs every
  • 00:41: year uh promotion that
  • 00:44: happens every number of years with a
  • 00:45: certain percentage raise
  • 00:47: and of course an initial starting salary
  • 00:50: and it grows through time
  • 00:52: and then the individual saves a certain
  • 00:55: portion of that
  • 00:56: each year and that is going to
  • 00:59: be invested at a certain interest rate
  • 01:02: to
  • 01:03: build up the individual's wealth over
  • 01:05: time
  • 01:06: and then we check
  • 01:10: the wealth year by year to determine
  • 01:13: once the individual has hit
  • 01:14: a certain goal of how much cash they
  • 01:17: want to retire
  • 01:19: and that's how we determine how long it
  • 01:22: takes for them to retire
  • 01:26: so as more quick recap this is what the
  • 01:28: salary equation
  • 01:29: looks like we start from an initial
  • 01:31: salary and then we grow that
  • 01:33: at the cost of living raises and we also
  • 01:36: grow that
  • 01:37: for the number of promotions at the
  • 01:40: promotion raise rate
  • 01:43: and for the wealth it is
  • 01:46: taking always whatever the prior wealth
  • 01:49: was
  • 01:50: and adding the investment return to it
  • 01:53: and then
  • 01:54: taking the cash saved in that year
  • 01:58: the salary times the savings rate and to
  • 02:01: get the wealth for that given
  • 02:03: year
  • 02:07: so let's go ahead and see what this
  • 02:09: looks like in python
  • 02:15: so uh the full completed example of this
  • 02:19: uh is there on the course site but i'm
  • 02:22: going to go ahead and build it out
  • 02:23: in the this and the following videos
  • 02:26: so i strip this down to
  • 02:29: just an initial uh basically template
  • 02:32: this
  • 02:33: is you know basically what you would get
  • 02:36: on
  • 02:36: any given project in the course as far
  • 02:39: as the python side
  • 02:41: where you know you have the basic inputs
  • 02:43: of the model
  • 02:44: already set up in a data class for you
  • 02:47: and you have you know the very start of
  • 02:50: a table of contents
  • 02:52: and a placeholder for a description and
  • 02:54: things like that
  • 02:56: so you know certainly you would want to
  • 02:58: fill out a full description
  • 03:00: of what your model is doing um
  • 03:04: but we'll leave that for now uh for the
  • 03:06: sake of time
  • 03:08: um so i'm just gonna go ahead
  • 03:11: and just run run everything in order
  • 03:14: just to start with so i have everything
  • 03:16: defined
  • 03:18: so now i have this model data
  • 03:21: which has all of my different
  • 03:24: variables attached to it
  • 03:30: now there's a couple um things that you
  • 03:32: can do
  • 03:33: which are going to make your experience
  • 03:36: of building the model a little bit
  • 03:39: smoother and help you avoid some
  • 03:41: pitfalls
  • 03:42: so one trick that i like
  • 03:45: is i'm going to go ahead and
  • 03:49: assign data to be model data
  • 03:53: and now we can use data instead of model
  • 03:56: data so why are we doing that
  • 04:00: uh so it goes along with another pattern
  • 04:05: that i'm about to show you
  • 04:06: where uh you know ultimately we're
  • 04:09: trying to write functions
  • 04:10: but it's not uh necessarily as easy to
  • 04:13: go straight
  • 04:14: to writing functions it adds additional
  • 04:16: structure
  • 04:17: and so it can be just a little bit more
  • 04:19: challenging to work with especially as a
  • 04:21: beginner
  • 04:23: and so what we can do is we can write
  • 04:26: out all the logic of the function
  • 04:28: just straight in the cell like this
  • 04:32: and then we can take that and we can
  • 04:34: just put it into a function
  • 04:35: once it works and
  • 04:39: the way that we'll define our functions
  • 04:40: we'll define them all to take data
  • 04:43: you know data being an instance of model
  • 04:45: inputs
  • 04:46: and it'll work with data not model data
  • 04:50: and the reason we're doing that is so
  • 04:52: that we don't accidentally use
  • 04:54: this model data
  • 04:57: without passing it to the function if
  • 05:00: you use it
  • 05:01: in the function without passing it to
  • 05:02: the function then it's going to be tied
  • 05:04: to this
  • 05:05: specific instance of the data and not
  • 05:08: any arbitrary data that we
  • 05:10: could pass to it so we want our
  • 05:12: functions uh
  • 05:13: to be able to just take an input and
  • 05:17: give an output
  • 05:18: we don't want them to be you know in the
  • 05:20: background
  • 05:21: using some different inputs that we
  • 05:22: didn't expect it to be using
  • 05:25: so this you know pattern of of going to
  • 05:27: using data
  • 05:29: and using data everywhere basically at
  • 05:31: the end you'll remove this
  • 05:33: data equals mod of data cell and so then
  • 05:36: you'll just be left with functions that
  • 05:37: take data
  • 05:38: and you'll be passing model data to them
  • 05:41: um
  • 05:42: and so they should still work and they
  • 05:44: should not be accessing the
  • 05:46: overall model data so
  • 05:49: that should become more clear as we go
  • 05:51: to build all this out but that's a quick
  • 05:53: overview of why i'm setting data equal
  • 05:56: to model data here
  • 05:59: so the first portion of the model that
  • 06:02: we want to build
  • 06:03: out is the salaries portion
  • 06:06: uh so i'm going to create a salaries
  • 06:08: header and we already use
  • 06:10: you know the level 1 header for the
  • 06:13: overall model title so i'm going to use
  • 06:14: the level 2 header
  • 06:16: for each of the sub models so you know
  • 06:20: the sub models are going to be salaries
  • 06:21: wealth and retirement
  • 06:24: um so we can go ahead and add salaries
  • 06:27: to
  • 06:27: our uh table of contents so you can just
  • 06:31: you know copy the previous
  • 06:34: so that you're able to work off that
  • 06:40: and here we can say determines the
  • 06:43: annual salary based on
  • 06:47: cost of living raises and promotions
  • 06:53: and now when we click that it takes us
  • 06:54: right to the salaries section
  • 06:59: so you know you'll see in the completed
  • 07:02: uh model that i have a full description
  • 07:05: written out here of what's going on and
  • 07:06: it has the equation
  • 07:08: and everything um but we'll just leave
  • 07:11: that off
  • 07:11: for now just for the sake of time just
  • 07:14: go and reference the completed example
  • 07:16: to see a good example of the kind of
  • 07:18: description that we should see
  • 07:20: for one of these sections but let's just
  • 07:23: go ahead and go right to the code
  • 07:26: so we know that we want to be taking
  • 07:30: the salary the initial salary
  • 07:35: times one plus the cost of living return
  • 07:37: to the number of years
  • 07:39: times one plus the promotion return to
  • 07:41: the number of promotions
  • 07:44: so how do we figure out the pieces
  • 07:48: for this so we don't know
  • 07:51: off the bat how many promotions there
  • 07:53: have been at a given year
  • 07:55: we know how often those promotions are
  • 07:58: occurring here by default every five
  • 07:59: years
  • 08:00: uh but we you know if the year is three
  • 08:03: if the year is seven
  • 08:05: ten whatever we don't immediately know
  • 08:07: how many promotions have occurred up
  • 08:09: until that point
  • 08:11: so we have to calculate that so i'm
  • 08:13: going to calculate that
  • 08:15: um so you know first
  • 08:18: let me just say you know let's work with
  • 08:20: year seven
  • 08:22: year equals seven um so let's
  • 08:25: say um you know year
  • 08:28: divided by uh the promotions every
  • 08:32: n years uh what do we get from that
  • 08:35: so uh year seven we've had 1.4
  • 08:38: promotions that doesn't we're not quite
  • 08:40: the way we're there would be one
  • 08:41: promotion right
  • 08:42: we get 10 that goes to 2 we hit 11. it's
  • 08:44: 2.2
  • 08:46: up to 14 we're at 2.8 so really we want
  • 08:50: just the integer portion of this right
  • 08:53: we don't care about the decimal
  • 08:55: as soon as it you know goes up to the
  • 08:57: next integer that's when we
  • 08:59: recognize that there's been another
  • 09:00: promotion so if we just take the integer
  • 09:04: portion of that then we will be left
  • 09:08: with what we want uh you know
  • 09:11: years 11 through 12 or 11 through 14.
  • 09:14: it's two promotions as soon as we have
  • 09:17: 15
  • 09:18: we get to three promotions so this is
  • 09:20: what we want
  • 09:23: and then we can go ahead and calculate
  • 09:27: calculate the salary at the given year
  • 09:30: so salary at year t
  • 09:32: is going to be the starting salary
  • 09:35: which is in the inputs times 1
  • 09:38: plus the cost of living raise
  • 09:42: and we're going to take that to the
  • 09:45: power
  • 09:45: of the year
  • 09:49: and then that's going to be times one
  • 09:51: plus
  • 09:52: the promotional raise uh
  • 09:56: to the power of the number of promotions
  • 10:00: um so then we see by year 15
  • 10:04: we have 120 000 roughly a year one
  • 10:07: sixty one thousand two hundred year two
  • 10:10: sixty four
  • 10:11: sixty two thousand uh you know starting
  • 10:13: off in a starting salary of
  • 10:14: six thousand two percent cost of living
  • 10:16: raise so this all sounds correct
  • 10:19: uh and year four uh just under 65 and
  • 10:22: then we hit year five and we get a big
  • 10:24: jump
  • 10:25: in the salary as we would expect coming
  • 10:27: along with the promotion
  • 10:28: so it looks like everything is working
  • 10:30: correctly
  • 10:33: so now we have a piece of the model
  • 10:35: which
  • 10:36: works appropriately let's go ahead and
  • 10:39: make this logical step
  • 10:41: into a function so
  • 10:44: uh what we can do you know this year uh
  • 10:47: is going to be an input to the function
  • 10:49: now and
  • 10:51: also the data is going to be the other
  • 10:52: input to the function so def
  • 10:55: uh you know salary at year and it's
  • 10:58: going to take the data and it's going to
  • 11:00: take the year
  • 11:01: and then we just uh
  • 11:05: you know highlight all that and press
  • 11:07: tab to shift it over
  • 11:09: and then we're just going to add a
  • 11:10: return on to the
  • 11:13: and now we have a function which does
  • 11:17: what we were trying to do
  • 11:21: so now we can get rid of this year
  • 11:24: um so at year four we see the same
  • 11:27: almost 65
  • 11:28: 000 year five we see the 76 000.
  • 11:34: and since we
  • 11:37: used just whatever data is being passed
  • 11:39: to it and not the overall model data
  • 11:42: that means that you know you could
  • 11:44: potentially
  • 11:45: uh you know construct you know other uh
  • 11:48: data you know say uh you know we wanted
  • 11:51: the salary to start at 40 000 instead
  • 11:54: and pass that data into there
  • 11:58: um then we can see that it's going to be
  • 12:00: based off of that
  • 12:02: new inputs whereas if you had to use
  • 12:05: model data
  • 12:06: in all of these and you may or may not
  • 12:07: have put an argument here for that
  • 12:10: then it's going to always use these
  • 12:12: values which are defined up here which
  • 12:14: makes your model significantly less
  • 12:16: flexible
  • 12:19: so we have our portion of the model
  • 12:23: that works getting the salary at a given
  • 12:25: year
  • 12:26: and you know definitely take a look at
  • 12:28: the full example
  • 12:30: you would want to put a description of
  • 12:31: what it does in the docs string and you
  • 12:33: want to describe what's going on in this
  • 12:35: section
  • 12:37: uh but the other thing that i think is
  • 12:39: appropriate to complete out this section
  • 12:41: is uh just showing how this works year
  • 12:45: over year
  • 12:46: so we define the function now let's look
  • 12:50: at what it looks like
  • 12:52: you know over a range of years so
  • 12:56: we're just going to go through six years
  • 12:59: and with the
  • 13:00: zero based indexing and python you know
  • 13:02: the the
  • 13:03: i is always starting at zero but we want
  • 13:05: our year to start from one so we're just
  • 13:06: going to add one
  • 13:08: to the i to get the actual year
  • 13:11: and then we're going to calculate the
  • 13:13: salary at that year
  • 13:15: and here we're going to pass it the
  • 13:18: model data because all this is happening
  • 13:20: outside of function and we want this to
  • 13:22: still work after we
  • 13:24: remove this when we're done with the
  • 13:25: model
  • 13:29: and then we want to go ahead and print
  • 13:32: a nice formatted string the salary
  • 13:35: at year
  • 13:38: and then the year is
  • 13:42: uh and then
  • 13:45: let's give it you know some commas in
  • 13:47: there uh and
  • 13:48: zero decimal places so then we can
  • 13:51: run that and we see that we get the
  • 13:54: salary year every year and indeed see
  • 13:56: you know cost of living raises going up
  • 13:58: through year four and then we hit year
  • 14:00: five a promotion occurs and we see a
  • 14:02: larger jump in salary so it looks like
  • 14:04: everything is working appropriately
  • 14:07: so definitely look at the full completed
  • 14:09: example for
  • 14:10: all the description of everything but
  • 14:13: that's the basic logic
  • 14:15: so next time we will come back to build
  • 14:17: the wealth section
  • 14:19: of the model and then the following
  • 14:21: video build the retirement section to
  • 14:23: finish it up
  • 14:24: so thanks for listening and see you next
  • 14:28: 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

  • 00:03: hey everyone
  • 00:04: nick duraburtis here teaching you
  • 00:05: financial modeling today we're going to
  • 00:08: be
  • 00:08: building out the wealth portion of the
  • 00:11: dynamic salary retirement model
  • 00:13: in python
  • 00:16: we left off last time and that we were
  • 00:19: uh you know building out the full python
  • 00:21: model and we had already done the salary
  • 00:24: portion of the model
  • 00:25: so in this video we're working on the
  • 00:28: welsh portion
  • 00:29: and we'll come back in the following
  • 00:30: video to finish up the model with the
  • 00:32: retirement portion so let's go ahead and
  • 00:36: go over to the jupiter notebook
  • 00:38: so we already have you know this setup
  • 00:41: stuff at the beginning
  • 00:42: and then we have the salary section
  • 00:44: built out already
  • 00:46: now we're getting to the wealth portion
  • 00:48: of the model
  • 00:50: so let's uh you know just like we did
  • 00:53: for salary make a section
  • 00:56: and clearly differentiate this from the
  • 00:58: salary related code
  • 01:00: and you would definitely want to have uh
  • 01:04: you know a description of what this
  • 01:05: section is doing as well as the
  • 01:07: equations
  • 01:08: involved but for the interest of time
  • 01:12: i'm gonna leave those out here
  • 01:14: um just take a look at the completed
  • 01:16: example to see
  • 01:17: what that should look like
  • 01:21: so to get the wealth we can go back to
  • 01:25: what the wealth equation
  • 01:28: was it is just taking whatever the prior
  • 01:32: wealth was
  • 01:34: and multiplying it by one plus the
  • 01:36: investment return
  • 01:38: and then we're going to add the cash
  • 01:41: save to that and the cash saved is the
  • 01:43: salary
  • 01:44: in that given year times the savings
  • 01:47: rate
  • 01:48: so we can think of this
  • 01:51: equation in two parts one is uh
  • 01:55: you know calculating the overall wealth
  • 01:56: equation and the other is
  • 01:58: figuring out this cash saved so let's
  • 02:01: first create a function which will get
  • 02:03: us the cash saved
  • 02:05: and then we can plug that into another
  • 02:07: function which gets us the overall
  • 02:09: wealth at a given year
  • 02:14: so we want to figure out the cash saved
  • 02:18: in any given year
  • 02:20: and you know obviously as we just looked
  • 02:23: at the equation it's just going to be
  • 02:26: salary times the savings rate right
  • 02:29: um but what is salary where do we get
  • 02:31: salary from
  • 02:33: well we have this whole other section of
  • 02:35: the model that
  • 02:36: was responsible for being able to
  • 02:38: calculate a salary at any given year
  • 02:41: so you know if we just you know say it's
  • 02:44: a certain year let's say a zero three
  • 02:47: um before we can do this calculation to
  • 02:50: get the
  • 02:51: cash saved we need to figure out the
  • 02:54: salary for that year
  • 02:56: so to get the salary of that year we had
  • 02:58: this nice
  • 02:59: convenient function where we just pass
  • 03:02: it our data
  • 03:03: and we pass it the year
  • 03:06: and it's going to give us the salary for
  • 03:08: that year year three in this case
  • 03:11: and then we can calculate the cash saved
  • 03:13: based off of that salary
  • 03:15: so let's see what that looks like
  • 03:18: so in year three we're gonna be saving
  • 03:20: almost sixteen thousand dollars
  • 03:22: which uh if our uh salary here is sixty
  • 03:26: three thousand and our savings rate
  • 03:27: is twenty five percent then that sounds
  • 03:30: correct
  • 03:32: and we can try it with some other years
  • 03:34: year 10
  • 03:36: year it's going up year one it's it's
  • 03:38: lower
  • 03:40: so this all seems appropriate for what
  • 03:42: we would expect to see
  • 03:44: for the cash saved in a given year
  • 03:47: so now that we know that it works
  • 03:48: appropriately we can go ahead and wrap
  • 03:50: it up into a function
  • 03:51: so it can be easily reused
  • 03:54: so year is going to become an input
  • 03:58: and the data is our other input so let's
  • 04:01: go ahead and write
  • 04:02: our function definition so cache
  • 04:06: saved during year
  • 04:09: and it's going to take the data and the
  • 04:11: year and then i'm going to highlight all
  • 04:13: that hit tab to shift it over
  • 04:15: and then return the cache saved and then
  • 04:18: we'll see when we call
  • 04:20: this function uh with the data uh
  • 04:23: and some year that will get the same
  • 04:26: kind of results that we were getting
  • 04:28: just with the planes code in the cell
  • 04:32: so now we have a function which can give
  • 04:35: us the
  • 04:36: cash saved at any given year
  • 04:40: now we need to figure out what the
  • 04:41: wealth is for that
  • 04:43: year so we know that
  • 04:46: we're just going to take this cash save
  • 04:48: portion and add it on
  • 04:49: so then the other portion is taking
  • 04:51: whatever the prior wealth was
  • 04:53: and then multiplying it by
  • 04:57: one plus the investment rate
  • 05:02: so
  • 05:07: we're going to figure out the wealth in
  • 05:08: any given year now
  • 05:11: and the first step that is calculating
  • 05:14: the cash saved which now we have
  • 05:17: a nice convenient function to do that
  • 05:20: cash saved during year
  • 05:22: let's again set up you know year equals
  • 05:25: five
  • 05:27: um and that's going to take the data and
  • 05:29: it's going to take the year
  • 05:30: as we have defined that and i
  • 05:34: you know as we just saw from the prior
  • 05:36: cell will give us the cash saved in that
  • 05:37: year
  • 05:39: so then we need to go and calculate the
  • 05:41: wealth
  • 05:42: so the wealth we know is going to be
  • 05:45: equal to the prior wealth
  • 05:47: times 1 plus data
  • 05:51: the interest rate and we're going to add
  • 05:53: the cash saved
  • 05:55: onto that now of course when we run this
  • 05:59: we're going to get an
  • 06:00: error because prior wealth is not
  • 06:02: defined we don't know what prior wealth
  • 06:04: is yet so let's just
  • 06:08: uh you know think of that as an input to
  • 06:10: this function that we're creating as
  • 06:12: well
  • 06:12: and say that we start with no well um
  • 06:16: and then the function is able to run um
  • 06:20: and it says we have ninety thousand
  • 06:21: dollars saved at year five which is
  • 06:23: not correct that's just how much we're
  • 06:25: saving in year five
  • 06:26: but it's because we have no wealth to
  • 06:29: begin with
  • 06:30: um so clearly we're gonna need to use
  • 06:33: this in a way that we're able to take
  • 06:35: whatever the wealth was from
  • 06:36: last year and pass it in next time
  • 06:39: for the wealth the prior wealth
  • 06:43: so let's go ahead and create this
  • 06:45: function as is
  • 06:46: and then we can look at how we can use
  • 06:48: this in a structure of
  • 06:50: taking the wealth from each year and
  • 06:52: saving it and then using it as the prior
  • 06:54: wealth
  • 06:54: for the next year so
  • 06:57: we can create our function wealth that
  • 07:00: year
  • 07:00: which is going to take the data and it's
  • 07:03: going to take the year
  • 07:04: and it's also going to take the prior
  • 07:06: wealth
  • 07:07: and we can tab that all over and we want
  • 07:10: to return the wealth
  • 07:11: at the end and just double check that we
  • 07:14: get the same thing when we passed the
  • 07:15: data we passed it the year and we passed
  • 07:17: the prior wealth
  • 07:19: just a quick sandy check to make sure
  • 07:21: you didn't break anything while
  • 07:22: transferring it over to a function
  • 07:24: we're doing d d get the same number here
  • 07:28: so we don't need these anymore
  • 07:32: and now we can go and look at how we
  • 07:35: would use this
  • 07:37: so this will be good just you know for
  • 07:40: two things one
  • 07:41: to to get a better understanding of how
  • 07:43: this will be plugged into the overall
  • 07:45: function which determines the years to
  • 07:47: retirement uh but also
  • 07:48: just as a quick check of showing how
  • 07:51: this section
  • 07:52: works standalone you know what happens
  • 07:55: to the wealth
  • 07:55: year by year so the way that we can do
  • 07:59: this
  • 08:01: is a fairly common pattern in
  • 08:03: programming
  • 08:04: where you initialize some variable
  • 08:07: and then you go throughout a loop and
  • 08:10: then at the end of the loop
  • 08:12: you're going to uh set that variable to
  • 08:15: be
  • 08:16: uh you know the main variable's value
  • 08:19: and it's going to be like the prior
  • 08:20: value
  • 08:21: and you you know you keep going through
  • 08:23: and at the end of the loop it always
  • 08:24: gets
  • 08:25: reassigned to whatever last uh the last
  • 08:28: value was
  • 08:30: so let's look at how that would work so
  • 08:33: first we set our prior wealth to zero
  • 08:35: we aren't starting with any money and so
  • 08:37: that makes sense
  • 08:38: if you had some other input in the model
  • 08:40: of you know starting cash
  • 08:42: then you would want to use that instead
  • 08:44: of zero
  • 08:46: and then we want to have some kind of
  • 08:48: loop and let's just
  • 08:49: look you know over six years so we can
  • 08:52: make sure to get that promotion
  • 08:54: in there and again just like we did for
  • 08:58: salaries
  • 08:58: uh we want to think about the year
  • 09:00: starting from one
  • 09:02: not starting from zero so we're going to
  • 09:04: take year as i plus one
  • 09:08: then we can get the wealth as the using
  • 09:11: our wealth at year function
  • 09:12: passing it uh this time model data
  • 09:16: because this
  • 09:16: is going to be permanently outside of a
  • 09:18: function uh
  • 09:19: the year and the prior wealth
  • 09:25: and then we can show that result so
  • 09:29: the wealth at year year
  • 09:33: is and then we're gonna put the wealth
  • 09:37: and let's put commas and zero decimal
  • 09:39: places
  • 09:44: and then at the end of the loop we're
  • 09:46: going to set prior wealth equal to
  • 09:48: wealth um so then when we run this
  • 09:51: we can see that indeed does go through
  • 09:54: each of the years
  • 09:54: and the money is accumulating like you
  • 09:57: might think it would
  • 09:59: uh you know we have after year two we
  • 10:02: have more than double of what we were
  • 10:04: able to save
  • 10:05: after the first year because we got two
  • 10:07: years of savings
  • 10:08: second year had the cost of living raise
  • 10:10: on the salary and then there's also an
  • 10:12: investment return
  • 10:13: baked in there as well so this all looks
  • 10:15: like
  • 10:16: what we would expect to see and then
  • 10:19: uh you know you can see that uh here
  • 10:22: from year four to five
  • 10:23: it's jumping by about 23 000
  • 10:26: uh and then going to year six
  • 10:30: it's an even um greater or sorry year
  • 10:33: four
  • 10:34: year three to four we have uh only a 16
  • 10:37: 000 increase
  • 10:38: and then year four to five we have a uh
  • 10:41: 23 000 increase so it's quite a bit
  • 10:43: bigger going to year five when we get
  • 10:45: that promotion
  • 10:48: and so this uh prior wealth pattern
  • 10:52: you can see that every time at the end
  • 10:54: of the loop it's setting prior wealth
  • 10:55: equal to wealth
  • 10:56: so that means that the next time that
  • 10:58: this loop comes around
  • 11:00: it's going to be using the wealth from
  • 11:01: the last calculation
  • 11:03: as the prior wealth and keep going
  • 11:06: through always using the last value
  • 11:08: of wealth as prior wealth
  • 11:12: and we'll apply the same exact pattern
  • 11:14: when we go to
  • 11:15: build out the retirement portion of the
  • 11:17: model in the next
  • 11:19: video so thanks for listening and i'll
  • 11:22: 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

  • 00:03: hey everyone
  • 00:04: nick dear burtis here teaching you
  • 00:06: financial modeling and today we're going
  • 00:08: to be building out the retirement
  • 00:10: portion
  • 00:11: of the dynamic salary retirement model
  • 00:14: in
  • 00:14: python and this retirement portion of
  • 00:17: the model is the final portion which is
  • 00:19: going to finish up
  • 00:20: the model and the lecture series
  • 00:24: so we've been working on this dynamic
  • 00:26: salary retirement model we built out the
  • 00:28: entire thing in excel
  • 00:30: and then in python we've already built
  • 00:32: out the salary and wealth portions
  • 00:34: so look at the prior videos if you
  • 00:36: haven't seen those
  • 00:37: and now we're just going to build out
  • 00:39: the retirement portion of the model
  • 00:41: and finish it out so let's jump back
  • 00:45: over to the jupiter notebook
  • 00:47: so we already have you know all this
  • 00:50: initial setup stuff and we have the
  • 00:51: salaries section and we have the wealth
  • 00:53: section
  • 00:54: and now we're going to build out the
  • 00:56: retirement section
  • 00:58: so here i'm going to have retirement and
  • 01:01: now we're going to
  • 01:02: write out logic which the goal here is
  • 01:06: as soon as this person hits the desired
  • 01:09: cash
  • 01:10: which is given in the inputs then
  • 01:13: uh whatever year it is that they've
  • 01:15: accumulated that much wealth
  • 01:17: that is the year that they're going to
  • 01:19: be able to retire
  • 01:22: so uh we don't have to completely start
  • 01:26: from scratch here we already have
  • 01:29: a way to get the wells going up over
  • 01:32: time
  • 01:34: so then we just need to take this and
  • 01:37: make it stop
  • 01:38: as soon as we hit the desired cache
  • 01:41: so i'm just going to go ahead and start
  • 01:44: from this code and just copy it
  • 01:47: and i'm going to switch data for model
  • 01:50: data since this is code which is going
  • 01:52: to go inside of a function so we want to
  • 01:54: use data not model data
  • 01:58: and i'll just give that another try
  • 02:00: there so it still works
  • 02:02: and gives us the salary or the wealth in
  • 02:05: each year
  • 02:08: and we could make this thing go out to
  • 02:10: you know 30 years or whatever
  • 02:12: and now we can see we do hit the point
  • 02:15: where
  • 02:16: we hit the desired cash we can see here
  • 02:18: that year 28 is going to be the year
  • 02:20: but we want the code to be able to stop
  • 02:22: specifically at that point and tell us
  • 02:24: that that was the year that they were
  • 02:26: able to retire
  • 02:30: so what we can do to accomplish that
  • 02:34: there are a few different ways that we
  • 02:36: can go about this
  • 02:38: um one way is with actually a different
  • 02:42: type of loop
  • 02:42: that we haven't covered uh called a
  • 02:45: while loop
  • 02:46: and a while loop uh you know whereas the
  • 02:49: for loop
  • 02:50: runs for whatever you're passing it you
  • 02:53: know range it will you know just run 30
  • 02:55: times
  • 02:57: if you have you know for uh something
  • 03:00: and a list then it's going to run for as
  • 03:04: many items as you have in that list
  • 03:07: etc whereas the while loop
  • 03:10: runs until some condition is
  • 03:13: met so it's kind of a combination
  • 03:16: between
  • 03:16: a loop and an if statement and it's
  • 03:19: going to stop the loop as soon as that
  • 03:21: if condition is true
  • 03:26: or sorry as soon as it is false it's
  • 03:28: going to stop the loop
  • 03:30: so um what we can do
  • 03:33: is we can also initialize
  • 03:36: wealth to zero before starting this
  • 03:41: and what we want to do is say while
  • 03:45: the wealth is less than
  • 03:48: the desired cash then we're going to do
  • 03:52: this
  • 03:55: but you'll notice as soon as i make that
  • 03:57: change and try to run this
  • 03:58: that it's not working anymore right
  • 04:00: because uh
  • 04:03: this i there's no i anymore we had i
  • 04:06: from my code that i deleted to where it
  • 04:08: was a string
  • 04:09: and so i'm getting an error but
  • 04:12: really we just need to have a new way of
  • 04:15: figuring out
  • 04:16: what the year is going to be so let's
  • 04:19: also
  • 04:20: start the year at zero
  • 04:23: and every time we're going to increment
  • 04:26: the year by one
  • 04:28: so that way the first loop is starting
  • 04:30: at zero then it has one so the first
  • 04:32: loop it's one and then it comes around
  • 04:33: to the next loop
  • 04:34: now it's going to add one again so the
  • 04:36: year is going to be two on the second
  • 04:37: loop as we would expect
  • 04:40: so now we run this and we can see it
  • 04:42: does indeed stop at year 28
  • 04:45: so this already has gotten us to
  • 04:48: what we wanted um only
  • 04:52: that we don't necessarily have it in a
  • 04:54: very usable format yet
  • 04:56: um you know it printed out what the year
  • 04:59: last year was but we don't have that
  • 05:01: saved in a variable that we could use
  • 05:04: in some other way uh it's just kind of
  • 05:07: you know the the print the prince of the
  • 05:09: wealth stop happening at that point
  • 05:13: so but let's look at the value of the
  • 05:16: year variable when this is all done
  • 05:19: oh that is 28. so
  • 05:22: the year variable now contains what we
  • 05:25: need as our final answer
  • 05:27: um so just putting year there at the end
  • 05:31: then we get the retirement year from
  • 05:33: that
  • 05:34: and the reason was because it kept going
  • 05:36: through the loops it kept incrementing
  • 05:37: the year
  • 05:38: and then as soon as we got enough cash
  • 05:41: uh this condition evaluated to false and
  • 05:44: then it stopped going through additional
  • 05:46: loops
  • 05:46: and so the last time that it incremented
  • 05:48: the year would be the last year that we
  • 05:50: added the wealth
  • 05:52: and that would be the same year where
  • 05:53: the wealth has now surpassed the desired
  • 05:55: cash
  • 05:56: so just the year variable is going to
  • 05:58: contain what we need
  • 06:02: so then we've gotten to the final answer
  • 06:05: but we want to clean up the output here
  • 06:07: a little bit
  • 06:09: um so you know we don't want to just
  • 06:13: uh you know print out a show a number at
  • 06:15: the end we want to say what this number
  • 06:17: means
  • 06:19: so we can say uh you know
  • 06:22: it will take you know year
  • 06:25: i need to put the f at this beginning of
  • 06:27: the string it will take year
  • 06:29: years to retire
  • 06:33: okay now this is a little bit better it
  • 06:35: will take 28 years to retire that's a
  • 06:37: lot
  • 06:37: more clear um but we do have like two
  • 06:40: different
  • 06:41: sections of the output here one is
  • 06:43: showing the wealth year over year
  • 06:44: and one is showing the final year's
  • 06:48: retirement
  • 06:49: so it makes sense to separate those so
  • 06:52: let's
  • 06:52: uh put like a header here
  • 06:56: in the output and we can put one here
  • 07:00: as well
  • 07:07: and now this is a little bit better we
  • 07:09: have kind of a header
  • 07:10: for each portion of the output but it's
  • 07:12: still all together
  • 07:13: there's a couple ways that we can add
  • 07:16: some separation here in between these
  • 07:18: lines
  • 07:18: one is just you know kind of simple
  • 07:21: intuitive you can just print
  • 07:23: an empty string and that's going to make
  • 07:25: an empty line there
  • 07:27: the other way to go about it is
  • 07:30: backslash
  • 07:30: n actually means make a new line so if
  • 07:34: we put
  • 07:34: backslash n retirement there then that's
  • 07:37: going to do the exact same thing
  • 07:39: as uh having the empty print so even if
  • 07:42: you wanted
  • 07:43: you know a few lines there of spacing
  • 07:46: then you can easily do that in one print
  • 07:49: statement there
  • 07:51: so now we have two clear sections in the
  • 07:53: output what are the wealths
  • 07:54: over time and what are the retirement
  • 07:56: details and
  • 07:58: we get our main answer here in the
  • 07:59: retirement details that it's going to
  • 08:01: take
  • 08:01: 28 years to retire
  • 08:05: so now this block
  • 08:08: does do what we want to do but we should
  • 08:11: go ahead and
  • 08:12: wrap this up into a function as well
  • 08:15: um so these uh or so the
  • 08:19: uh these are all initializing for the
  • 08:22: loop they aren't actually inputs this is
  • 08:23: all going to happen inside the function
  • 08:26: so years to retirement and the only
  • 08:30: input here is going to be the data and
  • 08:32: then we can take
  • 08:33: all of the remaining code and hit tab to
  • 08:36: indent it
  • 08:39: and we're going to switch this to
  • 08:41: returning the year
  • 08:42: at the end um so then when we call
  • 08:46: years to retirement on the model data
  • 08:49: then we see
  • 08:50: the whole summary of the output here
  • 08:51: that it's going to take 28 years to
  • 08:53: retire
  • 08:54: and we can also have that saved in a
  • 08:57: variable
  • 08:58: as well so that we could do additional
  • 09:01: things
  • 09:02: with that result
  • 09:05: and so now our model works we have it
  • 09:08: all together
  • 09:10: so now that you have all your sections
  • 09:12: working you've got your final result
  • 09:14: the next thing that you should do and
  • 09:15: building out your models is
  • 09:17: go to this kernel menu and then restart
  • 09:19: and run all cells
  • 09:21: because this is gonna say you know
  • 09:23: throughout your your session you've been
  • 09:24: jumping all over the place
  • 09:26: uh you know building things and then
  • 09:28: deleting them
  • 09:29: and so things are just in a weird state
  • 09:32: from you playing around with things
  • 09:33: so if you restart this kernel and run
  • 09:36: all cells
  • 09:37: that gives you a fresh session and it
  • 09:39: runs everything in order
  • 09:41: and that way we can be sure that the
  • 09:43: model does indeed work
  • 09:45: from end to end the next thing that i'm
  • 09:48: going to do
  • 09:49: is i'm going to go back and i'm going to
  • 09:51: remove this data equals model data cell
  • 09:56: and then i'm going to restart and run
  • 09:58: all cells again
  • 10:00: so there we want to see again that it
  • 10:02: goes all the way through without error
  • 10:04: and here actually it did have an error
  • 10:07: i accidentally left this one cache saved
  • 10:10: during year
  • 10:11: using the data not the model data
  • 10:15: so this one should be model data to show
  • 10:18: that
  • 10:18: because it's happening outside the
  • 10:19: function so now i'm going to restart and
  • 10:22: run all cells again
  • 10:25: and now we can see it ran all the way
  • 10:28: through just
  • 10:29: fine um and now
  • 10:32: because all the functions are set to
  • 10:34: take data
  • 10:36: and they're not directly using the model
  • 10:38: data
  • 10:39: that means that any possible data can be
  • 10:42: passed into this function
  • 10:44: um so you know you can change it so that
  • 10:48: uh you know you're passing the model
  • 10:50: inputs
  • 10:51: with you know let's do starting salary
  • 10:54: equals 30 000 and then you run it again
  • 10:58: and now we can see it takes 37 years to
  • 11:01: retire
  • 11:02: oh well what if instead we had uh
  • 11:05: the cost of living raised was higher at
  • 11:08: 4
  • 11:10: going back to the original starting
  • 11:11: salary then it would
  • 11:13: be three years quicker for us to retire
  • 11:15: from that additional return
  • 11:17: um and so you're able to pass whatever
  • 11:19: you want to it
  • 11:20: and as we look at other extensions to
  • 11:23: models
  • 11:24: as we go throughout the course you're
  • 11:25: going to be able to add whatever
  • 11:26: extensions you want very easily
  • 11:30: and it's not tied to this particular
  • 11:32: instance
  • 11:33: of the model inputs defined here at the
  • 11:36: top you can pass it whatever
  • 11:38: inputs you want and everything flows
  • 11:40: through the entire model
  • 11:43: so that wraps up this lecture series
  • 11:47: on going uh to build out the full
  • 11:51: dynamic salary retirement model
  • 11:53: in python the next lecture series is
  • 11:56: going to be focused
  • 11:57: on visualization so thanks for listening
  • 12:00: 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

  • 00:03: hey everyone
  • 00:04: nick derbertus here teaching you
  • 00:06: financial modeling and today
  • 00:08: we're going to talk about the lab
  • 00:09: exercise for the
  • 00:11: section of the course on the depth of a
  • 00:14: financial model
  • 00:14: in python where we focused on building
  • 00:17: out the dynamic salary model
  • 00:19: in python so the lab exercise here
  • 00:23: is the same as the one that we did for
  • 00:26: excel
  • 00:26: in the section on building out the excel
  • 00:29: dynamic salary retirement model
  • 00:32: and here what we want to do is we want
  • 00:34: to
  • 00:35: relax this assumption that there's some
  • 00:38: fixed amount of desired cash
  • 00:40: that we need for retirement and the way
  • 00:42: that we talked about
  • 00:44: relaxing that was that we're going to
  • 00:46: think about instead of
  • 00:47: just having a certain number instead
  • 00:49: we're going to have
  • 00:51: an amount that we want to spend each
  • 00:53: year in retirement as an input
  • 00:55: as well as the length of time that this
  • 00:57: individual
  • 00:58: is in retirement until they die
  • 01:01: so then based off of those inputs
  • 01:04: we can then calculate the desired cash
  • 01:08: within the model and plug that through
  • 01:11: into the existing structure
  • 01:12: so that the rest of the model works the
  • 01:14: same way that it did
  • 01:16: before so that's the basic idea
  • 01:20: and to accomplish this um something
  • 01:24: a way you might want to go about it is
  • 01:27: go ahead
  • 01:27: and remove the desired cache from the
  • 01:31: model inputs in the model
  • 01:34: and add these two new inputs
  • 01:37: and then it makes sense to create a
  • 01:39: function which is able to
  • 01:42: calculate the desired cache from the
  • 01:44: model inputs
  • 01:45: and then you'll just take uh calling
  • 01:47: that function
  • 01:48: and put it in the place of where you
  • 01:51: were just directly
  • 01:52: using the desired cache before and then
  • 01:55: you really won't have to change much at
  • 01:57: all
  • 01:57: about the existing model and it will
  • 01:59: just start working with this new
  • 02:01: desired cache calculation
  • 02:04: so thanks for listening and see you next
  • 02:07: time