Exploring the Parameter Space
********************************
Expand models to check the full range of inputs and see how it impacts the outputs.
Resources
============
- :download:`Slides - Exploring the Parameter Space `
- :download:`Lecture Notes - Exploring the Parameter Space `
- :download:`Dynamic Salary Retirement Model - Excel `
- :download:`Python Dicts, List Comprehensions, and Imports `
- :download:`Dictionaries, List Comprehensions, and Imports Labs `
- `Guide to Python Imports `_
- :download:`Sensitivity Analysis `
- :download:`Dynamic Salary Retirement Model - Python `
Introduction to Parameter Exploration
========================================
.. youtube:: IVC38ksxNfI
:height: 315
:width: 80%
:align: center
|
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
-------------
.. raw:: html
|
Introduction to Sensitivity Analysis
=======================================
.. youtube:: dMpQNAnIhJQ
:height: 315
:width: 80%
:align: center
|
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
-------------
.. raw:: html
|
Sensitivity Analysis in Excel
================================
.. youtube:: 1wECF1RjsNM
:height: 315
:width: 80%
:align: center
|
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
Resources
------------
- :download:`Dynamic Salary Retirement Model - Excel `
Transcript
-------------
.. raw:: html
|
Using Python Dictionaries
============================
.. youtube:: 6Q8LvUQmykE
:height: 315
:width: 80%
:align: center
|
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
Resources
------------
- :download:`Python Dicts, List Comprehensions, and Imports `
- :download:`Dictionaries, List Comprehensions, and Imports Labs `
Transcript
-------------
.. raw:: html
|
Python List Comprehensions - Convenient List Building
========================================================
.. youtube:: 6GpCoTG704o
:height: 315
:width: 80%
:align: center
|
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
Resources
------------
- :download:`Python Dicts, List Comprehensions, and Imports `
- :download:`Dictionaries, List Comprehensions, and Imports Labs `
Transcript
-------------
.. raw:: html
|
Python Imports and Installing Packages
=========================================
.. youtube:: xSX8sg7Twqw
:height: 315
:width: 80%
:align: center
|
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
Resources
------------
- :download:`Python Dicts, List Comprehensions, and Imports `
- `Guide to Python Imports `_
Transcript
-------------
.. raw:: html
|
Introduction to Sensitivity Analysis in Python
=================================================
.. youtube:: AybxBMC7dzo
:height: 315
:width: 80%
:align: center
|
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
Resources
------------
- :download:`Sensitivity Analysis `
Transcript
-------------
.. raw:: html
|
Sensitivity Analysis in Python Example
=========================================
.. youtube:: 7vACMHmUjYY
:height: 315
:width: 80%
:align: center
|
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.
Resources
------------
- :download:`Dynamic Salary Retirement Model - Python `
Transcript
-------------
.. raw:: html
|
Lab Exercise - Adding Sensitivity Analysis to Project 1 - Python
===================================================================
.. youtube:: YtE5oRv2Ot4
:height: 315
:width: 80%
:align: center
|
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
Resources
------------
- :download:`Dynamic Salary Retirement Model - Python `
Transcript
-------------
.. raw:: html
- 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
|