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
2dc2692a
Commit
2dc2692a
authored
Feb 10, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
using dict as const linear model weights
parent
a6fdafd0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
9 deletions
+19
-9
sqlengine.py
alphamind/data/engines/sqlengine.py
+9
-2
linearmodel.py
alphamind/model/linearmodel.py
+4
-3
test_linearmodel.py
alphamind/tests/model/test_linearmodel.py
+6
-4
No files found.
alphamind/data/engines/sqlengine.py
View file @
2dc2692a
...
@@ -408,7 +408,8 @@ class SqlEngine(object):
...
@@ -408,7 +408,8 @@ class SqlEngine(object):
def
fetch_benchmark
(
self
,
def
fetch_benchmark
(
self
,
ref_date
:
str
,
ref_date
:
str
,
benchmark
:
int
)
->
pd
.
DataFrame
:
benchmark
:
int
,
codes
:
Iterable
[
int
]
=
None
)
->
pd
.
DataFrame
:
query
=
select
([
IndexComponent
.
code
,
(
IndexComponent
.
weight
/
100.
)
.
label
(
'weight'
)])
.
where
(
query
=
select
([
IndexComponent
.
code
,
(
IndexComponent
.
weight
/
100.
)
.
label
(
'weight'
)])
.
where
(
and_
(
and_
(
IndexComponent
.
trade_date
==
ref_date
,
IndexComponent
.
trade_date
==
ref_date
,
...
@@ -416,7 +417,13 @@ class SqlEngine(object):
...
@@ -416,7 +417,13 @@ class SqlEngine(object):
)
)
)
)
return
pd
.
read_sql
(
query
,
self
.
engine
)
df
=
pd
.
read_sql
(
query
,
self
.
engine
)
if
codes
:
df
.
set_index
([
'code'
],
inplace
=
True
)
df
=
df
.
reindex
(
codes
)
.
fillna
(
0.
)
df
.
reset_index
(
inplace
=
True
)
return
df
def
fetch_benchmark_range
(
self
,
def
fetch_benchmark_range
(
self
,
benchmark
:
int
,
benchmark
:
int
,
...
...
alphamind/model/linearmodel.py
View file @
2dc2692a
...
@@ -19,7 +19,7 @@ from alphamind.utilities import alpha_logger
...
@@ -19,7 +19,7 @@ from alphamind.utilities import alpha_logger
class
ConstLinearModelImpl
(
object
):
class
ConstLinearModelImpl
(
object
):
def
__init__
(
self
,
weights
:
np
.
ndarray
=
None
):
def
__init__
(
self
,
weights
:
np
.
ndarray
=
None
):
self
.
weights
=
np
.
array
(
weights
)
.
flatten
()
self
.
weights
=
weights
.
flatten
()
def
fit
(
self
,
x
:
np
.
ndarray
,
y
:
np
.
ndarray
):
def
fit
(
self
,
x
:
np
.
ndarray
,
y
:
np
.
ndarray
):
pass
pass
...
@@ -32,13 +32,14 @@ class ConstLinearModel(ModelBase):
...
@@ -32,13 +32,14 @@ class ConstLinearModel(ModelBase):
def
__init__
(
self
,
def
__init__
(
self
,
features
=
None
,
features
=
None
,
weights
:
np
.
ndarray
=
None
):
weights
:
dict
=
None
):
super
()
.
__init__
(
features
)
super
()
.
__init__
(
features
)
if
features
is
not
None
and
weights
is
not
None
:
if
features
is
not
None
and
weights
is
not
None
:
pyFinAssert
(
len
(
features
)
==
len
(
weights
),
pyFinAssert
(
len
(
features
)
==
len
(
weights
),
ValueError
,
ValueError
,
"length of features is not equal to length of weights"
)
"length of features is not equal to length of weights"
)
self
.
impl
=
ConstLinearModelImpl
(
weights
)
if
weights
:
self
.
impl
=
ConstLinearModelImpl
(
np
.
array
([
weights
[
name
]
for
name
in
self
.
features
]))
def
save
(
self
):
def
save
(
self
):
model_desc
=
super
()
.
save
()
model_desc
=
super
()
.
save
()
...
...
alphamind/tests/model/test_linearmodel.py
View file @
2dc2692a
...
@@ -20,6 +20,7 @@ class TestLinearModel(unittest.TestCase):
...
@@ -20,6 +20,7 @@ class TestLinearModel(unittest.TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
n
=
3
self
.
n
=
3
self
.
features
=
[
'a'
,
'b'
,
'c'
]
self
.
train_x
=
pd
.
DataFrame
(
np
.
random
.
randn
(
1000
,
self
.
n
),
columns
=
[
'a'
,
'b'
,
'c'
])
self
.
train_x
=
pd
.
DataFrame
(
np
.
random
.
randn
(
1000
,
self
.
n
),
columns
=
[
'a'
,
'b'
,
'c'
])
self
.
train_y
=
np
.
random
.
randn
(
1000
)
self
.
train_y
=
np
.
random
.
randn
(
1000
)
self
.
train_y_label
=
np
.
where
(
self
.
train_y
>
0.
,
1
,
0
)
self
.
train_y_label
=
np
.
where
(
self
.
train_y
>
0.
,
1
,
0
)
...
@@ -27,16 +28,17 @@ class TestLinearModel(unittest.TestCase):
...
@@ -27,16 +28,17 @@ class TestLinearModel(unittest.TestCase):
def
test_const_linear_model
(
self
):
def
test_const_linear_model
(
self
):
weights
=
np
.
array
([
1.
,
2.
,
3.
])
features
=
[
'c'
,
'b'
,
'a'
]
model
=
ConstLinearModel
(
features
=
[
'a'
,
'b'
,
'c'
],
weights
=
dict
(
c
=
3.
,
b
=
2.
,
a
=
1.
)
model
=
ConstLinearModel
(
features
=
features
,
weights
=
weights
)
weights
=
weights
)
calculated_y
=
model
.
predict
(
self
.
predict_x
)
calculated_y
=
model
.
predict
(
self
.
predict_x
)
expected_y
=
self
.
predict_x
@
weights
expected_y
=
self
.
predict_x
[
features
]
@
np
.
array
([
weights
[
f
]
for
f
in
features
])
np
.
testing
.
assert_array_almost_equal
(
calculated_y
,
expected_y
)
np
.
testing
.
assert_array_almost_equal
(
calculated_y
,
expected_y
)
def
test_const_linear_model_persistence
(
self
):
def
test_const_linear_model_persistence
(
self
):
weights
=
np
.
array
([
1.
,
2.
,
3.
]
)
weights
=
dict
(
c
=
3.
,
b
=
2.
,
a
=
1.
)
model
=
ConstLinearModel
(
features
=
[
'a'
,
'b'
,
'c'
],
model
=
ConstLinearModel
(
features
=
[
'a'
,
'b'
,
'c'
],
weights
=
weights
)
weights
=
weights
)
...
...
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