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
706f4e19
Commit
706f4e19
authored
Jul 05, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
made neutralize workable with WLS
parent
0b987f3e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
7 deletions
+16
-7
neutralize.py
alphamind/data/neutralize.py
+16
-7
No files found.
alphamind/data/neutralize.py
View file @
706f4e19
...
...
@@ -13,12 +13,20 @@ from typing import Dict
import
alphamind.utilities
as
utils
def
neutralize
(
x
:
np
.
ndarray
,
y
:
np
.
ndarray
,
groups
:
np
.
ndarray
=
None
,
output_explained
=
False
,
output_exposure
=
False
)
\
def
neutralize
(
x
:
np
.
ndarray
,
y
:
np
.
ndarray
,
groups
:
np
.
ndarray
=
None
,
output_explained
:
bool
=
False
,
output_exposure
:
bool
=
False
,
weights
:
np
.
ndarray
=
None
)
\
->
Union
[
np
.
ndarray
,
Tuple
[
np
.
ndarray
,
Dict
]]:
if
y
.
ndim
==
1
:
y
=
y
.
reshape
((
-
1
,
1
))
if
weights
is
None
:
weights
=
np
.
ones
(
len
(
y
),
dtype
=
float
)
if
groups
is
not
None
:
res
=
np
.
zeros
(
y
.
shape
)
...
...
@@ -38,7 +46,7 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
start
=
0
for
diff_loc
in
index_diff
:
curr_idx
=
order
[
start
:
diff_loc
+
1
]
curr_x
,
b
=
_sub_step
(
x
,
y
,
curr_idx
,
res
)
curr_x
,
b
=
_sub_step
(
x
,
y
,
weights
,
curr_idx
,
res
)
if
output_exposure
:
for
i
in
range
(
exposure
.
shape
[
2
]):
exposure
[
curr_idx
,
:,
i
]
=
b
[:,
i
]
...
...
@@ -47,7 +55,7 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
explained
[
curr_idx
]
=
ls_explain
(
curr_x
,
b
)
start
=
diff_loc
+
1
else
:
b
=
ls_fit
(
x
,
y
)
b
=
ls_fit
(
x
,
y
,
weights
)
res
=
ls_res
(
x
,
y
,
b
)
if
output_explained
:
...
...
@@ -68,18 +76,19 @@ def neutralize(x: np.ndarray, y: np.ndarray, groups: np.ndarray=None, output_exp
@
nb
.
njit
(
nogil
=
True
,
cache
=
True
)
def
_sub_step
(
x
,
y
,
curr_idx
,
res
):
def
_sub_step
(
x
,
y
,
w
,
curr_idx
,
res
):
curr_x
=
x
[
curr_idx
]
curr_y
=
y
[
curr_idx
]
b
=
ls_fit
(
curr_x
,
curr_y
)
curr_w
=
w
[
curr_idx
]
b
=
ls_fit
(
curr_x
,
curr_y
,
curr_w
)
res
[
curr_idx
]
=
ls_res
(
curr_x
,
curr_y
,
b
)
return
curr_x
,
b
@
nb
.
njit
(
nogil
=
True
,
cache
=
True
)
def
ls_fit
(
x
:
np
.
ndarray
,
y
:
np
.
ndarray
)
->
np
.
ndarray
:
def
ls_fit
(
x
:
np
.
ndarray
,
y
:
np
.
ndarray
,
w
:
np
.
ndarray
)
->
np
.
ndarray
:
x_bar
=
x
.
T
b
=
np
.
linalg
.
solve
(
x_bar
@
x
,
x_bar
@
y
)
b
=
np
.
linalg
.
solve
(
x_bar
*
w
@
x
,
x_bar
*
w
@
y
)
return
b
...
...
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