What-if analysis with Python

Excel is widely used for building and using models of business problems to explore the impact of various model inputs on key outputs. Built in “what if?” tools such as Excel Data Tables and Goal Seek are well known to power spreadsheet modelers. How might we do similar modeling and analysis using Python?

Through a series of notebooks we will learn:

  • how to do data tables, goal seek, and Monte-Carlo simulation in Python,

  • the fundamentals of doing object-oriented programming in Python,

  • numerous advanced Python data manipulation functions and techniques.

Downloads and other resources

This downloads file will be used for the activities below.

Activities

We will start with the what_if_1_model_datatable.ipynb notebook. In it I will introduce the basic model we’ll be working with throughout this module. Then we’ll build a non-object oriented Python model and develop one approach to doing Excel style data tables. Then we’ll learn the basics of object oriented programming and build an OO version of the same model. Finally, we’ll develop a data_table function that uses the OO model. Here are a series of screencasts to help you as you work through this notebook:

Now we’ll build on this work and develop a Python based goal seek function. We’ll use the what_if_2_goalseek.ipynb notebook. This will involve exploring packages like SciPy for doing root finding and we’ll learn a more about what’s going on in Excel when you are using Goal Seek.

Now let’s see how we might do Monte-Carlo simulation with Python and add a new function to our growing little Python library of Excel style “what-if?” functions. We’ll use the what_if_3_simulation.ipynb notebook. In this part we’ll learn about random number generation in Python and see that we can leverage ideas from our data_table function to create a simulate function.