Commit e0bfc963 authored by Dr.李's avatar Dr.李
parents 01407313 cd3087a0
# -*- coding: utf-8 -*-
"""
Created on 2017-5-17
@author: cheng.li
"""
...@@ -76,10 +76,7 @@ def cross_section_analysis(ref_date, ...@@ -76,10 +76,7 @@ def cross_section_analysis(ref_date,
if __name__ == '__main__': if __name__ == '__main__':
import numpy as np from alphamind.api import SqlEngine, Universe, risk_styles, industry_styles
import pandas as pd
import statsmodels.api as sm
from alphamind.api import *
factor_name = 'SIZE' factor_name = 'SIZE'
data_source = 'postgres+psycopg2://postgres:A12345678!@10.63.6.220/alpha' data_source = 'postgres+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
......
...@@ -14,7 +14,7 @@ from alphamind.data.standardize import standardize ...@@ -14,7 +14,7 @@ from alphamind.data.standardize import standardize
from alphamind.data.winsorize import winsorize_normal from alphamind.data.winsorize import winsorize_normal
from alphamind.portfolio.constraints import Constraints from alphamind.portfolio.constraints import Constraints
from alphamind.portfolio.constraints import LinearConstraints from alphamind.portfolio.constraints import LinearConstraints
from alphamind.portfolio.longshortbulder import long_short_build from alphamind.portfolio.longshortbulder import long_short_builder
from alphamind.portfolio.rankbuilder import rank_build from alphamind.portfolio.rankbuilder import rank_build
from alphamind.portfolio.linearbuilder import linear_builder from alphamind.portfolio.linearbuilder import linear_builder
from alphamind.portfolio.meanvariancebuilder import mean_variance_builder from alphamind.portfolio.meanvariancebuilder import mean_variance_builder
...@@ -109,7 +109,7 @@ def er_portfolio_analysis(er: np.ndarray, ...@@ -109,7 +109,7 @@ def er_portfolio_analysis(er: np.ndarray,
weights = rank_build(er, use_rank=kwargs['use_rank'], masks=is_tradable).flatten() * benchmark.sum() / kwargs[ weights = rank_build(er, use_rank=kwargs['use_rank'], masks=is_tradable).flatten() * benchmark.sum() / kwargs[
'use_rank'] 'use_rank']
elif method == 'ls' or method == 'long_short': elif method == 'ls' or method == 'long_short':
weights = long_short_build(er).flatten() weights = long_short_builder(er).flatten()
elif method == 'mv' or method == 'mean_variance': elif method == 'mv' or method == 'mean_variance':
lbound, ubound, cons_exp, risk_lbound, risk_ubound = create_constraints(benchmark, **kwargs) lbound, ubound, cons_exp, risk_lbound, risk_ubound = create_constraints(benchmark, **kwargs)
cov = kwargs['cov'] cov = kwargs['cov']
......
# -*- coding: utf-8 -*-
"""
Created on 2018-1-15
@author: cheng.li
"""
import numpy as np
from alphamind.data.standardize import standardize
def factor_turn_over(factor_values: np.ndarray,
trade_dates: np.ndarray,
codes: np.ndarray,
use_standize: bool=True):
if use_standize:
factor_values = standardize(factor_values, trade_dates)
if __name__ == '__main__':
from alphamind.api import *
engine = SqlEngine()
factor = 'ep_q'
freq = '5b'
start_date = '2017-06-01'
end_date = '2017-08-01'
universe = Universe('custom', ['zz500'])
...@@ -11,15 +11,15 @@ from alphamind.utilities import simple_abssum ...@@ -11,15 +11,15 @@ from alphamind.utilities import simple_abssum
from alphamind.utilities import transform from alphamind.utilities import transform
def long_short_build(er: np.ndarray, def long_short_builder(er: np.ndarray,
leverage: float=1., leverage: float = 1.,
groups: np.ndarray=None, groups: np.ndarray = None,
masks: np.ndarray=None) -> np.ndarray: masks: np.ndarray = None) -> np.ndarray:
er = er.copy() er = er.copy()
if masks is not None: if masks is not None:
er[~masks] = 0. er[masks] = 0.
er[~masks] = er[~masks] - er[~masks].mean()
if er.ndim == 1: if er.ndim == 1:
er = er.reshape((-1, 1)) er = er.reshape((-1, 1))
......
...@@ -8,7 +8,7 @@ Created on 2017-5-9 ...@@ -8,7 +8,7 @@ Created on 2017-5-9
import unittest import unittest
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from alphamind.portfolio.longshortbulder import long_short_build from alphamind.portfolio.longshortbulder import long_short_builder
class TestLongShortBuild(unittest.TestCase): class TestLongShortBuild(unittest.TestCase):
...@@ -17,37 +17,38 @@ class TestLongShortBuild(unittest.TestCase): ...@@ -17,37 +17,38 @@ class TestLongShortBuild(unittest.TestCase):
self.x = np.random.randn(3000, 10) self.x = np.random.randn(3000, 10)
self.groups = np.random.randint(10, 40, size=3000) self.groups = np.random.randint(10, 40, size=3000)
choices = np.random.choice(3000, 100, replace=False) choices = np.random.choice(3000, 100, replace=False)
self.masks = np.full(3000, True, dtype=bool) self.masks = np.full(3000, False, dtype=bool)
self.masks[choices] = False self.masks[choices] = True
def test_long_short_build(self): def test_long_short_build(self):
x = self.x[:, 0].flatten() x = self.x[:, 0].flatten()
calc_weights = long_short_build(x).flatten() calc_weights = long_short_builder(x).flatten()
expected_weights = x / np.abs(x).sum() expected_weights = x / np.abs(x).sum()
np.testing.assert_array_almost_equal(calc_weights, expected_weights) np.testing.assert_array_almost_equal(calc_weights, expected_weights)
calc_weights = long_short_build(self.x, leverage=2) calc_weights = long_short_builder(self.x, leverage=2)
expected_weights = self.x / np.abs(self.x).sum(axis=0) * 2 expected_weights = self.x / np.abs(self.x).sum(axis=0) * 2
np.testing.assert_array_almost_equal(calc_weights, expected_weights) np.testing.assert_array_almost_equal(calc_weights, expected_weights)
def test_long_short_build_with_group(self): def test_long_short_build_with_group(self):
x = self.x[:, 0].flatten() x = self.x[:, 0].flatten()
calc_weights = long_short_build(x, groups=self.groups).flatten() calc_weights = long_short_builder(x, groups=self.groups).flatten()
expected_weights = pd.Series(x).groupby(self.groups).apply(lambda s: s / np.abs(s).sum()) expected_weights = pd.Series(x).groupby(self.groups).apply(lambda s: s / np.abs(s).sum())
np.testing.assert_array_almost_equal(calc_weights, expected_weights) np.testing.assert_array_almost_equal(calc_weights, expected_weights)
calc_weights = long_short_build(self.x, groups=self.groups) calc_weights = long_short_builder(self.x, groups=self.groups)
expected_weights = pd.DataFrame(self.x).groupby(self.groups).apply(lambda s: s / np.abs(s).sum(axis=0)) expected_weights = pd.DataFrame(self.x).groupby(self.groups).apply(lambda s: s / np.abs(s).sum(axis=0))
np.testing.assert_array_almost_equal(calc_weights, expected_weights) np.testing.assert_array_almost_equal(calc_weights, expected_weights)
def test_long_short_build_with_masks(self): def test_long_short_build_with_masks(self):
x = self.x[:, 0].flatten() x = self.x[:, 0].flatten()
calc_weights = long_short_builder(x, masks=self.masks, leverage=1.).flatten()
self.assertAlmostEqual(calc_weights.sum(), 0.)
masked_x = x.copy() masked_x = x.copy()
masked_x[~self.masks] = 0. masked_x[self.masks] = 0.
leverage = np.abs(masked_x).sum() masked_x[~self.masks] = masked_x[~self.masks] - masked_x[~self.masks].mean()
calc_weights = long_short_build(x, masks=self.masks, leverage=leverage).flatten() expected_weights = masked_x / np.abs(masked_x).sum()
expected_weights = x.copy()
expected_weights[~self.masks] = 0.
np.testing.assert_array_almost_equal(calc_weights, expected_weights) np.testing.assert_array_almost_equal(calc_weights, expected_weights)
......
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