Exploring the Parameter Space

Expand models to check the full range of inputs and see how it impacts the outputs.

Introduction to Parameter Exploration


Notes

  • If you’ve merely done a calculation, you have a single answer and can’t learn any more about the problem without redoing the calculations

  • Instead because we are building models which can flexibly take any input and convert that into the appropriate output, it opens up a lot of possible extensions to the model

  • This is our main focus for a large portion of the course: how can we extend any financial model to get a greater understanding of the underlying problem

  • Sensitivity analysis is useful to understand how the full range of possible inputs affects the main results of your model

  • Scenario analysis allows analyzing outcomes in example situations

  • Monte Carlo Simulation allows assigning outputs to a probability distribution, which helps you understand the risk of your result

Transcript

  • 00:02: hey everyone
  • 00:03: nick diabetis here teaching you
  • 00:05: financial modeling and today we're going
  • 00:07: to be talking about
  • 00:08: exploring the parameter space through
  • 00:10: sensitivity analysis
  • 00:13: so this is our first lecture out of this
  • 00:16: segment and this segment is really
  • 00:19: focused on
  • 00:21: we've built out a basic model now how do
  • 00:24: we get a deeper understanding
  • 00:25: of the problem than what the initial
  • 00:28: model
  • 00:28: says so
  • 00:32: so far you take some inputs
  • 00:36: you put them into your model and you get
  • 00:38: some outputs
  • 00:39: but that's just kind of one picture of
  • 00:42: what could happen
  • 00:43: based on the initial values of those
  • 00:46: inputs
  • 00:47: and certainly you can kind of change
  • 00:49: around the inputs and see what happens
  • 00:51: to the outputs but that's not a very
  • 00:54: systematic way
  • 00:55: of looking at the different values that
  • 00:58: there could be
  • 00:58: for the inputs um
  • 01:03: we can get a much deeper understanding
  • 01:05: of
  • 01:06: the whatever situation that we're
  • 01:08: modeling if
  • 01:10: we really explore all the different
  • 01:12: possible values that the inputs
  • 01:14: could be and how that affects the output
  • 01:17: from the model so that's
  • 01:21: one of the big reasons why i've been
  • 01:23: really emphasizing that
  • 01:25: in a model everything has to be linked
  • 01:27: together from the inputs all the way to
  • 01:29: the outputs
  • 01:30: changing your inputs should make a
  • 01:32: change in your outputs
  • 01:34: and you shouldn't have to be redoing a
  • 01:37: bunch of manual steps to get there if
  • 01:40: you just kind of hard code everything
  • 01:41: that's just a calculation and not a
  • 01:43: model
  • 01:43: because it's rigid and you can't change
  • 01:46: around
  • 01:46: the inputs and see how the outputs
  • 01:49: change in response to that
  • 01:52: and when you just do a calculation you
  • 01:54: get one answer
  • 01:55: and it's for a certain set of inputs
  • 01:58: maybe they're reasonable maybe they're
  • 01:59: not but when you build a model
  • 02:02: you set it up so it can take any inputs
  • 02:04: and so all of a sudden you're able to
  • 02:06: do much more exploration of the
  • 02:09: possibilities
  • 02:11: of the real world and incorporate that
  • 02:14: into your decision making
  • 02:17: so here's a quick visualization of
  • 02:22: what we're going to be doing with
  • 02:24: parameter expiration
  • 02:27: so the basic concept is we're going to
  • 02:30: run the model
  • 02:31: multiple times we're going to give the
  • 02:33: model different inputs
  • 02:34: each time and we're going to collect the
  • 02:36: outputs
  • 02:37: from each of those runs of the model and
  • 02:39: keep those associated with whatever
  • 02:41: inputs
  • 02:42: cause the outputs and then we're going
  • 02:45: to take all these
  • 02:46: uh inputs outputs all that information
  • 02:49: and put it into some kind of
  • 02:51: visualization that is easier to
  • 02:53: understand
  • 02:54: than the wrong numbers because very
  • 02:57: quickly this becomes a lot of numbers to
  • 02:59: deal with
  • 02:59: and so it's very difficult to parse
  • 03:03: through all that
  • 03:04: as the reader of the model unless
  • 03:06: there's an effective
  • 03:07: visualization to tie it all together
  • 03:12: so we'll talk about three different ways
  • 03:15: in this class
  • 03:17: to explore the the parameters or the
  • 03:20: inputs to the model
  • 03:22: and how those affect the outputs so the
  • 03:25: first one
  • 03:26: that we're going to be looking at in
  • 03:27: this lecture is sensitivity analysis
  • 03:31: we are also going to cover scenario
  • 03:33: analysis
  • 03:34: and monte carlo simulation as the other
  • 03:37: two methods
  • 03:39: so the differences between the three so
  • 03:42: sensitivity analysis
  • 03:45: um we're going to look at basically the
  • 03:48: full range of inputs that we think
  • 03:50: are reasonable for the model and see
  • 03:53: what full range of
  • 03:55: outputs we can get from that so in a lot
  • 03:58: of ways it's a good way to kind of
  • 04:00: stress your model
  • 04:00: and see if we take things all the way to
  • 04:03: uh
  • 04:04: as far as these inputs could reasonably
  • 04:07: go
  • 04:09: what happens to our outcome um
  • 04:13: whereas uh the other methods we have
  • 04:17: scenario analysis and monte carlo
  • 04:19: simulations so scenario analysis
  • 04:22: is about as the name implies trying to
  • 04:25: come up with specific
  • 04:26: realistic scenarios and building those
  • 04:29: into your model
  • 04:31: so very commonly that might be something
  • 04:33: like a
  • 04:34: good economy normal economy bad economy
  • 04:37: scenario
  • 04:39: and try to set all the values of the
  • 04:41: inputs to align
  • 04:42: with whatever the situation is and so
  • 04:45: you get a few different realistic
  • 04:47: outcomes
  • 04:48: out of your model under different
  • 04:49: conditions
  • 04:51: and then the the third
  • 04:53: [Music]
  • 04:55: that we're looking at is monte carlo
  • 04:57: simulation
  • 04:58: and monte carlo simulation is
  • 05:01: similar to uh sensitivity analysis
  • 05:05: and that we're throwing a bunch of
  • 05:06: different inputs at the model
  • 05:10: not only just a certain few that we've
  • 05:12: chosen
  • 05:13: for scenario analysis
  • 05:16: what the difference with monte carlo
  • 05:17: simulation is is that
  • 05:19: the inputs are actually random you set
  • 05:23: probability distributions for each of
  • 05:26: the inputs
  • 05:27: and each run of the model the inputs are
  • 05:29: drawn from those probability
  • 05:32: distributions randomly and then you take
  • 05:35: all these
  • 05:36: randomized values of the inputs pass
  • 05:38: them into the model
  • 05:39: and you get your output and what that
  • 05:41: allows you to do is you can actually get
  • 05:43: a probability distribution of the
  • 05:44: outputs from the model as well
  • 05:47: which can give you a good understanding
  • 05:49: of the risk
  • 05:50: of your result so
  • 05:55: let me give kind of a concrete example
  • 05:58: of using all three of these methods
  • 06:01: because
  • 06:02: they are similar to some extent and a
  • 06:04: lot of students wonder why
  • 06:06: why do we do all three why why is one
  • 06:08: not enough
  • 06:10: so you could think about um
  • 06:14: a capital budgeting model where you've
  • 06:17: got some project
  • 06:18: you have some initial investment on the
  • 06:20: project and then
  • 06:22: that project produces some cash flows
  • 06:24: and ultimately
  • 06:26: you're going to calculate an mpv of this
  • 06:29: project as a result
  • 06:30: and use that mpv to decide whether to
  • 06:33: take on the project
  • 06:35: so you've built out your model you've
  • 06:37: got the basic result
  • 06:39: and say you you've got a positive mpv
  • 06:41: and so it looks like okay
  • 06:43: we want to take this project but let's
  • 06:46: try to get a little better understanding
  • 06:48: of it before we jump the gun on that
  • 06:51: so then you can do uh sensitivity
  • 06:54: analysis
  • 06:56: and sensitivity analysis you can say
  • 06:59: well uh let's look at the full range of
  • 07:02: what's reasonable for the discount rate
  • 07:05: uh maybe it's as low as four percent but
  • 07:07: maybe it's as high as
  • 07:09: 12 and then uh you kind of space out
  • 07:13: you know try four five six seven eight
  • 07:15: nine ten twelve
  • 07:17: and see what npv comes out
  • 07:20: when you use all these different
  • 07:21: discount rates and there you may see
  • 07:24: oh well our baseline discount rate of
  • 07:26: seven percent it was mbv positive
  • 07:28: but really if it goes up to nine percent
  • 07:30: all of a sudden
  • 07:31: the npv is negative so we need to really
  • 07:34: carefully consider
  • 07:35: the discount rate have we accurately
  • 07:37: estimated the discount rate or is it
  • 07:39: really higher
  • 07:40: if it's higher all of a sudden this
  • 07:42: becomes not so
  • 07:43: attractive
  • 07:46: then thinking about scenario analysis we
  • 07:49: can go again with the example of the bad
  • 07:51: economy
  • 07:52: good economy normal economy cases
  • 07:56: so you might say that in the good
  • 07:59: economy
  • 08:00: you're gonna have higher demand uh
  • 08:03: it's going to be um
  • 08:06: a lower discount rate because it's
  • 08:08: easier to raise capital
  • 08:10: um and just kind of lining up all the
  • 08:13: different inputs into the model
  • 08:15: what all makes sense in a good economy
  • 08:17: putting that all together
  • 08:18: running the model and you get your good
  • 08:20: economy and pv
  • 08:22: and then you do the same thing for bad
  • 08:24: economy okay now it's going to be high
  • 08:25: discount rate we're going to have low
  • 08:27: demand
  • 08:28: um and so now we get our bad economy mpv
  • 08:31: so then you can understand uh with these
  • 08:34: kind of realistic scenarios
  • 08:36: well this this model says we got
  • 08:39: positive mpv
  • 08:40: in the normal in the good economy case
  • 08:41: but if the economy goes bad
  • 08:43: all of a sudden this is not an mpv
  • 08:45: positive project anymore so then you
  • 08:47: have to think about
  • 08:48: you know what's the chance of us going
  • 08:51: into a recession
  • 08:53: if it's high enough then maybe we don't
  • 08:55: actually take this project
  • 08:58: then lastly we have monte carlo
  • 09:00: simulation
  • 09:01: and for monte carlo simulation we assign
  • 09:04: distributions
  • 09:05: to each of these inputs so that's saying
  • 09:07: maybe okay the discount rate is
  • 09:09: seven percent by default
  • 09:12: but we're going to say now it is drawn
  • 09:14: from a normal distribution
  • 09:16: with a mean of seven percent and
  • 09:19: standard deviation
  • 09:20: of two percent so one time you run the
  • 09:23: model you may get seven percent another
  • 09:24: time you make it nine you may get eleven
  • 09:26: you may get four
  • 09:28: et cetera each time you run the model
  • 09:29: it's pulling a different value based off
  • 09:31: that normal distribution
  • 09:34: and you do the same thing for all the
  • 09:35: other inputs you do that for the demand
  • 09:38: uh whatever other inputs are going into
  • 09:40: the model
  • 09:42: all them now have probability
  • 09:43: distributions and then you go and you
  • 09:45: run this model
  • 09:46: a lot of times usually a thousand or ten
  • 09:48: thousand times
  • 09:50: um and you get all the mpvs out as a
  • 09:53: result
  • 09:54: and so what that gets you as long as you
  • 09:57: picked
  • 09:58: reasonable distributions for your inputs
  • 10:01: you now have a good estimate of the
  • 10:03: distribution of your mpv
  • 10:06: so that allows you to say oh well even
  • 10:09: though the baseline mpv is positive
  • 10:12: maybe uh through that analysis you can
  • 10:14: determine that it's actually a high risk
  • 10:16: project where on average
  • 10:18: the mpv is positive but ninety percent
  • 10:20: of the time it actually ends up negative
  • 10:22: it's only in those ten percent of the
  • 10:24: time that you have a very high mpv and
  • 10:26: it makes up for it and that allows the
  • 10:28: average to be positive
  • 10:30: but the majority of the time it's a
  • 10:32: negative mpv project
  • 10:33: so then that um really comes into the
  • 10:36: decision-making process
  • 10:38: of how much risk are we willing to take
  • 10:40: on with this project
  • 10:42: do we have plenty of sufficient capital
  • 10:44: such that it doesn't matter if this
  • 10:46: project fails
  • 10:47: okay well then yeah we can go with uh
  • 10:50: it's positive mpv let's take it
  • 10:52: but if you're capital constrained
  • 10:55: all of a sudden it becomes a more
  • 10:58: difficult decision because of the large
  • 11:00: risk
  • 11:00: involved in the project so all of these
  • 11:03: different considerations
  • 11:04: you would not have gotten by just
  • 11:06: building out the initial capital
  • 11:08: budgeting model
  • 11:09: it's only through exploring the
  • 11:11: parameter space
  • 11:12: with these different methods that we can
  • 11:14: get a much much richer understanding of
  • 11:16: the problem that we're trying to solve
  • 11:20: so next time we're going to come back
  • 11:21: and get into
  • 11:23: the theory of sensitivity analysis and
  • 11:26: what it looks like to carry that out so
  • 11:29: thanks for listening
  • 11:30: and see you next time

Introduction to Sensitivity Analysis


