def rolling_regression(returns, factor_returns,<EOL>rolling_window=APPROX_BDAYS_PER_MONTH * <NUM_LIT:6>,<EOL>nan_threshold=<NUM_LIT:0.1>):
<EOL>ret_no_na = returns.dropna()<EOL>columns = ['<STR_LIT>'] + factor_returns.columns.tolist()<EOL>rolling_risk = pd.DataFrame(columns=columns,<EOL>index=ret_no_na.index)<EOL>rolling_risk.index.name = '<STR_LIT>'<EOL>for beg, end in zip(ret_no_na.index[:-rolling_window],<EOL>ret_no_na.index[rolling_window:]):<EOL><INDENT>returns_period = ret_no_na[beg:end]<EOL>factor_returns_period = factor_returns.loc[returns_period.index]<EOL>if np.all(factor_returns_period.isnull().mean()) < nan_threshold:<EOL><INDENT>factor_returns_period_dnan = factor_returns_period.dropna()<EOL>reg = linear_model.LinearRegression(fit_intercept=True).fit(<EOL>factor_returns_period_dnan,<EOL>returns_period.loc[factor_returns_period_dnan.index])<EOL>rolling_risk.loc[end, factor_returns.columns] = reg.coef_<EOL>rolling_risk.loc[end, '<STR_LIT>'] = reg.intercept_<EOL><DEDENT><DEDENT>return rolling_risk<EOL>
Computes rolling factor betas using a multivariate linear regression
(separate linear regressions is problematic because the factors may be
confounded).

Parameters
----------
returns : pd.Series
    Daily returns of the strategy, noncumulative.
     - See full explanation in tears.create_full_tear_sheet.
factor_returns : pd.DataFrame
    Daily noncumulative returns of the benchmark factor to which betas are
    computed. Usually a benchmark such as market returns.
     - Computes rolling beta for each column.
     - This is in the same style as returns.
rolling_window : int, optional
    The days window over which to compute the beta. Defaults to 6 months.
nan_threshold : float, optional
    If there are more than this fraction of NaNs, the rolling regression
    for the given date will be skipped.

Returns
-------
pandas.DataFrame
    DataFrame containing rolling beta coefficients to SMB, HML and UMD