Commit e5182b39 authored by Dr.李's avatar Dr.李

update notebook

parent 4032f6a8
...@@ -3,9 +3,7 @@ ...@@ -3,9 +3,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import datetime as dt\n", "import datetime as dt\n",
...@@ -29,9 +27,7 @@ ...@@ -29,9 +27,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def time_function(py_callable, n):\n", "def time_function(py_callable, n):\n",
...@@ -45,18 +41,23 @@ ...@@ -45,18 +41,23 @@
"\n", "\n",
" bndl = np.zeros(n)\n", " bndl = np.zeros(n)\n",
" bndu = 0.01 * np.ones(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", "\n",
" curr_risk_exposure = risk_constraints.T @ w\n", " curr_risk_exposure = risk_constraints.T @ w\n",
" risk_targets = np.array([1., 0.015])\n",
"\n", "\n",
" constraints = [w >= bndl,\n", " constraints = [w >= bndl,\n",
" w <= bndu,\n", " w <= bndu,\n",
" curr_risk_exposure >= 1.,\n", " curr_risk_exposure >= risk_targets,\n",
" curr_risk_exposure <= 1.]\n", " curr_risk_exposure <= risk_targets]\n",
"\n", " \n",
" np.random.seed(1)\n",
" er = np.random.randn(n)\n", " er = np.random.randn(n)\n",
"\n", "\n",
"\n",
" objective = cvxpy.Minimize(-w.T * er)\n", " objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n", " prob = cvxpy.Problem(objective, constraints)\n",
" prob.solve(solver='GLPK')\n", " prob.solve(solver='GLPK')\n",
...@@ -69,12 +70,12 @@ ...@@ -69,12 +70,12 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", "\n",
"for n in range(200, 3200, 200):\n", "for n in range(200, 10200, 200):\n",
" elapsed, result = time_function(cvxpy_lp, n)\n", " elapsed, result = time_function(cvxpy_lp, n)\n",
" s = np.array(result[0].value).flatten()\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 @@ ...@@ -86,12 +87,17 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def clp_lp(n):\n", "def clp_lp(n):\n",
" np.random.seed(1)\n",
" er = np.random.randn(n)\n", " er = np.random.randn(n)\n",
"\n", "\n",
" bndl = np.zeros(n)\n", " bndl = np.zeros(n)\n",
" bndu = 0.01 * np.ones(n)\n", " bndu = 0.01 * np.ones(n)\n",
" risk_constraints = np.ones((n,1))\n", " risk_constraints1 = np.ones((n,1))\n",
" risk_target = np.ones(1), np.ones(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", " \n",
" result = linear_build(er, bndl, bndu, risk_constraints, risk_target)\n", " result = linear_build(er, bndl, bndu, risk_constraints, risk_target)\n",
" return result" " return result"
...@@ -103,12 +109,12 @@ ...@@ -103,12 +109,12 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "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", "\n",
"for n in range(200, 3200, 200):\n", "for n in range(200, 10200, 200):\n",
" elapsed, result = time_function(clp_lp, n)\n", " elapsed, result = time_function(clp_lp, n)\n",
" s = result[2]\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 @@ ...@@ -123,7 +129,7 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python [default]",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment