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
0dce4df3
Commit
0dce4df3
authored
May 05, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added linear programming builder
parent
bf2585a3
Changes
8
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
322 additions
and
82 deletions
+322
-82
benchmarks.py
alphamind/benchmarks/benchmarks.py
+3
-0
linearbuild.py
alphamind/benchmarks/portfolio/linearbuild.py
+65
-0
linearbuilder.py
alphamind/portfolio/linearbuilder.py
+47
-0
test_linearbuild.py
alphamind/tests/portfolio/test_linearbuild.py
+35
-0
test_suite.py
alphamind/tests/test_suite.py
+2
-0
factor analysis.ipynb
notebooks/factor analysis.ipynb
+166
-82
requirements.txt
requirements.txt
+3
-0
setup.py
setup.py
+1
-0
No files found.
alphamind/benchmarks/benchmarks.py
View file @
0dce4df3
...
...
@@ -15,6 +15,7 @@ from alphamind.benchmarks.portfolio.rankbuild import benchmark_build_rank
from
alphamind.benchmarks.portfolio.rankbuild
import
benchmark_build_rank_with_group
from
alphamind.benchmarks.portfolio.percentbuild
import
benchmark_build_percent
from
alphamind.benchmarks.portfolio.percentbuild
import
benchmark_build_percent_with_group
from
alphamind.benchmarks.portfolio.linearbuild
import
benchmark_build_linear
from
alphamind.benchmarks.settlement.simplesettle
import
benchmark_simple_settle
from
alphamind.benchmarks.settlement.simplesettle
import
benchmark_simple_settle_with_group
...
...
@@ -51,6 +52,8 @@ if __name__ == '__main__':
benchmark_build_percent_with_group
(
30
,
50000
,
0.1
,
3
)
benchmark_build_percent
(
50000
,
20
,
0.1
)
benchmark_build_percent_with_group
(
50000
,
20
,
0.1
,
300
)
benchmark_build_linear
(
100
,
3
,
3
,
100
)
benchmark_build_linear
(
1000
,
30
,
30
,
10
)
benchmark_simple_settle
(
3000
,
10
,
1000
)
benchmark_simple_settle_with_group
(
3000
,
10
,
1000
,
30
)
benchmark_simple_settle
(
30
,
10
,
50000
)
...
...
alphamind/benchmarks/portfolio/linearbuild.py
0 → 100644
View file @
0dce4df3
# -*- coding: utf-8 -*-
"""
Created on 2017-5-5
@author: cheng.li
"""
import
datetime
as
dt
import
numpy
as
np
from
scipy.optimize
import
linprog
from
cvxopt
import
matrix
from
cvxopt
import
solvers
from
alphamind.portfolio.linearbuilder
import
linear_build
solvers
.
options
[
'show_progress'
]
=
False
def
benchmark_build_linear
(
n_samples
:
int
,
n_risks
:
int
,
n_inds
,
n_loop
:
int
)
->
None
:
print
(
"-"
*
60
)
print
(
"Starting portfolio construction by linear programming"
)
print
(
"Parameters(n_samples: {0}, n_risks: {1}, n_inds: {2})"
.
format
(
n_samples
,
n_risks
,
n_inds
))
er
=
np
.
random
.
randn
(
n_samples
)
risk_exp
=
np
.
random
.
randn
(
n_samples
,
n_risks
)
bm
=
np
.
random
.
randint
(
n_inds
,
size
=
n_samples
)
.
astype
(
float
)
bm
/=
bm
.
sum
()
lbound
=
-
0.04
ubound
=
0.05
start
=
dt
.
datetime
.
now
()
for
_
in
range
(
n_loop
):
status
,
v
,
x
=
linear_build
(
er
,
lbound
,
ubound
,
risk_exp
,
bm
,
solver
=
'ECOS'
)
impl_model_time
=
dt
.
datetime
.
now
()
-
start
print
(
'{0:20s}: {1}'
.
format
(
'Implemented model (ECOS)'
,
impl_model_time
))
c
=
-
er
bounds
=
[(
lbound
,
ubound
)
for
_
in
range
(
n_samples
)]
A_eq
=
np
.
ones
((
1
,
n_samples
))
A_eq
=
np
.
vstack
((
A_eq
,
risk_exp
.
T
))
b_eq
=
np
.
hstack
((
np
.
array
([
1.
]),
risk_exp
.
T
@
bm
))
start
=
dt
.
datetime
.
now
()
for
_
in
range
(
n_loop
):
res
=
linprog
(
c
,
A_eq
=
A_eq
,
b_eq
=
b_eq
,
bounds
=
bounds
,
options
=
{
'maxiter'
:
10000
})
benchmark_model_time
=
dt
.
datetime
.
now
()
-
start
print
(
'{0:20s}: {1}'
.
format
(
'Benchmark model (scipy)'
,
benchmark_model_time
))
np
.
testing
.
assert_array_almost_equal
(
x
,
res
[
'x'
])
c
=
matrix
(
-
er
)
A
=
matrix
(
A_eq
)
b
=
matrix
(
b_eq
)
G
=
matrix
(
np
.
vstack
((
np
.
diag
(
np
.
ones
(
n_samples
)),
-
np
.
diag
(
np
.
ones
(
n_samples
)))))
h
=
matrix
(
np
.
hstack
((
ubound
*
np
.
ones
(
n_samples
),
-
lbound
*
np
.
ones
(
n_samples
))))
solvers
.
lp
(
c
,
G
,
h
,
solver
=
'glpk'
)
start
=
dt
.
datetime
.
now
()
for
_
in
range
(
n_loop
):
res2
=
solvers
.
lp
(
c
,
G
,
h
,
A
,
b
,
solver
=
'glpk'
)
benchmark_model_time
=
dt
.
datetime
.
now
()
-
start
print
(
'{0:20s}: {1}'
.
format
(
'Benchmark model (glpk)'
,
benchmark_model_time
))
np
.
testing
.
assert_array_almost_equal
(
x
,
np
.
array
(
res2
[
'x'
])
.
flatten
())
if
__name__
==
'__main__'
:
benchmark_build_linear
(
1000
,
30
,
30
,
1
)
\ No newline at end of file
alphamind/portfolio/linearbuilder.py
0 → 100644
View file @
0dce4df3
# -*- coding: utf-8 -*-
"""
Created on 2017-5-5
@author: cheng.li
"""
import
numpy
as
np
import
cvxpy
from
cvxopt
import
solvers
solvers
.
options
[
'glpk'
]
=
{
'msg_lev'
:
'GLP_MSG_OFF'
}
def
linear_build
(
er
,
lbound
,
ubound
,
risk_exposure
,
bm
,
risk_target
=
None
,
solver
=
None
):
n
,
m
=
risk_exposure
.
shape
w
=
cvxpy
.
Variable
(
n
)
if
risk_target
is
None
:
risk_target
=
np
.
zeros
(
m
)
curr_risk_exposure
=
risk_exposure
.
T
*
(
w
-
bm
)
objective
=
cvxpy
.
Minimize
(
-
w
.
T
*
er
)
constraints
=
[
w
>=
lbound
,
w
<=
ubound
,
curr_risk_exposure
==
risk_target
,
cvxpy
.
sum_entries
(
w
)
==
1.
]
prob
=
cvxpy
.
Problem
(
objective
,
constraints
)
prob
.
solve
(
solver
=
solver
)
return
prob
.
status
,
prob
.
value
,
np
.
array
(
w
.
value
)
.
flatten
()
if
__name__
==
'__main__'
:
er
=
np
.
arange
(
300
)
bm
=
np
.
ones
(
300
)
*
0.00333333333
risk_exposure
=
np
.
random
.
randn
(
300
,
10
)
s
,
v
,
x
=
linear_build
(
er
,
0.
,
0.01
,
risk_exposure
,
bm
)
print
(
s
)
print
(
x
.
sum
())
print
(
x
.
min
(),
','
,
x
.
max
())
print
((
x
-
bm
)
@
risk_exposure
)
alphamind/tests/portfolio/test_linearbuild.py
0 → 100644
View file @
0dce4df3
# -*- coding: utf-8 -*-
"""
Created on 2017-5-5
@author: cheng.li
"""
import
unittest
import
numpy
as
np
from
alphamind.portfolio.linearbuilder
import
linear_build
class
TestLinearBuild
(
unittest
.
TestCase
):
def
test_linear_build
(
self
):
er
=
np
.
random
.
randn
(
3000
)
risk_exp
=
np
.
random
.
randn
(
3000
,
30
)
bm
=
np
.
random
.
randint
(
100
,
size
=
3000
)
.
astype
(
float
)
bm
/=
bm
.
sum
()
eplson
=
1e-6
status
,
value
,
w
=
linear_build
(
er
,
0.
,
0.01
,
risk_exp
,
bm
)
self
.
assertEqual
(
status
,
'optimal'
)
self
.
assertAlmostEqual
(
np
.
sum
(
w
),
1.
)
self
.
assertTrue
(
np
.
all
(
w
<=
0.01
+
eplson
))
self
.
assertTrue
(
np
.
all
(
w
>=
-
eplson
))
calc_risk
=
(
w
-
bm
)
@
risk_exp
expected_risk
=
np
.
zeros
(
risk_exp
.
shape
[
1
])
np
.
testing
.
assert_array_almost_equal
(
calc_risk
,
expected_risk
)
if
__name__
==
'__main__'
:
unittest
.
main
()
\ No newline at end of file
alphamind/tests/test_suite.py
View file @
0dce4df3
...
...
@@ -14,6 +14,7 @@ from alphamind.tests.data.test_standardize import TestStandardize
from
alphamind.tests.data.test_winsorize
import
TestWinsorize
from
alphamind.tests.portfolio.test_rankbuild
import
TestRankBuild
from
alphamind.tests.portfolio.test_percentbuild
import
TestPercentBuild
from
alphamind.tests.portfolio.test_linearbuild
import
TestLinearBuild
from
alphamind.tests.settlement.test_simplesettle
import
TestSimpleSettle
from
alphamind.utilities
import
alpha_logger
from
alphamind.utilities
import
TestRunner
...
...
@@ -25,6 +26,7 @@ if __name__ == '__main__':
TestWinsorize
,
TestRankBuild
,
TestPercentBuild
,
TestLinearBuild
,
TestSimpleSettle
],
alpha_logger
)
runner
.
run
()
notebooks/factor analysis.ipynb
View file @
0dce4df3
This diff is collapsed.
Click to expand it.
requirements.txt
View file @
0dce4df3
cvxopt
>= 1.1.9
cvxpy
>= 0.4.9
cython
>= 0.25.2
numpy
>= 1.12.1
numba
>= 0.30.0
scikit-learn
>= 0.18.1
...
...
setup.py
View file @
0dce4df3
...
...
@@ -43,6 +43,7 @@ def generate_extensions(ext_modules, line_trace=False):
extensions
.
append
(
ext
)
return
extensions
if
platform
.
system
()
!=
"Windows"
:
import
multiprocessing
n_cpu
=
multiprocessing
.
cpu_count
()
...
...
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