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
67abdf2c
Commit
67abdf2c
authored
Mar 22, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update 2 examples
parent
0725efec
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
682 additions
and
0 deletions
+682
-0
Example 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
...ple 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
+489
-0
Example 9 - Linear Optimizer Comparison with CVXOPT.ipynb
...Example 9 - Linear Optimizer Comparison with CVXOPT.ipynb
+193
-0
No files found.
notebooks/Example 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
0 → 100644
View file @
67abdf2c
This diff is collapsed.
Click to expand it.
notebooks/Example 9 - Linear Optimizer Comparison with CVXOPT.ipynb
0 → 100644
View file @
67abdf2c
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"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_builder"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"solvers.options['glpk'] = {'msg_lev': 'GLP_MSG_OFF'}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"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_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 >= risk_targets,\n",
" curr_risk_exposure <= risk_targets]\n",
" \n",
" np.random.seed(1)\n",
" er = np.random.randn(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": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scale(n) time(ms) feval min(x) max(x) sum(x) x(0) + x(1)\n",
"200 39.72 -0.82 0.000000 0.010000 1.000000 0.015\n",
"400 25.20 -1.28 0.000000 0.010000 1.000000 0.015\n",
"600 29.23 -1.54 0.000000 0.010000 1.000000 0.015\n",
"800 32.27 -1.63 0.000000 0.010000 1.000000 0.015\n",
"1000 15.13 -1.72 0.000000 0.010000 1.000000 0.015\n",
"1200 16.79 -1.81 0.000000 0.010000 1.000000 0.015\n",
"1400 18.62 -1.90 0.000000 0.010000 1.000000 0.015\n",
"1600 20.90 -1.96 0.000000 0.010000 1.000000 0.015\n",
"1800 24.02 -2.03 0.000000 0.010000 1.000000 0.015\n",
"2000 27.05 -2.06 0.000000 0.010000 1.000000 0.015\n",
"2200 28.04 -2.07 0.000000 0.010000 1.000000 0.015\n",
"2400 30.25 -2.13 0.000000 0.010000 1.000000 0.015\n",
"2600 31.96 -2.14 0.000000 0.010000 1.000000 0.015\n",
"2800 34.44 -2.16 0.000000 0.010000 1.000000 0.015\n",
"3000 36.86 -2.19 0.000000 0.010000 1.000000 0.015\n"
]
}
],
"source": [
"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, 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}{6:>15}\".format(n, elapsed*1000, result[1].value, s.min(), s.max(), s.sum(), s[0] + s[1]))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"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_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_builder(er, bndl, bndu, risk_constraints, risk_target)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scale(n) time(ms) feval min(x) max(x) sum(x) x(0) + x(1)\n",
"200 2.95 -0.82 0.000000 0.010000 1.000000 0.015\n",
"400 2.34 -1.28 0.000000 0.010000 1.000000 0.015\n",
"600 2.44 -1.54 0.000000 0.010000 1.000000 0.015\n",
"800 2.91 -1.63 0.000000 0.010000 1.000000 0.015\n",
"1000 7.58 -1.72 0.000000 0.010000 1.000000 0.015\n",
"1200 3.89 -1.81 0.000000 0.010000 1.000000 0.015\n",
"1400 4.22 -1.90 0.000000 0.010000 1.000000 0.015\n",
"1600 4.37 -1.96 0.000000 0.010000 1.000000 0.015\n",
"1800 4.81 -2.03 0.000000 0.010000 1.000000 0.015\n",
"2000 4.98 -2.06 0.000000 0.010000 1.000000 0.015\n",
"2200 5.31 -2.07 0.000000 0.010000 1.000000 0.015\n",
"2400 6.13 -2.13 0.000000 0.010000 1.000000 0.015\n",
"2600 6.12 -2.14 0.000000 0.010000 1.000000 0.015\n",
"2800 6.73 -2.16 0.000000 0.010000 1.000000 0.015\n",
"3000 7.39 -2.19 0.000000 0.010000 1.000000 0.015\n"
]
}
],
"source": [
"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, 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}{6:>15}\".format(n, elapsed*1000, result[1], s.min(), s.max(), s.sum(), s[0] + s[1]))"
]
},
{
"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.4"
}
},
"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