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
ae80d282
Commit
ae80d282
authored
Mar 22, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://git.coding.net/wegamekinglc/Alpha-Mind
parents
6bdfa6c6
44f18827
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
866 deletions
+0
-866
Linear Optimizer Check.ipynb
notebooks/Linear Optimizer Check.ipynb
+0
-199
Quadratic Optimizer Check.ipynb
notebooks/Quadratic Optimizer Check.ipynb
+0
-667
No files found.
notebooks/Linear Optimizer Check.ipynb
deleted
100644 → 0
View file @
6bdfa6c6
{
"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_build"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"solvers.options['glpk'] = {'msg_lev': 'GLP_MSG_OFF'}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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_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 18.01 -0.82 0.000000 0.010000 1.000000 0.015\n",
"400 23.02 -1.28 -0.000000 0.010000 1.000000 0.015\n",
"600 42.63 -1.54 -0.000000 0.010000 1.000000 0.015\n",
"800 62.04 -1.63 -0.000000 0.010000 1.000000 0.015\n",
"1000 76.57 -1.72 -0.000000 0.010000 1.000000 0.015\n",
"1200 108.73 -1.81 -0.000000 0.010000 1.000000 0.015\n",
"1400 136.22 -1.90 -0.000000 0.010000 1.000000 0.015\n",
"1600 166.64 -1.96 -0.000000 0.010000 1.000000 0.015\n",
"1800 197.72 -2.03 -0.000000 0.010000 1.000000 0.015\n",
"2000 258.51 -2.06 -0.000000 0.010000 1.000000 0.015\n",
"2200 291.34 -2.07 -0.000000 0.010000 1.000000 0.015\n",
"2400 348.30 -2.13 -0.000000 0.010000 1.000000 0.015\n",
"2600 398.31 -2.14 -0.000000 0.010000 1.000000 0.015\n",
"2800 462.13 -2.16 -0.000000 0.010000 1.000000 0.015\n",
"3000 547.84 -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": {
"collapsed": true
},
"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_build(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 19.01 -0.82 0.000000 0.010000 1.000000 0.015\n",
"400 1.00 -1.28 0.000000 0.010000 1.000000 0.015\n",
"600 2.00 -1.54 0.000000 0.010000 1.000000 0.015\n",
"800 3.00 -1.63 0.000000 0.010000 1.000000 0.015\n",
"1000 2.00 -1.72 0.000000 0.010000 1.000000 0.015\n",
"1200 3.00 -1.81 0.000000 0.010000 1.000000 0.015\n",
"1400 2.02 -1.90 0.000000 0.010000 1.000000 0.015\n",
"1600 2.02 -1.96 0.000000 0.010000 1.000000 0.015\n",
"1800 1.98 -2.03 0.000000 0.010000 1.000000 0.015\n",
"2000 2.02 -2.06 0.000000 0.010000 1.000000 0.015\n",
"2200 2.00 -2.07 0.000000 0.010000 1.000000 0.015\n",
"2400 2.00 -2.13 0.000000 0.010000 1.000000 0.015\n",
"2600 3.00 -2.14 0.000000 0.010000 1.000000 0.015\n",
"2800 3.02 -2.16 0.000000 0.010000 1.000000 0.015\n",
"3000 3.00 -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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
notebooks/Quadratic Optimizer Check.ipynb
deleted
100644 → 0
View file @
6bdfa6c6
This diff is collapsed.
Click to expand it.
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