Notes

  • The formal definition of sensitivity analysis may seem complicated, but all we are doing is running the model multiple times with different inputs and showing the outputs

  • A key component of sensitivity analysis is the visualization, as now that there are many different outputs it is much easier to draw meaning from them when visualized

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: talking about an introduction to
  • 00:10: sensitivity analysis
  • 00:12: this is part of our lecture segment on
  • 00:14: exploring the parameter space
  • 00:18: so we introduced last time
  • 00:21: what exploring the parameter space means
  • 00:23: basically
  • 00:25: trying a bunch of different inputs in
  • 00:26: our model and looking at
  • 00:29: how that affects all the outputs and
  • 00:31: trying to
  • 00:32: gain a deeper understanding of the
  • 00:35: problem that we're modeling
  • 00:36: through that process so
  • 00:39: sensitivity analysis is one of the three
  • 00:42: approaches we're looking at
  • 00:44: in this course along with scenario
  • 00:46: analysis and monte carlo simulation
  • 00:49: and we're going to dig into formally
  • 00:52: what sensitivity analysis
  • 00:54: is so
  • 00:57: as with all the methods the general
  • 00:59: process is
  • 01:01: we're running the model a bunch of
  • 01:02: different times with different inputs
  • 01:05: uh and we're gonna get the outputs from
  • 01:07: each of those runs
  • 01:09: keep them all linked together and then
  • 01:11: visualize all that
  • 01:12: at the end that's true for all the three
  • 01:15: approaches
  • 01:16: but there are uh differences with each
  • 01:20: of the approaches and how they're
  • 01:21: carried out
  • 01:23: so for sensitivity analysis
  • 01:26: here would be the formal definition
  • 01:29: and just reading this it may look scary
  • 01:33: but really it's just a process that you
  • 01:36: have to follow
  • 01:37: and it's fairly straightforward once we
  • 01:40: actually get into how to apply it
  • 01:42: it just looks a little bit more
  • 01:44: complicated in like
  • 01:45: math and algorithmic terms
  • 01:48: so what we're doing in sensitivity
  • 01:50: analysis
  • 01:52: is for each one of the inputs
  • 01:55: we're going to choose a set of possible
  • 01:58: values for that input
  • 02:00: so say it's interest rate you might say
  • 02:03: it could be anywhere from three to ten
  • 02:04: percent three four five six seven eight
  • 02:06: nine ten percent
  • 02:07: those would be your values and you would
  • 02:10: want to do that for
  • 02:11: multiple different inputs uh whatever
  • 02:15: uh makes sense to analyze in your
  • 02:16: problem
  • 02:18: step two is then to
  • 02:22: look at all the different ways that you
  • 02:25: can combine each of these input values
  • 02:28: so formally that's called a cartesian
  • 02:30: product
  • 02:31: and that just means so say let's let's
  • 02:34: make a little bit smaller and say you're
  • 02:35: looking at
  • 02:36: five six seven percent interest rate and
  • 02:39: then
  • 02:40: uh we're going to look at uh
  • 02:43: demand of a thousand units
  • 02:46: five thousand units ten thousand units
  • 02:49: so it would be the
  • 02:50: five percent rate with the thousand
  • 02:54: uh demand and then the five percent rate
  • 02:56: with the five thousand demand and then
  • 02:58: the five percent rate went to ten
  • 02:59: thousand demand
  • 03:00: and then the six percent rate which
  • 03:02: either with each of the three demands
  • 03:04: and
  • 03:04: the seven percent rate which the with
  • 03:06: each of the three demands
  • 03:07: so nine uh sets of inputs all together
  • 03:10: for three of the interest rate and three
  • 03:15: of the demand so however many values of
  • 03:18: each of the inputs
  • 03:19: if you just multiply the number together
  • 03:22: that should be how many
  • 03:24: runs of the model you have in the end
  • 03:25: sets of inputs and
  • 03:27: outputs so
  • 03:30: you've got all those values combined
  • 03:31: together
  • 03:33: so then step three is to run the model
  • 03:36: and it's saying just run the model
  • 03:38: for each one of these different sets of
  • 03:40: inputs
  • 03:41: then step four is as you're running the
  • 03:44: model
  • 03:46: store the output from the model along
  • 03:49: with
  • 03:49: the inputs since we're running it so
  • 03:51: many different times with different
  • 03:52: inputs
  • 03:53: you got to keep everything linked
  • 03:54: together and then number five here
  • 03:58: is then to visualize the results
  • 04:02: so we have a bunch of numbers coming out
  • 04:05: we have to make that more easily
  • 04:08: understandable
  • 04:09: and so we do that through visualization
  • 04:14: so hopefully just talking through it
  • 04:18: made that a little bit more concrete but
  • 04:20: let's look at an actual example problem
  • 04:22: and how we would apply it
  • 04:24: to go even further in that concrete
  • 04:27: direction
  • 04:28: so let's keep talking about demand here
  • 04:31: we have a simple demand model so the
  • 04:32: entire
  • 04:33: model is a single equation here just
  • 04:36: demand
  • 04:37: equals the constant minus the elasticity
  • 04:41: of demand times the price and that's
  • 04:43: kind of a standard demand equation
  • 04:47: so here we had on this slide x's are
  • 04:50: our inputs right and so here x is going
  • 04:53: to be
  • 04:53: the demand constant the elasticity of
  • 04:55: demand and the price those are our three
  • 04:57: inputs
  • 04:58: into the demand model and the output of
  • 05:01: course is the demand
  • 05:02: itself so
  • 05:05: let's go through those five steps now or
  • 05:08: at least the first four
  • 05:09: so the first step uh is
  • 05:13: to choose values of each of these inputs
  • 05:16: that we want to look at whatever is the
  • 05:18: range of reasonable values
  • 05:20: for the inputs you typically want to go
  • 05:23: for whatever the full
  • 05:24: range of reasonable values are take kind
  • 05:27: of the lowest
  • 05:28: and the highest values that you think
  • 05:32: would be reasonable to see in the real
  • 05:33: world and then you can see the whole
  • 05:36: range between them
  • 05:38: so here uh just taking two demand
  • 05:41: constants to
  • 05:42: elasticities and two prices
  • 05:45: and those are our values we're going to
  • 05:49: use in the sensitivity analysis
  • 05:51: then step number two in that cartesian
  • 05:54: product
  • 05:55: or in simpler terms just combining all
  • 05:57: the inputs in every way that you can
  • 06:00: and so we get that table there at the
  • 06:03: bottom
  • 06:03: so it has for the 60 000
  • 06:07: uh demand constant we're going to see
  • 06:09: every other possible combination of
  • 06:12: input values it's with both the 200 and
  • 06:14: the 500
  • 06:15: it's with both the 50 and 100 and so we
  • 06:18: have all those combinations
  • 06:20: same thing for the hundred thousand
  • 06:21: domain constant and you can see that
  • 06:24: we've got it right because we've got two
  • 06:26: of each input
  • 06:28: two of each input and so it's two times
  • 06:30: two times two
  • 06:31: is eight total input cases and that's
  • 06:34: indeed what we have here
  • 06:38: so then going to step three is to run
  • 06:41: the model
  • 06:42: and step four is to keep those things
  • 06:44: linked together so we can kind of do all
  • 06:46: that
  • 06:46: here in this table and now we have
  • 06:51: calculated this equation for each one of
  • 06:53: these
  • 06:54: sets of inputs and by using this table
  • 06:57: we've kept them
  • 06:58: mapped together which inputs go to which
  • 07:02: output so
  • 07:05: that was our whole sensitivity analysis
  • 07:08: process right there
  • 07:09: really not complicated the only
  • 07:11: remaining step
  • 07:13: is then to visualize that it's kind of
  • 07:16: hard to just
  • 07:16: look at this and be able to extrapolate
  • 07:19: meaning out of it
  • 07:20: immediately because it's a bunch of
  • 07:21: numbers so we're going to look at ways
  • 07:24: we can
  • 07:25: make this easily understandable to where
  • 07:27: you can just glance at it
  • 07:29: and immediately see uh
  • 07:33: meaning out of the out of those large
  • 07:35: amounts of numbers
  • 07:37: so we'll come back and actually
  • 07:41: visualize that
  • 07:42: when we look at the applied examples in
  • 07:45: excel and python
  • 07:47: and that's going to be the focus of the
  • 07:49: next lecture video is to actually
  • 07:51: implement sensitivity analysis in excel
  • 07:54: so thanks for listening and see you next
  • 07:56: time

Sensitivity Analysis in Excel


Notes

  • Data tables in Excel allow calculating a cell multiple times, changing some other cell. This is perfect for sensitivity analysis if we target an output cell and change an input cell

  • One-way data tables change one input at a time, two-way data tables change two inputs at a time

  • You are basically limited to changing two inputs at once, without doing some clever hacks

  • Visualization rule of thumb: graph one-way data tables and use conditional formatting for two-way

  • Conditional formatting changes the format of cells based on conditions, such as putting the largest numbers in green and the smallest in red

  • Row input cell means that your inputs are going horizontally in a row. Column input cell means that your inputs going vertically in a column. For one-way data tables, you will use only one of the two. For two-way data tables, you will use both

Transcript

  • 00:02: hey everyone
  • 00:03: nick derbert is here teaching you
  • 00:04: financial modeling and today
  • 00:07: we're going to be looking at how to do
  • 00:08: sensitivity analysis
  • 00:10: in excel this is part of our lecture
  • 00:13: segment
  • 00:13: on exploring the parameter space with
  • 00:16: sensitivity analysis
  • 00:20: so in the last segment we went through
  • 00:22: all the
  • 00:23: theory behind sensitivity analysis and
  • 00:26: everything that's involved and kind of a
  • 00:28: simple example on how to do that
  • 00:30: now let's look at how we actually
  • 00:33: operationalize that in excel
  • 00:36: so excel has a nice built-in tool called
  • 00:39: a data table
  • 00:41: which makes this process fairly easy for
  • 00:44: us
  • 00:45: so a data table lets you look at the
  • 00:48: value
  • 00:49: of some cell while changing
  • 00:52: some different cells and so you can look
  • 00:55: at the value of an output
  • 00:57: of your model while changing around the
  • 01:00: inputs
  • 01:00: of your model without you having to go
  • 01:02: in and manually change those out
  • 01:06: and with data tables we have
  • 01:09: two different ways we can go about it
  • 01:13: one is a one-way data table and the
  • 01:15: other is a two-way data table
  • 01:17: so with a two-way data table we're
  • 01:20: looking at two different inputs at once
  • 01:23: and changing each of these two inputs
  • 01:25: and kind of the intersection
  • 01:27: of all those different
  • 01:30: outputs or runs of the model which
  • 01:32: produce those outputs
  • 01:35: and with the one-way data table we're
  • 01:37: looking only at a single
  • 01:39: input at once
  • 01:42: and that is sort of a limitation in
  • 01:46: excel
  • 01:46: that you basically can only really look
  • 01:49: at
  • 01:49: two variables at the same time in the
  • 01:52: analysis
  • 01:54: but it gets difficult to visualize more
  • 01:56: than two variables at once anyway
  • 01:59: so it's not a huge limitation
  • 02:03: even though uh it can be frustrating if
  • 02:05: you want to look at more
  • 02:06: variables you find yourself doing a lot
  • 02:08: of manual steps
  • 02:10: there is a way to to hack it into
  • 02:13: doing more than two variables at once if
  • 02:16: basically one of your variables
  • 02:18: was something like an identifier to
  • 02:21: do a vlookup on another table which has
  • 02:24: the real inputs
  • 02:25: you could set up something like that but
  • 02:28: that's a lot of effort
  • 02:29: and generally you're just going to be
  • 02:32: looking at
  • 02:32: two variables at once as a maximum in
  • 02:34: excel
  • 02:37: and you access this from the data tab
  • 02:40: and then what if analysis and then
  • 02:44: data table and then it asks for two
  • 02:47: inputs
  • 02:47: you're going to give it the row input
  • 02:49: cell that's any inputs you have going
  • 02:51: horizontally in a row
  • 02:52: and a column input cell that's any
  • 02:55: inputs you have
  • 02:56: going vertically down in a column and if
  • 02:59: it's a one-way data table you're only
  • 03:00: going to have one of the two filled in
  • 03:02: if it's two-way data table you're gonna
  • 03:04: have both filled in
  • 03:05: and we'll see that uh more precisely in
  • 03:08: an example here in a minute
  • 03:11: and then thinking about how we can
  • 03:14: visualize the results
  • 03:15: so there's two main ways
  • 03:19: in general to visualize sensitivity
  • 03:21: analysis
  • 03:23: and that's true for excel as well
  • 03:26: is that two approaches are graphing
  • 03:30: and conditional formatting so
  • 03:33: uh my rule of thumb is if it's a one-way
  • 03:36: data table you're looking at one
  • 03:37: variable at once
  • 03:38: a graph is going to be more effective
  • 03:41: and if you're looking at
  • 03:43: two variables that wants a two-way data
  • 03:45: table then
  • 03:46: conditional formatting is going to be
  • 03:48: the more effective approach
  • 03:50: that's not to say that you couldn't use
  • 03:52: conditional formatting with a one-way
  • 03:54: and you couldn't do a graph of a two-way
  • 03:57: but generally i think it makes the most
  • 03:59: sense to do it that way
  • 04:03: so here's an example of conditional
  • 04:05: formatting
  • 04:06: conditional formatting is just applying
  • 04:10: formatting to cells
  • 04:11: based on some conditions and what we're
  • 04:14: going to look at in this context
  • 04:16: is giving colors to higher and lower
  • 04:18: values
  • 04:19: so you can see in this table that the
  • 04:21: high values are red
  • 04:23: and the low values are green and that
  • 04:25: makes it very easy to just quickly look
  • 04:27: at this
  • 04:28: and understand where the higher and
  • 04:30: lower parts of this table are
  • 04:32: and that's going to be really useful for
  • 04:34: understanding the results of our
  • 04:35: sensitivity analysis
  • 04:38: so let's go ahead and work on an example
  • 04:42: so i'll be starting from the existing
  • 04:44: dynamic salary retirement model
  • 04:47: and now i'm going to be adding
  • 04:49: sensitivity analysis
  • 04:51: to it so uh
  • 04:54: and you can find the completed example
  • 04:56: of this on the course
  • 04:58: site which has everything in there
  • 05:00: already
  • 05:03: so let's first say what if
  • 05:06: the savings rate was different it's 25
  • 05:10: of course we can go in and we can change
  • 05:12: it and we can see the result of that
  • 05:15: but we want to have the values across a
  • 05:18: bunch of different
  • 05:19: values of saving rates we want to see
  • 05:20: all the years for retirement all at once
  • 05:22: in the model
  • 05:23: without having to go in and change the
  • 05:25: input values
  • 05:27: so we can do that with a data table
  • 05:31: so the first thing that we'll do to
  • 05:33: build out the data table
  • 05:34: is first you want to reference whatever
  • 05:37: cell it is that has the output
  • 05:39: that you're trying to look at you can
  • 05:40: only look at one output at once
  • 05:43: in a data table and so you may have to
  • 05:46: have multiple if you have multiple
  • 05:47: outputs from your model
  • 05:48: here we just have years to retirement as
  • 05:51: our output from the model so i'm going
  • 05:53: to reference that over to this cell
  • 05:57: then um we want to put the values of the
  • 06:01: inputs that we want to look at so that's
  • 06:03: the
  • 06:03: the step one and the formal sensitivity
  • 06:06: analysis process
  • 06:07: thinking about all the different values
  • 06:09: of the inputs we want to look at
  • 06:11: so for savings rate let's say let's
  • 06:14: start it at five percent and go up to 70
  • 06:16: percent
  • 06:17: in five percent increments so in excel
  • 06:20: once you type out
  • 06:21: uh two numbers they can
  • 06:24: deduce the pattern from that and keep
  • 06:27: increasing it from there
  • 06:28: so if you drag it then we'll continue to
  • 06:31: get five percent
  • 06:32: increments after that
  • 06:35: so now we have the setup to be able to
  • 06:37: create the
  • 06:38: data table and this is a one-way data
  • 06:42: table because we're looking at only
  • 06:44: one input at once and
  • 06:48: so we highlight the entire range here
  • 06:50: you want to make sure that you get
  • 06:52: the output cell as well as all of the
  • 06:54: inputs
  • 06:56: and you want to make sure that your
  • 06:58: output is to the right
  • 07:00: of all your inputs assuming you're doing
  • 07:03: a vertical
  • 07:04: one-way data table and so we highlight
  • 07:07: all this
  • 07:08: and then we can go to the data tab
  • 07:12: and then to what-if analysis
  • 07:15: and then to data table and that's when
  • 07:19: it pops up this thing which asks about
  • 07:21: the row input cell and the column input
  • 07:23: cell
  • 07:24: and for the one-way data table we're
  • 07:26: only going to fill in one of those
  • 07:28: so our inputs are arranged in a column
  • 07:31: and so we're going to use the column
  • 07:33: input cell and the cell that we want to
  • 07:36: target
  • 07:37: is the cell which contains the value of
  • 07:39: the input that we're trying to change
  • 07:41: here we're talking about the savings
  • 07:43: rate and so i'm going to
  • 07:46: reference the savings rate for the
  • 07:48: column
  • 07:49: input cell and that's it then once i hit
  • 07:52: ok
  • 07:54: we've got our results here so if you're
  • 07:56: able to save
  • 07:57: seventy percent every year your years
  • 08:00: retirement would drop from the baseline
  • 08:02: 28 down to 17 years
  • 08:05: so that's a huge difference and then if
  • 08:08: you
  • 08:08: were able to save less it's gonna take
  • 08:12: substantially longer to retire as well
  • 08:16: um so then we
  • 08:20: would want to label this savings rate
  • 08:24: so it's clear what we're actually
  • 08:26: changing here
  • 08:27: um and we can
  • 08:30: uh put a uh merge
  • 08:33: sell here years retirement
  • 08:38: versus savings rate
  • 08:41: and wrap the text on that
  • 08:44: so now we've got nice table
  • 08:48: and you would want to you know apply
  • 08:50: some coloring to this
  • 08:51: and put nice borders around it and
  • 08:53: everything
  • 08:54: i'm going to leave it there for now you
  • 08:56: can look at the completed example for
  • 08:58: the full formatting on it
  • 09:01: but one other thing i do want to mention
  • 09:03: which is
  • 09:04: helpful for these sata tables is we do
  • 09:07: have to have this reference cell here
  • 09:09: but there's no actual uh calculation
  • 09:12: going here this is the baseline result
  • 09:14: so it's helpful to hide this because
  • 09:16: it's just kind of distracting
  • 09:18: and so i usually just go ahead and make
  • 09:20: the text for that
  • 09:21: white so that it hides that cell even
  • 09:23: though it has to be there
  • 09:25: and now we just see each of the values
  • 09:28: with
  • 09:29: uh the input
  • 09:32: so then to visualize this i mentioned
  • 09:34: that graphs make a lot of sense
  • 09:37: for one-way data tables so let's go
  • 09:39: ahead and insert and go to recommended
  • 09:42: charts and we can pick an appropriate
  • 09:45: chart type for this
  • 09:48: and this is going to be the
  • 09:51: years retirement
  • 09:55: versus the savings rate
  • 09:59: uh we would want to add access titles
  • 10:04: this is the savings rate
  • 10:09: this is the years for retirement
  • 10:14: and then it's very easy to see that
  • 10:18: of course as we save more we're able to
  • 10:19: retire quicker
  • 10:21: but also you can see that the initial
  • 10:24: changes in the savings rate
  • 10:25: have a greater impact than the later
  • 10:28: changes in the savings rate
  • 10:29: it looks sort of exponential here and
  • 10:32: then as you get
  • 10:33: further out it looks more linear
  • 10:36: and so going from a ten percent to a
  • 10:39: twenty percent
  • 10:40: savings rate is a much bigger impact
  • 10:43: than going from a sixty savings rate to
  • 10:47: a 70
  • 10:48: savings rate and it's not very clear
  • 10:51: just looking at the numbers to draw that
  • 10:53: conclusion
  • 10:54: but looking at the graph it becomes very
  • 10:56: clear
  • 10:58: another thing which you might have
  • 11:00: noticed already
  • 11:01: but becomes even more clear in the graph
  • 11:03: is that it caps out at 40 and that seems
  • 11:05: incorrect um why is there no difference
  • 11:08: between five and ten percent
  • 11:10: savings rate seems like it should keep
  • 11:12: going up and it should
  • 11:14: uh the issue is that our model is
  • 11:17: actually capped at 40 years
  • 11:19: and so it's maxed out the model here and
  • 11:22: so
  • 11:23: if you did really need to evaluate five
  • 11:25: and ten percent savings rate you would
  • 11:26: then want to expand
  • 11:28: out the model so um sensitive analysis
  • 11:31: is really helpful for kind of stressing
  • 11:33: your model
  • 11:34: at the far ends and seeing if it can
  • 11:36: handle the inputs that you're throwing
  • 11:38: at it
  • 11:40: so that's the one-way data table now
  • 11:43: let's look
  • 11:44: at a two-way data table so let's keep
  • 11:47: working with the savings rate but now
  • 11:50: let's also change the
  • 11:53: uh how often we're getting the
  • 11:54: promotions
  • 11:56: in the same analysis so we can take the
  • 11:59: same savings rates
  • 12:01: those were fine savings rates to use
  • 12:06: and now we want to also line up the
  • 12:08: values that we want to look at
  • 12:10: for how often the promotions occur so
  • 12:15: we can just go up one by one and you can
  • 12:17: drag those to go
  • 12:19: all the way let's go out every 10 years
  • 12:24: and now you can see kind of a table
  • 12:26: starting to form here
  • 12:28: and whereas with the one-way data table
  • 12:31: we put the uh output we want to look at
  • 12:35: reference to up and to the right
  • 12:38: of our first input here
  • 12:42: the output is going to go right here
  • 12:44: it's going to go in this empty cell
  • 12:46: in between our column and our row input
  • 12:50: here we're going to reference the year's
  • 12:52: retirement
  • 12:54: so then now we have the setup to where
  • 12:56: we can create
  • 12:57: the two-way data table so if i highlight
  • 13:00: this entire range
  • 13:02: now i can go again to data what-if
  • 13:05: analysis
  • 13:06: data table and now that it's a two-way
  • 13:09: data table i'm going to fill in
  • 13:10: both of these and so the the numbers
  • 13:13: the input which is going in a row is the
  • 13:16: promotions every n years and then the
  • 13:19: input that's going in the column
  • 13:22: is the savings rate and then when i hit
  • 13:25: okay it will calculate the model a whole
  • 13:27: bunch of times
  • 13:28: for us so now we've got
  • 13:34: 14 times 9 different runs of the model
  • 13:40: all together without having to go and
  • 13:42: manually change out those
  • 13:43: inputs but you just look at this thing
  • 13:46: you know you can
  • 13:47: kind of see looking at it for a while
  • 13:49: okay lower values are over here
  • 13:51: higher values are over here but
  • 13:54: it's not so easy to just immediately get
  • 13:56: meaning out of that
  • 13:58: so the next step is then to add the
  • 14:01: conditional
  • 14:02: formatting so for conditional formatting
  • 14:04: it's on the home
  • 14:06: tab right here conditional formatting
  • 14:09: and there are a lot of things you can do
  • 14:11: with it but here we're just going to
  • 14:12: focus on the
  • 14:14: color scales and you can hover over the
  • 14:17: color scales to see what it's going to
  • 14:18: look like
  • 14:20: and i like to use the red and green
  • 14:23: color scales
  • 14:24: because people just intuitively
  • 14:26: associate green with good
  • 14:27: and red with bad if they're put together
  • 14:30: and so
  • 14:31: it makes a lot of sense whatever is
  • 14:34: uh whatever direction uh
  • 14:37: higher or lower is good in your model
  • 14:39: you would make that green
  • 14:41: and uh vice versa for red
  • 14:44: so here um looking at this first one
  • 14:48: it's actually highlighting the higher
  • 14:49: years to retirement in green
  • 14:51: but a low number of years until
  • 14:53: retirement is a good thing
  • 14:55: and so we want to go to this second
  • 14:56: color map and
  • 14:58: then we have the uh high years to
  • 15:02: retirement highlighted in red
  • 15:04: and the low years to retirement
  • 15:05: highlighted in
  • 15:07: green so
  • 15:10: then we can see now at a very easy
  • 15:12: glance
  • 15:13: uh where the low and high years to
  • 15:15: retirement are
  • 15:18: and that
  • 15:24: if you uh and we also see again this
  • 15:27: issue of it kind of maxing out the model
  • 15:30: in all these five and ten percent cases
  • 15:34: um so then you would want to add a
  • 15:38: little bit more
  • 15:39: formatting to this you would want to put
  • 15:41: labels promotions
  • 15:43: every number of years
  • 15:47: and we can then merge that
  • 15:52: and we would want to um
  • 15:56: merge over here as well and this can be
  • 15:58: the savings rate
  • 16:01: and to get that going vertically we can
  • 16:03: go into format cells
  • 16:05: change the alignment to 90 degrees
  • 16:09: and then center that
  • 16:13: and let's uh put that
  • 16:16: there and we can bold these things
  • 16:22: and then you know you would want to
  • 16:25: [Music]
  • 16:26: throw your title up here
  • 16:31: so this is going to be the ears to
  • 16:34: retirement
  • 16:36: or savings rate versus promotions
  • 16:40: every number of years
  • 16:47: and then we have our data table
  • 16:52: um so
  • 16:55: and then just like with the one-way data
  • 16:57: table
  • 16:58: i like to hide this cell because it's
  • 17:01: not
  • 17:01: anything meaningful for us so i just
  • 17:04: make that white
  • 17:05: and then we're left with just what we
  • 17:08: actually want to see
  • 17:10: um and again you would want to apply
  • 17:12: some coloring to the rest of this
  • 17:16: to make it kind of stand out as a table
  • 17:18: and you can see the full formatting of
  • 17:20: that
  • 17:21: in the completed excel example
  • 17:24: but that's that's the idea here of how
  • 17:26: to do a one-way data table
  • 17:28: with visualization and a two-way data
  • 17:30: table
  • 17:31: with visualization in excel
  • 17:35: and the lab exercise for
  • 17:38: this portion of the lecture is then to
  • 17:42: do this for your own project one
  • 17:45: model um so take your excel model from
  • 17:48: project one
  • 17:50: and you're going to um
  • 17:54: probably make a copy of it just in case
  • 17:56: something you mess up something
  • 17:58: but then you're going to implement a
  • 18:00: similar kind of analysis to what i just
  • 18:02: did
  • 18:03: so you're going to add a one-way
  • 18:08: data tables as well as a two-way data
  • 18:10: table looking at
  • 18:12: how the mpv changes in response to
  • 18:14: changing the number of machines
  • 18:16: and the initial demand so one way data
  • 18:20: table for each of those as well as a
  • 18:21: graph
  • 18:21: for each of those and then a two-way
  • 18:23: data table with conditional formatting
  • 18:25: looking at both at the same time
  • 18:30: so that sensitivity analysis in excel
  • 18:34: next time we're going to come back and
  • 18:37: cover some
  • 18:38: extra python basics before we can get
  • 18:41: into doing
  • 18:42: sensitivity analysis in python so
  • 18:45: thanks for listening and see you next
  • 18:48: time

