Book Image

Mastering Python for Finance

Book Image

Mastering Python for Finance

Overview of this book

Table of Contents (17 chapters)
Mastering Python for Finance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Multivariate linear regression of factor models


Many Python packages such as SciPy come with several variants of regression functions. In particular, the statsmodels package is a complement to SciPy with descriptive statistics and estimation of statistical models. The official page for statsmodels is http://statsmodels.sourceforge.net/.

In this example, we will use the ols function of the statsmodels module to perform an ordinary least squares regression and view its summary.

Let's assume that you have implemented an APT model with seven factors that return the values of Y. Consider the following set of data collected over 9 time periods, to . X1 to X7 are independent variables observed at each period. The regression problem is therefore structured as:.

A simple ordinary least squares regression on values of X and Y can be performed with the following code:

""" Least squares regression with statsmodels """
import numpy as np
import statsmodels.api as sm

# Generate some sample data
num_periods...