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
e5182b39
Commit
e5182b39
authored
Jul 23, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update notebook
parent
4032f6a8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
20 deletions
+26
-20
Linear Optimizer Check.ipynb
notebooks/Linear Optimizer Check.ipynb
+26
-20
No files found.
notebooks/Linear Optimizer Check.ipynb
View file @
e5182b39
...
...
@@ -3,9 +3,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"import datetime as dt\n",
...
...
@@ -29,9 +27,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"def time_function(py_callable, n):\n",
...
...
@@ -45,18 +41,23 @@
"\n",
" bndl = np.zeros(n)\n",
" bndu = 0.01 * np.ones(n)\n",
" risk_constraints = np.ones((n,1))\n",
" risk_constraints1 = np.ones((n,1))\n",
" risk_constraints2 = np.zeros((n,1))\n",
" risk_constraints2[0][0] = 1.\n",
" risk_constraints2[1][0] = 1.\n",
" risk_constraints = np.concatenate((risk_constraints1, risk_constraints2), axis=1)\n",
"\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
" risk_targets = np.array([1., 0.015])\n",
"\n",
" constraints = [w >= bndl,\n",
" w <= bndu,\n",
" curr_risk_exposure >= 1.,\n",
" curr_risk_exposure <= 1.]\n",
"\n",
" curr_risk_exposure >= risk_targets,\n",
" curr_risk_exposure <= risk_targets]\n",
" \n",
" np.random.seed(1)\n",
" er = np.random.randn(n)\n",
"\n",
"\n",
" objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" prob.solve(solver='GLPK')\n",
...
...
@@ -69,12 +70,12 @@
"metadata": {},
"outputs": [],
"source": [
"print(\"{0:<8}{1:>12}{2:>12}{3:>12}{4:>12}{5:>12}
\".format('Scale(n)', 'time(ms)', 'feval', 'min(x)', 'max(x)', 'sum(x
)'))\n",
"print(\"{0:<8}{1:>12}{2:>12}{3:>12}{4:>12}{5:>12}
{6:>15}\".format('Scale(n)', 'time(ms)', 'feval', 'min(x)', 'max(x)', 'sum(x)', 'x(0) + x(1
)'))\n",
"\n",
"for n in range(200,
3
200, 200):\n",
"for n in range(200,
10
200, 200):\n",
" elapsed, result = time_function(cvxpy_lp, n)\n",
" s = np.array(result[0].value).flatten()\n",
" print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12f}{4:>12f}{5:>12f}
\".format(n, elapsed*1000, result[1].value, s.min(), s.max(), s.sum()
))"
" print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12f}{4:>12f}{5:>12f}
{6:>15}\".format(n, elapsed*1000, result[1].value, s.min(), s.max(), s.sum(), s[0] + s[1]
))"
]
},
{
...
...
@@ -86,12 +87,17 @@
"outputs": [],
"source": [
"def clp_lp(n):\n",
" np.random.seed(1)\n",
" er = np.random.randn(n)\n",
"\n",
" bndl = np.zeros(n)\n",
" bndu = 0.01 * np.ones(n)\n",
" risk_constraints = np.ones((n,1))\n",
" risk_target = np.ones(1), np.ones(1)\n",
" risk_constraints1 = np.ones((n,1))\n",
" risk_constraints2 = np.zeros((n,1))\n",
" risk_constraints2[0][0] = 1.\n",
" risk_constraints2[1][0] = 1.\n",
" risk_constraints = np.concatenate((risk_constraints1, risk_constraints2), axis=1)\n",
" risk_target = np.array([1., 0.015]), np.array([1., 0.015])\n",
" \n",
" result = linear_build(er, bndl, bndu, risk_constraints, risk_target)\n",
" return result"
...
...
@@ -103,12 +109,12 @@
"metadata": {},
"outputs": [],
"source": [
"print(\"{0:<8}{1:>12}{2:>12}{3:>12}{4:>12}{5:>12}
\".format('Scale(n)', 'time(ms)', 'feval', 'min(x)', 'max(x)', 'sum(x
)'))\n",
"print(\"{0:<8}{1:>12}{2:>12}{3:>12}{4:>12}{5:>12}
{6:>15}\".format('Scale(n)', 'time(ms)', 'feval', 'min(x)', 'max(x)', 'sum(x)', 'x(0) + x(1
)'))\n",
"\n",
"for n in range(200,
3
200, 200):\n",
"for n in range(200,
10
200, 200):\n",
" elapsed, result = time_function(clp_lp, n)\n",
" s = result[2]\n",
" print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12f}{4:>12f}{5:>12f}
\".format(n, elapsed*1000, result[1], s.min(), s.max(), s.sum()
))"
" print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12f}{4:>12f}{5:>12f}
{6:>15}\".format(n, elapsed*1000, result[1], s.min(), s.max(), s.sum(), s[0] + s[1]
))"
]
},
{
...
...
@@ -123,7 +129,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python
3
",
"display_name": "Python
[default]
",
"language": "python",
"name": "python3"
},
...
...
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