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
fd8a9b2c
Commit
fd8a9b2c
authored
May 30, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update example
parent
18a6bd84
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
16 deletions
+49
-16
Example 7 - Portfolio Optimizer Performance.ipynb
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
+49
-16
No files found.
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
View file @
fd8a9b2c
...
...
@@ -28,9 +28,6 @@
"import numpy as np\n",
"import pandas as pd\n",
"import cvxpy\n",
"from cvxopt import solvers\n",
"from scipy.optimize import linprog\n",
"from scipy.optimize import minimize\n",
"from alphamind.api import *\n",
"from alphamind.portfolio.linearbuilder import linear_builder\n",
"from alphamind.portfolio.meanvariancebuilder import mean_variance_builder\n",
...
...
@@ -102,9 +99,8 @@
" A_eq = risk_constraints.T\n",
" b_eq = np.array([1.])\n",
" \n",
" solvers.options['glpk'] = {'msg_lev': 'GLP_MSG_OFF'}\n",
" w = cvxpy.Variable(n)\n",
" curr_risk_exposure =
risk_constraints.T @ w
\n",
" curr_risk_exposure =
w * risk_constraints
\n",
" \n",
" constraints = [w >= lbound,\n",
" w <= ubound,\n",
...
...
@@ -113,8 +109,8 @@
" objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" \n",
" prob.solve()\n",
" elasped_time2 = timeit.timeit(\"prob.solve()\",\n",
" prob.solve(
solver='ECOS'
)\n",
" elasped_time2 = timeit.timeit(\"prob.solve(
solver='ECOS'
)\",\n",
" number=number, globals=globals()) / number * 1000\n",
"\n",
" np.testing.assert_almost_equal(x1 @ er, np.array(w.value).flatten() @ er, 4)\n",
...
...
@@ -133,6 +129,15 @@
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"prob.value"
]
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -149,7 +154,7 @@
"source": [
"from cvxpy import pnorm\n",
"\n",
"df = pd.DataFrame(columns=u_names, index=['cvxpy', 'alphamind (
simplex)', 'alphamind (interior
)'])\n",
"df = pd.DataFrame(columns=u_names, index=['cvxpy', 'alphamind (
clp simplex)', 'alphamind (clp interior)', 'alphamind (ecos
)'])\n",
"turn_over_target = 0.5\n",
"number = 1\n",
"\n",
...
...
@@ -196,10 +201,18 @@
" objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" \n",
" prob.solve()\n",
" elasped_time2 = timeit.timeit(\"prob.solve()\",\n",
" prob.solve(
solver='ECOS'
)\n",
" elasped_time2 = timeit.timeit(\"prob.solve(
solver='ECOS'
)\",\n",
" number=number, globals=globals()) / number * 1000\n",
" \n",
" status, y, x2 = linear_builder(er,\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='simplex')\n",
" elasped_time3 = timeit.timeit(\"\"\"linear_builder(er,\n",
" lbound,\n",
" ubound,\n",
...
...
@@ -209,11 +222,31 @@
" current_position=current_position,\n",
" method='simplex')\"\"\", number=number, globals=globals()) / number * 1000\n",
" \n",
" status, y, x3 = linear_builder(er,\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='ecos')\n",
" elasped_time4 = timeit.timeit(\"\"\"linear_builder(er,\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='ecos')\"\"\", number=number, globals=globals()) / number * 1000\n",
" \n",
" \n",
" np.testing.assert_almost_equal(x1 @ er, np.array(w.value).flatten() @ er, 4)\n",
" np.testing.assert_almost_equal(x2 @ er, np.array(w.value).flatten() @ er, 4)\n",
" np.testing.assert_almost_equal(x3 @ er, np.array(w.value).flatten() @ er, 4)\n",
"\n",
" df.loc['alphamind (interior)', u_name] = elasped_time1\n",
" df.loc['alphamind (simplex)', u_name] = elasped_time3\n",
" df.loc['alphamind (clp interior)', u_name] = elasped_time1\n",
" df.loc['alphamind (clp simplex)', u_name] = elasped_time3\n",
" df.loc['alphamind (ecos)', u_name] = elasped_time4\n",
" df.loc['cvxpy', u_name] = elasped_time2\n",
" alpha_logger.info(f\"{u_name} is finished\")"
]
...
...
@@ -282,7 +315,7 @@
" number=number, globals=globals()) / number * 1000\n",
" \n",
" w = cvxpy.Variable(n)\n",
" risk = sum_squares(mul
_elemwise
(special_risk / 100., w)) + quad_form((w.T * risk_exposure).T, risk_cov / 10000.)\n",
" risk = sum_squares(mul
tiply
(special_risk / 100., w)) + quad_form((w.T * risk_exposure).T, risk_cov / 10000.)\n",
" objective = cvxpy.Minimize(-w.T * er + 0.5 * risk)\n",
" prob = cvxpy.Problem(objective)\n",
" prob.solve(solver='ECOS')\n",
...
...
@@ -359,7 +392,7 @@
" number=number, globals=globals()) / number * 1000\n",
" \n",
" w = cvxpy.Variable(n)\n",
" risk = sum_squares(mul
_elemwise
(special_risk / 100., w)) + quad_form((w.T * risk_exposure).T, risk_cov / 10000.)\n",
" risk = sum_squares(mul
tiply
(special_risk / 100., w)) + quad_form((w.T * risk_exposure).T, risk_cov / 10000.)\n",
" objective = cvxpy.Minimize(-w.T * er + 0.5 * risk)\n",
" constraints = [w >= lbound,\n",
" w <= ubound]\n",
...
...
@@ -441,7 +474,7 @@
" number=number, globals=globals()) / number * 1000\n",
" \n",
" w = cvxpy.Variable(n)\n",
" risk = sum_squares(mul
_elemwise
(special_risk / 100., w)) + quad_form((w.T * risk_exposure).T, risk_cov / 10000.)\n",
" risk = sum_squares(mul
tiply
(special_risk / 100., w)) + quad_form((w.T * risk_exposure).T, risk_cov / 10000.)\n",
" objective = cvxpy.Minimize(-w.T * er + 0.5 * risk)\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
" constraints = [w >= lbound,\n",
...
...
@@ -531,7 +564,7 @@
" number=number, globals=globals()) / number * 1000\n",
" \n",
" w = cvxpy.Variable(n)\n",
" risk = sum_squares(mul
_elemwise
(special_risk / 100., w)) + quad_form((w.T * risk_exposure).T, risk_cov / 10000.)\n",
" risk = sum_squares(mul
tiply
(special_risk / 100., w)) + quad_form((w.T * risk_exposure).T, risk_cov / 10000.)\n",
" objective = cvxpy.Minimize(-w.T * er)\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
" constraints = [w >= lbound,\n",
...
...
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