Using Python Dictionaries


Notes

  • There are three ways to loop through dictionaries: through the keys (the default), through the values (.values()), and through both at once (.items())

  • If you want one way to do it, just always loop through the items as you have access to both the key and the value at once

  • Combine dictionaries using update. Add items to dictionaries with brackets and assignment. Remove items from dictionaries with pop

Transcript

  • 00:02: hey everyone
  • 00:03: nick dear bertis here teaching you
  • 00:05: financial modeling today
  • 00:07: we're going to be talking about some
  • 00:09: additional
  • 00:10: python basics focusing on using
  • 00:13: python dictionaries this is part of our
  • 00:16: lecture series on
  • 00:17: exploring the parameter space and this
  • 00:20: is covered during this lecture series
  • 00:22: because we're going to need to use
  • 00:24: dictionaries to complete sensitivity
  • 00:26: analysis
  • 00:27: in python so
  • 00:31: we um like i just said we just need to
  • 00:35: cover a little bit of
  • 00:36: extra python basics before we can
  • 00:40: get into doing sensitivity analysis in
  • 00:43: python
  • 00:44: so let's look first at dictionaries
  • 00:48: and then the other topics we're going to
  • 00:50: look at in the
  • 00:52: follow-on videos within this extra basic
  • 00:55: segment
  • 00:56: are about list comprehensions and
  • 00:59: uh the python imports and
  • 01:02: installing packages but we're focusing
  • 01:06: in this video on
  • 01:07: dictionaries and we covered dictionaries
  • 01:09: before so
  • 01:11: if you forgot that or didn't see that
  • 01:13: video go back
  • 01:15: and look at the original video on
  • 01:17: dictionaries to get a quick introduction
  • 01:20: within the python basics segment
  • 01:23: but here we're going to dig more into
  • 01:26: using them
  • 01:28: so but we'll give just a quick recap on
  • 01:31: what a dictionary is
  • 01:33: and it's a basic python data type
  • 01:36: just like a list numbers floats integers
  • 01:41: uh strings all the basic data types
  • 01:44: booleans
  • 01:45: etc dictionary is one of the basics
  • 01:50: and like a list it holds other objects
  • 01:54: within it analysts
  • 01:58: you know is an ordered you just hold
  • 02:00: things in order
  • 02:02: for a dictionary it's key value pairs so
  • 02:05: it's
  • 02:05: you're storing your object
  • 02:08: under some key where you want to be able
  • 02:11: to look up the object by that
  • 02:13: key at a later time
  • 02:17: so here's an example of
  • 02:21: finding a dictionary looking up um
  • 02:24: values from the dictionary and looping
  • 02:27: through
  • 02:27: the dictionary so looping looping
  • 02:30: through the dictionary
  • 02:31: is totally new for us so
  • 02:34: here first i'm defining the dictionary
  • 02:37: and whenever you define a dictionary
  • 02:39: you're using
  • 02:40: the curly braces to do that that's the
  • 02:42: easy
  • 02:43: built-in way and
  • 02:47: you're going to have then the keys
  • 02:51: a key and then a colon and then the
  • 02:53: value and then a comma
  • 02:55: and then coming to the next key colon
  • 02:58: value
  • 02:58: comma and so on and
  • 03:02: so the example here is
  • 03:05: a dictionary of what level
  • 03:08: my coffee is at and how i'm feeling
  • 03:12: in relation to that so
  • 03:15: um when my coffee is at a high level
  • 03:19: i'm happy it's pretty high i'm still
  • 03:21: happy
  • 03:22: as it starts to drop to medium then
  • 03:24: feeling neutral
  • 03:26: if it's low then i'm getting sad i want
  • 03:29: more coffee
  • 03:30: if there's no coffee then i really gotta
  • 03:32: get some more and so i'm desperate
  • 03:35: and so in that dictionary you put its
  • 03:38: name
  • 03:39: and then you put brackets and you put a
  • 03:41: key
  • 03:42: here we're putting the pretty high key
  • 03:44: and so we're going to get the happy
  • 03:46: value as a result so that's how you can
  • 03:49: look up
  • 03:50: any item in the dictionary you look it
  • 03:52: up by its key
  • 03:54: so what's new for us is then looping
  • 03:57: through the dictionary
  • 03:59: and there are three different ways to
  • 04:01: loop through a dictionary
  • 04:03: which is part of why we didn't cover
  • 04:05: that yet because it's a little more
  • 04:07: complicated than looping through lists
  • 04:10: um the three ways you can loop through
  • 04:12: it are you can loop through the keys
  • 04:14: you can loop through the values or you
  • 04:16: can loop through both the keys and
  • 04:17: values at once
  • 04:19: and if you just want one way to do
  • 04:21: things you say why do we have three ways
  • 04:24: can we just always do one way to loop
  • 04:26: through dictionaries
  • 04:28: i would say just always do
  • 04:31: looping through both keys and values at
  • 04:33: once through calling
  • 04:34: dot items on the dictionary because that
  • 04:38: way you have access to both
  • 04:40: you don't necessarily have to use both
  • 04:42: um and that's why
  • 04:43: if you just want one way to do things
  • 04:45: then
  • 04:46: always do it with items it'll get you
  • 04:48: both the key and the value
  • 04:50: at the same time in the loop
  • 04:53: so the way that you do that with
  • 04:57: dot items so we have our familiar for
  • 05:00: loop here
  • 05:01: or something in something colon indi
  • 05:05: but here we're putting the dictionary's
  • 05:07: name but we're not just looping through
  • 05:09: the dictionary
  • 05:10: we're looping through dot items of
  • 05:13: the dictionary and that's what allows us
  • 05:15: to loop through both the key
  • 05:17: and the value at the same time
  • 05:20: and then over here we have not just a
  • 05:24: single value that we're looping through
  • 05:25: we're looping through two values at once
  • 05:27: the key
  • 05:28: and the value and so
  • 05:33: um you whatever you put first
  • 05:37: is in reference to the key and whatever
  • 05:39: you put after the comma
  • 05:41: you put it second that's in reference to
  • 05:44: the value
  • 05:45: so that's why uh here the copy levels
  • 05:49: are the keys and the emotions are the
  • 05:51: values and so that's why it's
  • 05:52: four copy level comma emotion because
  • 05:55: this is the key
  • 05:56: and this is the value and this is
  • 06:00: picking good names
  • 06:01: for each of those that correspond to
  • 06:03: what they actually are
  • 06:05: and then here i'm just putting out a
  • 06:07: sentence which explains
  • 06:09: how i'm feeling in relation to my coffee
  • 06:12: level so
  • 06:13: what i just said before when i got a lot
  • 06:14: of coffee i'm happy
  • 06:16: uh when it's out then i am not feeling
  • 06:20: great
  • 06:22: so that's the basic example and now we
  • 06:25: can
  • 06:26: look at how we can add and delete items
  • 06:30: from dictionaries after it has already
  • 06:32: been created
  • 06:34: so we have this coffee levels emotions
  • 06:37: dictionaries
  • 06:39: um and there's two way that we can add
  • 06:41: items one is we can take an entirely
  • 06:44: separate dictionary
  • 06:45: and bring all of it all of its items
  • 06:47: into this dictionary
  • 06:49: that's through the update method so you
  • 06:51: call dot update
  • 06:52: on the dictionary and you pass it
  • 06:55: another dictionary
  • 06:56: and that's going to take that second
  • 06:58: dictionaries items and bring them into
  • 07:00: the first
  • 07:02: so after the first now if the coffee is
  • 07:06: overflowing then i'm feeling burned
  • 07:08: and now that has come into the
  • 07:11: dictionary
  • 07:12: as well and then we can assign
  • 07:16: a single key value pair at once
  • 07:19: by uh basically doing what we would to
  • 07:23: look up a value
  • 07:24: by the key but then just putting equals
  • 07:27: and assigning the value
  • 07:29: so here i'm assigning the negative key
  • 07:32: to have the value of confused
  • 07:34: and so if there's a negative amount of
  • 07:37: coffee
  • 07:38: in my cup then i am confused because i
  • 07:41: don't know how that's
  • 07:42: possible so then
  • 07:47: we can remove items from the dictionary
  • 07:49: as well
  • 07:50: and we do that through the dot pop
  • 07:53: method
  • 07:54: so the dot pop you give it the key
  • 07:58: and then as a result it will give you
  • 08:01: the value you may or may not
  • 08:05: care what that value is sometimes you
  • 08:07: just call this
  • 08:09: and don't even assign it to anything
  • 08:12: just this would be your line of code
  • 08:13: if you really don't care about the value
  • 08:15: you just want to get this thing out of
  • 08:17: the dictionary
  • 08:19: but you can also save the value coming
  • 08:22: out of pop as well
  • 08:24: um so that's the quick overview on how
  • 08:27: to add and delete items from
  • 08:29: dictionaries
  • 08:30: let's go ahead and look at the jupyter
  • 08:33: notebook example
  • 08:34: so you see this actually happening in
  • 08:36: the code
  • 08:40: so here i'm just defining a dictionary
  • 08:43: again with curly braces
  • 08:45: um and with the key value pairs
  • 08:48: um key then colon then value
  • 08:52: and i like to write these on multiple
  • 08:54: lines
  • 08:56: uh but you can do it on a single line as
  • 08:58: well it's the same thing
  • 09:00: uh once you're inside those curly braces
  • 09:02: then you can have
  • 09:04: as many uh extra lines as you want
  • 09:07: you could do this if you felt like it
  • 09:12: it's only when you're outside of curly
  • 09:13: braces or parentheses or anything like
  • 09:15: that you can't just
  • 09:16: arbitrarily make lines
  • 09:21: and so i mentioned we have three
  • 09:24: different ways to
  • 09:25: loop through the dictionary through the
  • 09:27: keys through the values
  • 09:28: and through both at once and that's
  • 09:31: coming out of
  • 09:32: these keys values and items which gives
  • 09:36: you
  • 09:36: both at once so a and b are our keys
  • 09:40: one and two are our values and having
  • 09:43: those associated together
  • 09:45: is the items
  • 09:49: and so with dictionaries we can look up
  • 09:51: any value by
  • 09:52: its key so a and b are the keys one and
  • 09:55: two are the values
  • 09:56: and so we can look up one and two by a
  • 09:59: and b
  • 09:59: because that's the way we've set up the
  • 10:02: dictionary
  • 10:04: and then we can add a single item at
  • 10:07: once
  • 10:08: by basically doing the same
  • 10:11: way we would look up a key but then
  • 10:14: assigning afterwards and so here
  • 10:18: we've now added c the key c
  • 10:21: map to the value 10 into this dictionary
  • 10:26: and with the update method we can give
  • 10:28: it an entire second dictionary at once
  • 10:31: here we're going to add the d and e
  • 10:32: items into the dictionary
  • 10:34: and now we've got a b c d and e all
  • 10:37: in my dictionary
  • 10:42: and then you can also remove with pop
  • 10:45: and so here we're going to
  • 10:48: remove this a1 item out of the
  • 10:51: dictionary
  • 10:52: and the a value is going to be the one
  • 10:55: so that's what this print statement is
  • 10:58: showing you that
  • 11:00: we got the one out as a result of the
  • 11:02: pop
  • 11:03: and now that a associated one is
  • 11:06: no longer in the dictionary
  • 11:10: so um you don't necessarily
  • 11:14: need to do that assignment um here we're
  • 11:16: just gonna
  • 11:17: take b the b key associated to the two
  • 11:20: value
  • 11:21: out of the dictionary without assigning
  • 11:23: or anything
  • 11:24: and now we're left with just c d and e
  • 11:29: so looking at looping through dictionary
  • 11:33: what you would probably try to do in the
  • 11:35: first case is just to loop
  • 11:37: right through the dictionary without
  • 11:38: calling any method on it
  • 11:41: and what that does by default is it
  • 11:43: actually loops through just the keys
  • 11:45: so if you're trying to get at the values
  • 11:48: you're not going to get them directly
  • 11:49: that way
  • 11:50: um so this is basically implicitly doing
  • 11:54: the same thing
  • 11:55: as calling dot keys on the end of this
  • 11:59: but we saw we had not just dot keys we
  • 12:01: also had dot
  • 12:02: values and dot items and so when you
  • 12:06: go through values then
  • 12:09: it's going to go and print each one of
  • 12:12: the values and just to remind you what's
  • 12:15: in this dictionary
  • 12:16: 10 510 those are the three values
  • 12:20: in the dictionary
  • 12:23: and here without items then we can loop
  • 12:27: through both at once there you do need
  • 12:30: to
  • 12:31: put this comma whatever comes before the
  • 12:34: comma
  • 12:35: is going to be the variable that
  • 12:36: represents the key whatever comes after
  • 12:38: is the variable that represents the
  • 12:40: value
  • 12:41: and so we can use both of them at once
  • 12:45: in the loop so here we're saying both
  • 12:47: what the key is
  • 12:48: and what the value is in our sentence
  • 12:55: um and just to give you a little more
  • 12:58: context
  • 12:59: into what's going on in the background
  • 13:01: there
  • 13:03: if you look at what my dicks dot items
  • 13:07: is what we actually have in here
  • 13:11: is a list of tuples basically
  • 13:14: um and so what's actually coming
  • 13:18: if you loop here you're actually
  • 13:21: getting for items you're actually
  • 13:23: getting a tuple
  • 13:24: um as that value
  • 13:28: and a nice thing you can do in python is
  • 13:31: you can
  • 13:32: spread out a tuple if tuple has two
  • 13:35: items you can split it into two
  • 13:36: variables
  • 13:37: and that's exactly what we're doing here
  • 13:39: so we're taking this tuple
  • 13:41: of the key and the value and we're
  • 13:43: splitting it into separate variables
  • 13:46: by using this nice little shortcut um
  • 13:49: so that's just a little background to
  • 13:50: understand where the syntax
  • 13:52: comes from but you don't really need to
  • 13:55: worry about this
  • 13:56: just do this you can just copy this and
  • 13:58: work from this
  • 13:59: uh whenever you want to loop through a
  • 14:02: dictionary
  • 14:04: so that's the uh
  • 14:08: more more advanced than we touched on
  • 14:11: originally usage of
  • 14:12: dictionaries still basic but now kind of
  • 14:16: the full basics
  • 14:17: of dictionaries and we're going to come
  • 14:20: back next time to talk about
  • 14:22: list comprehensions in python so thanks
  • 14:25: for listening
  • 14:26: and see you next time
  • 14:30: you

