Fit a Weighted Least Squares (WLS) regression of dependent_ticker on independent_tickers.

Also known as: weighted regression.

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

Weighted Least Squares (WLS) in Python

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

pip install financetoolkit -U

Then call get_wls as shown below.

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")
weights = pd.Series(1.0, index=returns.index)

# AAPL (the first ticker) is dependent, MSFT is independent
toolkit.econometrics.get_wls(weights, 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_wls accepts the following parameters:

  • weights (pd.Series): The (positive) weight of each observation, aligned to the same period index as the return data (e.g. 1 / rolling_variance).
  • 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.
  • cov_type (str, optional): Which covariance estimator to use, applied to the weighted/ transformed problem – see get_ols’s cov_type for the full list of options. Defaults to “nonrobust”.
  • clusters (pd.Series | None, optional): The cluster label for each observation, required when cov_type="cluster". Defaults to None.
  • maxlags (int | None, optional): The maximum lag to include when estimating the HAC (Newey-West) covariance matrix, required when cov_type="HAC". See get_ols’s maxlags for the rule-of-thumb formula. Defaults to None.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.
Share