0

After reading some papers on Hedge fund replication Hasanhodzic & Lo and more globally on returns-based style analysis Can active equity managers be cloned using factor funds, I would try by myself to see If I can roughly copy an Equity index or Fund returns. I tried to do a rolling OLS with statsmodels but I quickly see that I need constraints for beta coefficients.

I try to fit :

Rfund= ∝ + ∑ β*Rriskfactor + ϵ

Indeed, for practical reasons (i.e get directly portfolio weights for each risk factor) I need that the sum of β = 1. Regression coefficients can be negative as a long/short strategies are employed.

Could you please give me a solution to use in Python as it seems that statsmodels can't be constrained. I see that maybe I should use Scipy minimize or cvxopt for quadratic program but I'm not familiar at all with those methods.

here my first attempt before facing constraints issues.

Thanks for your help !

import pandas as pd
from pandas_datareader import data as pdr
import datetime
import statsmodels.api as sm
from statsmodels.regression.rolling import RollingOLS
import matplotlib.pyplot as plt


all_returns = pd.DataFrame()

tickers = ['^GSPC', '^STOXX50E', 'IEF', 'EURUSD=X', 'CL=F', 'NG=F', 'HG=F', 'GC=F', 'AQMRX']
end_date = datetime.datetime.now() - datetime.timedelta(-1)
start_date = datetime.date(2018, 12, 30)

for t in tickers:
    df = pdr.DataReader(t, 'yahoo', start_date, end_date)
    all_returns = all_returns.assign(**{t: df['Close'].pct_change()})
    all_returns = all_returns.dropna()

x = all_returns[['^GSPC', '^STOXX50E', 'IEF', 'EURUSD=X', 'CL=F', 'NG=F', 'HG=F', 'GC=F']]
y = all_returns['AQMRX']

x = sm.add_constant(x)
rols = RollingOLS(y,x, window=60)
rres = rols.fit()
fig = rres.plot_recursive_coefficient(variables=['^GSPC','CL=F','IEF'], figsize=(14, 18))
plt.show()
  • GLM (gaussian) has `fit_constrained` method for imposing linear constraints https://www.statsmodels.org/dev/generated/statsmodels.genmod.generalized_linear_model.GLM.fit_constrained.html However, there is no `Rolling` version of it. – Josef Aug 20 '22 at 19:37
  • In simple cases like this it is possible to explicitly recode `x` to impose the constraint – Josef Aug 20 '22 at 19:42
  • Thanks for your feedback @Josef. So if I well understand using GLM for example : mod = glm("AQMRX ~ GSPC + STOXX50 + IEF + EURUSD + Crude", data=all_returns) res_c = mod.fit_constrained('GSPC + STOXX50 + IEF + EURUSD + Crude = 1') – lavibration Aug 22 '22 at 10:22
  • yes, that should be correct. – Josef Aug 22 '22 at 15:46
  • Sorry for the long time feedback it is indeed the right solution, but I have another question. How can I achieve two constraints : sum = 1 and each coef < 2 or -2 ? As I deal with portfolio positionig I would prevent too much leverage – lavibration Sep 02 '22 at 22:24
  • statsmodels does not support inference for inequality constraints that can be binding because inference is nonstandard and very difficult to implement. However scipy has constrained or bounded optimizers, e.g. simplex constraints https://github.com/statsmodels/statsmodels/pull/3647/files#diff-adf898fae7779e8506a9501f647c96267a4cecb77558a0a32d61ec07fb9ed44aR43 or https://github.com/statsmodels/statsmodels/pull/8317 – Josef Sep 03 '22 at 14:02

0 Answers0