Python List Comprehensions - Convenient List Building


Notes

  • List comprehensions are an example of “syntactic sugar,” or a feature of a programming language which is not necessary but makes things easier (makes the programming experience “sweeter”)

  • They allow us to write simple loops made to create lists with only a single line of code

Transcript

  • 00:03: hey everyone
  • 00:04: nick diabetes here teaching you
  • 00:05: financial modeling and today
  • 00:07: we're going to be talking about list
  • 00:09: comprehensions
  • 00:10: in python and this is part of our
  • 00:14: lecture series
  • 00:15: on exploring the parameter space and
  • 00:18: this section of the lecture series is
  • 00:21: covering some additional python basics
  • 00:23: that we need
  • 00:24: to be able to go implement sensitivity
  • 00:26: analysis
  • 00:27: in python so
  • 00:31: we um are picking up here
  • 00:34: in the lecture from how to do list
  • 00:38: comprehensions
  • 00:39: in python in the extra python basics
  • 00:41: section
  • 00:43: and so list comprehensions
  • 00:47: are not necessary at all
  • 00:50: they're something that we call syntactic
  • 00:54: sugar which means that it's something
  • 00:58: which makes things easier for you
  • 01:01: though it's not strictly necessary
  • 01:04: it's a feature of the language that
  • 01:06: makes writing the code
  • 01:08: sweeter
  • 01:11: even though it is completely possible
  • 01:14: to do it in a different way already it
  • 01:16: didn't actually extend the functionality
  • 01:18: of the language
  • 01:20: it just makes it easier to
  • 01:23: do certain things
  • 01:26: so what list comprehension basically
  • 01:29: does
  • 01:30: is it lets you do a for loop
  • 01:33: and create a list all in a single line
  • 01:37: so we've seen many times already in the
  • 01:41: course
  • 01:41: and it's a very very common pattern in
  • 01:45: programming to go through a loop
  • 01:48: and for each one of the loop you're
  • 01:51: going to
  • 01:52: [Music]
  • 01:53: do some kind of processing and create
  • 01:55: some kind of output
  • 01:57: and then put that output into the list
  • 01:59: and you're going to have one output into
  • 02:01: the list
  • 02:02: for each run of the loop
  • 02:05: um this comes up all the time
  • 02:09: and so you can see in the first block
  • 02:12: here
  • 02:12: how we know to do that right now and
  • 02:15: that's you first
  • 02:16: set up your output list before you start
  • 02:18: the loop
  • 02:19: then you have your loop and then within
  • 02:22: your loop
  • 02:23: you append to that list
  • 02:26: that you finally have all those results
  • 02:30: in a list so list comprehension
  • 02:34: lets you take these three lines
  • 02:37: and put it into one single line
  • 02:40: so here is the same exact loop with
  • 02:44: list comprehension and
  • 02:47: you can see that actually the loop
  • 02:49: statement itself
  • 02:50: four iron range five that comes over 100
  • 02:53: percent the same
  • 02:55: into the list comprehension and
  • 02:58: when you look at this the one other part
  • 03:00: that actually matters in here that's not
  • 03:02: just
  • 03:02: boilerplate of setting this all up is
  • 03:05: the actual computation part
  • 03:07: we're adding 10 to whatever the input
  • 03:11: value is
  • 03:12: and so that's the other part which comes
  • 03:14: over
  • 03:15: into the list comprehension so it's
  • 03:17: really minimized it down to
  • 03:19: just the things that we care about and
  • 03:21: not having to
  • 03:23: write all this other structure
  • 03:27: so it's whatever your output calculation
  • 03:30: is and then your loop statement
  • 03:33: and you just wrap that in brackets and
  • 03:36: that's a list comprehension
  • 03:38: and i think this is nice and readable as
  • 03:40: well this is just saying
  • 03:42: you know add 10 to each one of the
  • 03:46: inputs and the range up to five
  • 03:49: and make that into a list so i think it
  • 03:52: increases the readability on top of just
  • 03:54: making it easier and faster to write the
  • 03:56: code
  • 03:57: so a lot of advantages here with list
  • 04:00: comprehension
  • 04:01: if this feels overwhelming to you don't
  • 04:04: worry about it you can still write these
  • 04:06: loops just fine
  • 04:07: your code is going to function exactly
  • 04:09: the same
  • 04:10: it's just in my opinion a lot easier to
  • 04:13: write with
  • 04:14: list comprehensions than doing this
  • 04:16: full-on pattern
  • 04:18: now you don't want to abuse list
  • 04:20: comprehensions if you've got a
  • 04:22: complicated loop
  • 04:23: you probably don't want to make that
  • 04:24: into a list comprehension
  • 04:26: it's just going to become a really
  • 04:28: really complicated one-line statement
  • 04:31: but when you're doing simple things then
  • 04:33: a list comprehension
  • 04:34: makes a lot of sense
  • 04:37: so now let's go over and look at some
  • 04:41: examples of this and apply it in the
  • 04:43: jupiter notebook
  • 04:47: so still working on this additional
  • 04:49: python basics
  • 04:51: dictionaries list comprehensions
  • 04:52: notebook now in the list comprehensions
  • 04:55: section so first here i'm just
  • 04:59: showing the standard way that we've
  • 05:01: learned so far
  • 05:03: we've got an inputs list we want to add
  • 05:05: 5
  • 05:06: to each of those inputs that's our
  • 05:07: calculation the calculation
  • 05:09: could be absolutely anything that you
  • 05:11: want it could be running your entire
  • 05:12: model
  • 05:13: or whatever here we're just adding five
  • 05:16: and so we get
  • 05:17: five added to each of these inputs as
  • 05:19: the output
  • 05:21: and again that's setting up our empty
  • 05:23: list doing the loop statement
  • 05:25: doing the calculation appending that
  • 05:27: result and then we have all that
  • 05:30: and now it becomes one line uh just
  • 05:33: adding five
  • 05:34: to each of the inputs for each of the
  • 05:36: inputs
  • 05:37: and we're done so much easier
  • 05:40: in my opinion um
  • 05:45: and like i said uh
  • 05:49: this is a really trivial example here
  • 05:51: we're just adding five
  • 05:52: it was really simple but you can do
  • 05:55: anything you want
  • 05:56: with the logic
  • 06:00: and if you use functions you
  • 06:03: can really do a whole lot here because
  • 06:06: you can put all your complex logic
  • 06:08: into a function and then
  • 06:11: what the loop itself becomes simple and
  • 06:14: so you can condense the loop
  • 06:16: into a list comprehension
  • 06:19: without the function this would be a lot
  • 06:21: of logic to try and cram
  • 06:23: into a list comprehension but with the
  • 06:26: function
  • 06:26: it becomes very simple so all this
  • 06:29: function
  • 06:30: is doing is uh taking a numeric value
  • 06:34: if it's less than two it returns low uh
  • 06:37: if it's between two and three it returns
  • 06:40: mid
  • 06:41: and otherwise it returns high
  • 06:45: so with the original way that we've
  • 06:48: learned
  • 06:49: the list building pattern um
  • 06:53: it basically looks the same as
  • 06:56: the last loop um but instead of adding
  • 07:00: five now we're calling
  • 07:01: the function here and we're passing in
  • 07:04: the input and getting the output as a
  • 07:05: result of that and appending that in the
  • 07:07: list
  • 07:08: so for our inputs um
  • 07:11: which were one two three one is low two
  • 07:14: is mid and 3 is high
  • 07:19: and with list comprehension we can still
  • 07:21: make this into a single
  • 07:23: line so
  • 07:26: you can really do very powerful things
  • 07:28: if you take
  • 07:30: your complicated logic and you wrap it
  • 07:32: in a function
  • 07:34: then doing a loop with that function
  • 07:37: across a bunch of different inputs
  • 07:38: collecting all those
  • 07:40: results into a list becomes a single
  • 07:44: line now so much easier than before
  • 07:49: um and so that's the basics of list
  • 07:52: comprehensions and the majority of the
  • 07:54: time
  • 07:55: that's all you'll ever need there are
  • 07:58: more advanced ways to use them as well
  • 08:01: let's just kind of
  • 08:03: quickly go over those
  • 08:06: so we can do conditionals
  • 08:11: in the list comprehensions as well
  • 08:14: and that's conditional around whether to
  • 08:17: append whether to add the output to the
  • 08:19: final list
  • 08:21: so here again we're looking at one two
  • 08:23: three
  • 08:24: and if the inputs less than three then
  • 08:26: we're going to add it to the list
  • 08:27: and so we get one and two out of that
  • 08:30: and then looking at the list
  • 08:32: comprehension so
  • 08:34: you know here's the basic uh list
  • 08:36: comprehension which gets us
  • 08:38: just the original inputs back and then
  • 08:40: we add
  • 08:41: our condition onto that and
  • 08:44: it's going to give us only the
  • 08:48: inputs which satisfy the condition in
  • 08:51: the final
  • 08:52: resulting list
  • 08:55: and you can do those same calculations
  • 08:57: on the inputs as well
  • 08:59: so here it it kept the one and the two
  • 09:03: but now it's adding five to it as well
  • 09:07: um so that's conditional for whether to
  • 09:10: include the value in the resulting list
  • 09:11: you can also do a conditional
  • 09:13: on the value itself so
  • 09:16: here is our long form loop where
  • 09:20: if the input's less than three we're
  • 09:21: gonna append it and otherwise it's too
  • 09:24: high
  • 09:24: we're gonna append too high to the list
  • 09:27: um and so here we run that
  • 09:32: and we're going to get uh one two and
  • 09:35: two high because three
  • 09:36: is not less than three and we can do all
  • 09:40: this in one line with a list
  • 09:41: comprehension
  • 09:43: so when you're doing a conditional on
  • 09:45: the
  • 09:46: value then that conditional comes
  • 09:49: onto the left side of the list
  • 09:52: comprehension whereas here
  • 09:54: or conditional on whether to keep the
  • 09:56: value was on the right side
  • 09:57: of the list comprehension
  • 10:00: and so here we're going to do um
  • 10:06: we're going to do the input if the input
  • 10:08: is less than 3
  • 10:10: otherwise we're going to get too high so
  • 10:13: this is an inline
  • 10:14: if else statement
  • 10:18: if it's less than three otherwise too
  • 10:20: high
  • 10:21: and we're doing that for each of the
  • 10:23: inputs and so that's
  • 10:25: how we get the same result as this
  • 10:27: longer form loop
  • 10:29: um and you can combine all three of
  • 10:32: those elements if you'd like
  • 10:34: so by the time you're doing that it's
  • 10:37: starting to get a little bit hard to
  • 10:38: read honestly so
  • 10:40: um it may at this point be better just
  • 10:42: to go back to a long form loop now that
  • 10:44: you've got multiple things going on
  • 10:47: but you can combine all these elements
  • 10:49: if you want to
  • 10:52: um and then just a quick mention for
  • 10:56: there are comprehensions for other data
  • 10:58: types and pythons
  • 11:00: uh we haven't even talked about sets and
  • 11:03: generators as data types
  • 11:05: we're not really gonna go there in this
  • 11:07: course they're not really necessary for
  • 11:09: what we're doing
  • 11:10: but for those and as well as
  • 11:12: dictionaries you can do dictionary
  • 11:13: comprehensions as well
  • 11:16: but that's a little bit more advanced
  • 11:19: you can check out
  • 11:20: uh this resource for more information on
  • 11:22: that if you're
  • 11:23: interested
  • 11:26: and then that wraps up the material on
  • 11:29: list comprehension
  • 11:31: and there are lab exercises on this
  • 11:34: as well and i forgot to mention at the
  • 11:36: end of the dictionaries video
  • 11:38: that there are also lab exercises for
  • 11:41: that
  • 11:42: and the same notebook so just go through
  • 11:44: that whole
  • 11:45: um jupiter notebook the dicks and list
  • 11:47: comprehension
  • 11:48: lab and just complete all the exercises
  • 11:51: in both the dictionaries and list
  • 11:53: comprehensions sections
  • 11:55: for the lab exercise so we're going to
  • 11:59: come back in the next video
  • 12:00: to talk about the python import system
  • 12:03: and how to
  • 12:04: install packages so thanks for listening
  • 12:07: and see you next time

