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
5c4eef61
Commit
5c4eef61
authored
May 25, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added factor data packet structure
parent
c9c2c89c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
14 deletions
+75
-14
calculators.py
alphamind/analysis/calculators.py
+1
-1
factoranalysis.py
alphamind/analysis/factoranalysis.py
+74
-13
No files found.
alphamind/analysis/calculators.py
View file @
5c4eef61
...
...
@@ -8,7 +8,7 @@ Created on 2017-5-18
import
pandas
as
pd
def
calculate_turn_over
(
pos_table
)
:
def
calculate_turn_over
(
pos_table
:
pd
.
DataFrame
)
->
pd
.
DataFrame
:
turn_over_table
=
{}
total_factors
=
pos_table
.
columns
.
difference
([
'Code'
])
pos_table
.
reset_index
()
...
...
alphamind/analysis/factoranalysis.py
View file @
5c4eef61
...
...
@@ -5,9 +5,10 @@ Created on 2017-5-25
@author: cheng.li
"""
import
numpy
as
np
from
typing
import
Optional
from
typing
import
List
import
numpy
as
np
import
pandas
as
pd
from
alphamind.data.neutralize
import
neutralize
from
alphamind.portfolio.longshortbulder
import
long_short_build
from
alphamind.portfolio.rankbuilder
import
rank_build
...
...
@@ -37,13 +38,13 @@ def build_portfolio(er: np.ndarray,
builder
=
builder
.
lower
()
if
builder
==
'long_short'
:
if
builder
==
'l
s'
or
builder
==
'l
ong_short'
:
return
long_short_build
(
er
,
**
kwargs
)
elif
builder
==
'rank'
:
return
rank_build
(
er
,
**
kwargs
)
elif
builder
==
'percent
_build
'
:
elif
builder
==
'percent'
:
return
percent_build
(
er
,
**
kwargs
)
elif
builder
==
'linear_prog'
:
elif
builder
==
'linear_prog'
or
builder
==
'linear'
:
status
,
_
,
weight
=
linear_build
(
er
,
**
kwargs
)
if
status
!=
'optimal'
:
raise
ValueError
(
'linear programming optimizer in status: {0}'
.
format
(
status
))
...
...
@@ -51,18 +52,78 @@ def build_portfolio(er: np.ndarray,
return
weight
if
__name__
==
'__main__'
:
class
FDataPack
(
object
):
def
__init__
(
self
,
raw_factor
:
np
.
ndarray
,
factor_name
:
str
=
None
,
codes
:
List
=
None
,
groups
:
Optional
[
np
.
ndarray
]
=
None
,
benchmark
:
Optional
[
np
.
ndarray
]
=
None
,
risk_exp
:
Optional
[
np
.
ndarray
]
=
None
,
risk_names
:
List
[
str
]
=
None
):
self
.
raw_factor
=
raw_factor
if
factor_name
:
self
.
factor_name
=
factor_name
else
:
self
.
factor_name
=
'factor'
self
.
codes
=
codes
self
.
groups
=
groups
self
.
benchmark
=
benchmark
self
.
risk_exp
=
risk_exp
self
.
risk_names
=
risk_names
def
benchmark_risk_exp
(
self
)
->
np
.
ndarray
:
return
self
.
risk_exp
@
self
.
benchmark
def
factor_processing
(
self
,
pre_process
)
->
np
.
ndarray
:
if
self
.
risk_exp
is
None
:
return
factor_processing
(
self
.
raw_factor
,
pre_process
)
else
:
return
factor_processing
(
self
.
raw_factor
,
pre_process
,
self
.
risk_exp
)
def
to_df
(
self
)
->
pd
.
DataFrame
:
cols
=
[
self
.
factor_name
]
to_concat
=
[
self
.
raw_factor
]
if
self
.
groups
is
not
None
:
cols
.
append
(
'groups'
)
to_concat
.
append
(
self
.
groups
.
reshape
(
-
1
,
1
))
if
self
.
benchmark
is
not
None
:
cols
.
append
(
'benchmark'
)
to_concat
.
append
(
self
.
benchmark
)
if
self
.
risk_exp
is
not
None
:
cols
.
extend
(
self
.
risk_names
)
to_concat
.
append
(
self
.
risk_exp
)
from
alphamind.data.standardize
import
standardize
from
alphamind.data.winsorize
import
winsorize_normal
return
pd
.
DataFrame
(
np
.
concatenate
(
to_concat
,
axis
=
1
),
columns
=
cols
,
index
=
self
.
codes
)
if
__name__
==
'__main__'
:
raw_factor
=
np
.
random
.
randn
(
1000
,
1
)
pre_process
=
[
winsorize_normal
,
standardize
]
groups
=
np
.
random
.
randint
(
30
,
size
=
1000
)
benchmark
=
np
.
random
.
randn
(
1000
,
1
)
risk_exp
=
np
.
random
.
randn
(
1000
,
3
)
codes
=
list
(
range
(
1
,
1001
))
data_pack
=
FDataPack
(
raw_factor
,
'cfinc1'
,
codes
=
codes
,
groups
=
groups
,
benchmark
=
benchmark
,
risk_exp
=
risk_exp
,
risk_names
=
[
'market'
,
'size'
,
'growth'
])
print
(
data_pack
.
to_df
())
risk_factors
=
np
.
ones
((
1000
,
1
))
new_factor
=
factor_processing
(
raw_factor
,
pre_process
,
risk_factors
)
print
(
new_factor
.
sum
())
\ 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