Methods: Python programming for economists
This website contains the material for the MSc course Methods: Python programming for economists.
This website is under construction for 2024/2025
This year the course is taught by:
- Jan Boone
- Özgün Balaban
- Mery Ferrando
- Joyce Ladenstein
- Shaye Olij
Table of Contents
Screencasts
For this course a series of screencasts is available. The screencasts can be found on this webpage.
Organisation of the course
The course consists of a Career Development part and a python programming part. We discuss both briefly in this section.
Career Development part of the course
Career development activities (workshops and career events) are part of the course ‘Methods: Python Programming’. The goal of this career development part of the course is to help students from the Master Economics transition into their careers after graduation. The focus is on personal growth and development and provides practical tools to be well prepared for the job application process.
In both semester 1 and semester 2, workshop(s) will be offered by Student Career Services. To pass the course ‘Methods: Python Programming’, you must participate in at least 1 workshop of your choice and at least 1 career event. You will receive a partial grade once you have completed the Python programming part of the course. At the end of the academic year, you will be assessed whether you have met the set criteria of the career development part of the course through a pass or fail. Only with a positive grade for the Python programming part of the course and a pass for the career development part of the course you have passed the course ‘Methods: Python Programming’..
For more information and for an overview of workshops and career events offered throughout the year check the ‘Career Development for MSc Economics’ Canvas page.
For the python part we will use the following format:
Since we have recorded screencasts, we will not repeat these during the lectures. The lectures are meant for interaction (only).
- in the python lectures we focus on answering your questions;
- in order to prepare these answers, it is useful if you create an issue via this link;
- using the link, create an "issue" where you explain what your question is: what did you try and why did it not work?
- hence not a something like "I do not know the answer to: Question Calculate the total welfare that this planner can achieve. Denote this value
max_welfare
." - but "I tried to calculate total welfare in the following way … but then I get the following error …"
- describing your question like this, helps you better understand what your problem is
- and it helps us to answer the question in class.
- hence not a something like "I do not know the answer to: Question Calculate the total welfare that this planner can achieve. Denote this value
- using the link, create an "issue" where you explain what your question is: what did you try and why did it not work?
- for us to be able to prepare these answers, submit the issue on the Friday before the week in which we discuss the topic
- hence, finish the sections in the week before they are discussed in class
- if you can solve everything yourself, there is no need to attend the lecture (but you are, of course, welcome to do so)
- this is one of the advantages of programming: you can see for yourself whether it works or not.
A suggested workflow to learn programming efficiently is:
- follow the Datacamp courses and make notes of what you learn at Datacamp in a notebook on the jupyterlab server
- in this way it is easy to refer back to useful code snippets that you see on Datacamp
- go through the jupyter notebook for the lecture and try to answer the questions and assignments
- if you get stuck with a problem, watch the corresponding screencast for this part. It gives hints and ways to solve the questions in the notebook
- if you still cannot figure it out, create an issue on github to ask us
- we will answer your questions in class
In September there is a lecture on gender economics. In the lecture Mery will discuss some papers on this topic. There is a notebook where the models discussed are simulated in Python. This notebook is then discussed in a following lecture. The notebook will teach you something about gender economics and Python. At the same time, they show you what we expect you to do in the final assignment for this course: take an existing theory paper and solve the model in Python and simulate outcomes of the model.
Remark: We have been teaching this course for a couple of years now. Every year there are a number of students who attend the lectures without preparing anything and watch the screencasts without trying to program themselves. We live in a free country and you choose how to spend your time. However, we do feel the need to warn you that this strategy does not work well. You actually end up spending far more time on finishing the course than when you follow the planning we suggest below. The reason is as follows: learning a programming language is frustrating. You learn by making mistakes and then trying to figure out what goes wrong. When you make these mistakes during the semester, you are well covered: we are there to help you. When you make these mistakes for the first time when trying to finish your assignment before the deadline, you are in trouble. We will try to help you but not within an hour, or within a day. And because you need to finish before the deadline, you have to figure everything out by yourself under time pressure. Not a pleasant experience and one you can easily avoid. But the choice is yours.
Lecture schedule
day | date | time | staff | room | topic | datacamp | assignment |
---|---|---|---|---|---|---|---|
Fri | 2024-09-06 | 10:45-12:30 | Jan | Cube 216 | Intro python, markdown, github, jupyterlab | ||
Fri | 2024-09-13 | 10:45-12:30 | Joyce, Jan | Cube 216 | Career Development, lists, functions and numpy | Intro (1-4) | |
Fri | 2024-09-20 | 12:45-14:30 | Özgün | Cube 215 | Matplotlib, dictionaries, numpy arrays, pandas | Intermediate (1,2) | |
Fri | 2024-09-27 | 10:45-12:30 | Mery | Cube 216 | gender economics | Intermediate (3,4,5) | |
Thu | 2024-10-17 | 10:45-12:30 | Joyce | E 219 Cult.tuin | Workshop 'stress management' (part 1) | pandas (1,2) | |
Thu | 2024-10-24 | 10:45-12:30 | Joyce | E 219 Cult.tuin | Workshop ‘finding purpose’ (part 1) | pandas (3,4) | |
Thu | 2024-10-31 | 12:45-14:30 | Joyce | E 219 Cult.tuin | Workshop 'stress management' (part 2) | ||
Fri | 2024-11-01 | 14:45-16:30 | Özgün | Cube 215 | Booleans, if statements, loops | ||
Fri | 2024-11-08 | 10:45-12:30 | Jan | Cube 216 | Pandas: dataframe, groupby, selection | assignment 1 (first time) | |
Fri | 2024-11-14 | 12:45-14:30 | Joyce | Cube 15 | Workshop ‘finding purpose’ (part 2) | ||
Fri | 2024-11-15 | 10:45-12:30 | Jan | Cube 216 | The market and asymmetric information | assignment 1 (second time) | |
Fri | 2024-11-22 | 10:45-12:30 | Jan | Cube 216 | Financial crisis, Empirical research | assignment 2 (first time) | |
Fri | 2024-11-29 | 10:45-12:30 | Jan | Cube 216 | Notebook gender economics and Q&A | assignment 2 (second time) | |
Fri | 2024-12-06 | 10:45-12:30 | Özgün/Jan | CZ 114 | Reserve | ||
Mon | 2025-01-06 | DEADLINE final assignment |
If you have programmed with python before, there is no need to attend the introduction to python lectures. In this case, attend the first lecture, the lectures by Joyce and Mery and then join the python lectures again when we start with "The market and asymmetric information".
First Lecture
Initial Assignment
Do the following three steps:
- create a github account on github (you need this account to submit your final assignment)
- we advise you to do this before Friday September 13, 2024
- go to
- jupyter lab
- IT suggests that you use the Firefox browser to access jupyter lab
- sometimes it helps to access jupyter lab with an incognito/private window
- or –if all else fails– you can use google's colab
- jupyter lab
- create a new python notebook and type the following code in the first cell:
%%bash git clone https://github.com/janboone/Python-programming-for-economists.git
- then press the Shift key and Enter key at the same time
- this creates a folder on the server `Python-programming-for-economists` that contains the material for the course.
- Note: you can only run this command once. If you run it again, you get an error since the folder already exists.
- If you want to see the assignments in jupyter lab, you can also type:
%%bash git clone https://github.com/janboone/python_assignments
Assignments
For this course there are three assignments: two of these are voluntary (assignments 1 and 2), the final assignment is mandatory.
- you can view the assignments in this repository: https://github.com/janboone/python_assignments
- instructions for the assignments can be found below.
Datacamp
You can get access to Datacamp via the university website.
From Datacamp, do the following courses:
- Intro: https://www.datacamp.com/courses/intro-to-python-for-data-science
- Intermediate: https://www.datacamp.com/courses/intermediate-python-for-data-science
- Pandas: https://learn.datacamp.com/courses/data-manipulation-with-pandas
These courses teach you the basic Python syntax. In the lectures and notebook for the course, we use parts of Python more specific to economics; e.g. commands to solve equations, equilibria etc. These parts of the course complement each other. It is not the case that all Python that we use, you will first see in Datacamp.
It is up to you how to combine the Datacamp courses with the Screencasts. We suggest to finish the Datacamp Intro course first. As there is no economics on Datacamp, some students prefer to start with the screencasts after the Intro. Others prefer to finish more Datacamp courses before they start on the screencasts. Just see what works for you. But make sure you follow the planning above, otherwise you might get lost if you are too far behind and the lectures will not be as useful to you.
Most students like Datacamp to get used to the python syntax. A minority of students really dislike Datacamp. If you do not like it either, you can also read Think Python. You can buy the book or read the free online version. The jupyter notebooks can be found on github. You can clone this repository and work with the notebooks. You can also find some wonderful presentations on Youtube by the author Allen Downey.
Deadlines
- The deadlines for assignments 1 and 2 are in the lecture schedule above.
- The deadline for the final python assignment is: Monday January 6th, 2025.
- The resit deadline for the python assignment is: Monday May 12th, 2025. Let us know by email that you have submitted your assignment for the resit.
- Your grade is determined by:
- a "pass" on the Career Services assignments;
- final assignment;
- a bonus point if you get a "pass" on assignments 1 and 2.
Follow the instructions below and on Canvas explaining how to submit an assignment on github and fill in the google form etc.
Also note the rules for the resit of the final assignment in case you submitted an assignment for the first exam opportunity (you cannot discuss/program the same paper twice for your assignment).
Questions
If you have questions/comments about this course, go to the issues page open a new issue (with the green "New issue" button) and type your question. Use a title that is informative (e.g. not "question", but "question about the second assignment"). Go to the next box ("Leave a comment") and type your question. Then click on "Submit new issue". We will answer your question as quickly as possible.
The advantages of the issue page include:
- if you have a question, other students may have it as well; in this way we answer the questions in a way that everyone can see it. Also before asking the question, you may want to check whether it was asked/answered before on the issue page
- we answer your question more quickly than when you email us
- you increase your knowledge of github!
Only when you need to include privately sensitive information ("my cat has passed away"), you can send an email.
In order to post issues, you need to create a github account (which you need anyway to follow this course).
Note that if your question is related to another issue, you can react to the earlier issue and leave a comment in that "conversation".
Assignments
For this course there are three assignments. The first two assignments are relatively small programming assignments. You can find them in this repository: https://github.com/janboone/python_assignments: assignment_1.ipynb, assignment_2.ipynb
. If you finish both assignments before their deadlines and done a good job (get a pass on both assignments), you earn one bonus point for the final assignment. You get either no bonus point or one point (we do not grade 0.38 bonus point…).
Note that github records the time at which you submit your assignment.
Each assignment is submitted two times. In particular, to get a pass on assignments 1 and 2, you need to do the following for each assignment:
- finish and submit the assignment before the deadline (see Lecture schedule above)
- present and explain the python code that you use;
- you do not necessarily fail if you cannot solve the problem (completely);
- use the notebook to report what you tried, what you googled, what worked and what did not work and explain why;
- submit the assignment on your team's github classroom page;
- after the assignment is discussed in class:
- complete the sections:
- What did you get wrong the first time and why?
- What did you learn from this assignment?
- and submit the assignment again on github.
- complete the sections:
- we grade both assignments after assignment 2 has been discussed in class and submitted by you again.
The idea of assignments 1 and 2 is the following. These are challenging programming assignments. You need to think about the problem, google for the relevant python code and explain the code that you use. Even if you do not succeed, you can still explain to us what you tried and why it did (or did not) work. Give us the code that you have and explain to us what the code does or is supposed to do.
This resembles the way you will use python in practice. There will not be a step-by-step guide of the things you need to do. Mostly you will face a (sometimes vague) problem, and need to figure out how programming can help you to solve this.
The assignment_1.ipynb
notebook starts with an example assignment that we solve for you to give you an idea of what we expect.
The final assignment is more involved as we explain below.
Note that you can do the assignments alone or with at max. one other student (i.e. max group size is 2). Further, the assignments that you do, you do with the same team (i.e. either alone or with the same fellow student).
Submitting assignments
Assignment 1 and 2 and the final assignment are all submitted using github classroom:
- on Canvas we will give you the link to the github repos. with the assignment notebooks;
- instructions on how to work with github classroom can be found here
- to submit your assignments:
- do not change the name of the assignment notebooks
- when you submit your first assignment, fill in the google form where the link to the form is on Canvas
- push the assignment notebook to your team's github classroom repository
Final Assignment
- for the deadlines of the final assignment, see Deadlines above
what we are looking for
The idea of the final assignment is that you report your findings in a transparent way that can easily be verified/reproduced by others. The intended audience is your fellow students. They should be able to understand the code you write together with the explanations that you give for this code.
The following ingredients will be important when we evaluate your final assignment:
- Start from a theory paper; e.g. one you have read for another course.
- Briefly describe what the paper does and what the main results are.
- Then formulate a clear and transparent question that cannot be immediately answered by the paper.
- Extend the paper's model (a bit) using the fact that you will simulate the model and do not need to provide an analytical solution.
- note: we do not expect a major extension of the model; just a small change and use simulations to show how results differ due to this adaptation of the model.
- hint: choose a question/extension where you can show off your programming skills!
- Extend the paper's model (a bit) using the fact that you will simulate the model and do not need to provide an analytical solution.
- Briefly motivate why this question is interesting.
- Give the answer that you find (as a preview).
- Mention the main assumptions that you need to get this answer.
- Use \(\LaTeX\) to introduce and explain the model of the paper. Describe the main equations (using \(\LaTeX\)) of the model.
- When you use information (e.g. a literature reference), create a link to this information. The reader then only needs to click to find the relevant information.
- Describe your (small) extension of the model.
- Explain how you move from the analytical equations of the paper to Python code.
- Use some optimization methods (e.g. agents maximize utility or firms minimize costs).
- Solve for an equilibrium using Python (using fixed points).
- Simulate outcomes by using different values for parameters and save the outcomes of the simulations in a pandas dataframe.
- Explain your code, the reader –think of your fellow students– must be able to easily follow what you are doing.
- Explain your code in Markdown blocks, not as comments in code blocks.
- Present graphs of your simulation results using
matplotlib
. - Discuss what the figures show (e.g. \(x\) is increasing in \(y\)) and explain the economic intuition for this relation (between \(x\) and \(y\)).
- Present a clear conclusion/answer to your question.
- Finish with a brief discussion of your results.
Remark:
- you can copy code from the web; but
- make sure that you explain the code that you use so that another student of the course understands it and can use it;
- give the reference of the code that you copy.
resit of final assignment
The resit of the final assignment needs to start from a new paper compared to the one you handed in before. Simply adjusting your first submission based on our feedback will be not be enough.
Otherwise, follow the procedure above on how to submit the assignment and fill in the google form (if you have not done so before). Also send us an email that you submitted the assignment for the resit.
Python as programming language
We use Python as programming language. A fair question is: why Python? The non-scientific answer is: because we like it a lot.
You can also check the following links:
- https://www.datacamp.com/community/blog/python-scientific-computing-case
- https://github.com/jupyter/jupyter/wiki
Resources for python:
- https://scipy-lectures.github.io/
- http://www.physics.nyu.edu/pine/pymanual/html/pymanMaster.html
- https://aeturrell.github.io/coding-for-economists/intro.html (introduction to python with economics in mind)
- https://python.quantecon.org/intro.html (fairly advanced economics with python)
- https://www.youtube.com/playlist?list=PLi01XoE8jYohWFPpC17Z-wWhPOSuh8Er- (video lectures on a number of python topics)
Jupyter notebooks
Markdown
For the assignment it is useful to know a bit of markdown. You can either google "markdown tutorial" or use one of the following websites:
\(\LaTeX\)
For the python assignment it is useful to familiarize yourself with \(\LaTeX\). Note that you do not need to type a whole document in \(\LaTeX\) (so don't worry about preambles etc.), you just need to know how to type \(x^2\), \(\alpha,\beta\) or have math displayed like
\begin{equation} a^2 + b^2 = c^2 \end{equation}Google "latex tutorial" or go to pages like:
- http://users.dickinson.edu/~richesod/latex/latexcheatsheet.pdf
- https://www.latex-tutorial.com/tutorials/amsmath/
- http://www.andy-roberts.net/writing/latex/mathematics_1
and focus on typesetting.