Python Imports and Installing Packages


Notes

  • Import can be used for third-party and built-in packages, but also for your own code offloaded into separate files

  • I had you install Anaconda to get Python because it includes most of the Python packages we would want to use pre-installed. So we haven’t had to install any package up to now

  • With more than 250k packages out there and only about 200 installed in Anaconda, the time will come when you need to install something

  • It will even happen in this course because we will use some packages I have created

Transcript

  • 00:03: hey everyone
  • 00:03: nick dear burst here teaching you
  • 00:05: financial modeling today we're going to
  • 00:07: be talking about
  • 00:09: imports in python and installing python
  • 00:12: packages this is part of our lecture
  • 00:15: segment on exploring the parameter space
  • 00:17: with sensitivity analysis and the sub
  • 00:20: segment
  • 00:21: on additional python basics that we need
  • 00:24: to be able to
  • 00:25: attack sensitivity analysis in python
  • 00:30: so we're here in this extra python basic
  • 00:34: section
  • 00:34: of the sensitivity analysis lecture
  • 00:38: starting here with understanding imports
  • 00:41: in python
  • 00:43: so we've already used the import
  • 00:45: statement a little bit we've imported
  • 00:47: numpy we've
  • 00:48: imported pandas um
  • 00:51: and what that import is doing
  • 00:54: it is loading this external package
  • 00:58: which we can then use and
  • 01:02: that makes it sound and seem like these
  • 01:05: third-party packages
  • 01:06: numpy pandas and all the others that we
  • 01:09: can potentially use
  • 01:11: makes them sound like there's some kind
  • 01:12: of uh
  • 01:14: you know very structured special things
  • 01:17: um that you need to set it up in some
  • 01:19: really special way
  • 01:21: um but it's quite a bit simpler than
  • 01:23: that really it's
  • 01:24: they're just sets of python code and
  • 01:27: just a bunch of files
  • 01:29: and when you import it it's just loading
  • 01:31: the python code in those files
  • 01:34: into your notebook or wherever you're
  • 01:36: working
  • 01:38: and so just like the authors of numpy
  • 01:42: and pandas have written
  • 01:44: all these python files with functions
  • 01:47: and classes that you can use
  • 01:49: you can also write your own code and put
  • 01:52: them in python files
  • 01:54: and import your code from python files
  • 01:57: in the same exact way so the way that
  • 02:00: that works
  • 02:01: is you say import something
  • 02:04: what happens is python searches
  • 02:08: on your computer in certain spots to see
  • 02:11: if it can find
  • 02:12: uh something dot p y dot p
  • 02:15: y is a python file the plain text file
  • 02:19: with a dot py extension
  • 02:23: and so it's first it has a certain order
  • 02:26: that it goes and searches
  • 02:27: your computer in and the first place
  • 02:30: it's going to look
  • 02:31: is in your current folder
  • 02:34: so if you have right next to your
  • 02:37: jupiter notebook you have a something.py
  • 02:40: defined there and it has some functions
  • 02:42: there are classes in there
  • 02:44: when you import something it's going to
  • 02:46: bring in the code
  • 02:47: from that something.py
  • 02:51: file and then if
  • 02:54: it can't find any something there then
  • 02:56: it's going to go on
  • 02:57: and continue looking through the
  • 02:59: different locations that it has set up
  • 03:01: to search
  • 03:02: and beyond your current directory the
  • 03:05: main other locations are going to be
  • 03:08: a certain spot where it installs all
  • 03:10: your packages
  • 03:11: um and there's often a couple different
  • 03:15: spots that it can put the packages so
  • 03:16: those will be multiple different spots
  • 03:18: that it looks in
  • 03:19: and order so when you install a package
  • 03:22: it'll put it in this special spot
  • 03:25: so when you import then if you don't
  • 03:28: have a file defined with that name it'll
  • 03:30: go look
  • 03:30: where your packages are and then be able
  • 03:32: to import it from
  • 03:34: there so that is something to be
  • 03:37: mindful of because it does always look
  • 03:40: in the current directory first
  • 03:42: if you actually created a numpy.py
  • 03:45: file in the current directory then when
  • 03:48: you import numpy
  • 03:50: you're no longer getting the numpy
  • 03:51: package you're getting your
  • 03:53: numpy.py file and so that is something
  • 03:57: to be
  • 03:57: careful about that you don't want to
  • 04:00: define
  • 04:01: python files with the same names as
  • 04:04: packages that you might be meaning to
  • 04:06: import it could definitely cause a
  • 04:08: conflict
  • 04:12: so you can write your own custom code
  • 04:16: and you can then import it
  • 04:19: into your notebook later on so
  • 04:22: if you have any functions or classes
  • 04:24: that you create as
  • 04:26: as you're building out your models and
  • 04:28: you say hey that's actually a pretty
  • 04:30: generally useful thing that i just built
  • 04:32: i'm probably going to want to use that
  • 04:34: in my next model not just this model
  • 04:37: then you can take that function or take
  • 04:39: that class and you can offload it
  • 04:41: into the separate python file and then
  • 04:45: no matter how many notebooks you have
  • 04:46: you can just keep importing that same
  • 04:49: function or class in your notebook and
  • 04:52: then
  • 04:52: say that you wanted to add a new feature
  • 04:55: to that function
  • 04:56: it now gets a new default a new argument
  • 04:59: um and it can do additional things now
  • 05:03: um you make that change in one spot in
  • 05:06: your python file and that will carry
  • 05:08: through to all your notebooks that are
  • 05:10: using it
  • 05:11: so it goes even further in this code
  • 05:13: reuse direction
  • 05:15: to keep from having to repeat yourself
  • 05:19: so what happens
  • 05:23: when you import a file is it executes
  • 05:27: everything in that file so
  • 05:30: you can think of you know like executing
  • 05:33: a code cell
  • 05:34: in jupyter you can think of your python
  • 05:36: file as being the contents of one code
  • 05:39: cell
  • 05:40: and importing that file is the same as
  • 05:43: running
  • 05:43: that code cell so generally
  • 05:48: all that you want to have in there for
  • 05:50: the most part is going to be function
  • 05:51: and class
  • 05:52: definitions you don't want to generally
  • 05:54: be doing anything
  • 05:55: uh you just want the setup the
  • 05:57: definitions so that you
  • 05:59: can do something with those functions
  • 06:02: and classes
  • 06:03: um because uh
  • 06:06: weird things can happen if you you know
  • 06:09: are importing something multiple times
  • 06:11: and you're expecting it to do something
  • 06:13: python only imports it once and then
  • 06:16: it caches it so it doesn't have to keep
  • 06:17: importing it so
  • 06:19: you can run into unexpected behavior if
  • 06:21: you're trying to run logic
  • 06:23: when you import instead just have
  • 06:25: functions and classes
  • 06:27: for the most part and then
  • 06:31: then when you import it you'll have all
  • 06:33: those defines so that you can use them
  • 06:36: in your notebook
  • 06:39: and you know it's a little bit more
  • 06:41: advanced to go to the structure than
  • 06:42: just having everything all in one
  • 06:44: jupiter notebook of course
  • 06:46: but it is a more maintainable structure
  • 06:49: as you go to build larger and more
  • 06:52: complex models and as you go to build
  • 06:54: uh more and more different models where
  • 06:57: you might want to share
  • 06:58: code across your different models
  • 07:03: so it's a good thing to learn about
  • 07:07: you might not have to go there in this
  • 07:09: class
  • 07:10: [Music]
  • 07:12: as we get further in the course the
  • 07:14: fourth project in the course is the dcf
  • 07:16: model and the third project
  • 07:18: is a wac model which the whack is part
  • 07:21: of the dcf model and so i have seen
  • 07:24: students
  • 07:24: take their functions from the wac model
  • 07:26: and refactor them out
  • 07:28: into these separate python files and
  • 07:30: then use them
  • 07:31: in their dcf model so they don't have to
  • 07:33: like rewrite all the whack parts of the
  • 07:35: model so that's
  • 07:36: definitely a good application for this
  • 07:38: within this class
  • 07:40: but i'm mainly mentioning it here just
  • 07:42: so one that you understand
  • 07:44: what imports are and that they're not
  • 07:45: anything really special it's just
  • 07:48: running the contents of some file which
  • 07:50: has python code in it
  • 07:52: um and that's so also you can know about
  • 07:54: this as
  • 07:55: a way to grow into as you start to go
  • 07:58: and continue to apply this stuff
  • 08:00: out of the course and maybe you find
  • 08:02: your jupiter notebooks to be getting
  • 08:03: really long and unwieldy
  • 08:07: or maybe it's even just that you have a
  • 08:08: lot of code in your jupiter notebook and
  • 08:10: you really want it more focused on just
  • 08:12: the results
  • 08:13: and not showing so much code to the
  • 08:14: reader of the model
  • 08:16: for all these different use cases going
  • 08:18: to offloading your code
  • 08:20: into separate python files is a good
  • 08:24: solution
  • 08:27: so um as we mentioned
  • 08:31: these these packages that you can
  • 08:32: install all they are are just plain
  • 08:34: python files and defined functions and
  • 08:37: classes
  • 08:37: just like we've already learned how to
  • 08:39: do
  • 08:42: and going along with this course i had
  • 08:45: you install anaconda
  • 08:47: for your python distribution and that's
  • 08:49: because
  • 08:50: it comes with around 200 packages
  • 08:53: already pre-installed
  • 08:54: and it's most of the common ones that we
  • 08:56: would want to use so most of the time
  • 08:58: you don't need to install the package
  • 09:00: you already have it installed from
  • 09:01: anaconda
  • 09:03: but there's around 200 in anaconda and
  • 09:05: there's around
  • 09:07: 000 packages out there that you could
  • 09:10: potentially use
  • 09:11: huge number of packages and this is one
  • 09:14: of those things that makes python so
  • 09:16: great
  • 09:16: there's a package out there for
  • 09:18: everything that you could want to do
  • 09:19: essentially um it'll make your life so
  • 09:22: much easier just finding the correct
  • 09:24: package to work with instead of having
  • 09:26: to
  • 09:26: recreate everything yourself so
  • 09:31: you find this package you googled about
  • 09:33: it you landed on some github page or
  • 09:36: pipi page or something
  • 09:38: explaining what this package is now you
  • 09:40: say i want to use this
  • 09:41: what's the next step so um
  • 09:45: the way to do this is with pip pip
  • 09:48: is the python package installer um
  • 09:52: and it's very simple it's just pip
  • 09:55: install and then the name of the package
  • 09:57: and that
  • 09:57: installs the package for you now where
  • 10:00: do you run this
  • 10:01: that happens in the terminal so that
  • 10:04: anaconda prompt
  • 10:05: would be a good place to run that uh but
  • 10:08: jupiter does
  • 10:09: actually have a shortcut where you can
  • 10:11: run terminal commands
  • 10:13: instead of just python code and you do
  • 10:16: that by just putting an exclamation mark
  • 10:18: before whatever you're running in the
  • 10:20: cell and that says
  • 10:22: hey jupiter i'm trying to run something
  • 10:23: in a terminal not
  • 10:26: in as python code um
  • 10:29: and so that would be exclamation park
  • 10:32: pip install
  • 10:33: and then the name of the package to
  • 10:36: install it from
  • 10:37: jupiter and you only need to install a
  • 10:40: package once
  • 10:41: once you install it you have it on your
  • 10:43: computer for as long as you have python
  • 10:45: installed
  • 10:47: the only time that you might want to run
  • 10:49: this again is
  • 10:51: to upgrade the package you can add an
  • 10:53: upgrade flag into that
  • 10:55: we'll talk about that a little bit later
  • 10:56: in the course when we need to use that
  • 10:59: but generally install it once you have
  • 11:01: it and you can just use it you just have
  • 11:03: to import it in each session
  • 11:04: but then you can keep using it just like
  • 11:06: we have been using numpy or pandas or
  • 11:08: anything you have
  • 11:09: already installed
  • 11:12: um so let's look at a quick
  • 11:15: example of that so let's jump over to
  • 11:18: the jupiter
  • 11:19: notebook and this is the same one with
  • 11:22: all the other additional basics
  • 11:23: dictionaries list comprehensions
  • 11:25: here we're um imports and installing
  • 11:29: packages
  • 11:31: and so um the reason that we're getting
  • 11:34: to installing packages now
  • 11:36: in the course is because
  • 11:39: uh we're going to go and do sensitivity
  • 11:42: analysis
  • 11:43: with a purpose-built package which is
  • 11:45: made for that purpose
  • 11:48: and the reason this is not an anaconda
  • 11:50: is because
  • 11:51: i actually wrote this package um
  • 11:54: and it doesn't hasn't gotten popular
  • 11:57: enough yet to make its way into anaconda
  • 11:59: it's got a long way to go
  • 12:00: until it gets there maybe someday uh but
  • 12:02: until then
  • 12:03: we're going to have to just install it
  • 12:05: manually
  • 12:06: uh to be able to use it so
  • 12:10: that's uh what we have here
  • 12:13: is pip install sensitivity is the name
  • 12:16: of the package
  • 12:17: i was amazed that this name was
  • 12:19: available for me to take
  • 12:21: uh it was the perfect name for the
  • 12:23: package
  • 12:24: um and all you do
  • 12:27: is just exclamation mark pip install and
  • 12:30: then the name of the package
  • 12:31: so let's go ahead and try to run that
  • 12:34: and when you do these exclamation mark
  • 12:35: commands
  • 12:36: uh it does it looks like it's doing
  • 12:38: nothing for a long time and then it
  • 12:40: spits out the result
  • 12:41: all at once when it's done you don't see
  • 12:43: it like stream
  • 12:44: the result as you go if you're running
  • 12:46: in the terminal you would see all this
  • 12:48: stuff happen line by line and jupiter
  • 12:51: just spits it all out at the end
  • 12:53: um and so you're going to see probably a
  • 12:56: lot of requirement already satisfied
  • 12:59: that's fine just ignore that just scroll
  • 13:01: all the way down to the end
  • 13:04: and here it looks like i even uh
  • 13:08: i actually already had the package
  • 13:09: installed and so the first one
  • 13:12: up here was requirement already
  • 13:13: satisfied sensitivity
  • 13:16: um but if you uh
  • 13:19: pick some other package that you don't
  • 13:21: have
  • 13:23: and you should see this for sensitivity
  • 13:25: when you go to install it
  • 13:28: you're going to see at the end
  • 13:30: successfully installed
  • 13:32: and then the name of the package and
  • 13:34: then the version of the package
  • 13:36: and then you can start using that
  • 13:38: package as soon as you want
  • 13:40: um so with sensitivity i can go ahead
  • 13:43: and import sensitivity
  • 13:45: and then i would be able to use the
  • 13:47: package
  • 13:48: so make sure to run that with
  • 13:50: sensitivity
  • 13:51: not this other package and then go ahead
  • 13:54: and try the import
  • 13:55: you should see it just execute and the
  • 13:57: number change here
  • 13:58: and nothing else happen and that means
  • 14:02: that you have
  • 14:03: installed it correctly
  • 14:07: so that's a quick overview on
  • 14:10: imports in python and installing
  • 14:11: packages we're going to come back
  • 14:14: next time to start implementing
  • 14:16: sensitivity analysis
  • 14:18: in python using the sensitivity
  • 14:21: package so thanks for listening and see
  • 14:24: you next time

