Fit a Generalized Least Squares (GLS) regression of dependent_ticker on independent_tickers, given a known error covariance structure omega.

Also known as: GLS.

For more information about the method, see regression_model.get_gls.

Generalised Least Squares (GLS) in Python

get_gls is part of the Econometrics module of the open-source Finance Toolkit. Install it with:

pip install financetoolkit -U

Then call get_gls as shown below.

import numpy as np
import pandas as pd
from financetoolkit import Toolkit

toolkit = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY")

returns = toolkit.econometrics._get_price_column("weekly", "Return")
n = len(returns["AAPL"].dropna())
omega = pd.DataFrame(np.eye(n))

# AAPL (the first ticker) is dependent; MSFT is independent
toolkit.econometrics.get_gls(omega, period="weekly")

Which returns:

  Coefficient Std. Error t-Statistic P-Value
Intercept 0.0016 0.0024 0.6712 0.5031
MSFT 0.8681 0.0596 14.5659 0

Parameters

get_gls accepts the following parameters:

  • omega (pd.DataFrame): The (symmetric, positive-definite) error covariance structure, up to a scalar, shape (n, n).
  • dependent_ticker (str | None, optional): The dependent (predicted) asset. Defaults to None, meaning the Toolkit instance’s first ticker.
  • independent_tickers (str | list[str] | None, optional): The independent (predictor) asset(s). Defaults to None, meaning every other ticker in the Toolkit instance besides dependent_ticker.
  • include_benchmark (bool, optional): Whether to include “Benchmark” in the default independent ticker(s) (has no effect when independent_tickers is given explicitly). Defaults to False.
  • period (str, optional): The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”.
  • column (str, optional): The historical data column to regress on. Defaults to “Return”.
  • add_constant (bool, optional): Whether to include an intercept. Defaults to True.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.
Share