Preliminaries for getting starting with the code examples
To begin following the Python code examples provided in this supplement you will first need to install Python, Jupyter notebook and the required Python packages. We recommend installing Python via Anaconda. The Anaconda distribution is easy to get started with and includes a large number of popular packages for scientific computing with the installation.
After installing Python it we recommend building a virtual environment to run the code in this book. We will use the built-in venv package. To build the environment on your personal computer, open the Anaconda Prompt, nevigate to your desired build location and enter the following command:
python -m venv venv
This creates the folder called venv. Note that the second venv in the above command is the name of the virtual environment and thus can be changed into other informative names for your own reference.
Then, to install all the required python packages within this newly created environment run we first activate the virtual environment.
On Windows:
venv\Scripts\activate.bat
On MacOS:
source venv/bin/activate
We will require the following Python packages to run the code in this book.
numpy
pandas
scipy
scikit-learn
numdifftools
statsmodels
matplotlib
notebook
openpyxl
linearmodels
seaborn
xlrd
You can install each package separately in the virtual environment. Alternatively, copy the the above list (without the index numbers) into a file called requirements.txt and run the following to install all packages simultaneously
pip install -r requirements.txt
To create the ipython kernel for use in the jupyter notebooks run the following command inside the venv,
To test your installation try launching Jupyter Notebook as follows
jupyter notebook
Then, create a new notebook called test.ipynb. Choose the SFA_book_venv kernel from the kernel -> change kernel -> SFA_book_venv using the menu bar within the jupyter notebook. Try running the following code and verifying that you get the same output.
import numpy as np np.random.seed(123) X = np.random.normal(size=(10, 5)) y = np.random.normal(size=(10,1)) b = np.linalg.pinv(X.T@X)@X.T@yprint(b)