Introduction to Sensitivity Analysis in Python


Notes

  • With sensitivity analysis in Python, we can get DataFrames looking similar to the tables with conditional formatting we saw in Excel

  • We can also output hexbin plots which show the same graphically

  • Unlike Excel, we are not limited to analyzing two inputs at once, though it still makes sense to visualize the output in pairs of inputs

  • The basic setup with pure Python is fairly simple: use loops over the possible input values and call your model function in the loop, saving the output associated with the inputs. Changing multiple inputs at once means multiple nested loops.

  • Then you have to add the conditional formatting or hexbin plot

  • The sensitivity package reduces this all to a couple lines of code, regardless of how many inputs you want to look at

Transcript

  • 00:03: hey everyone
  • 00:04: nick dear bertis here teaching you
  • 00:05: financial modeling and today
  • 00:07: i'm going to be talking about
  • 00:08: sensitivity analysis
  • 00:10: in python this is part of our lecture on
  • 00:13: exploring the parameter space
  • 00:17: so we left off last time we had gone
  • 00:20: through some
  • 00:21: additional python basics that we were
  • 00:23: going to need to be able to carry out
  • 00:26: sensitivity analysis in python i was on
  • 00:30: list comprehensions dictionaries
  • 00:32: installing packages
  • 00:34: and now we're going to go ahead and
  • 00:36: actually implement sensitivity analysis
  • 00:40: so for sensitivity analysis in python
  • 00:43: we're looking at
  • 00:45: two main kinds of ways that we can
  • 00:47: visualize the output
  • 00:49: so the new one which is different from
  • 00:52: what we're able to do in excel
  • 00:53: is this hex bin plot that you see here
  • 00:56: and this is analogous to the conditional
  • 00:59: formatting
  • 01:00: on a table in excel only in graphical
  • 01:03: form
  • 01:04: so here uh it makes the regions
  • 01:08: of this two-dimensional area
  • 01:10: corresponding to the value of the
  • 01:12: outcome
  • 01:13: so you have your two different inputs on
  • 01:15: the two different axes
  • 01:16: and then your outcome is the color
  • 01:19: um and so here high years to retirement
  • 01:22: is red
  • 01:24: low years to retirement is green and
  • 01:25: getting darker green
  • 01:27: the more that it decreases so we can see
  • 01:30: clearly from this graph that
  • 01:33: as savings rate increases and as the
  • 01:35: person gets promoted more often
  • 01:37: then they're going to be able to retire
  • 01:38: sooner
  • 01:42: the other main output that we have is
  • 01:45: exactly what we have in excel which is a
  • 01:47: table with conditional formatting
  • 01:49: applied to it
  • 01:51: so this is showing the same information
  • 01:54: as the hexton plot only with each of the
  • 01:58: numbers
  • 01:58: in there as well uh so it's mostly a
  • 02:01: personal preference
  • 02:03: uh with which one you want to go with
  • 02:05: but these are the two main ways to
  • 02:08: look at the two inputs at once with
  • 02:10: sensitivity analysis and
  • 02:12: visualize that of course with one way
  • 02:14: then you can just plot that
  • 02:16: with a typical line plot
  • 02:20: so the process to carry out sensitivity
  • 02:23: analysis and python
  • 02:26: if you don't want to use any packages
  • 02:27: you just want to use pure python
  • 02:30: then it's basically you do a loop
  • 02:33: over each input that you want to
  • 02:36: examine so you might say i want to look
  • 02:39: at
  • 02:40: uh 30 40 50 savings rates
  • 02:44: and so it'd be like four save rate in
  • 02:47: 30 40 50 in a list um
  • 02:50: and then you run the model with uh each
  • 02:53: of those inputs
  • 02:54: passed in collect the results keep them
  • 02:57: associated
  • 02:58: with which inputs are related to those
  • 03:00: outputs
  • 03:01: and then put it all together to
  • 03:03: visualize
  • 03:06: um so then if you have more than one
  • 03:09: input
  • 03:10: you just go ahead and do additional
  • 03:12: loops and nest those loops
  • 03:14: within each other so for save rate and
  • 03:17: savings rates for
  • 03:18: uh number of promotions in
  • 03:21: uh you know two three four five
  • 03:25: something like that nested under each
  • 03:28: other
  • 03:29: and then nested under all the loops is
  • 03:32: where you actually run your model
  • 03:34: with each of those inputs again collect
  • 03:36: the outputs associate them with the
  • 03:38: inputs and then
  • 03:39: visualize it
  • 03:42: um then you know you start to look up
  • 03:45: more
  • 03:46: than two or three inputs at once and all
  • 03:47: of a sudden you're nested nested nested
  • 03:49: more and more and all these loops
  • 03:51: um and that can be pretty messy um
  • 03:55: so there is a way to resolve that there
  • 03:57: is a
  • 03:58: python has a built-in package called
  • 04:01: intertools
  • 04:02: tools for iteration which goes a little
  • 04:05: bit beyond
  • 04:06: just what the base language does
  • 04:10: and one of those basically allows you to
  • 04:13: do
  • 04:13: like a nested for loop but with only a
  • 04:16: single line of code so that's what this
  • 04:18: editor tools product is
  • 04:20: um and so you can use that to then
  • 04:23: collapse however many loops you would
  • 04:25: have onto one line um
  • 04:28: but then it's a little more difficult to
  • 04:30: understand what's going on there
  • 04:34: so i would if you're sticking with
  • 04:37: this approach for sensitivity analysis
  • 04:39: and we'll talk in a minute
  • 04:41: about a package we can use that makes
  • 04:43: all this much easier
  • 04:45: but with the pure python approach i
  • 04:47: would say two or three inputs at once
  • 04:49: just write out in the individual loops
  • 04:52: as you get more than that
  • 04:53: then you should consider using
  • 04:55: intertools.product
  • 04:57: as a replacement for the nested loops
  • 05:02: so here's an example of what that
  • 05:05: looks like the code that can make
  • 05:07: sensitivity analysis happen
  • 05:10: with just base python so
  • 05:13: here we're saying that we have a
  • 05:15: function called model and that's
  • 05:17: the function which runs our entire model
  • 05:20: so it takes the inputs
  • 05:21: and then it spits out the result of
  • 05:24: running the model
  • 05:25: so that can be you know calculating the
  • 05:27: years to retirement from the savings
  • 05:29: rate investment rate etc
  • 05:30: or any other model that you could
  • 05:32: possibly imagine
  • 05:35: as long as you can you can always
  • 05:37: represent the model as a function
  • 05:39: uh converting inputs to outputs and so
  • 05:42: if you do that
  • 05:43: then you can fit it into this structure
  • 05:44: just fine
  • 05:47: so we have two different inputs we're
  • 05:49: trying to examine here input one input
  • 05:51: two
  • 05:52: and for each of those we have two
  • 05:54: different values of
  • 05:56: the inputs that we wanna look at so for
  • 05:57: input one we wanna look at one and two
  • 06:00: input two we wanna look at four and five
  • 06:03: we set up a list to store all the
  • 06:05: results and then here we have the nested
  • 06:08: loop
  • 06:08: so we're going through each of these
  • 06:10: input lists getting an input
  • 06:12: out of each one and then within all the
  • 06:15: loops
  • 06:15: calling the model with each of the
  • 06:18: inputs then we get the result
  • 06:21: from the model and we associate the
  • 06:24: uh inputs with the output in the result
  • 06:28: here by creating a list of tuples
  • 06:31: where each tuple has everything grouped
  • 06:33: together
  • 06:35: then we create a data frame from that
  • 06:38: list of tuples
  • 06:39: and we can see the results of the
  • 06:41: sensitivity analysis here
  • 06:43: in the data frame of course this is not
  • 06:46: doing any visualization yet
  • 06:48: that part is going to be added on after
  • 06:50: this
  • 06:54: um so this
  • 06:58: was kind of the process which is out
  • 07:02: there
  • 07:02: this is what people are doing right now
  • 07:04: in general
  • 07:06: um and i thought that there should be a
  • 07:08: good package
  • 07:09: which makes this process easier
  • 07:12: especially
  • 07:13: you know not only doing this but then
  • 07:14: taking the result and visualizing it as
  • 07:16: well
  • 07:20: but i really couldn't find any tool out
  • 07:23: there which
  • 07:24: was built for this purpose
  • 07:27: i think a lot of that reason is because
  • 07:31: certainly a lot of financial
  • 07:34: institutions
  • 07:35: are already using python and they're
  • 07:37: building models out in python
  • 07:40: but for the most part these institutions
  • 07:44: are not going to be open sourcing their
  • 07:47: code they're just going to be keeping it
  • 07:49: within the company as their own
  • 07:51: intellectual property
  • 07:53: uh they want to have the rights to it
  • 07:55: and not anyone else
  • 07:56: and so i think there have been a lot of
  • 07:59: these tools created
  • 08:02: but they're just not out there for
  • 08:03: anyone to use they're within these
  • 08:05: banks and financial institutions that
  • 08:08: keep a close
  • 08:09: guard on the tools that they've
  • 08:10: developed
  • 08:13: so it really takes uh you know someone
  • 08:17: who both has the knowledge and uh
  • 08:21: is willing to be able to contribute
  • 08:24: to create an open source package without
  • 08:27: a lot
  • 08:28: of immediate benefit from it so
  • 08:31: a professor teaching a financial
  • 08:33: modeling course
  • 08:34: is kind of the ideal person to do that
  • 08:39: so and you know unlike in excel
  • 08:43: you can write add-ons for excel but
  • 08:45: that's kind of heavy weight
  • 08:47: um and in general people just use excel
  • 08:50: as is
  • 08:52: in python you have the base language but
  • 08:54: then you have all these additional 250
  • 08:57: 000
  • 08:57: packages open source packages which have
  • 08:59: been created to extend the functionality
  • 09:01: of python
  • 09:02: and it's really easy for anyone to
  • 09:05: create a package
  • 09:07: and contribute to the ecosystem create a
  • 09:10: tool that solves a problem
  • 09:12: so that nobody else has to continue
  • 09:14: solving the same problem in their own
  • 09:16: work
  • 09:18: so i went ahead and created the
  • 09:20: sensitivity package
  • 09:22: in python which simplifies this whole
  • 09:25: flow as well as the visualization
  • 09:27: of the results and makes this whole
  • 09:30: thing
  • 09:30: very easy so i took the time
  • 09:34: to kind of solve this once in a general
  • 09:36: way and now everyone in the world
  • 09:38: can benefit from my efforts
  • 09:42: so that is really uh the big power of
  • 09:45: python that anyone in the world can
  • 09:47: contribute and it can make everyone else
  • 09:49: and the world
  • 09:50: more productive so everyone's not
  • 09:53: reinventing the wheel all the time
  • 09:57: so let's look at an example with
  • 10:00: the sensitivity package
  • 10:03: so from sensitivity we're going to
  • 10:05: import sensitivity analyzer that's the
  • 10:08: main class which
  • 10:09: handles all this and then you create a
  • 10:12: dictionary
  • 10:14: where the keys of the dictionaries are
  • 10:15: the names of your
  • 10:17: inputs the arguments to the function
  • 10:19: your model function
  • 10:21: and the values are what
  • 10:24: each individual values of the inputs you
  • 10:28: want to look at
  • 10:28: so one and two for input one 4 and 5 for
  • 10:32: input 2.
  • 10:34: then you pass to sensitivity analyzer
  • 10:37: that dictionary of the inputs
  • 10:40: and your model function and that creates
  • 10:43: an instance of the sensitivity analyzer
  • 10:46: and then the df data frame
  • 10:50: attribute of the sensitivity analyzer
  • 10:52: already has the results
  • 10:54: of doing this nested loop
  • 10:57: and collecting all the results and
  • 10:59: everything that have been done here
  • 11:01: and we'll see in a minute here in the
  • 11:03: example that is also
  • 11:04: similarly easy to visualize all the
  • 11:07: results as well
  • 11:10: so let's go ahead and look at the
  • 11:13: jupiter example
  • 11:14: for all of this
  • 11:18: so the first thing that we're going to
  • 11:19: do is
  • 11:21: install the sensitivity package
  • 11:25: and so in this is something that you
  • 11:27: would run in a terminal but jupiter has
  • 11:29: this shortcut
  • 11:30: uh as mentioned in the video on
  • 11:32: installing packages
  • 11:34: that uh you put the exclamation mark
  • 11:36: that means run it
  • 11:37: in a terminal in the background and then
  • 11:39: we want to do pip install
  • 11:41: sensitivity and i'm going to go ahead
  • 11:44: and add in
  • 11:44: upgrade because i already have it
  • 11:47: installed and i know that i've released
  • 11:49: some updates for the package and i want
  • 11:51: to have all those updates here
  • 11:53: so i'm going to run that and it's going
  • 11:54: to look like it's doing nothing for a
  • 11:56: little bit and then eventually
  • 11:57: it will pop out all the output from the
  • 11:59: result
  • 12:00: as we see now and we can see
  • 12:05: that it successfully installed
  • 12:08: sensitivity
  • 12:10: as of making this video 0.2.5 is the
  • 12:12: current
  • 12:13: version um you may just get
  • 12:16: requirement already satisfied for
  • 12:18: everything if you already have the
  • 12:20: newest version
  • 12:22: um and then if you run that first you
  • 12:25: don't really have to do this step but
  • 12:26: i'm just going to go ahead and
  • 12:27: restart the kernel for good measure
  • 12:31: so now let's uh go through
  • 12:35: what the process looks like kind of the
  • 12:38: original way without the sensitivity
  • 12:39: package
  • 12:40: and then we'll go into how to use the
  • 12:42: sensitivity package
  • 12:45: um so i'm going to import pandas
  • 12:48: that we'll use for collecting the
  • 12:49: results and i'm defining the model
  • 12:52: function so again this can be absolutely
  • 12:55: anything
  • 12:56: it can be your full financial model
  • 12:59: here we're just taking one number to the
  • 13:02: power of another number
  • 13:03: it really doesn't matter what happens
  • 13:05: inside of here that's the beauty of
  • 13:07: using
  • 13:07: functions we can plug whatever
  • 13:11: functionality
  • 13:11: we want into this structure and it works
  • 13:14: exactly the same
  • 13:17: so when i run the model i can get a
  • 13:21: result from it
  • 13:24: so then if we want to look at three
  • 13:26: different values for x1 three different
  • 13:28: values for x2
  • 13:31: then when we look at the nested loop we
  • 13:33: can see
  • 13:35: that we get basically each combination
  • 13:38: each pairwise combination of the inputs
  • 13:43: so we get nine total cases from three
  • 13:47: cases for each
  • 13:50: then we can add in calculating the model
  • 13:53: running the model
  • 13:55: um so that's just calling the model
  • 13:57: function
  • 13:58: on the inputs from the loops
  • 14:01: and then uh getting the result and here
  • 14:04: we're just going to print out the result
  • 14:05: as the next step
  • 14:08: um and then after that let's collect the
  • 14:12: results
  • 14:13: so taking the same loop but now adding
  • 14:16: to it that
  • 14:17: instead of just printing the result i'm
  • 14:19: going to have an output list
  • 14:21: and i'm going to append to that a tuple
  • 14:25: which contains
  • 14:26: each of the inputs as well as the output
  • 14:29: and then we can see that we have each
  • 14:31: pair of inputs
  • 14:32: associated with the output
  • 14:39: then we can put that into a data frame
  • 14:42: so if we give a data frame a list of
  • 14:44: tuples and then the names of the columns
  • 14:46: it's able to create a data frame from
  • 14:48: that
  • 14:52: and then we can add some styling
  • 14:55: about the result so here i'm adding a
  • 14:59: red yellow green color map
  • 15:01: um to the y column the output column
  • 15:04: uh which is now showing the high results
  • 15:08: in green
  • 15:08: and the low results in red
  • 15:13: then the other way to go about it is to
  • 15:15: use a hexman plot
  • 15:17: um so that's also
  • 15:21: off of df.plot just like we had done for
  • 15:24: all the other plots with pandas
  • 15:27: and you give it x and y as your two
  • 15:31: input variables and then
  • 15:35: c that stands for color
  • 15:38: is going to be determined by the outcome
  • 15:40: variable
  • 15:42: and then this grid size says
  • 15:45: going in each direction how many um
  • 15:49: like blocks should there be this is like
  • 15:52: three by three
  • 15:53: blocks in the graph
  • 15:57: again we pass that color map
  • 16:00: red yellow green um
  • 16:03: and then this share x equals false
  • 16:07: makes all the formatting work correctly
  • 16:11: um so that's without the library
  • 16:15: and then with these color maps you can
  • 16:17: do underscore r
  • 16:19: to reverse the color map
  • 16:22: if it turns out that a low number of the
  • 16:25: result is a better thing
  • 16:26: like years to retirement we want to
  • 16:28: retire quicker so there you would want
  • 16:29: the reverse
  • 16:30: red yellow green color map
  • 16:34: but all this is a little bit involved
  • 16:36: it's not super complicated but
  • 16:37: there's a fair number of steps here so
  • 16:40: the sensitivity
  • 16:41: uh package is going to make this all
  • 16:43: easier
  • 16:44: so with the sensitivity package we set
  • 16:47: up that
  • 16:48: dictionary of the inputs and then we
  • 16:51: create
  • 16:52: the sensitivity analyzer
  • 16:55: and we pass to the sensitivity analyzer
  • 16:59: that dictionary as well as the model
  • 17:02: function
  • 17:03: and notice that there's no open close
  • 17:04: parenthesis here
  • 17:06: that's because we're passing the model
  • 17:09: function
  • 17:10: itself and not the result
  • 17:13: of calling the model function as soon as
  • 17:16: you put the parenthesis it's going to
  • 17:17: call it evaluate it return the result
  • 17:20: we want to pass the function itself so
  • 17:23: that sensitivity analyzer can use this
  • 17:25: function
  • 17:26: that we're passing to it
  • 17:30: um so you run sensitivity analyzer and
  • 17:33: you'll see
  • 17:33: this progress far go by here the model
  • 17:36: didn't take any time so it basically
  • 17:38: filled up immediately
  • 17:39: um 450 iterations per second
  • 17:43: um and you'll see a random nine
  • 17:45: iterations in total because we have
  • 17:47: three times three
  • 17:48: uh cases of inputs
  • 17:51: um so you know there's definitely a
  • 17:55: chance that you can
  • 17:56: put too many inputs here and you get a
  • 17:58: really huge number of cases
  • 18:00: and then you'll see this thing just
  • 18:01: taking forever and then you should
  • 18:02: reevaluate how many inputs you want to
  • 18:04: look at
  • 18:08: so then we've run sensitivity analyzer
  • 18:13: now we can get that same exact
  • 18:17: data frame which we had seen before but
  • 18:19: just it's already there
  • 18:21: ready for us we didn't have to go and
  • 18:23: calculate it
  • 18:25: and then for the plots
  • 18:29: we just do sa.plot and then we have that
  • 18:32: hexman plot
  • 18:33: easy as that and we can also get these
  • 18:38: styled data frames
  • 18:40: by doing sa.style dfs
  • 18:44: and that is going to give you the data
  • 18:46: frame
  • 18:47: with um conditional formatting applied
  • 18:50: to it
  • 18:52: and there are options that you can apply
  • 18:55: to
  • 18:56: the sensitivity analyzer as well to
  • 18:57: customize the output
  • 19:00: and so i'm going to do a few different
  • 19:03: things here
  • 19:05: so i'm going to give it some labels so
  • 19:07: that it'll change out x1 x2 for
  • 19:10: whatever names i want to call the inputs
  • 19:14: i'm going to pass grid size of three so
  • 19:16: that it makes larger
  • 19:18: blocks here reverse colors that was like
  • 19:22: adding underscore r
  • 19:23: to the color wrap before it just flips
  • 19:25: the color map
  • 19:27: then you can also pass a custom color
  • 19:29: map
  • 19:31: and if you want to know what are all the
  • 19:36: color maps you can use just google map
  • 19:38: plot lib color maps
  • 19:40: um that will take you to choosing color
  • 19:42: maps in matplotlib
  • 19:45: and you'll see all these different ones
  • 19:47: that you can pick
  • 19:50: so all these names on the left are
  • 19:52: things that you can pass
  • 19:54: to color map to get a different color
  • 19:56: map
  • 19:58: um and then uh
  • 20:01: yep that's all the options so then i'm
  • 20:04: going to run this and then plot and now
  • 20:06: we can see
  • 20:07: it has a large grid size it has the new
  • 20:09: color map it's reversed the color map
  • 20:11: and we see the labels coming in instead
  • 20:14: of the
  • 20:14: actual names of the arguments
  • 20:18: and we see that on the
  • 20:23: data frames as well
  • 20:26: um and
  • 20:30: the really beautiful thing about this
  • 20:32: library is you can keep adding more
  • 20:34: inputs
  • 20:34: that you're looking at and you really
  • 20:37: don't have to change your code
  • 20:39: at all it works in the exact same
  • 20:40: structure
  • 20:43: so here we have a second model it's got
  • 20:45: three inputs this time
  • 20:47: and now we've got our dictionary which
  • 20:50: has the three different
  • 20:52: inputs um and three different values of
  • 20:55: each
  • 20:56: so we run that this time we can see it
  • 20:58: ran 27 different
  • 21:00: cases three times three times three um
  • 21:04: and what we see when we plot it is that
  • 21:07: now we get
  • 21:08: three different plots all at once
  • 21:10: without having to do any extra effort
  • 21:12: we get x1 versus x2 we get x1 versus
  • 21:16: x3 and we get x2 versus x3 so we get all
  • 21:19: the different combinations of the
  • 21:21: different inputs
  • 21:22: and the relationships between them
  • 21:25: so really easy to run a lot of different
  • 21:27: inputs and visualize it all at once with
  • 21:30: just a couple lines of code
  • 21:33: and then we'll see the same thing with
  • 21:35: the style data frames
  • 21:37: um we'll get one verse two one verse
  • 21:41: three two verse three and as you
  • 21:43: continue to add more
  • 21:44: inputs you'll get all those combinations
  • 21:46: so it can become
  • 21:47: a lot of output really quickly
  • 21:51: um and if you need to look up
  • 21:54: any so this style data frames you can
  • 21:57: see i saved it into styled
  • 21:59: predict it does automatically print out
  • 22:02: the results
  • 22:03: even if you save it into a variable
  • 22:08: you'll see what we have here is actually
  • 22:10: a dictionary
  • 22:12: where the keys are tuples
  • 22:17: and the values are the style data frames
  • 22:20: so you can also look up
  • 22:22: reference any of the individual results
  • 22:25: that you want to
  • 22:26: look at
  • 22:32: so we can also do some additional
  • 22:34: styling
  • 22:36: we can change the number formatting of
  • 22:38: the results as well
  • 22:40: and you can pass this in either when you
  • 22:42: create the sensitivity
  • 22:43: analyzer at the beginning or to the
  • 22:46: style dfs
  • 22:49: command you can pass the number format
  • 22:52: and this number format works exactly the
  • 22:54: same as the
  • 22:55: pandas number formatting uh that we
  • 22:57: learned from the styling
  • 22:59: penis data frames lecture
  • 23:02: um where you basically put the same kind
  • 23:04: of string
  • 23:06: as like an f uh only you don't put the
  • 23:09: variable here
  • 23:10: you just put nothing there and then a
  • 23:11: colon and then the format
  • 23:13: code so you can see this is doing
  • 23:15: dollars and then
  • 23:16: uh formatted with commas and zero
  • 23:19: decimal places
  • 23:20: and we see that all coming here as well
  • 23:27: so that covers the introduction to
  • 23:30: sensitivity analysis
  • 23:31: in python we'll come back next time to
  • 23:35: add the sensitivity analysis into our
  • 23:37: existing dynamic salary retirement
  • 23:39: model so thanks for listening and see
  • 23:42: you next time

