0% found this document useful (0 votes)
18 views

S2 Getting Started With Python and Excel

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

S2 Getting Started With Python and Excel

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Basic Problem Excel Solution Python Solution Extend & Iterate

Getting Started with Python and Excel


Building a Basic Model in Both Excel and Python

Nick DeRobertis1
1 University of Florida

Department of Finance, Insurance, and Real Estate

June 9, 2021

DeRobertis (UF) Getting Started June 9, 2021 1 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Table of Contents

1 An Introductory Model

2 Excel Solution

3 Python Solution

4 Extending the Model and Iteration

DeRobertis (UF) Getting Started June 9, 2021 2 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Approaching a Problem with Two Tools

The focus today is to get


familiar working in both Excel
and Python

We will approach this by


building a simple model with
both tools

In later lectures, we will move to


combining the tools

DeRobertis (UF) Getting Started June 9, 2021 3 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

A Simple Retirement Problem

Let’s take what is perhaps the simplest finance problem, which


everyone should understand

While you may have approached such a problem with a calculator


before, we will build models for it instead

Martha is saving for retirement. She earns $60,000 per year and is
able to save 25% of that. If she invests her savings, earning 5% per
year, and she needs $1,500,000 to retire, how soon can she retire?

DeRobertis (UF) Getting Started June 9, 2021 4 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Breaking Down the Retirement Problem


A General Structure

Inputs Process Outputs

Real-world Problem

Cash in the
Wages, Savings Investment bank, person
retires

A Model of the Problem


Cash Flows, Savings FV of CF, time
Model
Rate, Interest Rates until retirement
DeRobertis (UF) Getting Started June 9, 2021 5 / 22
Basic Problem Excel Solution Python Solution Extend & Iterate

Table of Contents

1 An Introductory Model

2 Excel Solution

3 Python Solution

4 Extending the Model and Iteration

DeRobertis (UF) Getting Started June 9, 2021 6 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Solving the Problem in Excel

It is easy to use Excel as a


calculator and just type the
math in directly. But we want
to build a model.
Changing inputs should result in
a change to outputs. The way
to do this in Excel is cell
references
Fixed references become
important when trying to drag
formulas, e.g. $A$2 (fully fixed),
$A2 (fixed on column), or A$2
(fixed on row).

DeRobertis (UF) Getting Started June 9, 2021 7 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Simple Retirement Problem in Excel

Intro Excel Exercise


Go to the course site and download Simple Retirement Model Excel
Follow along as I recreate the simple model.

DeRobertis (UF) Getting Started June 9, 2021 8 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Table of Contents

1 An Introductory Model

2 Excel Solution

3 Python Solution

4 Extending the Model and Iteration

DeRobertis (UF) Getting Started June 9, 2021 9 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

How We’ll Work in Python

Using Python in the terminal is


kind of a pain. And so, tools were
born.

Jupyter is a graphical interface we


can use for Python. It also
supports over 40 other languages
such as R, SAS, Julia, and Scala

You can use jupyter notebook or


jupyter lab. The latter has a lot
more features outside of the
notebook. We will focus on using
jupyter lab in this class as it is
the future of Jupyter.

DeRobertis (UF) Getting Started June 9, 2021 10 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Let’s Get Set up with Jupyter

Launch Jupyter Notebook


1 Launch Anaconda Navigator
2 Find Jupyter Notebook on the main screen, and click launch
3 You should see a list of folders and files. Click New and then Python 3
4 Now you should see a code cell with In [ ]: next to it

If you don’t have Anaconda Navigator, just open a terminal (search cmd
on Windows, terminal on Mac). Then in the terminal, type jupyter
lab and enter. Then continue with the third step.

DeRobertis (UF) Getting Started June 9, 2021 11 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Some Python Basics

In Excel, the basic unit is a cell. In Python, the basic unit is an object.

In Excel, content in a cell is either a number (123) or a string (ABC)

In Python, all objects have types. They might also be a number or a


string, or something else.

Rather than using a cell reference like $A$2, we assign names to


objects in Python

my_number = 6
my_string = 'ABC'

DeRobertis (UF) Getting Started June 9, 2021 12 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Doing Some Math in Python

Basic operations in Python are


straightforward
2 + 5 = 7
6 - 2 = 4

Note: Deprecation warning 2 * 3 = 6


In the future, these numpy financial 5 / 2 = 2.5
functions are being moved to a separate
package numpy_financial. For the A lot more is available using the
purposes of this class, this won’t matter, numpy package
but in the future you may have to install
numpy_financial to use these functions.
np.pv, np.nper,
In the meantime, you will see a warning np.fv, np.pmt
come up when calling the functions.
All numpy financial functions

DeRobertis (UF) Getting Started June 9, 2021 13 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Simple Retirement Problem in Python

