-
Notifications
You must be signed in to change notification settings - Fork 214
/
rmr.py
53 lines (42 loc) · 1.51 KB
/
rmr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import pandas as pd
from .. import tools
from ..algo import Algo
from .olmar import OLMAR
def norm(x):
if isinstance(x, pd.Series):
axis = 0
else:
axis = 1
return np.sqrt((x ** 2).sum(axis=axis))
class RMR(OLMAR):
"""Robust Median Reversion. Strategy exploiting mean-reversion by robust
L1-median estimator. Practically the same as OLMAR.
Reference:
Dingjiang Huang, Junlong Zhou, Bin Li, Steven C.H. Hoi, Shuigeng Zhou
Robust Median Reversion Strategy for On-Line Portfolio Selection, 2013.
http://ijcai.org/papers13/Papers/IJCAI13-296.pdf
"""
PRICE_TYPE = "raw"
REPLACE_MISSING = True
def __init__(self, window=5, eps=10.0, tau=0.001):
"""
:param window: Lookback window.
:param eps: Constraint on return for new weights on last price (average of prices).
x * w >= eps for new weights w.
:param tau: Precision for finding median. Recommended value is around 0.001. Strongly
affects algo speed.
"""
super().__init__(window, eps)
self.tau = tau
def predict(self, x, history):
"""find L1 median to historical prices"""
y = history.mean()
y_last = None
while y_last is None or norm(y - y_last) / norm(y_last) > self.tau:
y_last = y
d = norm(history - y)
y = history.div(d, axis=0).sum() / (1.0 / d).sum()
return y / x
if __name__ == "__main__":
tools.quickrun(RMR())