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
a029d6b9
Commit
a029d6b9
authored
Jun 24, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update example
parent
2843e07c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
138 additions
and
98 deletions
+138
-98
optimizer check.ipynb
notebooks/optimizer check.ipynb
+138
-8
optimizer check2.ipynb
notebooks/optimizer check2.ipynb
+0
-90
No files found.
notebooks/optimizer check.ipynb
View file @
a029d6b9
...
...
@@ -108,7 +108,7 @@
"source": [
"%%time\n",
"ne_factor_values = neutralize(risk_exposure_values, factor_values)\n",
"signal = ne_factor_values @ weights"
"signal
_full
= ne_factor_values @ weights"
]
},
{
...
...
@@ -128,7 +128,7 @@
"source": [
"%%time\n",
"risk_cov_values = risk_cov_data.loc[risk_factor_columns, risk_factor_columns].astype(float)\n",
"sec_cov_values = risk_exposure_values @ risk_cov_values @ risk_exposure_values.T + np.diag(specific_risk.SRISK.values)"
"sec_cov_values
_full
= risk_exposure_values @ risk_cov_values @ risk_exposure_values.T + np.diag(specific_risk.SRISK.values)"
]
},
{
...
...
@@ -137,7 +137,10 @@
"metadata": {},
"outputs": [],
"source": [
"sec_cov_values"
"n = 500\n",
"\n",
"sec_cov_values = sec_cov_values_full[:n, :n]\n",
"signal = signal_full[:n]"
]
},
{
...
...
@@ -158,7 +161,7 @@
"w = Variable(n)\n",
"\n",
"lbound = 0.\n",
"ubound =
0.01
\n",
"ubound =
1. / n * 20
\n",
"\n",
"objective = Minimize(risk_penlty * quad_form(w, sec_cov_values) - signal * w)\n",
"constraints = [w >= lbound,\n",
...
...
@@ -220,7 +223,7 @@
"h = np.zeros(2*n)\n",
"for i in range(n):\n",
" G[i, i] = 1.\n",
" h[i] =
0.01
\n",
" h[i] =
1. / n * 20
\n",
" G[i+n, i] = -1.\n",
" h[i+n] = 0.\n",
" \n",
...
...
@@ -236,6 +239,62 @@
"sol = solvers.qp(P, q, G, h, A, b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Performace Timing\n",
"-------------------------"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import datetime as dt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def time_function(py_callable, n):\n",
" start = dt.datetime.now()\n",
" py_callable(n)\n",
" return (dt.datetime.now() - start).total_seconds()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def cvxpy(n):\n",
" w = Variable(n)\n",
"\n",
" lbound = 0.\n",
" ubound = 1. / n * 20\n",
"\n",
" objective = Minimize(risk_penlty * quad_form(w, sec_cov_values) - signal * w)\n",
" constraints = [w >= lbound,\n",
" w <= ubound,\n",
" sum_entries(w) == 1,]\n",
"\n",
" prob = Problem(objective, constraints)\n",
" prob.solve(verbose=False, solver='CVXOPT', display=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
...
...
@@ -244,7 +303,78 @@
},
"outputs": [],
"source": [
"sec_cov_values.tofile('sec_cov_values.dat')"
"def cvxopt(n):\n",
" P = matrix(sec_cov_values)\n",
" q = -matrix(signal)\n",
"\n",
" G = np.zeros((2*n, n))\n",
" h = np.zeros(2*n)\n",
" for i in range(n):\n",
" G[i, i] = 1.\n",
" h[i] = 0.01\n",
" G[i+n, i] = -1.\n",
" h[i+n] = 0.\n",
"\n",
" G = matrix(G)\n",
" h = matrix(h)\n",
"\n",
" A = np.ones((1, n))\n",
" b = np.ones(1)\n",
"\n",
" A = matrix(A)\n",
" b = matrix(b)\n",
" \n",
" solvers.options['show_progress'] = False\n",
" sol = solvers.qp(P, q, G, h, A, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s = time_function(cvxopt, n)\n",
"print(s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n_steps = list(range(200, 2001, 200))\n",
"cvxpy_times = [None] * len(n_steps)\n",
"cvxopt_times = [None] * len(n_steps)\n",
"\n",
"for i, n in enumerate(n_steps):\n",
" print(\"Scale for {0}\".format(n))\n",
" sec_cov_values = sec_cov_values_full[:n, :n]\n",
" signal = signal_full[:n]\n",
" cvxpy_times[i] = time_function(cvxpy, n) * 1000\n",
" cvxopt_times[i] = time_function(cvxopt, n) * 1000"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({'cvxpy': cvxpy_times,\n",
" 'cvxopt': cvxopt_times},\n",
" index=n_steps)\n",
"df.index.name = 'Problem Scale (n)'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ipopt_times = [63.4241, 157.568, 208.774, 436.105, 522.81, 744.118, 1133.87, 1685.59, 1409.36, 3364.33]"
]
},
{
...
...
@@ -255,7 +385,7 @@
},
"outputs": [],
"source": [
"
signal.tofile('signal.dat')
"
"
df['ipopt'] = ipopt_times
"
]
},
{
...
...
@@ -270,7 +400,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python
[default]
",
"display_name": "Python
3
",
"language": "python",
"name": "python3"
},
...
...
notebooks/optimizer check2.ipynb
deleted
100644 → 0
View file @
2843e07c
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from cvxopt import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sec_cov_values = np.fromfile('sec_cov_values.dat')\n",
"signal = np.fromfile('signal.dat')\n",
"sec_cov_values = sec_cov_values.reshape((len(signal), -1))\n",
"n = len(sec_cov_values)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"P = matrix(sec_cov_values)\n",
"q = -matrix(signal)\n",
"\n",
"G = np.zeros((2*n, n))\n",
"h = np.zeros(2*n)\n",
"for i in range(n):\n",
" G[i, i] = 1.\n",
" h[i] = 0.01\n",
" G[i+n, i] = -1.\n",
" h[i+n] = 0.\n",
" \n",
"G = matrix(G)\n",
"h = matrix(h)\n",
" \n",
"A = np.ones((1, n))\n",
"b = np.ones(1)\n",
"\n",
"A = matrix(A)\n",
"b = matrix(b)\n",
"\n",
"sol = solvers.qp(P, q, G, h, A, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:py2]",
"language": "python",
"name": "conda-env-py2-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"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