Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
a56d4d0d
Commit
a56d4d0d
authored
Jul 19, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed bug
parent
9b5ad813
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
20 additions
and
23 deletions
+20
-23
factoranalysis.py
alphamind/analysis/factoranalysis.py
+10
-6
sqlengine.py
alphamind/data/engines/sqlengine.py
+2
-3
linearbuilder.py
alphamind/portfolio/linearbuilder.py
+3
-3
test_factoranalysis.py
alphamind/tests/analysis/test_factoranalysis.py
+4
-2
utilities.py
alphamind/utilities.py
+1
-9
No files found.
alphamind/analysis/factoranalysis.py
View file @
a56d4d0d
...
@@ -75,6 +75,7 @@ class FDataPack(object):
...
@@ -75,6 +75,7 @@ class FDataPack(object):
codes
:
List
=
None
,
codes
:
List
=
None
,
groups
:
Optional
[
np
.
ndarray
]
=
None
,
groups
:
Optional
[
np
.
ndarray
]
=
None
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
constraints
:
Optional
[
np
.
ndarray
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
risk_names
:
List
[
str
]
=
None
):
risk_names
:
List
[
str
]
=
None
):
...
@@ -91,10 +92,11 @@ class FDataPack(object):
...
@@ -91,10 +92,11 @@ class FDataPack(object):
else
:
else
:
self
.
benchmark
=
None
self
.
benchmark
=
None
self
.
risk_exp
=
risk_exp
self
.
risk_exp
=
risk_exp
self
.
constraints
=
constraints
self
.
risk_names
=
risk_names
self
.
risk_names
=
risk_names
def
benchmark_
risk_exp
(
self
)
->
np
.
ndarray
:
def
benchmark_
constraints
(
self
)
->
np
.
ndarray
:
return
self
.
benchmark
@
self
.
risk_exp
return
self
.
benchmark
@
self
.
constraints
def
settle
(
self
,
weights
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
)
->
pd
.
DataFrame
:
def
settle
(
self
,
weights
:
np
.
ndarray
,
dx_return
:
np
.
ndarray
)
->
pd
.
DataFrame
:
...
@@ -151,6 +153,7 @@ def factor_analysis(factors: pd.DataFrame,
...
@@ -151,6 +153,7 @@ def factor_analysis(factors: pd.DataFrame,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
,
is_tradable
:
Optional
[
np
.
ndarray
]
=
None
,
constraints
:
Optional
[
np
.
ndarray
]
=
None
,
method
=
'risk_neutral'
,
method
=
'risk_neutral'
,
**
kwargs
)
->
Tuple
[
pd
.
DataFrame
,
Optional
[
pd
.
DataFrame
]]:
**
kwargs
)
->
Tuple
[
pd
.
DataFrame
,
Optional
[
pd
.
DataFrame
]]:
...
@@ -160,7 +163,8 @@ def factor_analysis(factors: pd.DataFrame,
...
@@ -160,7 +163,8 @@ def factor_analysis(factors: pd.DataFrame,
data_pack
=
FDataPack
(
raw_factors
=
factors
.
values
,
data_pack
=
FDataPack
(
raw_factors
=
factors
.
values
,
groups
=
industry
,
groups
=
industry
,
benchmark
=
benchmark
,
benchmark
=
benchmark
,
risk_exp
=
risk_exp
)
risk_exp
=
risk_exp
,
constraints
=
constraints
)
er
=
data_pack
.
factor_processing
([
winsorize_normal
,
standardize
],
[
standardize
])
@
factor_weights
er
=
data_pack
.
factor_processing
([
winsorize_normal
,
standardize
],
[
standardize
])
@
factor_weights
...
@@ -177,12 +181,12 @@ def factor_analysis(factors: pd.DataFrame,
...
@@ -177,12 +181,12 @@ def factor_analysis(factors: pd.DataFrame,
risk_lbound
=
kwargs
[
'risk_bound'
][
0
]
risk_lbound
=
kwargs
[
'risk_bound'
][
0
]
risk_ubound
=
kwargs
[
'risk_bound'
][
1
]
risk_ubound
=
kwargs
[
'risk_bound'
][
1
]
else
:
else
:
risk_lbound
=
data_pack
.
benchmark_
risk_exp
()
risk_lbound
=
data_pack
.
benchmark_
constraints
()
risk_ubound
=
data_pack
.
benchmark_
risk_exp
()
risk_ubound
=
data_pack
.
benchmark_
constraints
()
weights
=
build_portfolio
(
er
,
weights
=
build_portfolio
(
er
,
builder
=
'linear'
,
builder
=
'linear'
,
risk_
exposure
=
risk_exp
,
risk_
constraints
=
constraints
,
lbound
=
lbound
,
lbound
=
lbound
,
ubound
=
ubound
,
ubound
=
ubound
,
risk_target
=
(
risk_lbound
,
risk_ubound
),
risk_target
=
(
risk_lbound
,
risk_ubound
),
...
...
alphamind/data/engines/sqlengine.py
View file @
a56d4d0d
...
@@ -20,7 +20,6 @@ from alphamind.data.dbmodel.models import FactorMaster
...
@@ -20,7 +20,6 @@ from alphamind.data.dbmodel.models import FactorMaster
from
alphamind.data.dbmodel.models
import
Strategy
from
alphamind.data.dbmodel.models
import
Strategy
from
alphamind.data.dbmodel.models
import
DailyReturn
from
alphamind.data.dbmodel.models
import
DailyReturn
from
alphamind.data.dbmodel.models
import
IndexComponent
from
alphamind.data.dbmodel.models
import
IndexComponent
from
alphamind.data.dbmodel.models
import
Universe
as
UniverseTable
from
PyFin.api
import
advanceDateByCalendar
from
PyFin.api
import
advanceDateByCalendar
risk_styles
=
[
'BETA'
,
risk_styles
=
[
'BETA'
,
...
@@ -180,7 +179,7 @@ class SqlEngine(object):
...
@@ -180,7 +179,7 @@ class SqlEngine(object):
total_data
=
{
'risk_cov'
:
risk_cov_data
}
total_data
=
{
'risk_cov'
:
risk_cov_data
}
if
benchmark
:
if
benchmark
:
query
=
select
([
IndexComponent
.
code
,
(
IndexComponent
.
weight
/
100.
)
.
lable
(
'weight'
)])
.
where
(
query
=
select
([
IndexComponent
.
Code
,
(
IndexComponent
.
weight
/
100.
)
.
label
(
'weight'
)])
.
where
(
and_
(
and_
(
IndexComponent
.
Date
==
ref_date
,
IndexComponent
.
Date
==
ref_date
,
IndexComponent
.
indexCode
==
benchmark
IndexComponent
.
indexCode
==
benchmark
...
@@ -205,7 +204,7 @@ if __name__ == '__main__':
...
@@ -205,7 +204,7 @@ if __name__ == '__main__':
import
datetime
as
dt
import
datetime
as
dt
universe
=
Universe
(
'zz500'
,
[
'zz500'
],
filter_cond
=
Uqer
.
BLEV
>=
0.1
)
universe
=
Universe
(
'zz500'
,
[
'zz500'
],
filter_cond
=
(
Uqer
.
BLEV
>=
0.1
)
&
(
Uqer
.
BLEV
<=
0.1
)
)
engine
=
SqlEngine
(
db_url
)
engine
=
SqlEngine
(
db_url
)
ref_date
=
'2017-07-04'
ref_date
=
'2017-07-04'
...
...
alphamind/portfolio/linearbuilder.py
View file @
a56d4d0d
...
@@ -18,13 +18,13 @@ solvers.options['glpk'] = {'msg_lev': 'GLP_MSG_OFF'}
...
@@ -18,13 +18,13 @@ solvers.options['glpk'] = {'msg_lev': 'GLP_MSG_OFF'}
def
linear_build
(
er
:
np
.
ndarray
,
def
linear_build
(
er
:
np
.
ndarray
,
lbound
:
Union
[
np
.
ndarray
,
float
],
lbound
:
Union
[
np
.
ndarray
,
float
],
ubound
:
Union
[
np
.
ndarray
,
float
],
ubound
:
Union
[
np
.
ndarray
,
float
],
risk_
exposure
:
np
.
ndarray
,
risk_
constraints
:
np
.
ndarray
,
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
],
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
],
solver
:
str
=
None
)
->
Tuple
[
str
,
np
.
ndarray
,
np
.
ndarray
]:
solver
:
str
=
None
)
->
Tuple
[
str
,
np
.
ndarray
,
np
.
ndarray
]:
n
,
m
=
risk_
exposure
.
shape
n
,
m
=
risk_
constraints
.
shape
w
=
cvxpy
.
Variable
(
n
)
w
=
cvxpy
.
Variable
(
n
)
curr_risk_exposure
=
risk_
exposure
.
T
@
w
curr_risk_exposure
=
risk_
constraints
.
T
@
w
if
not
risk_target
:
if
not
risk_target
:
constraints
=
[
w
>=
lbound
,
constraints
=
[
w
>=
lbound
,
...
...
alphamind/tests/analysis/test_factoranalysis.py
View file @
a56d4d0d
...
@@ -50,7 +50,8 @@ class TestFactorAnalysis(unittest.TestCase):
...
@@ -50,7 +50,8 @@ class TestFactorAnalysis(unittest.TestCase):
d1returns
=
self
.
d1returns
,
d1returns
=
self
.
d1returns
,
industry
=
industry
,
industry
=
industry
,
benchmark
=
benchmark
,
benchmark
=
benchmark
,
risk_exp
=
self
.
risk_factor
)
risk_exp
=
self
.
risk_factor
,
constraints
=
self
.
risk_factor
)
weight
=
weight_table
.
weight
weight
=
weight_table
.
weight
...
@@ -70,7 +71,8 @@ class TestFactorAnalysis(unittest.TestCase):
...
@@ -70,7 +71,8 @@ class TestFactorAnalysis(unittest.TestCase):
d1returns
=
self
.
d1returns
,
d1returns
=
self
.
d1returns
,
industry
=
industry
,
industry
=
industry
,
benchmark
=
benchmark
,
benchmark
=
benchmark
,
risk_exp
=
self
.
risk_factor
)
risk_exp
=
self
.
risk_factor
,
constraints
=
self
.
risk_factor
)
weight
=
weight_table
.
weight
weight
=
weight_table
.
weight
self
.
assertEqual
(
analysis_table
[
'er'
]
.
sum
()
/
analysis_table
[
'er'
][
-
1
],
2.0
)
self
.
assertEqual
(
analysis_table
[
'er'
]
.
sum
()
/
analysis_table
[
'er'
][
-
1
],
2.0
)
...
...
alphamind/utilities.py
View file @
a56d4d0d
...
@@ -273,12 +273,4 @@ def aggregate(groups, x, func, ddof=1):
...
@@ -273,12 +273,4 @@ def aggregate(groups, x, func, ddof=1):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
pass
x1
=
np
.
random
.
randint
(
30
,
size
=
1000
)
\ No newline at end of file
array
=
np
.
unique
(
x1
)
x2
=
np
.
random
.
randint
(
30
,
size
=
1000
)
res
=
array_index
(
array
,
x2
)
print
(
res
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment