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
9730f9d7
Commit
9730f9d7
authored
Jul 22, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added new notebook
parent
9c27c497
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
145 additions
and
0 deletions
+145
-0
Linear Optimizer Check.ipynb
notebooks/Linear Optimizer Check.ipynb
+145
-0
No files found.
notebooks/Linear Optimizer Check.ipynb
0 → 100644
View file @
9730f9d7
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import datetime as dt\n",
"import numpy as np\n",
"import cvxpy\n",
"from cvxopt import solvers\n",
"from alphamind.portfolio.linearbuilder import linear_build"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"solvers.options['glpk'] = {'msg_lev': 'GLP_MSG_OFF'}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def time_function(py_callable, n):\n",
" start = dt.datetime.now()\n",
" result = py_callable(n)\n",
" elapsed = (dt.datetime.now() - start).total_seconds()\n",
" return elapsed, result\n",
"\n",
"def cvxpy_lp(n):\n",
" w = cvxpy.Variable(n)\n",
"\n",
" bndl = np.zeros(n)\n",
" bndu = 0.01 * np.ones(n)\n",
" risk_constraints = np.ones((n,1))\n",
"\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
"\n",
" constraints = [w >= bndl,\n",
" w <= bndu,\n",
" curr_risk_exposure >= 1.,\n",
" curr_risk_exposure <= 1.]\n",
"\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",
" return w, prob"
]
},
{
"cell_type": "code",
"execution_count": null,
"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",
"\n",
"for n in range(200, 3200, 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()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def clp_lp(n):\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",
" \n",
" result = linear_build(er, bndl, bndu, risk_constraints, risk_target)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": null,
"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",
"\n",
"for n in range(200, 3200, 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()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
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