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
ae63c24a
Commit
ae63c24a
authored
Aug 09, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed bug for mv optimizer
parent
857b3ce6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
489 additions
and
124 deletions
+489
-124
factoranalysis.py
alphamind/analysis/factoranalysis.py
+2
-2
sqlengine.py
alphamind/data/engines/sqlengine.py
+8
-23
example2.py
alphamind/examples/example2.py
+65
-54
Linear Optimizer Check.ipynb
notebooks/Linear Optimizer Check.ipynb
+60
-12
Quadratic Optimizer Check.ipynb
notebooks/Quadratic Optimizer Check.ipynb
+354
-33
No files found.
alphamind/analysis/factoranalysis.py
View file @
ae63c24a
...
@@ -230,8 +230,8 @@ def factor_analysis(factors: pd.DataFrame,
...
@@ -230,8 +230,8 @@ def factor_analysis(factors: pd.DataFrame,
lbound
,
ubound
,
cons_exp
,
risk_lbound
,
risk_ubound
=
create_constraints
(
benchmark
,
**
kwargs
)
lbound
,
ubound
,
cons_exp
,
risk_lbound
,
risk_ubound
=
create_constraints
(
benchmark
,
**
kwargs
)
cov
=
kwargs
[
'cov'
]
cov
=
kwargs
[
'cov'
]
if
'lam
bda
'
in
kwargs
:
if
'lam'
in
kwargs
:
lam
=
kwargs
[
'lam
bda
'
]
lam
=
kwargs
[
'lam'
]
else
:
else
:
lam
=
1.
lam
=
1.
...
...
alphamind/data/engines/sqlengine.py
View file @
ae63c24a
...
@@ -209,33 +209,18 @@ class SqlEngine(object):
...
@@ -209,33 +209,18 @@ class SqlEngine(object):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
db_url
=
'mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha?charset=cp936'
db_url
=
'mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha?charset=cp936'
from
alphamind.data.dbmodel.models
import
Uqer
import
datetime
as
dt
import
datetime
as
dt
universe
=
Universe
(
'custom'
,
[
'
ashare
'
])
universe
=
Universe
(
'custom'
,
[
'
zz500
'
])
engine
=
SqlEngine
(
db_url
)
engine
=
SqlEngine
(
db_url
)
ref_date
=
'2017-0
7-21
'
ref_date
=
'2017-0
1-17
'
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
data
=
engine
.
fetch_data
(
ref_date
,
[
'EPS'
],
codes
,
905
)
d1ret
=
engine
.
fetch_dx_return
(
ref_date
,
codes
,
horizon
=
0
)
start
=
dt
.
datetime
.
now
()
missing_codes
=
[
c
for
c
in
data
[
'factor'
]
.
Code
if
c
not
in
set
(
d1ret
.
Code
)]
for
i
in
range
(
100
):
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
print
(
dt
.
datetime
.
now
()
-
start
)
print
(
codes
)
print
(
len
(
codes
))
universe
=
Universe
(
'zz500'
,
[
'zz500'
])
engine
=
SqlEngine
(
db_url
)
ref_date
=
'2017-07-04'
start
=
dt
.
datetime
.
now
()
for
i
in
range
(
100
):
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
print
(
dt
.
datetime
.
now
()
-
start
)
print
(
codes
)
print
(
len
(
codes
))
print
(
len
(
data
[
'factor'
]))
print
(
len
(
d1ret
))
print
(
missing_codes
)
alphamind/examples/example2.py
View file @
ae63c24a
...
@@ -5,69 +5,80 @@ Created on 2017-7-10
...
@@ -5,69 +5,80 @@ Created on 2017-7-10
@author: cheng.li
@author: cheng.li
"""
"""
import
datetime
as
dt
import
numpy
as
np
import
pandas
as
pd
import
pandas
as
pd
from
alphamind.analysis.factoranalysis
import
factor_analysis
from
alphamind.analysis.factoranalysis
import
factor_analysis
from
alphamind.data.engines.sqlengine
import
risk_styles
from
alphamind.data.engines.sqlengine
import
industry_styles
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.data.engines.sqlengine
import
SqlEngine
from
alphamind.data.engines.sqlengine
import
SqlEngine
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.engines.sqlengine
import
industry_styles
from
PyFin.api
import
bizDatesList
from
PyFin.api
import
bizDatesList
from
PyFin.api
import
makeSchedule
engine
=
SqlEngine
(
'mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha'
)
engine
=
SqlEngine
(
'mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha'
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
universe
=
Universe
(
'custom'
,
[
'zz500'
])
dates
=
bizDatesList
(
'china.sse'
,
'2017-01-01'
,
'2017-08-05'
)
factors
=
[
'EPS'
,
'FEARNG'
,
'VAL'
,
'NIAP'
]
f_weights
=
np
.
array
([
1.
,
1.
,
1.
,
1.
])
used_risk_styles
=
[
'SIZE'
]
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
constraint_risk
=
[]
total_risks
=
used_risk_styles
+
industry_styles
build_type
=
'risk_neutral'
def
calculate_one_day
(
ref_date
,
factors
,
factor_weights
,
horizon_end
=
None
):
rets
=
[]
print
(
ref_date
)
for
date
in
dates
:
print
(
date
)
ref_date
=
date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
total_data
=
engine
.
fetch_data
(
ref_date
,
factors
,
codes
,
905
)
data
=
engine
.
fetch_data
(
ref_date
,
factors
,
codes
,
905
,
risk_model
=
'short'
)
returns
=
engine
.
fetch_dx_return
(
ref_date
,
codes
,
0
)
factor_data
=
total_data
[
'factor'
]
factor_df
=
factor_data
[[
'Code'
,
'industry'
,
'weight'
,
'isOpen'
]
+
total_risks
+
factors
]
.
dropna
()
total_data
=
pd
.
merge
(
data
[
'factor'
],
returns
,
on
=
[
'Code'
])
.
dropna
()
risk_cov
=
data
[
'risk_cov'
]
dx_return
=
engine
.
fetch_dx_return
(
ref_date
,
codes
,
expiry_date
=
horizon_end
)
factor_df
=
pd
.
merge
(
factor_df
,
dx_return
,
on
=
[
'Code'
])
total_risks
=
risk_cov
.
Factor
risk_cov
=
risk_cov
[
total_risks
]
weights
,
_
=
factor_analysis
(
factor_df
[
factors
],
risk_exp
=
total_data
[
total_risks
]
factor_weights
,
stocks_cov
=
((
risk_exp
.
values
@
risk_cov
.
values
@
risk_exp
.
values
.
T
)
+
np
.
diag
(
total_data
.
SRISK
**
2
))
/
10000.
factor_df
.
industry
.
values
,
None
,
f_data
=
total_data
[
factors
]
detail_analysis
=
False
,
benchmark
=
factor_df
.
weight
.
values
,
industry
=
total_data
.
industry_code
.
values
risk_exp
=
factor_df
[
total_risks
]
.
values
,
dx_return
=
total_data
.
dx
.
values
is_tradable
=
factor_df
.
isOpen
.
values
.
astype
(
bool
),
benchmark
=
total_data
.
weight
.
values
method
=
build_type
)
risk_exp
=
total_data
[
neutralize_risk
]
.
values
constraint_exp
=
total_data
[
constraint_risk
]
.
values
return
ref_date
,
(
weights
.
weight
-
factor_df
.
weight
)
.
dot
(
factor_df
.
dx
)
risk_exp_expand
=
np
.
concatenate
((
constraint_exp
,
np
.
ones
((
len
(
risk_exp
),
1
))),
axis
=
1
)
.
astype
(
float
)
risk_names
=
constraint_risk
+
[
'total'
]
if
__name__
==
'__main__'
:
risk_target
=
risk_exp_expand
.
T
@
benchmark
from
matplotlib
import
pyplot
as
plt
lbound
=
0.
ubound
=
0.05
+
benchmark
factors
=
[
'BDTO'
,
'CFinc1'
,
'DivP'
,
'EPS'
,
'RVOL'
,
'DROEAfterNonRecurring'
]
factor_weights
=
[
0.10
,
0.30
,
0.15
,
0.18
,
0.11
,
0.35
]
constraint
=
Constraints
(
risk_exp_expand
,
risk_names
)
biz_dates
=
makeSchedule
(
'2015-01-01'
,
'2017-07-07'
,
'1w'
,
'china.sse'
)
for
i
,
name
in
enumerate
(
risk_names
):
constraint
.
set_constraints
(
name
,
lower_bound
=
risk_target
[
i
],
upper_bound
=
risk_target
[
i
])
ers
=
[]
dates
=
[]
try
:
pos
,
analysis
=
factor_analysis
(
f_data
,
for
i
,
ref_date
in
enumerate
(
biz_dates
[:
-
1
]):
f_weights
,
ref_date
=
ref_date
.
strftime
(
"
%
Y-
%
m-
%
d"
)
industry
,
try
:
dx_return
,
ref_date
,
er
=
calculate_one_day
(
ref_date
,
factors
,
factor_weights
,
horizon_end
=
biz_dates
[
i
+
1
])
benchmark
=
benchmark
,
dates
.
append
(
ref_date
)
risk_exp
=
risk_exp
,
ers
.
append
(
er
)
is_tradable
=
total_data
.
isOpen
.
values
.
astype
(
bool
),
except
Exception
as
e
:
method
=
'mv'
,
print
(
str
(
e
)
+
": {0}"
.
format
(
ref_date
))
constraints
=
constraint
,
cov
=
stocks_cov
,
res
=
pd
.
Series
(
ers
,
index
=
dates
)
use_rank
=
100
,
res
.
cumsum
()
.
plot
()
lam
=
100.
,
plt
.
show
()
lbound
=
lbound
,
ubound
=
ubound
)
except
:
rets
.
append
(
0.
)
print
(
"{0} is error!"
.
format
(
date
))
else
:
rets
.
append
(
analysis
.
er
[
-
1
])
ret_series
=
pd
.
Series
(
rets
,
dates
)
\ No newline at end of file
notebooks/Linear Optimizer Check.ipynb
View file @
ae63c24a
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
"cells": [
"cells": [
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
1
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -15,7 +15,7 @@
...
@@ -15,7 +15,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
2
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -26,8 +26,10 @@
...
@@ -26,8 +26,10 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"outputs": [],
"source": [
"source": [
"def time_function(py_callable, n):\n",
"def time_function(py_callable, n):\n",
...
@@ -66,13 +68,36 @@
...
@@ -66,13 +68,36 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
4
,
"metadata": {},
"metadata": {},
"outputs": [],
"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": [
"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",
"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,
10
200, 200):\n",
"for n in range(200,
3
200, 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}{6:>15}\".format(n, elapsed*1000, result[1].value, s.min(), s.max(), s.sum(), s[0] + s[1]))"
" 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]))"
...
@@ -80,7 +105,7 @@
...
@@ -80,7 +105,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
7
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -105,13 +130,36 @@
...
@@ -105,13 +130,36 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
8
,
"metadata": {},
"metadata": {},
"outputs": [],
"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": [
"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",
"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,
10
200, 200):\n",
"for n in range(200,
3
200, 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}{6:>15}\".format(n, elapsed*1000, result[1], s.min(), s.max(), s.sum(), s[0] + s[1]))"
" 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]))"
...
@@ -129,7 +177,7 @@
...
@@ -129,7 +177,7 @@
],
],
"metadata": {
"metadata": {
"kernelspec": {
"kernelspec": {
"display_name": "Python
[default]
",
"display_name": "Python
3
",
"language": "python",
"language": "python",
"name": "python3"
"name": "python3"
},
},
...
...
notebooks/Quadratic Optimizer Check.ipynb
View file @
ae63c24a
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
"cells": [
"cells": [
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
1
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -25,7 +25,7 @@
...
@@ -25,7 +25,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
2
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -36,7 +36,7 @@
...
@@ -36,7 +36,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
3
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -48,7 +48,7 @@
...
@@ -48,7 +48,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
4
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -70,9 +70,17 @@
...
@@ -70,9 +70,17 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
5
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wall time: 121 ms\n"
]
}
],
"source": [
"source": [
"%%time\n",
"%%time\n",
"w = Variable(n)\n",
"w = Variable(n)\n",
...
@@ -90,9 +98,51 @@
...
@@ -90,9 +98,51 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
6
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"ECOS 2.0.4 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS\n",
"\n",
"It pcost dcost gap pres dres k/t mu step sigma IR | BT\n",
" 0 -2.559e-02 -3.082e+01 +3e+03 9e-01 8e-05 1e+00 7e+00 --- --- 1 1 - | - - \n",
" 1 -2.473e+01 -2.541e+01 +7e+01 1e-01 2e-06 1e-02 2e-01 0.9779 1e-04 1 2 2 | 0 0\n",
" 2 -1.412e+00 -1.437e+00 +3e+00 5e-03 6e-08 5e-04 7e-03 0.9598 2e-03 2 3 3 | 0 0\n",
" 3 -1.019e+00 -1.035e+00 +2e+00 3e-03 4e-08 1e-03 5e-03 0.4692 3e-01 3 4 4 | 0 0\n",
" 4 -9.719e-01 -9.842e-01 +2e+00 3e-03 3e-08 2e-03 4e-03 0.5240 6e-01 3 4 3 | 0 0\n",
" 5 -3.633e-01 -3.662e-01 +4e-01 7e-04 8e-09 6e-04 1e-03 0.8629 1e-01 3 4 4 | 0 0\n",
" 6 -2.058e-01 -2.066e-01 +1e-01 2e-04 2e-09 2e-04 3e-04 0.8197 1e-01 3 4 4 | 0 0\n",
" 7 -1.579e-01 -1.581e-01 +3e-02 5e-05 6e-10 5e-05 7e-05 0.8711 1e-01 3 3 3 | 0 0\n",
" 8 -1.495e-01 -1.496e-01 +1e-02 2e-05 2e-10 2e-05 3e-05 0.7478 2e-01 2 3 3 | 0 0\n",
" 9 -1.468e-01 -1.468e-01 +7e-03 1e-05 2e-10 1e-05 2e-05 0.7025 5e-01 3 4 4 | 0 0\n",
"10 -1.440e-01 -1.440e-01 +1e-03 2e-06 3e-11 3e-06 4e-06 0.9500 1e-01 2 2 2 | 0 0\n",
"11 -1.434e-01 -1.434e-01 +2e-04 3e-07 4e-12 4e-07 5e-07 0.8916 4e-02 3 4 3 | 0 0\n",
"12 -1.433e-01 -1.433e-01 +2e-05 3e-08 3e-13 3e-08 4e-08 0.9253 3e-03 2 2 2 | 0 0\n",
"13 -1.433e-01 -1.433e-01 +7e-07 2e-09 1e-14 1e-09 2e-09 0.9585 5e-04 2 1 2 | 0 0\n",
"14 -1.433e-01 -1.433e-01 +4e-08 1e-09 9e-16 8e-11 1e-10 0.9563 2e-02 3 2 2 | 0 0\n",
"15 -1.433e-01 -1.433e-01 +1e-09 1e-10 5e-17 3e-12 4e-12 0.9676 8e-04 3 2 2 | 0 0\n",
"\n",
"OPTIMAL (within feastol=1.5e-10, reltol=1.0e-08, abstol=1.5e-09).\n",
"Runtime: 0.184433 seconds.\n",
"\n",
"Wall time: 265 ms\n"
]
},
{
"data": {
"text/plain": [
"-0.14330450116149787"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"%%time\n",
"%%time\n",
"prob.solve(verbose=True)"
"prob.solve(verbose=True)"
...
@@ -100,18 +150,62 @@
...
@@ -100,18 +150,62 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
7
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"('optimal', -0.14330450116149787)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"prob.status, prob.value"
"prob.status, prob.value"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
8
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" pcost dcost gap pres dres k/t\n",
" 0: -2.3359e-02 -3.1007e+01 7e+02 1e+01 9e-03 1e+00\n",
" 1: -1.1146e+01 -1.5653e+01 6e+01 1e+00 1e-03 2e-02\n",
" 2: -1.7778e+00 -2.7042e+00 7e+00 3e-01 3e-04 3e-02\n",
" 3: -3.8072e-01 -5.2347e-01 8e-01 5e-02 4e-05 5e-03\n",
" 4: -2.0231e-01 -2.3403e-01 2e-01 1e-02 9e-06 1e-03\n",
" 5: -1.5540e-01 -1.6212e-01 3e-02 2e-03 2e-06 2e-04\n",
" 6: -1.4464e-01 -1.4561e-01 4e-03 3e-04 3e-07 3e-05\n",
" 7: -1.4358e-01 -1.4376e-01 8e-04 6e-05 5e-08 4e-06\n",
" 8: -1.4340e-01 -1.4346e-01 3e-04 2e-05 2e-08 1e-06\n",
" 9: -1.4333e-01 -1.4334e-01 5e-05 4e-06 4e-09 2e-07\n",
"10: -1.4331e-01 -1.4331e-01 8e-06 6e-07 5e-10 3e-08\n",
"11: -1.4330e-01 -1.4330e-01 4e-07 3e-08 3e-11 1e-09\n",
"12: -1.4330e-01 -1.4330e-01 3e-08 2e-09 7e-12 9e-11\n",
"Optimal solution found.\n",
"Wall time: 195 ms\n"
]
},
{
"data": {
"text/plain": [
"-0.1433045111926663"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"%%time\n",
"%%time\n",
"prob.solve(verbose=True, solver='CVXOPT')"
"prob.solve(verbose=True, solver='CVXOPT')"
...
@@ -119,18 +213,50 @@
...
@@ -119,18 +213,50 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
9
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"('optimal', -0.1433045111926663)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"prob.status, prob.value"
"prob.status, prob.value"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
10
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" pcost dcost gap pres dres\n",
" 0: -2.6176e-01 -2.1588e+01 5e+02 2e+01 1e-15\n",
" 1: -2.1701e-01 -1.8470e+01 4e+01 8e-01 2e-15\n",
" 2: -4.4680e-02 -5.2189e+00 6e+00 5e-02 2e-15\n",
" 3: -4.2473e-02 -1.4144e+00 1e+00 1e-02 3e-15\n",
" 4: -9.7185e-02 -3.4512e-01 3e-01 1e-03 2e-15\n",
" 5: -1.2997e-01 -1.8100e-01 5e-02 1e-04 1e-15\n",
" 6: -1.4069e-01 -1.4831e-01 8e-03 1e-05 1e-15\n",
" 7: -1.4300e-01 -1.4373e-01 7e-04 9e-07 1e-15\n",
" 8: -1.4329e-01 -1.4332e-01 3e-05 2e-08 1e-15\n",
" 9: -1.4330e-01 -1.4330e-01 8e-07 3e-10 1e-15\n",
"10: -1.4330e-01 -1.4330e-01 5e-08 6e-12 1e-15\n",
"Optimal solution found.\n",
"Wall time: 55.1 ms\n"
]
}
],
"source": [
"source": [
"%%time\n",
"%%time\n",
"P = matrix(sec_cov_values)\n",
"P = matrix(sec_cov_values)\n",
...
@@ -158,14 +284,25 @@
...
@@ -158,14 +284,25 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
16
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wall time: 37 ms\n"
]
}
],
"source": [
"source": [
"%%time\n",
"%%time\n",
"lbound = np.zeros(n)\n",
"lbound = np.zeros(n)\n",
"ubound = np.ones(n) * 20 / n\n",
"ubound = np.ones(n) * 20 / n\n",
"qpopt = QPOptimizer(signal, sec_cov_values, lbound, ubound, 1.)\n",
"cons_matrix = np.ones((1, n))\n",
"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()\n",
"qpopt.feval()\n",
"qpopt.status()"
"qpopt.status()"
]
]
...
@@ -180,7 +317,7 @@
...
@@ -180,7 +317,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
17
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -191,7 +328,7 @@
...
@@ -191,7 +328,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
18
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -205,7 +342,7 @@
...
@@ -205,7 +342,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
19
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -228,7 +365,7 @@
...
@@ -228,7 +365,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
20
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -261,7 +398,7 @@
...
@@ -261,7 +398,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
23
,
"metadata": {
"metadata": {
"collapsed": true
"collapsed": true
},
},
...
@@ -270,15 +407,42 @@
...
@@ -270,15 +407,42 @@
"def ipopt(n):\n",
"def ipopt(n):\n",
" lbound = np.zeros(n)\n",
" lbound = np.zeros(n)\n",
" ubound = np.ones(n) * 0.01\n",
" ubound = np.ones(n) * 0.01\n",
" qpopt = QPOptimizer(signal, sec_cov_values, lbound, ubound, 1.)\n",
" cons_matrix = np.ones((1, n))\n",
" 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()"
" qpopt.feval()"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
null
,
"execution_count":
24
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scale(n) cvxpy cvxopt ipopt\n",
"200 197.63 45.03 25.02\n",
"400 1029.54 392.71 66.05\n",
"600 2765.32 1290.11 101.07\n",
"800 5906.85 3147.61 123.10\n",
"1000 11623.60 5949.30 181.69\n",
"1200 19327.03 9837.68 279.76\n",
"1400 28894.03 14138.84 529.42\n",
"1600 40706.67 21410.84 550.92\n",
"1800 59016.47 29610.92 737.62\n",
"2000 77980.53 41321.95 818.84\n",
"2200 98591.72 52743.93 1310.13\n",
"2400 139175.23 76199.42 2156.20\n",
"2600 170922.06 89493.43 1591.01\n",
"2800 197469.52 112762.10 1569.06\n",
"3000 246957.32 135002.46 1484.89\n",
"3200 291831.05 160416.66 1919.71\n"
]
}
],
"source": [
"source": [
"n_steps = list(range(200, 3201, 200))\n",
"n_steps = list(range(200, 3201, 200))\n",
"cvxpy_times = [None] * len(n_steps)\n",
"cvxpy_times = [None] * len(n_steps)\n",
...
@@ -298,11 +462,168 @@
...
@@ -298,11 +462,168 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {
"metadata": {},
"collapsed": true
"outputs": [
},
{
"outputs": [],
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>cvxopt</th>\n",
" <th>cvxpy</th>\n",
" <th>ipopt</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Problem Scale (n)</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>200</th>\n",
" <td>49.039</td>\n",
" <td>207.656</td>\n",
" <td>27.019</td>\n",
" </tr>\n",
" <tr>\n",
" <th>400</th>\n",
" <td>358.899</td>\n",
" <td>976.404</td>\n",
" <td>51.080</td>\n",
" </tr>\n",
" <tr>\n",
" <th>600</th>\n",
" <td>1222.190</td>\n",
" <td>2811.124</td>\n",
" <td>108.764</td>\n",
" </tr>\n",
" <tr>\n",
" <th>800</th>\n",
" <td>3051.363</td>\n",
" <td>5794.853</td>\n",
" <td>121.119</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1000</th>\n",
" <td>5858.366</td>\n",
" <td>11435.303</td>\n",
" <td>192.262</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1200</th>\n",
" <td>9806.112</td>\n",
" <td>18855.293</td>\n",
" <td>272.055</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1400</th>\n",
" <td>13928.818</td>\n",
" <td>28437.877</td>\n",
" <td>536.453</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1600</th>\n",
" <td>20971.899</td>\n",
" <td>39959.689</td>\n",
" <td>583.744</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1800</th>\n",
" <td>28056.363</td>\n",
" <td>63106.509</td>\n",
" <td>630.622</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>39500.814</td>\n",
" <td>74439.178</td>\n",
" <td>797.706</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2200</th>\n",
" <td>49913.609</td>\n",
" <td>97029.306</td>\n",
" <td>1434.759</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2400</th>\n",
" <td>71256.090</td>\n",
" <td>140969.086</td>\n",
" <td>2002.379</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2600</th>\n",
" <td>88315.444</td>\n",
" <td>159561.312</td>\n",
" <td>1576.816</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2800</th>\n",
" <td>112728.898</td>\n",
" <td>200788.253</td>\n",
" <td>1523.572</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3000</th>\n",
" <td>135712.700</td>\n",
" <td>259485.171</td>\n",
" <td>1641.147</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3200</th>\n",
" <td>175046.983</td>\n",
" <td>314367.647</td>\n",
" <td>2021.378</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" cvxopt cvxpy ipopt\n",
"Problem Scale (n) \n",
"200 49.039 207.656 27.019\n",
"400 358.899 976.404 51.080\n",
"600 1222.190 2811.124 108.764\n",
"800 3051.363 5794.853 121.119\n",
"1000 5858.366 11435.303 192.262\n",
"1200 9806.112 18855.293 272.055\n",
"1400 13928.818 28437.877 536.453\n",
"1600 20971.899 39959.689 583.744\n",
"1800 28056.363 63106.509 630.622\n",
"2000 39500.814 74439.178 797.706\n",
"2200 49913.609 97029.306 1434.759\n",
"2400 71256.090 140969.086 2002.379\n",
"2600 88315.444 159561.312 1576.816\n",
"2800 112728.898 200788.253 1523.572\n",
"3000 135712.700 259485.171 1641.147\n",
"3200 175046.983 314367.647 2021.378"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"df = pd.DataFrame({'cvxpy': cvxpy_times,\n",
"df = pd.DataFrame({'cvxpy': cvxpy_times,\n",
" 'cvxopt': cvxopt_times,\n",
" 'cvxopt': cvxopt_times,\n",
...
@@ -324,7 +645,7 @@
...
@@ -324,7 +645,7 @@
],
],
"metadata": {
"metadata": {
"kernelspec": {
"kernelspec": {
"display_name": "Python
[default]
",
"display_name": "Python
3
",
"language": "python",
"language": "python",
"name": "python3"
"name": "python3"
},
},
...
...
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