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
9d3a00d0
Commit
9d3a00d0
authored
Aug 07, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added lambda parameter and its corresponding test
parent
dc58dce6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
7 deletions
+39
-7
factoranalysis.py
alphamind/analysis/factoranalysis.py
+7
-1
test_meanvariancebuild.py
alphamind/tests/portfolio/test_meanvariancebuild.py
+32
-6
No files found.
alphamind/analysis/factoranalysis.py
View file @
9d3a00d0
...
...
@@ -230,13 +230,19 @@ def factor_analysis(factors: pd.DataFrame,
lbound
,
ubound
,
cons_exp
,
risk_lbound
,
risk_ubound
=
create_constraints
(
benchmark
,
**
kwargs
)
cov
=
kwargs
[
'cov'
]
if
'lambda'
in
kwargs
:
lam
=
kwargs
[
'lambda'
]
else
:
lam
=
1.
status
,
_
,
weights
=
mean_variance_builder
(
er
,
cov
=
cov
,
bm
=
benchmark
,
lbound
=
lbound
,
ubound
=
ubound
,
risk_exposure
=
cons_exp
,
risk_target
=
(
risk_lbound
,
risk_ubound
))
risk_target
=
(
risk_lbound
,
risk_ubound
),
lam
=
lam
)
if
status
!=
'optimal'
:
raise
ValueError
(
'mean variance optimizer in status: {0}'
.
format
(
status
))
else
:
...
...
alphamind/tests/portfolio/test_meanvariancebuild.py
View file @
9d3a00d0
...
...
@@ -13,12 +13,11 @@ from alphamind.portfolio.meanvariancebuilder import mean_variance_builder
class
TestMeanVarianceBuild
(
unittest
.
TestCase
):
def
test_mean_variance_builder
(
self
):
er
=
np
.
random
.
randint
(
0
,
10
,
size
=
3
)
/
10.
cov
=
np
.
array
([[
0.04
,
0.01
,
0.02
],
[
0.01
,
0.05
,
0.03
],
[
0.02
,
0.03
,
0.06
]])
ids_var
=
np
.
diag
(
np
.
random
.
randint
(
2
,
5
,
size
=
3
)
/
100.
)
er
=
np
.
array
([
0.01
,
0.02
,
0.03
])
cov
=
np
.
array
([[
0.02
,
0.01
,
0.02
],
[
0.01
,
0.02
,
0.03
],
[
0.02
,
0.03
,
0.02
]])
ids_var
=
np
.
diag
([
0.01
,
0.02
,
0.03
])
cov
+=
ids_var
bm
=
np
.
array
([
0.3
,
0.3
,
0.4
])
...
...
@@ -37,3 +36,30 @@ class TestMeanVarianceBuild(unittest.TestCase):
self
.
assertTrue
(
np
.
all
(
x
>=
lbound
)
-
1.e-6
)
self
.
assertTrue
(
np
.
all
(
x
@
risk_exposure
<=
risk_target
[
1
]
+
1.e-6
))
self
.
assertTrue
(
np
.
all
(
x
@
risk_exposure
>=
risk_target
[
0
]
-
1.e-6
))
np
.
testing
.
assert_array_almost_equal
(
x
,
[
0.1
,
0.4
,
0.5
])
def
test_mean_variance_builder_with_none_unity_lambda
(
self
):
er
=
np
.
array
([
0.01
,
0.02
,
0.03
])
cov
=
np
.
array
([[
0.02
,
0.01
,
0.02
],
[
0.01
,
0.02
,
0.03
],
[
0.02
,
0.03
,
0.02
]])
ids_var
=
np
.
diag
([
0.01
,
0.02
,
0.03
])
cov
+=
ids_var
bm
=
np
.
array
([
0.3
,
0.3
,
0.4
])
lbound
=
np
.
array
([
0.
,
0.
,
0.
])
ubound
=
np
.
array
([
0.4
,
0.4
,
0.5
])
risk_exposure
=
np
.
array
([[
1.
,
1.
,
1.
],
[
1.
,
0.
,
1.
]])
.
T
risk_target
=
(
np
.
array
([
bm
.
sum
(),
0.3
]),
np
.
array
([
bm
.
sum
(),
0.7
]))
status
,
_
,
x
=
mean_variance_builder
(
er
,
cov
,
bm
,
lbound
,
ubound
,
risk_exposure
,
risk_target
,
lam
=
100
)
self
.
assertTrue
(
status
==
'optimal'
)
self
.
assertAlmostEqual
(
x
.
sum
(),
bm
.
sum
())
self
.
assertTrue
(
np
.
all
(
x
<=
ubound
+
1.e-6
))
self
.
assertTrue
(
np
.
all
(
x
>=
lbound
)
-
1.e-6
)
self
.
assertTrue
(
np
.
all
(
x
@
risk_exposure
<=
risk_target
[
1
]
+
1.e-6
))
self
.
assertTrue
(
np
.
all
(
x
@
risk_exposure
>=
risk_target
[
0
]
-
1.e-6
))
np
.
testing
.
assert_array_almost_equal
(
x
,
[
0.2950
,
0.3000
,
0.4050
])
\ 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