... background material available at https://github.com/softecon/talks
In [1]:
import this
Python is used by computer programmers and scientists alike. Thus, tools from software engineering are readily available.
Python is an open-source project. This ensures that all implementation details can be critically examined. There are no licence costs and low barriers to recomputability.
Python can be easily linked to high-performance languages such as C and Fortran. Python is ideal for prototyping with a focus on readability, design patterns, and ease of testing.
Python has numerous high-quality libraries for scientific computing under active development.
First things first, here is the ``Hello, World!'' program in Python.
In [2]:
print("Hello, World!")
Most of our required tools are part of the SciPy Stack, a collection of open source software for scientific computing in Python.
|
|
|
|
|
|
|
Depending on your particular specialization, this package might be of additional interest to you, e.g. statsmodels.
In [3]:
# Import relevant libraries from the SciPy Stack
import numpy as np
# Specify parametrization
num_agents = 1000
num_covars = 3
betas_true = np.array([0.22, 0.30, -0.1]).T
# Set a seed to ensure recomputability in light of randomness
np.random.seed(4292367295)
# Sample exogenous agent characteristics from a uniform distribution in
# a given shape
X = np.random.rand(num_agents, num_covars)
# Sample random disturbances from a standard normal distribution and rescale
eps = np.random.normal(scale=0.1, size=num_agents)
# Construct endogenous agent characteristic
Y = np.dot(X, betas_true) + eps
In [4]:
# Import relevant libraries from the SciPy Stack
import statsmodels.api as sm
# Specify and fit the model
rslt = sm.OLS(Y, X).fit()
In [5]:
# Provide some summary information
print(rslt.summary())
In [6]:
# Import relevant libraries from the SciPy Stack
import matplotlib.pyplot as plt
# Initialize canvas
ax = plt.figure(figsize=(12, 8)).add_subplot(111, axisbg='white')
# Plot actual and fitted values
ax.plot(np.dot(X, rslt.params), Y, 'o', label='True')
ax.plot(np.dot(X, rslt.params), rslt.fittedvalues, 'r--.', label="Predicted")
# Set axis labels and ranges
ax.set_xlabel(r'$X\hat{\beta}$', fontsize=20)
ax.set_ylabel(r'$Y$', fontsize=20)
# Remove first element on y-axis
ax.yaxis.get_major_ticks()[0].set_visible(False)
# Add legend
plt.legend(loc='upper center', bbox_to_anchor=(0.50, -0.10),
fancybox=False, frameon=False, shadow=False, ncol=2, fontsize=20)
# Add title
plt.suptitle('Synthetic Sample', fontsize=20)
# Save figure
plt.savefig('images/scatterplot.png', bbox_inches='tight', format='png')
In [7]:
from IPython.display import Image
Image(filename='images/scatterplot.png', width=700, height=700)
Out[7]:
Unit Testing Integration
Graphical Debugger
Version Control Integration
Coding Assistance
Code Completion
Syntax and Error Highlighting
...
Let us check it all out for our Basic Example.
Set up your machine for scientific computing with Python
Visit Continuum Analytics and download Anaconda for your own computer. Anaconda is a free Python distribution with all the required packages to get you started.
Install PyCharm. Make sure to hook it up to your Anacadona distribution (instructions).
Check out the additional resources to dive more into the details.
Gaël Varoquaux, Emmanuelle Gouillart, Olaf Vahtras (eds.). SciPy Lecture Notes, available at http://www.scipy-lectures.org.
Hans Petter Langtangen. A Primer on Scientific Programming with Python, Springer, New York, NY.
Thomas J. Sargent, John Stachurski (2016). Quantitative Economics. Online Lecture Notes.
Software Engineering for Economists Initiative, Online Resources.
Numerous additional lecture notes, tutorials, online courses, and books are available online.
Philipp Eisenhauer
Software Engineering for Economists Initiative
In [8]:
import urllib; from IPython.core.display import HTML
HTML(urllib.urlopen('http://bit.ly/1K5apRH').read())
Out[8]: