Commit 344ddcd4 authored by Dr.李's avatar Dr.李

update analysis api

parent 33f4135c
......@@ -46,26 +46,26 @@ def factor_analysis(factors: pd.DataFrame,
er = factor_processing(factors.values, pre_process, risk_exp, post_process) @ factor_weights
return er_analysis(er,
industry,
d1returns,
constraints,
detail_analysis,
benchmark,
is_tradable,
method,
**kwargs)
def er_analysis(er: np.ndarray,
industry: np.ndarray,
dx_return: np.ndarray,
constraints: Optional[Constraints]=None,
detail_analysis=True,
benchmark: Optional[np.ndarray] = None,
is_tradable: Optional[np.ndarray] = None,
method='risk_neutral',
**kwargs) -> Tuple[pd.DataFrame, Optional[pd.DataFrame]]:
return er_portfolio_analysis(er,
industry,
d1returns,
constraints,
detail_analysis,
benchmark,
is_tradable,
method,
**kwargs)
def er_portfolio_analysis(er: np.ndarray,
industry: np.ndarray,
dx_return: np.ndarray,
constraints: Optional[Constraints]=None,
detail_analysis=True,
benchmark: Optional[np.ndarray] = None,
is_tradable: Optional[np.ndarray] = None,
method='risk_neutral',
**kwargs) -> Tuple[pd.DataFrame, Optional[pd.DataFrame]]:
def create_constraints(benchmark, **kwargs):
if 'lbound' in kwargs:
lbound = kwargs['lbound']
......
......@@ -36,19 +36,13 @@ def quantile_analysis(factors: pd.DataFrame,
post_process = [standardize]
er = factor_processing(factors.values, pre_process, risk_exp, post_process) @ factor_weights
raw_return = q_anl_impl(er, n_bins, dx_return)
if benchmark is not None:
b_ret = np.dot(benchmark, dx_return)
b_total = benchmark.sum()
return raw_return * b_total - b_ret
else:
return raw_return
return er_quantile_analysis(er, n_bins, dx_return, benchmark)
def q_anl_impl(er: np.ndarray,
n_bins: int,
dx_return: np.ndarray) -> np.ndarray:
def er_quantile_analysis(er: np.ndarray,
n_bins: int,
dx_return: np.ndarray,
benchmark: Optional[np.ndarray]=None,) -> np.ndarray:
er = er.flatten()
q_groups = quantile(er, n_bins)
......@@ -57,7 +51,12 @@ def q_anl_impl(er: np.ndarray,
dx_return.shape = -1, 1
group_return = agg_mean(q_groups, dx_return).flatten()
return group_return
if benchmark is not None:
b_ret = np.dot(benchmark, dx_return)
b_total = benchmark.sum()
return group_return * b_total - b_ret
else:
return group_return
if __name__ == '__main__':
......@@ -81,7 +80,7 @@ if __name__ == '__main__':
post_process=[standardize])
er = x_w @ f_df.T
expected = q_anl_impl(er, 5, r)
expected = er_quantile_analysis(er, 5, r)
print(calculated)
print(expected)
......@@ -7,7 +7,7 @@ Created on 2017-8-16
from alphamind.data.engines.sqlengine import SqlEngine
from alphamind.analysis.factoranalysis import factor_analysis
from alphamind.analysis.factoranalysis import er_analysis
from alphamind.analysis.factoranalysis import er_portfolio_analysis
from alphamind.analysis.quantileanalysis import quantile_analysis
from alphamind.data.engines.universe import Universe
from alphamind.data.processing import factor_processing
......@@ -21,7 +21,7 @@ from alphamind.data.engines.sqlengine import macro_styles
__all__ = [
'SqlEngine',
'factor_analysis',
'er_analysis',
'er_portfolio_analysis',
'quantile_analysis',
'Universe',
'factor_processing',
......
......@@ -8,7 +8,7 @@ Created on 2017-8-16
import unittest
import numpy as np
import pandas as pd
from alphamind.analysis.quantileanalysis import q_anl_impl
from alphamind.analysis.quantileanalysis import er_quantile_analysis
from alphamind.analysis.quantileanalysis import quantile_analysis
from alphamind.data.processing import factor_processing
from alphamind.data.standardize import standardize
......@@ -37,7 +37,7 @@ class TestQuantileAnalysis(unittest.TestCase):
s = pd.Series(self.r, index=q_groups)
expected_res = s.groupby(level=0).mean()
calculated_res = q_anl_impl(x, n_bins, self.r)
calculated_res = er_quantile_analysis(x, n_bins, self.r)
np.testing.assert_array_almost_equal(expected_res.values, calculated_res)
......@@ -52,7 +52,7 @@ class TestQuantileAnalysis(unittest.TestCase):
post_process=[])
er = self.x_w @ self.x.T
expected = q_anl_impl(er, self.n_bins, self.r)
expected = er_quantile_analysis(er, self.n_bins, self.r)
np.testing.assert_array_almost_equal(calculated, expected)
def test_quantile_analysis_with_factor_processing(self):
......@@ -70,7 +70,7 @@ class TestQuantileAnalysis(unittest.TestCase):
[winsorize_normal, standardize],
self.risk_exp,
[standardize]).T
expected = q_anl_impl(er, self.n_bins, self.r)
expected = er_quantile_analysis(er, self.n_bins, self.r)
np.testing.assert_array_almost_equal(calculated, expected)
def test_quantile_analysis_with_benchmark(self):
......@@ -89,7 +89,7 @@ class TestQuantileAnalysis(unittest.TestCase):
[winsorize_normal, standardize],
self.risk_exp,
[standardize]).T
raw_er = q_anl_impl(er, self.n_bins, self.r)
raw_er = er_quantile_analysis(er, self.n_bins, self.r)
expected = raw_er * self.b_w.sum() - np.dot(self.b_w, self.r)
np.testing.assert_array_almost_equal(calculated, expected)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment