Installing Python

This short guide will help you get started with the anaconda python distribution on your PC or MAC. If you happen to run linux, please contact Professor Hicks directly.

Important Note:

Please use a pristine Anaconda Python install for this class. If you have an existing Anaconda install that you would like to keep, please see me about your options. If you have an existing Anaconda install, and don't care to keep it, just delete the folder prior to initiating the installation steps below. Just take care to save any of your work in the Anaconda folder before deleting it.

Installing Python

Both the PC and MAC installers can be downloaded at http://continuum.io/downloads. Choose the 64bit Python 3.x version for your platform (3.6 at the time of this writing).

This short guide will help you get started with the anaconda python distribution on your PC or MAC. If you happen to run linux, please contact Professor Hicks directly.

Once you have downloaded the installer, do the following:

  1. Navigate to your your browser's download folder
  2. Open the python executable downloaded in step (1) (e.g. Anaconda-3.x.x-Windows-x_86_64.exe [PC] or Anaconda-3.x-mac-x_86_64.dmg [MAC])
  3. Click through the install screens and choose to install "Just Me" and don't change the default install location. Leave any "Advanced Options" at their default values.

Using Python

Now that python is installed we can use it. There are several ways to run python code, and I will show you a method allowing you to use your web browser as a "front-end" for python.

We will be using the Ipython Notebook (now called Jupyter). The easiest way to open it is to locate and use the Anaconda Launcher. Hopefully, the install process will put this in your Program Files (PC) or Applications (Mac). If you see a new icon labeled Anaconda Launcher, click it and choose ipython notebook. Sometimes, the install process doesn't create the Anaconda icon. In this case, it is possible to locate it in the Anaconda folder in your local home directory.

In case you can't find the Anaconda icon, you can also launch it from the terminal or command prompt window, type ipython notebook.

If you have made it this far, your web browser should pop up and you should see a window like this (note: the actual filenames and folders you will see will reflect the actual information you have on your computer):

This listing allows you to see existing and running Ipython Notebooks. You won't have any, so let's create one. You can navigate to a folder of your choosing and create a new notebook by clicking on New and then Python 3 as shown below [Note, the screenshot below is from an old python version and shows Python 2):

Testing the installation

Having created a new notebook, you should see a window that looks like this:

You can re-name your new untitled notebook by clicking on Untitled and changing the name:

Ipython Notebooks have execution blocks where you can enter and execute python code. These blocks look like this:

Copy and paste the following code into the first (and only at this point) execution block:

%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.formula.api as sm
import sympy as sympy

You should see output like this after pressing the shift enter keys at the same time to execute the code:

In [1]:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.formula.api as sm
import sympy as sympy

Next, we will make sure things are working. Paste this code into the next execution cell and press shift enter:

In [2]:
# create some toy data
time=np.arange(1,101)
price = np.random.normal(10,3,(np.size(time)))

# load into pandas dataframe
df = pd.DataFrame(zip(time,price),columns=['time','price'])

# run summary statistics
df.describe()
Out[2]:
time price
count 100.000000 100.000000
mean 50.500000 9.866077
std 29.011492 2.946530
min 1.000000 2.643740
25% 25.750000 8.113189
50% 50.500000 9.724360
75% 75.250000 11.569507
max 100.000000 18.147543

We can also visualize our data easily using the pandas package:

In [3]:
# plot histogram of price
df.price.plot(kind='hist')
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f0a1deddfd0>
In [4]:
# or a trace plot through time:
df.price.plot()
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f09e1adb050>

Next we will test the statsmodels library:

In [5]:
import statsmodels.formula.api as smf
formula = "price ~ time"
results = smf.ols(formula,df).fit()
print(results.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                  price   R-squared:                       0.002
Model:                            OLS   Adj. R-squared:                 -0.008
Method:                 Least Squares   F-statistic:                    0.2010
Date:                Tue, 25 Aug 2015   Prob (F-statistic):              0.655
Time:                        07:35:12   Log-Likelihood:                -249.35
No. Observations:                 100   AIC:                             502.7
Df Residuals:                      98   BIC:                             507.9
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept      9.6340      0.596     16.160      0.000         8.451    10.817
time           0.0046      0.010      0.448      0.655        -0.016     0.025
==============================================================================
Omnibus:                        0.653   Durbin-Watson:                   2.125
Prob(Omnibus):                  0.721   Jarque-Bera (JB):                0.250
Skew:                           0.036   Prob(JB):                        0.883
Kurtosis:                       3.234   Cond. No.                         117.
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

Markdown Text and Notebook Annotations

Changing the execution cell type to markdown here:

allows you to enter markdown compliant passages and get nicely formatted text when you execute the cell by pressing shift enter. For example, executing the following markdown code in a markdown execution cell:

This is some markdown code.  I can type sentences and paragraphs here.  Or, if I want a numbered list, here it is:

1. Dogs
2. Cats
3. Elephants

I can include links in my markdown passages:
![](http://rlhick.people.wm.edu/site_pics/cute-kitten-saying-hello.jpg)

And I can include latex equations inline: $\frac{\alpha}{\beta}$.  Or I can put them on their own line
$$
W_j = \sum_{i\ne j} W_{ij} \epsilon_i
$$

results in this output:

This is some markdown code. I can type sentences and paragraphs here. Or, if I want a numbered list, here it is:

  1. Dogs
  2. Cats
  3. Elephants

I can include links in my markdown passages:

And I can include latex equations inline: $\frac{\alpha}{\beta}$. Or I can put them on their own line $$ W_j = \sum_{i\ne j} W_{ij} \epsilon_i $$

Youtube Videos

In [1]:
from IPython.display import YouTubeVideo
YouTubeVideo("d0nERTFo-Sk")
Out[1]:

Installing Additional Packages

Anaconda has many, but not all, packages we might use. For example, seaborn isn't included. To install it (and other python packages you want that aren't packaged with Anaconda), follow these steps:

Mac or Linux

From inside the ipython notebook desribed above, choose Terminal from the New dropdown box shown below:

You should see a terminal screen that looks something like this:

In the terminal prompt, type

conda install seaborn

press enter and after a little time, it should install without incident.

Windows

In Windows, choose Program Files -> Anaconda2 (64-bit) and you should see the following list of programs:

Choose Anaconda Prompt and at the command prompt, type

conda install seaborn

press enter and after a little time, it should install without incident.

Installing pymc3

The best way to install packages is via the conda command.

Mac or Linux

As described above, goto the Terminal and type:

conda install gcc
conda config --add channels conda-forge
conda install pymc3

and press enter. Again after a short while, the installation should complete without incident.

Windows

Unfortunately, there are a few extra steps for Windows machines.

  1. Download and initiate the git software from here: http://git-scm.com/download/win

  2. On most of the screens click ok and go with default options. But there are some exceptions.

    • Uncheck everything except Git Bash Here
    • Ensure that you Use git from the Windows Command Prompt
  3. Proceed to the Anaconda Prompt in Program Files-> Anaconda2 (64-bit):
  4. At the Anaconda prompt type
    conda install mingw libpython
  5. At the Anaconda prompt, type
     conda config --add channels conda-forge
     conda install pymc3
    and press enter. Again after a short while, the installation should complete without incident.