Sensitivity Analysis in Python Example


Notes

  • Here we will focus only on using the sensitivity package rather than carrying everything out manually

  • We need to go back and add an optional argument to the model about whether it should print the results, otherwise we will have the results printed a huge number of times as we run the sensitivity analysis

  • The sensitivity package is made to work with functions where each input is passed separately, whereas our model function takes a single dataclass instance. To make our model function work with the sensitivity package, we need to create a wrapper function which takes the separate arguments, creates the dataclass from them, passes that into the model function and returns the result. You can copy my snippet to do this with your model.

  • List comprehensions are a nice easy way to specify values in a range, but you can also hard-code these lists

  • Be careful not to look at too many input values as execution could be very slow. The progress bar will tell you how many cases of the model you are running and show how long it is taking.

  • There are a number of options to customize the output from the library. You can change the names of the inputs and results, the color map, change direction of the colors, the grid size on the hexbin plots, and the number formatting in styled DataFrames.

Transcript

  • 00:02: hey everyone
  • 00:03: nick diabetis here teaching you
  • 00:04: financial modeling today
  • 00:06: we're going to be looking at an example
  • 00:09: of adding sensitivity analysis
  • 00:11: to an existing model this is part of our
  • 00:14: segment
  • 00:15: on exploring the parameter space
  • 00:19: so we left off last time having done
  • 00:22: an introduction on sensitivity analysis
  • 00:25: in python and looked at some of the
  • 00:27: different outputs we were going to
  • 00:28: produce and approaches
  • 00:30: we could use and now we're coming to
  • 00:33: actually apply that to our existing
  • 00:35: dynamic salary retirement
  • 00:37: model so let's go ahead and jump over to
  • 00:41: that model
  • 00:42: so we can build this extension to the
  • 00:45: model out
  • 00:47: and you can find the um completed
  • 00:51: extension from this exercise on the
  • 00:54: course site
  • 00:55: and i would recommend that you take a
  • 00:57: look at that
  • 00:58: completed example as you go to
  • 01:02: work the lab exercise for this and to
  • 01:04: add sensitivity analysis to your own
  • 01:06: models in the future so
  • 01:12: um we're gonna add sensitivity analysis
  • 01:15: so we can go ahead
  • 01:16: and make that as an additional section
  • 01:19: down here
  • 01:19: sensitivity analysis
  • 01:26: um and you would want to put a
  • 01:28: description of
  • 01:29: what you're going to be analyzing why
  • 01:31: what are the takeaways
  • 01:32: um but i'm going to leave that out for
  • 01:35: now for a second time
  • 01:38: um and
  • 01:41: i know that i'm going to need to use the
  • 01:43: sensitivity analyzer so i'm going to go
  • 01:45: ahead
  • 01:45: and add that import up here
  • 01:49: from sensitivity import
  • 01:52: sensitivity analyzer um
  • 01:55: sometimes yep if you tab complete
  • 01:57: sometimes they will tap complete it for
  • 01:59: you so that
  • 02:00: worked out for me then i'm going to
  • 02:04: restart kernel
  • 02:05: and run all cells so that we have
  • 02:07: everything from the model
  • 02:09: defined and then can just work with it
  • 02:10: below for the sensitivity
  • 02:12: analysis
  • 02:15: um now
  • 02:19: we're ready to start building out the
  • 02:21: sensitivity analysis
  • 02:22: but one thing that you may notice about
  • 02:25: my existing model function used to
  • 02:27: retirement
  • 02:28: is when i call it it prints out
  • 02:32: output and when we go to do sensitivity
  • 02:35: analysis
  • 02:36: basically this function is going to get
  • 02:38: right a bunch of times
  • 02:40: with different inputs and
  • 02:44: um we're going to see that
  • 02:47: doing that without any changes we would
  • 02:50: get this whole print output
  • 02:52: for as many times as we're running the
  • 02:53: model and we could be running the model
  • 02:55: thousands of times
  • 02:57: so we don't want to get thousands of
  • 02:59: copies of this
  • 03:00: print output for the different output
  • 03:03: cases
  • 03:04: we're already going to be storing the
  • 03:05: main output that we want to look at
  • 03:07: separately so before we can even add the
  • 03:09: sensitivity analysis
  • 03:11: i'm going to go back and make a
  • 03:13: modification to the existing model
  • 03:16: to make it optional whether it prints
  • 03:18: out
  • 03:19: the results um
  • 03:22: so we've already talked about um
  • 03:26: optional arguments before
  • 03:29: we can add an optional argument here uh
  • 03:32: print output equals false so it defaults
  • 03:36: to it should not print any output
  • 03:39: and now i'm just going to go to each of
  • 03:41: the print statements and say if print
  • 03:42: output
  • 03:45: then it's going to print otherwise it's
  • 03:47: not going to print
  • 03:49: so i'm just going to do the same thing
  • 03:52: uh with each of these
  • 04:01: and i'm going to rerun that to redefine
  • 04:03: that function
  • 04:06: and now you'll see that when i run this
  • 04:09: i don't get
  • 04:09: any output there i just have the final
  • 04:12: answer
  • 04:12: storm and the variable well with this
  • 04:15: usage we do actually want to
  • 04:17: print the output so i'm going to put
  • 04:19: print output equals true
  • 04:21: and now we're back to exactly what we
  • 04:23: had before for the original model
  • 04:26: but we're also able to call the model
  • 04:29: without producing all that print output
  • 04:32: which is what we're going to need
  • 04:33: to go into sensitivity analysis
  • 04:37: so now the model is ready for us to go
  • 04:40: and add sensitivity analysis
  • 04:45: so now we want to use the sensitivity
  • 04:47: analyzer
  • 04:50: but there's one other thing we have to
  • 04:53: do
  • 04:54: to prepare the model function
  • 04:58: before we can pass it to the sensitivity
  • 05:00: analyzer
  • 05:01: the sensitivity analyzer is expecting to
  • 05:04: be able to pass
  • 05:04: each argument separately to the function
  • 05:07: but the way that we structure our models
  • 05:09: is we use a data class
  • 05:11: we pass only a single data class into
  • 05:13: the function
  • 05:14: and not each of the separate arguments
  • 05:17: so we have to create a function
  • 05:19: that will take the separate arguments
  • 05:21: construct the data class from those
  • 05:23: arguments
  • 05:24: and then pass the data class into the
  • 05:27: function
  • 05:28: and return the result of that function
  • 05:31: so that it's basically a wrapper
  • 05:32: around the model function which allows
  • 05:34: it to take the input separately
  • 05:36: rather than as a data class so
  • 05:39: we can write that function so i'm going
  • 05:41: to call that year's retirement
  • 05:45: separate args
  • 05:49: and i'll come back to the arguments
  • 05:50: section in a moment
  • 05:52: um and then what we're going to do is
  • 05:55: we're going to
  • 05:57: create an instance of model inputs i'll
  • 06:00: also
  • 06:00: come back to this in a moment and then
  • 06:03: we're going to return
  • 06:05: calling the original years retirement
  • 06:07: function
  • 06:08: on that data
  • 06:12: now what we're going to do for the
  • 06:14: arguments themselves
  • 06:16: there's actually a shortcut in python
  • 06:19: which just says
  • 06:20: let me take any number of keyword
  • 06:23: arguments
  • 06:24: keyword being where you say like
  • 06:26: starting salary equals
  • 06:28: ten thousand or uh promo raise equals
  • 06:31: five percent so we can take any number
  • 06:34: of those
  • 06:36: and then it will just take all those and
  • 06:38: be able to pass them into the model
  • 06:39: inputs
  • 06:40: so that um shortcut
  • 06:44: here is a star star
  • 06:48: on a variable name and the convention is
  • 06:50: to do star star
  • 06:52: quarks so that just says any number of
  • 06:55: keyword arguments
  • 06:56: it's going to take those and it's going
  • 06:58: to put them into a dictionary
  • 06:59: coming into the function you have a
  • 07:01: quarks dictionary
  • 07:03: um so let me just print that to make
  • 07:06: that clear
  • 07:09: and then doing it again here it spreads
  • 07:10: it back out into
  • 07:12: the keyword arguments so this will take
  • 07:15: keyword arguments and collect them into
  • 07:16: a dictionary
  • 07:17: and then this will take that dictionary
  • 07:19: and spread it back out into the keyword
  • 07:22: arguments
  • 07:23: um so not expecting everyone to fully
  • 07:26: understand what's going on there
  • 07:28: you don't have to understand it you can
  • 07:31: just copy my code snippet and
  • 07:33: change out the model function
  • 07:36: and the name of the function and you'll
  • 07:38: be good to go to use the sensitivity
  • 07:40: analyzer
  • 07:42: so to show what this is doing i'm going
  • 07:44: to call this and i'm going to say
  • 07:46: starting salary equals 10 000
  • 07:50: and you'll see from the print output
  • 07:53: that it
  • 07:55: prints out the
  • 07:59: starting salary in a dictionary
  • 08:02: with this as the value and
  • 08:07: you can add other arguments here 30
  • 08:09: promotion raise
  • 08:11: we can see that also comes into the
  • 08:13: dictionary
  • 08:16: and then we ultimately get the result
  • 08:20: from that
  • 08:25: so
  • 08:29: this is our wrapper for the function
  • 08:33: which then is able to
  • 08:37: call the model and return the result
  • 08:49: so now that we have that working
  • 08:54: then we can go on to
  • 08:58: the next step which is to actually uh
  • 09:01: use the sensitivity analyzer
  • 09:05: um that's something weird is going on
  • 09:09: here because no matter what
  • 09:10: inputs i'm putting i'm still getting uh
  • 09:13: 28 years to
  • 09:14: retirement um
  • 09:24: oh yeah i see the mistake here uh here
  • 09:27: and a lot of people may make these
  • 09:28: mistakes in their own models
  • 09:30: um here data was being passed to the
  • 09:33: function but here i used
  • 09:34: model data so it wasn't actually able to
  • 09:36: pick up the data which is being passed
  • 09:38: to the function it was just using the
  • 09:40: overall model data each time
  • 09:42: and so i'm going to make sure that that
  • 09:45: matches up with the function argument
  • 09:48: and now i'm going to restart and run
  • 09:51: this thing
  • 09:52: all again and now we should see a number
  • 09:55: other than 28 come up here hopefully
  • 09:59: yeah there we go now it's actually
  • 10:01: working if you make a million dollars
  • 10:03: off the bat then you'll be able to
  • 10:04: retire in only five years
  • 10:09: okay and then back without passing any
  • 10:12: arguments we're back to 28 so it looks
  • 10:14: like this is all working properly now
  • 10:19: um so next we want to create that
  • 10:22: dictionary
  • 10:23: of the inputs that we want to change
  • 10:26: around
  • 10:27: so i'm going to start it out simple and
  • 10:29: we'll expand on this
  • 10:31: so sensitivity values um so let me just
  • 10:34: look at starting salary
  • 10:37: let me look at you know as low as 40
  • 10:41: 000 or as high as
  • 10:45: a hundred and twenty thousand and then
  • 10:48: let's look at the interest rate
  • 10:53: um and let's look at
  • 10:56: a three percent rate and a ten percent
  • 10:59: rate
  • 11:04: so then we have everything that we need
  • 11:06: to go ahead and run the sensitivity
  • 11:08: analyzer
  • 11:09: so then i'm going to create an instance
  • 11:12: of the sensitivity analyzer
  • 11:15: i'm going to pass it the sensitivity
  • 11:18: values
  • 11:20: and i'm going to pass it the model
  • 11:22: function
  • 11:23: the one that takes the separate
  • 11:25: arguments
  • 11:27: so then we see that it ran four
  • 11:29: different input cases here
  • 11:32: two times two and
  • 11:35: now we can look at the results so we can
  • 11:37: look at the full result
  • 11:38: data frame there each of the pairwise
  • 11:42: combinations
  • 11:42: and the user retirement
  • 11:46: um and we can
  • 11:49: create the resulting
  • 11:52: hexman plot
  • 11:56: and then we can um
  • 12:01: create the style data frames
  • 12:08: here just a single data frame for now
  • 12:12: um so that's pretty much what you would
  • 12:13: want to be seeing from sensitivity
  • 12:15: analysis as well as
  • 12:16: you know some description of the results
  • 12:19: that you found
  • 12:20: at the end um but there's a fair amount
  • 12:25: we can do here
  • 12:26: to clean things up and to make them more
  • 12:29: comprehensive
  • 12:31: so first thing is we can pass some
  • 12:34: additional options to sensitivity
  • 12:36: analyzer to clean up the output
  • 12:41: so some of those additional options
  • 12:44: would be
  • 12:46: that we want to
  • 12:49: label the results so we can do result
  • 12:52: name equals years to retirement because
  • 12:54: we can see right now it's just coming as
  • 12:56: result
  • 12:58: uh result
  • 13:01: result but we put years to retirement
  • 13:04: here as a result name and then we get
  • 13:07: uh years retirement years for retirement
  • 13:10: here's to retirement
  • 13:15: um and then we'll notice that
  • 13:18: right now it's highlighting the high
  • 13:20: years to retirement as bringing in the
  • 13:22: low
  • 13:23: as red that's the opposite of what we
  • 13:25: want
  • 13:26: so we want to reverse uh
  • 13:29: the colors reverse colors equals true
  • 13:34: um and then with that we will see
  • 13:37: um that now it's showing red for the
  • 13:42: high year's retirement that's bad we
  • 13:43: want to retire sooner
  • 13:47: um you'll notice that this grid is very
  • 13:50: sparse
  • 13:51: um so i'm going to pass a
  • 13:55: grid size of three and then you'll see
  • 13:59: now this will get more filled out as we
  • 14:01: add more inputs here in a moment but
  • 14:03: i think this makes more sense um
  • 14:07: for these data um
  • 14:10: and the other thing that we'll want to
  • 14:12: do is add some labels
  • 14:15: as well and it has to match up the keys
  • 14:19: match up again with the input names
  • 14:23: and then the values would be how you
  • 14:25: want it to look in the output so
  • 14:27: interest rate interest rate
  • 14:31: and then we can pass labels equals
  • 14:34: labels
  • 14:36: and we run these again now we can see in
  • 14:39: all the output
  • 14:39: that we're now getting the labels
  • 14:43: as well so it's starting to look clean
  • 14:47: um and the last thing we can do
  • 14:51: is add a number format um
  • 14:54: so here's your retirement not a currency
  • 14:57: we don't want a dollar sign
  • 15:01: and we can just have zero decimal places
  • 15:07: so then um we'll see now
  • 15:10: in the style data frames that we have no
  • 15:12: decimal places
  • 15:14: on the output
  • 15:19: so that looks good as far as the
  • 15:21: formatting
  • 15:22: options for the output now
  • 15:26: let's look at some more values of the
  • 15:28: inputs and look at more inputs
  • 15:32: so it can get tedious to type out all
  • 15:35: the different values that you might want
  • 15:36: to
  • 15:37: look at so that's why i like to use
  • 15:40: list comprehensions to uh write out the
  • 15:44: outputs
  • 15:45: so um if we want to look at
  • 15:49: um salaries and the range of
  • 15:52: say forty thousand to uh seventy
  • 15:56: thousand
  • 15:57: what we can do is uh
  • 16:03: we can just do a simple for i and range
  • 16:05: loop
  • 16:06: and multiply the result
  • 16:09: by ten thousand you know without the ten
  • 16:11: thousand it would be
  • 16:12: four five six seven then we just
  • 16:14: multiply it by ten thousand
  • 16:16: and then we have that range of salaries
  • 16:19: so i'm gonna put that
  • 16:20: in here as the starting salary
  • 16:24: then for interest rate um
  • 16:28: here we want to look at percentages so
  • 16:30: let's divide by 100
  • 16:32: and let's look from three to six
  • 16:36: percent so i'm going to take that and
  • 16:39: put that into the interest rate
  • 16:41: um and let's look at the other inputs as
  • 16:45: well
  • 16:45: uh so promos every and years and all
  • 16:50: these things
  • 16:51: should be matching up with the
  • 16:53: attributes
  • 16:54: on the model data
  • 16:58: so promos every n years
  • 17:02: let's look at um in the range
  • 17:06: of um
  • 17:09: how about from four to seven years
  • 17:13: uh so i'll put that there for promos
  • 17:15: everyone years
  • 17:18: um let's add cost of living raise
  • 17:23: um and cost of living raise that is
  • 17:26: another percentage we can divide by 100
  • 17:30: and let's go from a one percent to a
  • 17:32: three percent
  • 17:34: so i'm going to put that there um
  • 17:37: and we can add the promotion raise promo
  • 17:40: raise
  • 17:43: um and for that one um
  • 17:47: here we can look in increments of five
  • 17:50: percent
  • 17:50: let's look from ten percent to twenty
  • 17:52: percent in five percent increments
  • 17:54: um so let's keep that divided by a
  • 17:56: hundred since it's
  • 17:57: um a percentage and we're going to go
  • 18:01: from 10
  • 18:02: up to 25 but let's go in five
  • 18:05: steps so this is 10 15 20
  • 18:09: um of course you can write these out
  • 18:12: manually
  • 18:13: if you want just writing those numbers
  • 18:14: in
  • 18:16: but i like using the list comprehensions
  • 18:18: because it's also easy to go and adjust
  • 18:20: these ranges
  • 18:20: as well um
  • 18:24: let's see so let's also add the
  • 18:28: savings rate savings rate
  • 18:33: um and for savings rate let's go up to
  • 18:36: from 10 to 40
  • 18:38: and 10 increments um
  • 18:42: so that would be this 20 30
  • 18:48: um and let's look at the desire and cash
  • 18:54: so for desired cash um
  • 18:56: [Music]
  • 18:58: let's look at from
  • 19:01: a million dollars to 2.5 million dollars
  • 19:05: um in 500 000 increments so that's going
  • 19:09: to be uh
  • 19:10: 10 to 26 minutes of five
  • 19:13: and multiplying by a hundred thousand
  • 19:18: so that looks correct now we can take
  • 19:21: that and put that in there as well
  • 19:24: so then we have all these different
  • 19:26: values of the inputs that we want to
  • 19:28: look at
  • 19:30: um let's add their labels as well
  • 19:35: so promos every years
  • 19:41: promos every and years
  • 19:46: let's do the cost of living raise
  • 19:54: cost of living raise
  • 19:58: let's do the primaries
  • 20:02: motion rays
  • 20:06: i got interest rate already add savings
  • 20:09: rate
  • 20:12: savings rate and add desired cash
  • 20:21: okay um now we've got a bunch of
  • 20:24: different values of all these different
  • 20:25: inputs
  • 20:26: we've got all the labels we had all our
  • 20:28: options from before to make it look good
  • 20:30: let's go ahead and run this and now
  • 20:33: you'll see we have a lot more inputs
  • 20:34: that we're looking at we have 6 900
  • 20:36: different cases
  • 20:38: and now it actually takes some
  • 20:39: substantial time to run the model
  • 20:41: and we see that progress bar go by as
  • 20:44: it's running all these different input
  • 20:46: cases now it's
  • 20:48: done we look at this data frame
  • 20:51: um
  • 20:52: [Music]
  • 20:55: it's going to be
  • 20:58: [Music]
  • 21:00: you know having a lot more rows in it
  • 21:01: this time
  • 21:03: um and we should similarly see
  • 21:06: additional plots and style
  • 21:08: data frames all of this coming together
  • 21:12: um so there's a big advantage
  • 21:16: uh over excel in here in that you can
  • 21:19: pass as many possible inputs as you want
  • 21:23: you're not limited to just looking at
  • 21:24: two at once
  • 21:26: and it's going to be able to generate a
  • 21:29: lot of different results for you
  • 21:32: so now we've got
  • 21:35: we have the 6900 different runs of the
  • 21:38: model and data frame
  • 21:41: and we've got all these different
  • 21:43: combinations of the inputs
  • 21:44: and these flops so we can take any pair
  • 21:48: that we want to look at
  • 21:50: here it's all about the promotions
  • 21:51: promotional race promotions everyone
  • 21:53: years
  • 21:53: as you get quicker promotions we start
  • 21:56: to hit
  • 21:57: or sorry slower promotions than we get
  • 21:59: to longer years to retirement up to 40
  • 22:02: over here
  • 22:04: and as we get higher promotion raises
  • 22:06: then we get shorter years to retirement
  • 22:09: but we can see in the range of inputs
  • 22:11: that we're looking at
  • 22:12: it's not a very huge impact on the years
  • 22:15: to retirement
  • 22:20: so then you have the same information
  • 22:24: over here in the style
  • 22:26: data frames so you can really pick
  • 22:28: whichever one of the two
  • 22:30: you like better for showing the results
  • 22:33: you don't have to
  • 22:34: show both like i'm doing here
  • 22:38: and it would be good to at the end just
  • 22:40: put a description
  • 22:41: of your findings some of the notable
  • 22:43: things from the sensitivity analysis
  • 22:46: to kind of wrap everything together
  • 22:50: so that's adding sensitivity analysis to
  • 22:54: an existing model in python using the
  • 22:56: sensitivity
  • 22:57: package so that concludes
  • 23:01: our segment on exploring the parameter
  • 23:04: space
  • 23:05: we will examine a couple other
  • 23:07: techniques
  • 23:09: which explore the parameter space in the
  • 23:11: future lectures as well
  • 23:14: so thanks for listening and i'll see you
  • 23:16: next time

