Commit 651c34cb authored by Dr.李's avatar Dr.李

update example

parent 61bfd381
......@@ -207,8 +207,8 @@
"source": [
"def time_function(py_callable, n):\n",
" start = dt.datetime.now()\n",
" py_callable(n)\n",
" return (dt.datetime.now() - start).total_seconds()"
" val = py_callable(n)\n",
" return (dt.datetime.now() - start).total_seconds(), val"
]
},
{
......@@ -229,7 +229,8 @@
" sum_entries(w) == 1,]\n",
"\n",
" prob = Problem(objective, constraints)\n",
" prob.solve(verbose=False, solver='CVXOPT', display=False)"
" prob.solve(verbose=False, solver='CVXOPT', display=False)\n",
" return prob.value"
]
},
{
......@@ -260,7 +261,8 @@
" b = matrix(b)\n",
" \n",
" solvers.options['show_progress'] = False\n",
" sol = solvers.qp(P, q, G, h, A, b)"
" sol = solvers.qp(P, q, G, h, A, b)\n",
" return sol['primal objective']"
]
},
{
......@@ -276,7 +278,7 @@
" clb = np.ones(1)\n",
" cub = np.ones(1)\n",
" qpopt = QPOptimizer(signal, sec_cov_values, lbound, ubound, cons_matrix, clb, cub, 1.)\n",
" qpopt.feval()"
" return qpopt.feval()"
]
},
{
......@@ -294,27 +296,16 @@
"for i, n in enumerate(n_steps):\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\n",
" ipopt_times[i] = time_function(ipopt, n) * 1000\n",
" cvxpy_times[i], val1 = time_function(cvxpy, n)\n",
" cvxopt_times[i], val2 = time_function(cvxopt, n)\n",
" ipopt_times[i], val3 = time_function(ipopt, n)\n",
" \n",
" print(\"{0:<8}{1:>12.2f}{2:>12.2f}{3:>12.2f}\".format(n, cvxpy_times[i], cvxopt_times[i], ipopt_times[i]))"
" np.testing.assert_almost_equal(val1, val2, 4)\n",
" np.testing.assert_almost_equal(val2, val3, 4)\n",
" \n",
" print(\"{0:<8}{1:>12.4f}{2:>12.4f}{3:>12.4f}\".format(n, cvxpy_times[i], cvxopt_times[i], ipopt_times[i]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
......@@ -339,7 +330,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.5"
},
"varInspector": {
"cols": {
......
......@@ -113,8 +113,8 @@
" objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" \n",
" prob.solve(solver='GLPK')\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='GLPK')\",\n",
" prob.solve()\n",
" elasped_time2 = timeit.timeit(\"prob.solve()\",\n",
" number=number, globals=globals()) / number * 1000\n",
"\n",
" np.testing.assert_almost_equal(x1 @ er, np.array(w.value).flatten() @ er, 4)\n",
......@@ -177,13 +177,13 @@
" current_position=current_position,\n",
" method='interior')\n",
" elasped_time1 = timeit.timeit(\"\"\"linear_builder(er,\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='interior')\"\"\", number=number, globals=globals()) / number * 1000\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='interior')\"\"\", number=number, globals=globals()) / number * 1000\n",
" \n",
" w = cvxpy.Variable(n)\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
......@@ -196,18 +196,18 @@
" objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" \n",
" prob.solve(solver='GLPK')\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='GLPK')\",\n",
" prob.solve()\n",
" elasped_time2 = timeit.timeit(\"prob.solve()\",\n",
" number=number, globals=globals()) / number * 1000\n",
" \n",
" elasped_time3 = timeit.timeit(\"\"\"linear_builder(er,\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='simplex')\"\"\", number=number, globals=globals()) / number * 1000\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='simplex')\"\"\", number=number, globals=globals()) / number * 1000\n",
" \n",
" \n",
" np.testing.assert_almost_equal(x1 @ er, np.array(w.value).flatten() @ er, 4)\n",
......@@ -529,7 +529,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.5"
},
"varInspector": {
"cols": {
......
......@@ -355,7 +355,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.6.5"
},
"varInspector": {
"cols": {
......
......@@ -58,7 +58,7 @@
"\n",
" objective = cvxpy.Minimize(-w.T * er)\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" prob.solve(solver='GLPK')\n",
" prob.solve()\n",
" return w, prob"
]
},
......@@ -113,6 +113,13 @@
" 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": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
......@@ -137,7 +144,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
"version": "3.6.5"
},
"varInspector": {
"cols": {
......
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