|
22 | 22 | import pandas as pd
|
23 | 23 | import scipy as sp
|
24 | 24 | import scipy.stats as stats
|
25 |
| -from sklearn import linear_model |
26 | 25 |
|
27 | 26 | from .deprecate import deprecated
|
28 | 27 | from .interesting_periods import PERIODS
|
@@ -547,62 +546,6 @@ def rolling_beta(returns, factor_returns,
|
547 | 546 |
|
548 | 547 | return out
|
549 | 548 |
|
550 |
| - |
551 |
| -def rolling_regression(returns, factor_returns, |
552 |
| - rolling_window=APPROX_BDAYS_PER_MONTH * 6, |
553 |
| - nan_threshold=0.1): |
554 |
| - """ |
555 |
| - Computes rolling factor betas using a multivariate linear regression |
556 |
| - (separate linear regressions is problematic because the factors may be |
557 |
| - confounded). |
558 |
| -
|
559 |
| - Parameters |
560 |
| - ---------- |
561 |
| - returns : pd.Series |
562 |
| - Daily returns of the strategy, noncumulative. |
563 |
| - - See full explanation in tears.create_full_tear_sheet. |
564 |
| - factor_returns : pd.DataFrame |
565 |
| - Daily noncumulative returns of the benchmark factor to which betas are |
566 |
| - computed. Usually a benchmark such as market returns. |
567 |
| - - Computes rolling beta for each column. |
568 |
| - - This is in the same style as returns. |
569 |
| - rolling_window : int, optional |
570 |
| - The days window over which to compute the beta. Defaults to 6 months. |
571 |
| - nan_threshold : float, optional |
572 |
| - If there are more than this fraction of NaNs, the rolling regression |
573 |
| - for the given date will be skipped. |
574 |
| -
|
575 |
| - Returns |
576 |
| - ------- |
577 |
| - pandas.DataFrame |
578 |
| - DataFrame containing rolling beta coefficients to SMB, HML and UMD |
579 |
| - """ |
580 |
| - |
581 |
| - # We need to drop NaNs to regress |
582 |
| - ret_no_na = returns.dropna() |
583 |
| - |
584 |
| - columns = ['alpha'] + factor_returns.columns.tolist() |
585 |
| - rolling_risk = pd.DataFrame(columns=columns, |
586 |
| - index=ret_no_na.index) |
587 |
| - |
588 |
| - rolling_risk.index.name = 'dt' |
589 |
| - |
590 |
| - for beg, end in zip(ret_no_na.index[:-rolling_window], |
591 |
| - ret_no_na.index[rolling_window:]): |
592 |
| - returns_period = ret_no_na[beg:end] |
593 |
| - factor_returns_period = factor_returns.loc[returns_period.index] |
594 |
| - |
595 |
| - if np.all(factor_returns_period.isnull().mean()) < nan_threshold: |
596 |
| - factor_returns_period_dnan = factor_returns_period.dropna() |
597 |
| - reg = linear_model.LinearRegression(fit_intercept=True).fit( |
598 |
| - factor_returns_period_dnan, |
599 |
| - returns_period.loc[factor_returns_period_dnan.index]) |
600 |
| - rolling_risk.loc[end, factor_returns.columns] = reg.coef_ |
601 |
| - rolling_risk.loc[end, 'alpha'] = reg.intercept_ |
602 |
| - |
603 |
| - return rolling_risk |
604 |
| - |
605 |
| - |
606 | 549 | def gross_lev(positions):
|
607 | 550 | """
|
608 | 551 | Calculates the gross leverage of a strategy.
|
|
0 commit comments