Lab Exercise - Adding Sensitivity Analysis to Project 1 - Python


Notes

  • The lab exercise here is the same as that we did for Excel but now just doing it in Python

  • You are free to use the manual approach or the sensitivity package but I would recommend the sensitivity package

  • Be sure to upgrade your sensitivity package before working on the exercise

  • The lecture on adding sensitivity analysis to the Python Dynamic Salary Retirement model will be very helpful for this

  • You may need to restructure your model before you can even begin adding sensitivity analysis. You must have a function which accepts the data and produces the final output from that data, and your functions must not access the overall model_data

Transcript

  • 00:03: hey everyone
  • 00:04: nick dear burtis here teaching you
  • 00:05: financial modeling today we're going to
  • 00:08: be
  • 00:08: talking about the lab exercise for
  • 00:11: sensitivity analysis
  • 00:12: in python as part of the lecture series
  • 00:15: on exploring the parameter space
  • 00:18: so we left off last time we had covered
  • 00:21: all the material on sensitivity analysis
  • 00:23: in python and now we're coming here to
  • 00:26: the last slide which has the lab
  • 00:28: exercise
  • 00:29: for python sensitivity analysis
  • 00:32: and that is analogous to the exercise we
  • 00:35: completed in excel
  • 00:37: you're going to be adding sensitivity
  • 00:38: analysis to your project
  • 00:40: 1 model um
  • 00:44: and so what we just went through with
  • 00:46: adding
  • 00:47: the uh sensitivity analysis to the
  • 00:50: dynamic salary retirement model in
  • 00:52: python
  • 00:53: is going to be really useful for
  • 00:55: completing this exercise so
  • 00:56: watch that carefully and carry out some
  • 00:59: of the same steps over there
  • 01:02: now since this is working with your own
  • 01:05: model
  • 01:07: there's a good chance that your model
  • 01:09: will not have the structure
  • 01:10: required to be able to extend it in any
  • 01:13: way
  • 01:14: if you're accessing the model data
  • 01:17: directly
  • 01:18: inside your logic rather than having a
  • 01:21: function which accepts an arbitrary data
  • 01:23: and working with that arbitrary data
  • 01:26: then this is not going to work you're
  • 01:28: going to have to go back
  • 01:29: and restructure it you should never be
  • 01:32: accessing that
  • 01:35: original model data from within your
  • 01:37: functions
  • 01:39: and you should have one function where
  • 01:41: you can pass it
  • 01:42: the data class and
  • 01:46: get the result of the entire model
  • 01:50: uh the mpv and cash flows um
  • 01:55: by just running a single function so
  • 01:58: you'll need to have both of those things
  • 01:59: in place before you can even go and
  • 02:01: start
  • 02:02: on this exercise so you should
  • 02:04: definitely go back and restructure
  • 02:06: things
  • 02:07: to be able to complete this and
  • 02:10: don't just think well my model is too
  • 02:13: far gone i'll just skip this lab
  • 02:14: exercise because
  • 02:16: as we go forward in the course we're
  • 02:18: gonna have more of these similar
  • 02:19: exercises of extending project one
  • 02:22: and so you really need to to rebuild it
  • 02:25: in a way
  • 02:26: that is going to be flexible to any
  • 02:28: extensions that you can add to it
  • 02:31: and i set it up this way because you
  • 02:33: want to always structure your models
  • 02:34: this way and this will really teach you
  • 02:36: why
  • 02:37: you want to structure them this way that
  • 02:39: way you can actually
  • 02:41: extend them rather than the model is
  • 02:43: what it is
  • 02:46: so that's the main idea here and then
  • 02:49: unlike the excel exercise where you're
  • 02:52: looking at doing just conditional
  • 02:53: formatting in a table
  • 02:55: here we're going to have the hexman plot
  • 02:57: as well as the styled
  • 02:59: data frame as the outputs
  • 03:03: so that's the lab exercise for the
  • 03:06: python sensitivity analysis and that
  • 03:08: wraps up this
  • 03:09: segment on exploring the parameter space
  • 03:12: so
  • 03:12: thanks for listening next time we'll be
  • 03:14: talking about
  • 03:16: probability modeling so i'll see you
  • 03:19: next time