Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #14

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ Out[4]:
'excess_annual_volatility': 0.14574262245479186,
'excess_max_drawdown': 0.050301584825935344,
'var': 0.025161857442751474,
'win_rate': 0.3}
'win_rate': 0.3,
'correlation': 0.7136492542281812}
```
5 changes: 5 additions & 0 deletions rqrisk/risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,10 @@ def win_rate(self):
def excess_win_rate(self):
return len(self._portfolio[self._portfolio > self._benchmark]) / self.period_count

@indicator_property(min_period_count=2)
def correlation(self):
# 相关系数算法参考 https://numpy.org/doc/stable/reference/generated/numpy.corrcoef.html?highlight=corr#numpy.corrcoef
return np.corrcoef(self._portfolio, self._benchmark)[0][1]

def all(self):
return {k: getattr(self, k) for k, v in self.__class__.__dict__.items() if isinstance(v, IndicatorProperty)}
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[metadata]
name = rqrisk
version = 1.0.3
version = 1.0.4

[versioneer]
VCS = git
Expand Down
21 changes: 21 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
index=pd.date_range('2000-1-30', periods=9, freq='W')
)

volatile_monthly_benchmark = pd.Series(
np.array([1, 2, -5, 3, 10, -3, -1, 4, 1]) / 100,
index=pd.date_range('2000-1-30', periods=9, freq='M')
)


def _r(returns, benchmark_returns, risk_free_rate, period=DAILY):
if benchmark_returns is None:
Expand Down Expand Up @@ -262,3 +267,19 @@ def _assert(returns, benchmark, desired_win_rate):
_assert(positive_returns, zero_benchmark, 1)
_assert(negative_returns, zero_benchmark, 0)


def test_correlation():
""" 测试相关系数 """
def _assert(returns, benchmark, period):
r = _r(returns, benchmark, 0, period)
assert_almost_equal(r.correlation, returns.corr(benchmark))

_assert(one_return, one_benchmark, DAILY) # np.nan
_assert(positive_returns, simple_benchmark, DAILY) # np.nan
_assert(positive_returns, volatile_benchmark, DAILY) # 0.05773502691896258
_assert(negative_returns, simple_benchmark, DAILY) # np.nan
_assert(negative_returns, volatile_benchmark, DAILY) # -0.32984900530449723
_assert(weekly_returns, simple_weekly_benchamrk, WEEKLY) # np.nan
_assert(weekly_returns, volatile_weekly_benchmark, WEEKLY) # -0.3411258215912419
_assert(monthly_returns, simple_monthly_benchamrk, MONTHLY) # np.nan
_assert(monthly_returns, volatile_monthly_benchmark, MONTHLY) # -0.3411258215912419