Intro Python Exercise


Go to the course site and download Simple Retirement Model Python
In Jupyter, then navigate to your Downloads folder (or wherever you
saved it)
You should then see Simple Retirement Model.ipynb come up in the
list of files in Jupyter. Click it to open it and follow along.

DeRobertis (UF) Getting Started June 9, 2021 14 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Table of Contents

1 An Introductory Model

2 Excel Solution

3 Python Solution

4 Extending the Model and Iteration

DeRobertis (UF) Getting Started June 9, 2021 15 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Extending the Model - Multiple Interest Rates

Now we’ve got basic models to determine how long it will take
Martha to retire.

We’ve got a few assumptions built into the model. One is that
Martha will earn 5% on her investments

Rates of return are volatile, so we want to see how long it would take
her to retire if her return was different

DeRobertis (UF) Getting Started June 9, 2021 16 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Programming Fundamentals - Iteration

In programming, for model building or otherwise, you often need to


repeat the same process for multiple different things

In Excel, you would do this by dragging formulas.

In Python, as in most other programming languages, we would use a


for loop

This says, do something, for each value I pass into the loop

DeRobertis (UF) Getting Started June 9, 2021 17 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Iteration - Python vs. Excel

Python Iteration
inputs = [5, 10, 15] Excel Iteration
for item in inputs:
new_value = item + 2
print(new_value)

7
12
17

DeRobertis (UF) Getting Started June 9, 2021 18 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Explaining Python Iteration

There’s a few things to unpack


here
Python Iteration
inputs = [5, 10, 15]
Here’s another type of object:
for item in inputs:
not a number or a string, but a
new_value = item + 2
list
print(new_value)

A list holds multiple objects, 7


and you can add or remove 12
items from lists 17

DeRobertis (UF) Getting Started June 9, 2021 19 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Explaining Python Iteration (pt. 2)

Here we define a list of three


numbers as inputs
Python Iteration
inputs = [5, 10, 15]
Then we use a for loop to get
for item in inputs:
each input out of the list, and
new_value = item + 2
add 2 to it to create the new
print(new_value)
value
7
Finally we print each value as it 12
is generated 17

DeRobertis (UF) Getting Started June 9, 2021 20 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Iterating the Existing Model

Expanding on Python and Excel


I will now expand the existing Excel and Python models to examine
multiple interest rates
Continue viewing the same previously downloaded files.

DeRobertis (UF) Getting Started June 9, 2021 21 / 22


Basic Problem Excel Solution Python Solution Extend & Iterate

Vary Savings Rate Lab

Extending a Simple Retirement Model


1 Now we want to see the effect of savings rate on time until
retirement, in addition to interest rate
2 In both Excel and Python, calculate the years to retirement for
savings rates of 10%, 25%, and 40%, and each of these cases with
each of the interest rate cases, 4%, 5%, and 6%
3 Be sure that you drag formulas in Excel and use for loops in Python
to accomplish this
4 In total you should have 9 calculated years to retirement numbers, in
each of the two models.
Answers: Slide 24 Resources: Slide 25

DeRobertis (UF) Getting Started June 9, 2021 22 / 22


Lecture Resources

Lecture Resources
1 Slides - Getting Started with Python and Excel
2 Lecture Notes - Getting Started with Python and Excel
3 Simple Retirement Model - Excel
4 Simple Retirement Model - Python

DeRobertis (UF) Getting Started June 9, 2021 23 / 22


Vary Savings Rate Lab, Answers
Extending a Simple Retirement Model, Answers
1 Martha has 61.1 years to retirement if she earns a 4% return and saves 10%.
2 Martha has 41.0 years to retirement if she earns a 4% return and saves 25%.
3 Martha has 31.9 years to retirement if she earns a 4% return and saves 40%.
4 Martha has 53.3 years to retirement if she earns a 5% return and saves 10%.
5 Martha has 36.7 years to retirement if she earns a 5% return and saves 25%.
6 Martha has 29.0 years to retirement if she earns a 5% return and saves 40%.
7 Martha has 47.6 years to retirement if she earns a 6% return and saves 10%.
8 Martha has 33.4 years to retirement if she earns a 6% return and saves 25%.
9 Martha has 26.7 years to retirement if she earns a 6% return and saves 40%.

Exercise: Slide 22 Resources: Slide 25

DeRobertis (UF) Getting Started June 9, 2021 24 / 22


Vary Savings Rate Lab Resources

Extending a Simple Retirement Model Resources


1 Simple Retirement Model - Excel
2 Simple Retirement Model - Python
3 Slides - Getting Started with Python and Excel

Exercise: Slide 22 Answers: Slide 24

DeRobertis (UF) Getting Started June 9, 2021 25 / 22

You might also like