Skip to content

Commit cde65e6

Browse files
remove sklearn dependency
1 parent 88c86ef commit cde65e6

File tree

2 files changed

+0
-58
lines changed

2 files changed

+0
-58
lines changed

pyfolio/timeseries.py

-57
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import pandas as pd
2323
import scipy as sp
2424
import scipy.stats as stats
25-
from sklearn import linear_model
2625

2726
from .deprecate import deprecated
2827
from .interesting_periods import PERIODS
@@ -547,62 +546,6 @@ def rolling_beta(returns, factor_returns,
547546

548547
return out
549548

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-
606549
def gross_lev(positions):
607550
"""
608551
Calculates the gross leverage of a strategy.

setup.py

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
'pandas>=0.18.1',
4949
'pytz>=2014.10',
5050
'scipy>=0.14.0',
51-
'scikit-learn>=0.16.1',
5251
'seaborn>=0.10.1',
5352
'empyrical>=0.5.0',
5453
'quantrocket-moonchart>=1.8.0'

0 commit comments

Comments
 (0)